def getkey(self): if isinstance(self.lnode, AlphaNode): key = removedups(self.lnode.svars + self.lnode.vars + self.rnode.vars) elif isinstance(self.lnode, BetaNode): key = removedups(self.lnode.pattern + self.rnode.vars) return key
def getbindings(self, row, useBuiltin=None): bindings = dict() #check for builtins if useBuiltin: key = useBuiltin.indexKey() else: if isinstance(self.lnode, AlphaNode): key = removedups(self.lnode.svars + self.lnode.vars + self.rnode.vars) elif isinstance(self.lnode, BetaNode): key = removedups(self.lnode.pattern + self.rnode.vars) for i, v in enumerate(key): bindings[v] = row[i] return bindings
def __init__(self, lnode, rnode): from builtins import builtinp self.lnode = lnode self.rnode = rnode #####Make this opaque if isinstance(lnode, BetaNode): self.lvars = lnode.pattern elif isinstance(lnode, AlphaNode): self.lvars = lnode.vars self.rvars = rnode.vars # Detect builtins if isinstance(rnode, AlphaNode): if builtinp(self.rnode): self.svars = lnode.svars else: # store svars in lexical order self.svars = [v for v in self.lvars if v in self.rvars] else: self.svars = [v for v in self.lvars if v in self.rvars] sortVars(self.svars) #destructively sort vars self.parents = list() self.children = list() self.pattern = removedups(self.svars + self.lvars + self.rvars) # Redundant var, will be refactored out self.vars = self.pattern self.builtinInput = None self.ind = Memory() self.inferredFacts = Set() # a pointer to the rule node (which contains the rhs) self.rule = None
def makeBetaNetwork(self, rule, betaNode, alphaNodeList): """I have more than 2 alpha nodes and so I make a network""" length = len(alphaNodeList) if length == 0: betaNode.rule = self.makeRuleNode(rule) betaNode.rule.betaNode = betaNode else: alpha = alphaNodeList[0] betaChild = self.makeBetaNode(betaNode, alpha) # connect our newly created BetaNode to its parent BetaNode, # and connect the parent to its child betaChild.parents = [betaNode] betaNode.children = [betaChild] sharedJoinVars = self.getSharedVars(betaNode, alpha) sortVars(sharedJoinVars) if not builtinp(alpha): # adjust our beta node shared variables betaNode.svars = sharedJoinVars # Our betanode has children, and so set up our # pattern in an order that is conducive to the children JOINs betaNode.pattern = removedups(sharedJoinVars + betaNode.pattern) # connect our AlphaNode to its relevant BetaNode alpha.betaNodes = [betaChild] self.rete.betaNodeStore.addNode(betaNode) self.rete.betaNodeStore.addNode(betaChild) alphaNodeList = alphaNodeList[1:] return self.makeBetaNetwork(rule, betaChild, alphaNodeList)
def setupIndex(self): if not self.svars: self.svars = [self.vars[0]] # need to account for 0 var case # len(shared) <= len(self.vars) # We need to remove dups. # unshared and shared are *disjoint*, so only need to remove # dups in each self.unshared = list(removedups([v for v in self.vars if v not in self.svars]))
def add(self, fact): bindings = self.match(fact) if bindings: #make sure key has shared vars first key = removedups(self.svars + self.vars) return self.index(self.ind, bindings, key, fact) else: return False
def setupIndex(self): if not self.svars: self.svars = [self.vars[0]] # need to account for 0 var case # len(shared) <= len(self.vars) # We need to remove dups. # unshared and shared are *disjoint*, so only need to remove # dups in each self.unshared = list( removedups([v for v in self.vars if v not in self.svars]))
def indexKey(self): """I return the key according to which we build the bindings for the rows resulting builtin evaluation""" inputNode = self.getInputNode() if isinstance(inputNode, nodes.AlphaNode): return removedups(inputNode.svars + inputNode.vars) elif isinstance(inputNode, nodes.BetaNode): return inputNode.pattern
def getbindings(self, row, useBuiltin=None): """I return a set of bindings for row, where row is a set of values from my AlphaMemory (note that rows that do not have constants, only variable values.)""" bindings = dict() key = removedups(self.svars + self.vars) for i, val in enumerate(key): bindings[val] = row[i] #print "key alpha:",key #print "row alpha:",row return bindings
def sort(self): self.generateDependencies() from builtins import builtinp builtins = list() for node in self.nodes: if builtinp(node): builtins.append(node) builtins = removedups(builtins) # for nodes with pattern like (_:exivar FIRST ?x) we want to make sure ?x's are bound when joining, #so add them at the end (like builtins) listVarNodes = [ node for node in self.nodes if node.pattern.p in [FIRST, REST, INCLUDES, TYPE, SEMANTICS] ] listVarNodes = removedups(listVarNodes) nonBuiltins = [x for x in self.nodes if x not in builtins] nonBuiltins = [x for x in nonBuiltins if x not in listVarNodes] # Sort the non-builtin alphas so that nodes that share variables (if any) # are adjacent sortedNonBuiltins = list() for nb in nonBuiltins: for v in nb.vars: nodesThatShare = self.sharedIndex.get(v) if nodesThatShare: sortedNonBuiltins.extend(nodesThatShare) sortedNonBuiltins.append(nb) # If there were any nodes that shared variables, use the sorted list of nodes if sortedNonBuiltins: nonBuiltins = removedups(sortedNonBuiltins) #only sort builtins and list nodes (rdf:first, rest, etc.) #the other nodes should come first unsortedNodes = removedups(builtins + listVarNodes) # make full topological sort self.nodes = removedups(nonBuiltins + self.topSort(unsortedNodes, [])) """
def getSharedVars(self, node1, node2): if isinstance(node1, BetaNode): lvars = node1.pattern else: lvars = node1.vars if isinstance(node2, BetaNode): rvars = node2.pattern else: rvars = node2.vars result = removedups([item for item in lvars \ if item in rvars]) return result
def sort(self): self.generateDependencies() from builtins import builtinp builtins = list() for node in self.nodes: if builtinp(node): builtins.append(node) builtins = removedups(builtins) # for nodes with pattern like (_:exivar FIRST ?x) we want to make sure ?x's are bound when joining, #so add them at the end (like builtins) listVarNodes = [node for node in self.nodes if node.pattern.p in [FIRST, REST, INCLUDES, TYPE, SEMANTICS]] listVarNodes = removedups(listVarNodes) nonBuiltins = [x for x in self.nodes if x not in builtins] nonBuiltins = [x for x in nonBuiltins if x not in listVarNodes] # Sort the non-builtin alphas so that nodes that share variables (if any) # are adjacent sortedNonBuiltins = list() for nb in nonBuiltins: for v in nb.vars: nodesThatShare = self.sharedIndex.get(v) if nodesThatShare: sortedNonBuiltins.extend(nodesThatShare) sortedNonBuiltins.append(nb) # If there were any nodes that shared variables, use the sorted list of nodes if sortedNonBuiltins: nonBuiltins = removedups(sortedNonBuiltins) #only sort builtins and list nodes (rdf:first, rest, etc.) #the other nodes should come first unsortedNodes = removedups(builtins + listVarNodes) # make full topological sort self.nodes = removedups(nonBuiltins + self.topSort(unsortedNodes, [])) """
def compile(self, rules): for rule in rules: print "%s of %s" % (rules.index(rule), len(rules)) alphaNodesOfRule = AlphaStore() for pattern in rule.lhs: anodePattern = Pattern(pattern.s, pattern.p, pattern.o) anode = self.makeAlphaNode(anodePattern) alphaNodesOfRule.addNode(anode) alphaNodesOfRule.sort() self.rete.alphaNodeStore.sort() alphaNodesOfRule = removedups(alphaNodesOfRule) l = len(alphaNodesOfRule) if l == 0: # probably malformed input raise Exception elif l == 1: # If the rule has one pattern, we create two identical anodes # for it so that we can have a BetaNode with a left and right input beta1 = self.makeBetaNode(alphaNodesOfRule[0], alphaNodesOfRule[0], futureJoins=False) alphaNodesOfRule[0].betaNodes = [beta1] beta1.rule = self.makeRuleNode(rule) beta1.rule.betaNode = beta1 elif l == 2: # here we build a one beta with the only two alphas as its inputs beta1 = self.makeBetaNode(alphaNodesOfRule[0], alphaNodesOfRule[1], futureJoins=False) # connect our AlphaNodes to the BetaNode alphaNodesOfRule[0].betaNodes = [beta1] alphaNodesOfRule[1].betaNodes = [beta1] beta1.rule = self.makeRuleNode(rule) beta1.rule.betaNode = beta1 self.rete.betaNodeStore.addNode(beta1) else: beta1 = self.makeBetaNode(alphaNodesOfRule[0], alphaNodesOfRule[1], futureJoins=True) alphaNodesOfRule[0].betaNodes = [beta1] alphaNodesOfRule[1].betaNodes = [beta1] self.rete.betaNodeStore.addNode(beta1) # we've consumed the first two alpha nodes alphaNodeList = alphaNodesOfRule[2:] self.makeBetaNetwork(rule, beta1, alphaNodeList) return self.rete
def getSharedVars(self, node1, node2): """I return a list of shared variables between two nodes""" lvars = list() rvars = list() # check for builtins - do not calculate the shared variables # between a builtin and a regular Beta Node, since the index order # does not change if isinstance(node1, BetaNode): lvars = node1.pattern else: lvars = node1.vars if isinstance(node2, BetaNode): rvars = node2.pattern else: rvars = node2.vars result = removedups([item for item in lvars \ if item in rvars]) return result
def getkey(self): return removedups(self.svars + self.vars)