Getting Llyn To Return Lists From Builtins


I managed to get Llyn to return lists from builtins successfully - the
current code doesn't work (it doesn't seem as if it was supposed to
work, since there's a name error). In def _fromPython, where it checks
to see whether the type of object being returned from a builtin is a
list, I added:-

        elif type(x) == type([]):
            g, y = self.nil, x[:]
            if str(y) not in self.FPLists:
               for e in y:
                   g1 = self.genid(RESOURCE)
                   self.storeQuad((context, self.forSome, context,
                   self.storeQuad((context, self.first, g1,
self._fromPython(context, e)))
                   self.storeQuad((context,, g1, g))
                   g = g1
               self.FPLists[str(y)] = g
               return g
            else: return self.FPLists[str(y)]

self.FPLists is just a blank dictionary, for interning the list
GenIDs. That's... a bit hacky, but I'm sure you'll come up with
something better. In any case, it works well enough for now.

Here's a sample builtin that returns a list (I just threw it in the
cwm_string module):-

class BI_split(LightBuiltIn, Function):
    def evaluateObject(self, store, context, subj, subj_py):
        if thing.verbosity() > 80: progress("split
        str, schar = subj_py
        res = str.split(schar)
        return store._fromPython(context, res)

and a test:-

     @prefix : <> .
     @prefix string: <> .

    this     :forAll <#x> .

          ( ""
            "#"  )
             string:split <#x> .

        }     :implies {<#x>     a <#Result> .
        } .
      ( ""
        "blargh"  )
         a <#Result> .



Which paves the way for some great future builtins.

Whilst I'm at it, I have a mild request too: could you allow loading
of builtins modules from a separate configuration file? It would be
nice to let people add their own modules easily without having to have
them incorporated in the SWAP stuff, and although it *is* possible to
add your own module by editing the source code, I think it
would be neater to have a separate file that people can mess around
with, so you don't end up with nasty CVS checkout conflicts etc.
Hmm... then you could allow people to dynamically load the modules
from the Web as well, I guess.


Kindest Regards,
Sean B. Palmer
@prefix : <> .
:Sean :homepage <> .

Received on Saturday, 2 March 2002 12:37:11 UTC