import sys from rdflib import BNode, Namespace, Literal, URIRef, Variable from rdflib.Graph import Graph, ConjunctiveGraph import terms from helpers import convertBNodeToFact, handleURL LOG = Namespace("http://www.w3.org/2000/10/swap/log#") formulas = dict() """ class GimpedN3Store(N3Store): def remove(self, triple): raise NotImplementedError() def triples(self, triple): raise NotImplementedError() def remove_clause(self, triple): raise NotImplementedError() def create_clause(self, identifier=None): if identifier is None: identifier = BNode() self.clauses[identifier] = ClauseLoader(parent=self) return identifier class ClauseLoader(GimpedN3Store): def __init__(self, parent=None): super(ClauseLoader, self).__init__(parent=parent) self.patterns = [] self.depth = 1 def _convert(self, resource): #convert clauses to pychinko formulas here #check to see if resource points to a clause if (resource in self.clauses): #TODO convert the clause to a formula and add it to the map formulas[resource] = Formula(self.clauses[resource].patterns) if resource in self.get_universals(): return Univar(resource) elif isinstance(resource, BNode) or resource in self.get_existentials(): return Exivar(resource) elif isinstance(resource, URIRef): return URI(resource) return resource def add(self, (subj, pred, obj)): #print subj,pred, obj pattern = None #is this used anywhere? if pred == BUILTINS_ASSERT: pattern = Pattern(BuiltinRule([Pattern('Root', 'a', 'Formula')], removedups(self.get_clause(obj).patterns), BUILTINS_ASSERT), None, None) elif pred == LOG_INCLUDES: pattern = Pattern(self._convert(subj), self._convert(pred), self._convert(obj)) elif pred == LOG_IMPLIES: self.formulas[subj] = removedups(self.get_clause(subj).patterns) self.formulas[obj] = removedups(self.get_clause(obj).patterns) pattern = Pattern(Rule(removedups(self.get_clause(subj).patterns), removedups(self.get_clause(obj).patterns),(subj, pred, obj)), None, None) else: pattern = Pattern(self._convert(subj), self._convert(pred), self._convert(obj)) self.patterns.append(pattern) """ def convertBackToGraph(facts): g = Graph() for i in facts: g.add((i.s, i.p, i.o)) #serialize the graph g.serialize() outputFile = handleURL('pit.out') outputfp = open(outputFile, 'w') outputfp.write(g.serialize()) #this could be done faster -use serialize's arguments outputfp.close() class N3Loader(Graph): # does graph have information about subgraphs def _convert(self, node, convertBNode=False): if isinstance(node, Variable): print "variable:", node return terms.Univar(node) elif isinstance(node, Graph): return terms.Formula(N3Loader(store=node.store, identifier=node.identifier)) #make it work for forSome vars, they should be returned as Exivars, #all others, as BNodes elif isinstance(node, BNode): if convertBNode: return convertBNodeToFact(terms.Exivar(node.n3())) else: return terms.Exivar(node.n3()) elif isinstance(node, URIRef): return node elif isinstance(node, Literal): return node else: raise Exception("Unexpected Type: %s" % type(node)) def patterns(self): for s, p, o in self: yield terms.Pattern(self._convert(s), self._convert(p), self._convert(o)) # make sure this works ok # what about facts with bnodes def facts(self): for s, p, o in self: if p!= LOG.implies and (not isinstance(s, Variable) or isinstance(s, BNode)) and (not isinstance(o, Variable) or isinstance(o, BNode)): #print "fact:", s,p,o yield terms.Fact(self._convert(s, True), self._convert(p, True), self._convert(o, True)) def rules(self): # for s, p, o in self.triples((None, None, None)): # print s,p,o #rule has rhs and lhs, both of which are lists of patterns for s, p, o in self.triples((None, LOG.implies, None)): subj = N3Loader(store=s.store, identifier=s.identifier) obj = N3Loader(store=o.store, identifier=o.identifier) lhs = list(subj.patterns()) rhs = list(obj.patterns()) yield terms.Rule(lhs, rhs, (s, p, o)) def formulas(g): for s, p, o in self: if isinstance(s, Graph): yield s if instance(o, Graph): yield o