예제 #1
0
 def compile(self, variableSymtab=syt.SymbolTable()):
     self.variableSymtab = variableSymtab
     self.lhs.compile(variableSymtab)
     for g in self.rhs:
         g.compile(variableSymtab)
     for f in self.features:
         f.compile(variableSymtab)
예제 #2
0
    def variabilize(self):
        """Convert the variables to integer indices, -1,-2, ... and save their
        original names in "variableList", and the number of distinct
        variables in 'nvars."""
        if self.nvars >= 0:
            pass  #already done
        else:
            varTab = syt.SymbolTable()

            def convertArgs(args):
                return map(
                    lambda a: -varTab.getId(a)
                    if isVariableAtom(a) else a, args)

            def convertGoal(g):
                return Goal(g.functor, convertArgs(g.args))

            if self.lhs: self.lhs = convertGoal(self.lhs)
            self.rhs = map(convertGoal, self.rhs)
            if self.features:
                self.features = map(convertGoal, self.features)
            if self.findall:
                self.findall = map(convertGoal, self.findall)
            self.variableList = varTab.getSymbolList()
            self.nvars = len(self.variableList)
예제 #3
0
def compileRule(rule, variableSymtab=None):
    if not variableSymtab: variableSymtab = syt.SymbolTable()
    rule.lhs.compile(variableSymtab)
    for g in rule.rhs:
        g.compile(variableSymtab)
    for g in rule.features:
        g.compile(variableSymtab)
    return variableSymtab
예제 #4
0
def compileState(state, variableSymtab=None):
    if not variableSymtab: variableSymtab = syt.SymbolTable()
    for g in state.queryGoals:
        g.compile(variableSymtab)
    if state.theta.size() > 0:
        state.theta.compile(variableSymtab)
    state._freeze()
    return variableSymtab
예제 #5
0
 def __init__(self, wamprog, plugins=[], debugMode=DEFAULT_DEBUG_MODE):
     self.debugMode = debugMode
     self.state = Interpreter.MutableState()
     self.wamprog = wamprog
     self.constantTable = symtab.SymbolTable()
     self.plugins = plugins + wamplugin.builtInPlugins()
     # to answer a query, we add to the end of the compiled
     # program. this records the end of the compiled program,
     # so we can clear out previously-compiled queries
     self.coreProgramSize = 0
예제 #6
0
    def __init__(self):
        #maps symbols to numeric ids
        self.stab = symtab.SymbolTable()
        # track if the matrix is arity one or arity two
        self.arity = {}
        #matEncoding[p] encodes predicate p as a matrix
        self.matEncoding = {}
        #preimageEncoding[mode] encodes the preimage of a binary predicate wrt a mode as a matrix
        self.preimageEncoding = {}

        #buffer data for a sparse matrix: buf[pred][i][j] = f
        def dictOfFloats():
            return collections.defaultdict(float)

        def dictOfFloatDicts():
            return collections.defaultdict(dictOfFloats)

        self.buf = collections.defaultdict(dictOfFloatDicts)
예제 #7
0
def compileLPToFile(inputFile, outputFile):
    """Convert a file containing a logic program to a simple, integer-based
	encoding of that logic program - an encoding that doesn't really need to
	be parsed.

	format is as follows. Each line is a rule in three #-separated
	parts.  Part 1 is the rule itself, as a conjunction of goals - the
	first goal is the LHS/consequent.  Part 2 are the features, as a
	conjunction of goals.  Part 3 are the variables used in the rule,
	in a comma-sep list.  The i-th variable in this list (starting
	from 1) is assigned index -i in Parts 2 and 3.
	
	A conjunction of goals (Parts 2-3 above) is a &-separated list of
	items, each of which encodes a goal.  Each goal is a comma-sep
	list of integers: the first encodes the functor/arity, the
	remainder the arguments of the goal.

	Example, where the decoded rule is after the --> token: 
	
	"""
    lp = parser.parseFile(inputFile)
    fp = open(outputFile, 'w')
    for r in lp:
        variableSymtab = syt.SymbolTable()
        fp.write(",".join(
            [str(c) for c in compileGoalToIds(r.lhs, variableSymtab)]))
        for g in r.rhs:
            fp.write(
                " & " +
                ",".join([str(c)
                          for c in compileGoalToIds(g, variableSymtab)]))
        fp.write(" # ")
        separator = ""
        for f in r.features:
            fp.write(
                separator +
                ",".join([str(c)
                          for c in compileGoalToIds(f, variableSymtab)]))
            separator = " & "
        fp.write(" # ")
        fp.write(",".join(variableSymtab.getSymbolList()))
        fp.write("\n")
예제 #8
0
def compileComponent(component, variableSymtab=None):
    if not variableSymtab: variableSymtab = syt.SymbolTable()
    for goal in component.goals():
        goal.compile(variableSymtab)
    component._freeze()
    return variableSymtab
예제 #9
0
 def compile(self, variableSymtab=syt.SymbolTable()):
     self.variableSymtab = variableSymtab
     self.origArgs = self.args
     self._setArgs(compileArgs(self.args, variableSymtab))
     return variableSymtab