def generateDependencies(self):
        from builtins import funcBuiltinp
        for a in self.nodes:
            if funcBuiltinp(a):
                #means that object is a variable, look for dependencies on that var.
                for x in self.nodes:
                    if x != a and a.pattern.o in [x.pattern.s, x.pattern.o]:
                        a.dependents.append(x)

            #rdf:rest should always be before rdf:first
            elif a.pattern.p == REST:
                for x in self.nodes:
                    if x.pattern.p == FIRST and x.pattern.s == a.pattern.s:
                        a.dependents.append(x)

        for a in self.nodes:
            if funcBuiltinp(a):

                # (?x ?a) math:sum z. we want to have x and a  bound before evaluating math:sum.
                #so, look for the variables in the list, and see where they occur in the nodes set, then add dependencies
                #first, generate the variables from the list
                vars = self.listToPython(convertBNodeToFact(a.pattern.s),
                                         self.nodes, [])

                #now add dependencies
                for x in self.nodes:
                    if x.pattern.p == FIRST and x.pattern.o in vars:
                        x.dependents.append(a)
        """                        
Beispiel #2
0
    def topSort(self, unsorted, sorted):
##        k = len(unsorted)
##        if k > self.lastTopSort:
##            import traceback
##            #traceback.print_stack()
##        self.__class__.lastTopSort = k
        from builtins import funcBuiltinp
##        if not unsorted:
##            #for i in sorted:
##            #   print "s", i            
##            return sorted
##        else:
##            first = [x for x in unsorted if self.indegree(x,unsorted)==0 and not funcBuiltinp(x)]            
##            if first:                
##                sorted.append(first[0])
##                unsorted.remove(first[0])
##                #print len(unsorted)
##                return self.topSort(unsorted, sorted)
##            else:               
##                first = [x for x in unsorted if self.indegree(x,unsorted)==0]                            
##                sorted.append(first[0])
##                unsorted.remove(first[0])
##                return self.topSort(unsorted, sorted)

        try: set
        except:
             from sets import Set as set
        unsorted = set(unsorted)
        inDegrees = {}
        for node in unsorted:
            inDegrees[node] = 0
        for node in unsorted:
            for parent in node.dependents:
                if parent in inDegrees:
                    inDegrees[parent] = inDegrees[parent] + 1
        zeros = set()
        simpleZeros = set()
        for node in inDegrees:
            if inDegrees[node] == 0:
                if funcBuiltinp(node):
                    zeros.add(node)
                else:
                    simpleZeros.add(node)
        while zeros or simpleZeros:
            if simpleZeros:
                top = simpleZeros.pop()
            else:
                top = zeros.pop()
            sorted.append(top)
            for node in top.dependents:
                if node in inDegrees:
                    inDegrees[node] = inDegrees[node] - 1
                    if inDegrees[node] == 0:
                        if funcBuiltinp(node):
                            zeros.add(node)
                        else:
                            simpleZeros.add(node)
        if inDegrees and max(inDegrees.values()) != 0:
            raise ValueError
        return sorted
Beispiel #3
0
    def generateDependencies(self):
        from builtins import funcBuiltinp
        for a in self.nodes:
            if funcBuiltinp(a):
                #means that object is a variable, look for dependencies on that var.
                for x in self.nodes:
                    if x != a and a.pattern.o in [x.pattern.s, x.pattern.o]:
                        a.dependents.append(x)

            #rdf:rest should always be before rdf:first                          
            elif a.pattern.p == REST:
                for x in self.nodes:
                    if  x.pattern.p == FIRST and x.pattern.s == a.pattern.s:
                        a.dependents.append(x)                                    
        
        for a in self.nodes:
            if funcBuiltinp(a):
                
                # (?x ?a) math:sum z. we want to have x and a  bound before evaluating math:sum.
                #so, look for the variables in the list, and see where they occur in the nodes set, then add dependencies                
                #first, generate the variables from the list
                vars = self.listToPython(convertBNodeToFact(a.pattern.s), self.nodes, [])

                #now add dependencies
                for x in self.nodes:
                    if x.pattern.p==FIRST and x.pattern.o in vars:
                        x.dependents.append(a)

        """                        
    def topSort(self, unsorted, sorted):
        ##        k = len(unsorted)
        ##        if k > self.lastTopSort:
        ##            import traceback
        ##            #traceback.print_stack()
        ##        self.__class__.lastTopSort = k
        from builtins import funcBuiltinp
        ##        if not unsorted:
        ##            #for i in sorted:
        ##            #   print "s", i
        ##            return sorted
        ##        else:
        ##            first = [x for x in unsorted if self.indegree(x,unsorted)==0 and not funcBuiltinp(x)]
        ##            if first:
        ##                sorted.append(first[0])
        ##                unsorted.remove(first[0])
        ##                #print len(unsorted)
        ##                return self.topSort(unsorted, sorted)
        ##            else:
        ##                first = [x for x in unsorted if self.indegree(x,unsorted)==0]
        ##                sorted.append(first[0])
        ##                unsorted.remove(first[0])
        ##                return self.topSort(unsorted, sorted)

        try:
            set
        except:
            from sets import Set as set
        unsorted = set(unsorted)
        inDegrees = {}
        for node in unsorted:
            inDegrees[node] = 0
        for node in unsorted:
            for parent in node.dependents:
                if parent in inDegrees:
                    inDegrees[parent] = inDegrees[parent] + 1
        zeros = set()
        simpleZeros = set()
        for node in inDegrees:
            if inDegrees[node] == 0:
                if funcBuiltinp(node):
                    zeros.add(node)
                else:
                    simpleZeros.add(node)
        while zeros or simpleZeros:
            if simpleZeros:
                top = simpleZeros.pop()
            else:
                top = zeros.pop()
            sorted.append(top)
            for node in top.dependents:
                if node in inDegrees:
                    inDegrees[node] = inDegrees[node] - 1
                    if inDegrees[node] == 0:
                        if funcBuiltinp(node):
                            zeros.add(node)
                        else:
                            simpleZeros.add(node)
        if inDegrees and max(inDegrees.values()) != 0:
            raise ValueError
        return sorted
    def evalBuiltins(self, returnBindings=False):
        t = time.time()
        """I evaluate my attached builtins nodes."""
        from builtins import builtinp, funcBuiltinp
        #Three cases to handle: left is a builtin and right is a plain anode,
        #left is a plain anode and right is a builtin, or both the left
        #and right are builtins
        evaldRows = list()
        builtinNodes = list()

        if builtinp(self.lnode):
            builtinNodes.append(self.lnode)
        if builtinp(self.rnode):
            builtinNodes.append(self.rnode)

        for bi in builtinNodes:

            inputNode = bi.getInputNode()
            builtinInput = keysToList(inputNode.ind)

            for row in builtinInput:

                row = row[0]  #Needed since row[1] is the justification set

                #print row
                #print bi.pattern
                bindings = inputNode.getbindings(row, useBuiltin=bi)
                #need to check and substitute bi.pattern.s and bi.pattern.o for possible bindings here
                #check and substitute if subj or obj are lists, they also might have  variables in the lists

                subjInput = convertBNodeToFact(bi.pattern.s)

                objInput = convertBNodeToFact(bi.pattern.o)

                if isinstance(bi.pattern.s, Variable):
                    if bi.pattern.s in bindings:
                        subjInput = convertBNodeToFact(bindings[bi.pattern.s])

                if isinstance(bi.pattern.o, Variable):
                    if bi.pattern.o in bindings:
                        objInput = convertBNodeToFact(bindings[bi.pattern.o])

                if subjInput in py_lists:
                    subjInput = [
                        self.bindPossibleVar(x, bindings)
                        for x in py_lists[subjInput]
                    ]

                if objInput in py_lists:
                    objInput = [
                        self.bindPossibleVar(x, bindings)
                        for x in py_lists[objInput]
                    ]

                # print "builtin", bi, subjInput, objInput
                result = bi.evaluate(subjInput, objInput)

                if result:

                    #hack: append the bindings if it's log:includes
                    if bi.pattern.p == INCLUDES:
                        bindings[bi.pattern.s] = subjInput
                        bindings[bi.pattern.o] = objInput
                        row.append(subjInput)
                        row.append(objInput)

                    #a bit inefficient
                    if returnBindings:
                        evaldRows.append(bindings)
                    else:
                        evaldRows.append([row,
                                          []])  #Empty justification for now
                    #add to memory --
                    ##NOTE: Need to add justification (proof tracing) for data resulting
                    ##from builtins
                    #print "row after bindings", row
                    #if it's a functional builtin, it's different - need to return result and store in memory

                    if funcBuiltinp(self.lnode) or funcBuiltinp(self.rnode):
                        #have no idea why it works
                        row.append(result)
                        #print "row added in evalbuiltins:", row
                        #add this fact
                        self.add(row)
                        #print "___row:", row
                    else:
                        #print "___row:", row
                        self.add(row)

        #print "evaldRows:", evaldRows
        print "evalBultins time:", time.time() - t
        return evaldRows
Beispiel #6
0
    def evalBuiltins(self, returnBindings=False):
        t = time.time()
        """I evaluate my attached builtins nodes."""
        from builtins import builtinp, funcBuiltinp                        
        #Three cases to handle: left is a builtin and right is a plain anode,
        #left is a plain anode and right is a builtin, or both the left
        #and right are builtins
        evaldRows = list()
        builtinNodes = list()
        
        
        if builtinp(self.lnode):
            builtinNodes.append(self.lnode)            
        if builtinp(self.rnode):
            builtinNodes.append(self.rnode)

        
        for bi in builtinNodes:
            
            inputNode = bi.getInputNode()            
            builtinInput = keysToList(inputNode.ind)
          
            
            for row in builtinInput:
                
                row = row[0] #Needed since row[1] is the justification set

                #print row
                #print bi.pattern
                bindings = inputNode.getbindings(row, useBuiltin=bi)                
                #need to check and substitute bi.pattern.s and bi.pattern.o for possible bindings here                
                #check and substitute if subj or obj are lists, they also might have  variables in the lists                
                
                subjInput = convertBNodeToFact(bi.pattern.s)
                                
                objInput = convertBNodeToFact(bi.pattern.o)
                
                
                if isinstance(bi.pattern.s, Variable):                    
                    if bi.pattern.s in bindings:                    
                        subjInput = convertBNodeToFact(bindings[bi.pattern.s])
                        
                if isinstance(bi.pattern.o, Variable):                    
                    if bi.pattern.o  in bindings:
                        objInput = convertBNodeToFact(bindings[bi.pattern.o])

                if subjInput in py_lists:
                    subjInput = [self.bindPossibleVar(x, bindings) for x in py_lists[subjInput]]

                if objInput in py_lists:
                    objInput = [self.bindPossibleVar(x, bindings) for x in py_lists[objInput]]
                
                
                # print "builtin", bi, subjInput, objInput                    
                result = bi.evaluate(subjInput, objInput)
                
                
                
                if result:

                    #hack: append the bindings if it's log:includes
                    if bi.pattern.p == INCLUDES:                    
                        bindings[bi.pattern.s]= subjInput
                        bindings[bi.pattern.o]= objInput
                        row.append(subjInput)
                        row.append(objInput)                        

                    
                    #a bit inefficient
                    if returnBindings:
                        evaldRows.append(bindings)
                    else:
                        evaldRows.append([row, []])  #Empty justification for now
                    #add to memory --
                    ##NOTE: Need to add justification (proof tracing) for data resulting
                    ##from builtins
                    #print "row after bindings", row
                    #if it's a functional builtin, it's different - need to return result and store in memory
                    
                    if funcBuiltinp(self.lnode) or funcBuiltinp(self.rnode):
                        #have no idea why it works
                        row.append(result)                        
                        #print "row added in evalbuiltins:", row
                        #add this fact
                        self.add(row)
                        #print "___row:", row
                    else:
                        #print "___row:", row
                        self.add(row)
                    
        #print "evaldRows:", evaldRows
        print "evalBultins time:", time.time() - t                        
        return evaldRows