Beispiel #1
0
	def get_foaf(self):
		import sha
		try:
			from rdflib.Graph import Graph
			from rdflib import URIRef, Literal, BNode, Namespace, URIRef
			from rdflib import RDF
		except ImportError:
			raise Exception, "Please install RDFLib from http://rdflib.net"

		FOAF_NS = "http://xmlns.com/foaf/0.1/"

		store = Graph()
		store.bind("foaf", FOAF_NS)
		FOAF = Namespace(FOAF_NS)

		user = BNode()
		store.add((user, RDF.type, FOAF["Person"]))
		for (k,v) in self.get_attributes(True, "FOAF").items():
			store.add((user, FOAF[k], Literal(v)))
		#store.add((user, FOAF["family_name"], Literal(attributes["LAST_NAME"])))
		#store.add((user, FOAF["nick"], Literal(attributes["NICKNAME"])))
		#store.add((user, FOAF["homepage"], URIRef(attributes["HOMEPAGE_URI"])))
		#store.add((user, FOAF["mbox_sha1sum"], Literal(sha.new(attributes["EMAIL"]).hexdigest())))
		#store.add((user, FOAF["jabberID"], Literal(attributes["IM_JID"])))
		return store
Beispiel #2
0
 def testConjunction(self):
     self.addStuffInMultipleContexts()
     triple = (self.pizza, self.likes, self.pizza)
     # add to context 1
     graph = Graph(self.graph.store, self.c1)
     graph.add(triple)
     self.assertEquals(len(self.graph), len(graph))
Beispiel #3
0
def parse_to_graph(uri):
    dictionary = None
    for (pid, pattern) in patterns.items():
        m = pattern(uri)
        if m:
            dictionary = m.groupdict()
            dictionary["type"] = pid
            break

    if not dictionary:
        raise ValueError("Can't parse URI %s" % uri)

    graph = Graph()
    for key, value in Util.ns.items():
        graph.bind(key,  Namespace(value));
    bnode = BNode()
    for key in dictionary:
        if dictionary[key] == None:
            continue
        if key.startswith("_"):
            continue
        if key == "type":
            graph.add((bnode,RDF.type,URIRef(types[dictionary[key]])))
        else:
            graph.add((bnode, predicate[key], Literal(dictionary[key])))

    return graph
    def parse_from_soup(self,soup,basefile):
        g = Graph()
        self.log.info("%s: Parsing" % basefile)
        if basefile == "teu":
            # FIXME: Use a better base URI?
            uri = 'http://rinfo.lagrummet.se/extern/celex/12008M'
            startnode = soup.findAll(text="-"*50)[1].parent
            g.add((URIRef(uri),DCT['title'],Literal("Treaty on European Union")))
        elif basefile == "tfeu":
            uri = 'http://rinfo.lagrummet.se/extern/celex/12008E'
            startnode = soup.findAll(text="-"*50)[2].parent
            g.add((URIRef(uri),DCT['title'],Literal("Treaty on the Functioning of the European Union")))

        lines = deque()
        for p in startnode.findNextSiblings("p"):
            if p.string == "-" * 50:
                self.log.info("found the end")
                break
            else:
                if p.string:
                    lines.append(unicode(p.string))

        self.log.info("%s: Found %d lines" % (basefile,len(lines)))
        body = self.make_body(lines)
        self.process_body(body, '', uri)
        # print serialize(body)
        return {'meta':g,
                'body':body,
                'lang':'en',
                'uri':uri}
Beispiel #5
0
def parse_to_graph(uri):
    dictionary = None
    for (pid, pattern) in patterns.items():
        m = pattern(uri)
        if m:
            dictionary = m.groupdict()
            dictionary["type"] = pid
            break

    if not dictionary:
        raise ValueError("Can't parse URI %s" % uri)

    graph = Graph()
    for key, value in Util.ns.items():
        graph.bind(key, Namespace(value))
    bnode = BNode()
    for key in dictionary:
        if dictionary[key] == None:
            continue
        if key.startswith("_"):
            continue
        if key == "type":
            graph.add((bnode, RDF.type, URIRef(types[dictionary[key]])))
        else:
            graph.add((bnode, predicate[key], Literal(dictionary[key])))

    return graph
Beispiel #6
0
 def testConjunction(self):
     self.addStuffInMultipleContexts()
     triple = (self.pizza, self.likes, self.pizza)
     # add to context 1
     graph = Graph(self.graph.store, self.c1)
     graph.add(triple)
     self.assertEquals(len(self.graph), len(graph))
Beispiel #7
0
class UniversalRestrictionTest(unittest.TestCase):
    def setUp(self):
        self.ontGraph = Graph()
        self.ontGraph.bind('ex', EX_NS)
        self.ontGraph.bind('owl', OWL_NS)
        Individual.factoryGraph = self.ontGraph
                
    def testNegatedDisjunctionTest(self):
        contains=Property(EX_NS.contains)
        omega = EX.Omega
        alpha = EX.Alpha
        innerDisjunct = omega | alpha
        foo = EX.foo
        testClass1 = foo & (contains|only|~innerDisjunct)
        testClass1.identifier = EX_NS.Bar
        
        self.assertEqual(repr(testClass1),
                "ex:foo that ( ex:contains only ( not ( ex:Omega or ex:Alpha ) ) )")
        NormalFormReduction(self.ontGraph)
        self.assertEqual(repr(testClass1),
                "ex:foo that ( not ( ex:contains some ( ex:Omega or ex:Alpha ) ) )")
        
        individual1 = BNode()
        individual2 = BNode()
        foo.extent = [individual1]
        contains.extent = [(individual1,individual2)]
        (EX.Baz).extent = [individual2]
        ruleStore,ruleGraph,network=SetupRuleStore(makeNetwork=True)
        posRules,ignored=CalculateStratifiedModel(network,self.ontGraph,[EX_NS.Bar])
        self.failUnless(not posRules,"There should be no rules in the 0 strata!")
        self.assertEqual(len(ignored),2,"There should be 2 'negative' rules")
        testClass1.graph = network.inferredFacts 
        self.failUnless(individual1 in testClass1.extent,
                        "%s should be in ex:Bar's extent"%individual1)        

    def testNominalPartition(self):
        partition = EnumeratedClass(EX_NS.part,
                                    members=[EX_NS.individual1,
                                             EX_NS.individual2,
                                             EX_NS.individual3])
        subPartition = EnumeratedClass(members=[EX_NS.individual1])
        partitionProp = Property(EX_NS.propFoo,
                                 range=partition.identifier)
        self.testClass = (EX.Bar) & (partitionProp|only|subPartition)
        self.testClass.identifier = EX_NS.Foo         
        self.assertEqual(repr(self.testClass),
                        "ex:Bar that ( ex:propFoo only { ex:individual1 } )")        
        self.assertEqual(repr(self.testClass.identifier),
                        "rdflib.URIRef('http://example.com/Foo')")        
        NormalFormReduction(self.ontGraph)
        self.assertEqual(repr(self.testClass),
        "ex:Bar that ( not ( ex:propFoo value ex:individual2 ) ) and ( not ( ex:propFoo value ex:individual3 ) )")
        ruleStore,ruleGraph,network=SetupRuleStore(makeNetwork=True)
        
        ex = BNode()
        (EX.Bar).extent = [ex]
        self.ontGraph.add((ex,EX_NS.propFoo,EX_NS.individual1))
        CalculateStratifiedModel(network,self.ontGraph,[EX_NS.Foo])
        self.failUnless((ex,RDF.type,EX_NS.Foo) in network.inferredFacts,
                        "Missing level 1 predicate (ex:Foo)")
Beispiel #8
0
class UniversalRestrictionTest(unittest.TestCase):
    def setUp(self):
        self.ontGraph = Graph()
        self.ontGraph.bind('ex', EX_NS)
        self.ontGraph.bind('owl', OWL_NS)
        Individual.factoryGraph = self.ontGraph
                
    def testNegatedDisjunctionTest(self):
        contains=Property(EX_NS.contains)
        omega = EX.Omega
        alpha = EX.Alpha
        innerDisjunct = omega | alpha
        foo = EX.foo
        testClass1 = foo & (contains|only|~innerDisjunct)
        testClass1.identifier = EX_NS.Bar
        
        self.assertEqual(repr(testClass1),
                "ex:foo that ( ex:contains only ( not ( ex:Omega or ex:Alpha ) ) )")
        NormalFormReduction(self.ontGraph)
        self.assertEqual(repr(testClass1),
                "ex:foo that ( not ( ex:contains some ( ex:Omega or ex:Alpha ) ) )")
        
        individual1 = BNode()
        individual2 = BNode()
        foo.extent = [individual1]
        contains.extent = [(individual1,individual2)]
        (EX.Baz).extent = [individual2]
        ruleStore,ruleGraph,network=SetupRuleStore(makeNetwork=True)
        posRules,ignored=CalculateStratifiedModel(network,self.ontGraph,[EX_NS.Bar])
        self.failUnless(not posRules,"There should be no rules in the 0 strata!")
        self.assertEqual(len(ignored),2,"There should be 2 'negative' rules")
        testClass1.graph = network.inferredFacts 
        self.failUnless(individual1 in testClass1.extent,
                        "%s should be in ex:Bar's extent"%individual1)        

    def testNominalPartition(self):
        partition = EnumeratedClass(EX_NS.part,
                                    members=[EX_NS.individual1,
                                             EX_NS.individual2,
                                             EX_NS.individual3])
        subPartition = EnumeratedClass(members=[EX_NS.individual1])
        partitionProp = Property(EX_NS.propFoo,
                                 range=partition.identifier)
        self.testClass = (EX.Bar) & (partitionProp|only|subPartition)
        self.testClass.identifier = EX_NS.Foo         
        self.assertEqual(repr(self.testClass),
                        "ex:Bar that ( ex:propFoo only { ex:individual1 } )")        
        self.assertEqual(repr(self.testClass.identifier),
                        "rdflib.URIRef('http://example.com/Foo')")        
        NormalFormReduction(self.ontGraph)
        self.assertEqual(repr(self.testClass),
        "ex:Bar that ( not ( ex:propFoo value ex:individual2 ) ) and ( not ( ex:propFoo value ex:individual3 ) )")
        ruleStore,ruleGraph,network=SetupRuleStore(makeNetwork=True)
        
        ex = BNode()
        (EX.Bar).extent = [ex]
        self.ontGraph.add((ex,EX_NS.propFoo,EX_NS.individual1))
        CalculateStratifiedModel(network,self.ontGraph,[EX_NS.Foo])
        self.failUnless((ex,RDF.type,EX_NS.Foo) in network.inferredFacts,
                        "Missing level 1 predicate (ex:Foo)")
Beispiel #9
0
    def get_foaf(self):
        import sha
        try:
            from rdflib.Graph import Graph
            from rdflib import URIRef, Literal, BNode, Namespace, URIRef
            from rdflib import RDF
        except ImportError:
            raise Exception, "Please install RDFLib from http://rdflib.net"

        FOAF_NS = "http://xmlns.com/foaf/0.1/"

        store = Graph()
        store.bind("foaf", FOAF_NS)
        FOAF = Namespace(FOAF_NS)

        user = BNode()
        store.add((user, RDF.type, FOAF["Person"]))
        for (k, v) in self.get_attributes(True, "FOAF").items():
            store.add((user, FOAF[k], Literal(v)))
        #store.add((user, FOAF["family_name"], Literal(attributes["LAST_NAME"])))
        #store.add((user, FOAF["nick"], Literal(attributes["NICKNAME"])))
        #store.add((user, FOAF["homepage"], URIRef(attributes["HOMEPAGE_URI"])))
        #store.add((user, FOAF["mbox_sha1sum"], Literal(sha.new(attributes["EMAIL"]).hexdigest())))
        #store.add((user, FOAF["jabberID"], Literal(attributes["IM_JID"])))
        return store
Beispiel #10
0
def main(fill_graph_func):
    """A useful main function for converters.

    :see-also: fill_graph defining the expected interface
    """
    global OPTIONS
    OPTIONS, args = parse_options()
    graph = Graph()
    graph.bind("", "file://%s/" % pathname2url(abspath(curdir)))
    graph.bind("ma", MA)
    graph.bind("owl", OWL)
    graph.bind("xsd", XSD)

    if OPTIONS.owl_import:
        ont = BNode()
        graph.add((ont, RDF.type, OWL.Ontology))
        graph.add((ont, OWL.imports, URIRef("http://www.w3.org/ns/ma-ont")))

    if OPTIONS.extended:
        graph.bind("foaf", FOAF)
        graph.bind("lexvo", LEXVO)

    for filename in args:
        fill_graph_func(graph, filename, OPTIONS.profile, OPTIONS.extended)
    try:
        print graph.serialize(format=OPTIONS.format)
    except Exception:
        # for debug reason
        pprint(list(graph))
        raise
Beispiel #11
0
def createTestOntGraph():
    graph = Graph()
    graph.bind('ex',EX_NS,True)
    Individual.factoryGraph = graph
    kneeJoint = EX_CL.KneeJoint
    joint = EX_CL.Joint
    
    knee  = EX_CL.Knee
    isPartOf = Property(EX_NS.isPartOf)
    graph.add((isPartOf.identifier,RDF.type,OWL_NS.TransitiveProperty))
    structure = EX_CL.Structure
    leg = EX_CL.Leg
    hasLocation = Property(EX_NS.hasLocation,subPropertyOf=[isPartOf])
    # graph.add((hasLocation.identifier,RDFS.subPropertyOf,isPartOf.identifier))

    kneeJoint.equivalentClass = [joint & (isPartOf|some|knee)]
    legStructure = EX_CL.LegStructure
    legStructure.equivalentClass = [structure & (isPartOf|some|leg)]
    structure += leg
    structure += joint
    locatedInLeg = hasLocation|some|leg
    locatedInLeg += knee
    

    # print graph.serialize(format='n3')

    # newGraph = Graph()
    # newGraph.bind('ex',EX_NS,True)

#    newGraph,conceptMap = StructuralTransformation(graph,newGraph)
#    revDict = dict([(v,k) for k,v in conceptMap.items()])

#    Individual.factoryGraph = newGraph
#    for oldConceptId ,newConceptId in conceptMap.items():
#        if isinstance(oldConceptId,BNode):
#            oldConceptRepr = repr(Class(oldConceptId,graph=graph))
#            if oldConceptRepr.strip() == 'Some Class':
#                oldConceptRepr = manchesterSyntax(
#                    oldConceptId,
#                    graph)
#            print "%s -> %s"%(
#                oldConceptRepr,
#                newConceptId
#            )
#
#        else:
#            print "%s -> %s"%(
#                oldConceptId,
#                newConceptId
#            )
#
#    for c in AllClasses(newGraph):
#        if isinstance(c.identifier,BNode) and c.identifier in conceptMap.values():
#            print "## %s ##"%c.identifier
#        else:
#            print "##" * 10
#        print c.__repr__(True)
#        print "################################"
    return graph
Beispiel #12
0
def createTestOntGraph():
    graph = Graph()
    graph.bind('ex', EX_NS, True)
    Individual.factoryGraph = graph
    kneeJoint = EX_CL.KneeJoint
    joint = EX_CL.Joint

    knee = EX_CL.Knee
    isPartOf = Property(EX_NS.isPartOf)
    graph.add((isPartOf.identifier, RDF.type, OWL_NS.TransitiveProperty))
    structure = EX_CL.Structure
    leg = EX_CL.Leg
    hasLocation = Property(EX_NS.hasLocation, subPropertyOf=[isPartOf])
    # graph.add((hasLocation.identifier,RDFS.subPropertyOf,isPartOf.identifier))

    kneeJoint.equivalentClass = [joint & (isPartOf | some | knee)]
    legStructure = EX_CL.LegStructure
    legStructure.equivalentClass = [structure & (isPartOf | some | leg)]
    structure += leg
    structure += joint
    locatedInLeg = hasLocation | some | leg
    locatedInLeg += knee

    # print graph.serialize(format='n3')

    # newGraph = Graph()
    # newGraph.bind('ex',EX_NS,True)

    #    newGraph,conceptMap = StructuralTransformation(graph,newGraph)
    #    revDict = dict([(v,k) for k,v in conceptMap.items()])

    #    Individual.factoryGraph = newGraph
    #    for oldConceptId ,newConceptId in conceptMap.items():
    #        if isinstance(oldConceptId,BNode):
    #            oldConceptRepr = repr(Class(oldConceptId,graph=graph))
    #            if oldConceptRepr.strip() == 'Some Class':
    #                oldConceptRepr = manchesterSyntax(
    #                    oldConceptId,
    #                    graph)
    #            print "%s -> %s"%(
    #                oldConceptRepr,
    #                newConceptId
    #            )
    #
    #        else:
    #            print "%s -> %s"%(
    #                oldConceptId,
    #                newConceptId
    #            )
    #
    #    for c in AllClasses(newGraph):
    #        if isinstance(c.identifier,BNode) and c.identifier in conceptMap.values():
    #            print "## %s ##"%c.identifier
    #        else:
    #            print "##" * 10
    #        print c.__repr__(True)
    #        print "################################"
    return graph
Beispiel #13
0
def construct(dictionary):
    # Step 1: massage the data to a rdflib graph
    graph = Graph()
    bnode = BNode()
    for key in dictionary:
        if key == "type":
            graph.add((bnode, RDF.type, URIRef(types[dictionary[key]])))
        else:
            graph.add((bnode, predicate[key], Literal(dictionary[key])))
    # print graph.serialize(format="nt")
    return construct_from_graph(graph)
Beispiel #14
0
def construct(dictionary):
    # Step 1: massage the data to a rdflib graph
    graph = Graph()
    bnode = BNode()
    for key in dictionary:
        if key == "type":
            graph.add((bnode,RDF.type,URIRef(types[dictionary[key]])))
        else:
            graph.add((bnode, predicate[key], Literal(dictionary[key])))
    # print graph.serialize(format="nt")
    return construct_from_graph(graph)
Beispiel #15
0
    def addStuffInMultipleContexts(self):
        c1 = self.c1
        c2 = self.c2
        triple = (self.pizza, self.hates, self.tarek) # revenge!

        # add to default context
        self.graph.add(triple)
        # add to context 1
        graph = Graph(self.graph.store, c1)
        graph.add(triple)
        # add to context 2
        graph = Graph(self.graph.store, c2)
        graph.add(triple)
Beispiel #16
0
    def addStuffInMultipleContexts(self):
        c1 = self.c1
        c2 = self.c2
        triple = (self.pizza, self.hates, self.tarek)  # revenge!

        # add to default context
        self.graph.add(triple)
        # add to context 1
        graph = Graph(self.graph.store, c1)
        graph.add(triple)
        # add to context 2
        graph = Graph(self.graph.store, c2)
        graph.add(triple)
Beispiel #17
0
    def command(self):
        if self.opts.infile == "-":
            infile = sys.stdin
        else:
            infile = open(self.opts.infile)

        facts = Graph()
        facts.parse(infile, format=self.get_informat())
        infile.close()

#        rstore, rgraph = SetupRuleStore()
#        network = ReteNetwork(rstore)
#        rules = HornFromN3(self.opts.rules)
#        for rule in rules:
#            network.buildNetworkFromClause(rule)
#        network.feedFactsToAdd(generateTokenSet(facts))
#
#        facts += network.inferredFacts

        minimised = Graph()
        minimised.namespace_manageer = facts.namespace_manager
        minimised += facts
        
        for s,p,o in facts.triples((None, None, None)):
            minimised.remove((s,p,o))
            rules = HornFromN3(self.opts.rules)
            topDownStore = TopDownSPARQLEntailingStore(
                minimised.store,
                minimised,
                idb=rules,
                decisionProcedure=0,
                derivedPredicates=[p])
            target = Graph(topDownStore)

            q = "ASK WHERE { %s }" % " ".join(x.n3() for x in (s,p,o))
            log.debug("QUERY %s" % q)
            result = target.query(q).askAnswer[0]
            log.debug("RESULT %s" % result)
            if not result:
                minimised.add((s,p,o))
                if self.opts.debug:
                    log.info("KEEP %s." % " ".join(x.n3() for x in (s,p,o)))
            elif self.opts.debug:
                log.info("DROP %s." % " ".join(x.n3() for x in (s,p,o)))

        if self.opts.outfile == "-":
            outfile = sys.stdout
        else:
            outfile = open(self.opts.outfile, "w+")
        minimised.serialize(outfile, format=self.opts.format)
        outfile.close()
Beispiel #18
0
class EARLPlugin(Plugin):
    """
    Activate the EARL plugin to generate a report of the test results
    using EARL.
    """
    name = 'EARL'

    def begin(self):
        self.graph = Graph()
        self.graph.bind("earl", EARL.uri)

    def finalize(self, result):
        # TODO: add plugin options for specifying where to send
        # output.
        self.graph.serialize("file:results-%s.rdf" % date_time(),
                             format="pretty-xml")

    def addDeprecated(self, test):
        print "Deprecated: %s" % test

    def addError(self, test, err, capt):
        print "Error: %s" % test

    def addFailure(self, test, err, capt, tb_info):
        print "Failure: %s" % test

    def addSkip(self, test):
        print "Skip: %s" % test

    def addSuccess(self, test, capt):
        result = BNode()  # TODO: coin URIRef
        self.graph.add((result, RDFS.label, Literal(test)))
        self.graph.add((result, RDFS.comment, Literal(type(test))))
        self.graph.add((result, RDF.type, EARL.TestResult))
        self.graph.add((result, EARL.outcome, EARL["pass"]))
Beispiel #19
0
class EARLPlugin(Plugin):
    """
    Activate the EARL plugin to generate a report of the test results
    using EARL.
    """

    name = "EARL"

    def begin(self):
        self.graph = Graph()
        self.graph.bind("earl", EARL.uri)

    def finalize(self, result):
        # TODO: add plugin options for specifying where to send
        # output.
        self.graph.serialize("file:results-%s.rdf" % date_time(), format="pretty-xml")

    def addDeprecated(self, test):
        print "Deprecated: %s" % test

    def addError(self, test, err, capt):
        print "Error: %s" % test

    def addFailure(self, test, err, capt, tb_info):
        print "Failure: %s" % test

    def addSkip(self, test):
        print "Skip: %s" % test

    def addSuccess(self, test, capt):
        result = BNode()  # TODO: coin URIRef
        self.graph.add((result, RDFS.label, Literal(test)))
        self.graph.add((result, RDFS.comment, Literal(type(test))))
        self.graph.add((result, RDF.type, EARL.TestResult))
        self.graph.add((result, EARL.outcome, EARL["pass"]))
Beispiel #20
0
    def test(self):
        g = Graph()
        NS = Namespace("http://quoting.test/")
        for i, case in enumerate(cases):
            g.add((NS['subj'], NS['case%s' % i], Literal(case)))
        n3txt = g.serialize(format="n3")
        #print n3txt

        g2 = Graph()
        g2.parse(StringInputSource(n3txt), format="n3")
        for i, case in enumerate(cases):
            l = g2.value(NS['subj'], NS['case%s' % i])
            #print repr(l), repr(case)
            self.assertEqual(l, Literal(case))
Beispiel #21
0
    def testLenInOneContext(self):
        c1 = self.c1
        # make sure context is empty

        self.graph.remove_context(self.get_context(c1))
        graph = Graph(self.graph.store, c1)
        oldLen = len(self.graph)

        for i in range(0, 10):
            graph.add((BNode(), self.hates, self.hates))
        self.assertEquals(len(graph), oldLen + 10)
        self.assertEquals(len(self.get_context(c1)), oldLen + 10)
        self.graph.remove_context(self.get_context(c1))
        self.assertEquals(len(self.graph), oldLen)
        self.assertEquals(len(graph), 0)
Beispiel #22
0
    def transform_sparql_construct(self, rdf, construct_query):
        """Perform a SPARQL CONSTRUCT query on the RDF data and return a new graph."""

        logging.debug("performing SPARQL CONSTRUCT transformation")

        if construct_query[0] == '@':  # actual query should be read from file
            construct_query = file(construct_query[1:]).read()

        logging.debug("CONSTRUCT query: %s", construct_query)

        newgraph = Graph()
        for triple in rdf.query(construct_query):
            newgraph.add(triple)

        return newgraph
Beispiel #23
0
    def testLenInOneContext(self):
        c1 = self.c1
        # make sure context is empty

        self.graph.remove_context(self.get_context(c1))
        graph = Graph(self.graph.store, c1)
        oldLen = len(self.graph)

        for i in range(0, 10):
            graph.add((BNode(), self.hates, self.hates))
        self.assertEquals(len(graph), oldLen + 10)
        self.assertEquals(len(self.get_context(c1)), oldLen + 10)
        self.graph.remove_context(self.get_context(c1))
        self.assertEquals(len(self.graph), oldLen)
        self.assertEquals(len(graph), 0)
Beispiel #24
0
def intro_sparql(request):
    """ Introduction to using SPARQL to query an rdflib graph - http://code.google.com/p/rdflib/wiki/IntroSparql """
    g = Graph()
    g.parse("http://bigasterisk.com/foaf.rdf")
    g.parse("http://www.w3.org/People/Berners-Lee/card.rdf")
    FOAF = Namespace("http://xmlns.com/foaf/0.1/")
    g.parse("http://danbri.livejournal.com/data/foaf")
    [
        g.add((s, FOAF['name'], n))
        for s, _, n in g.triples((None, FOAF['member_name'], None))
    ]
    graph_as_list = []
    """ The Graph.parse 'initNs' argument is a dictionary of namespaces to be expanded in the query string """
    """ Example 'row': (rdflib.Literal('Dan Brickley', language=u'en', datatype=None), rdflib.Literal('Brad Fitzpatrick', language=u'en', datatype=None))  """
    for row in g.query(
            'SELECT ?aname ?bname \
         WHERE {\
             ?a foaf:knows ?b .\
             ?a foaf:name ?aname .\
             ?b foaf:name ?bname .\
             }',
            initNs=dict(foaf=Namespace("http://xmlns.com/foaf/0.1/"))):
        exec "line = '%s knows %s'" % row
        row = row  # explore...
        graph_as_list.append(line)
    context = {
        'row': row,
        'graph': graph_as_list,
    }
    return render_to_response('rdf/intro_sparql.html', context)
Beispiel #25
0
    def compare(self, addrs):
        self.lastPollTime = time.time()

        newGraph = Graph()
        addrs = set(addrs)
        for addr in addrs.difference(self.lastAddrs):
            self.recordAction("arrive", addr)
        for addr in self.lastAddrs.difference(addrs):
            self.recordAction("leave", addr)
        for addr in addrs:
            uri = deviceUri(addr)
            newGraph.add((ROOM["bluetooth"], ROOM["senses"], uri))
            if addr in nameCache:
                newGraph.add((uri, RDFS.label, Literal(nameCache[addr])))
        self.lastAddrs = addrs
        self.currentGraph = newGraph
Beispiel #26
0
    def buildRDFGraph(self,identifier=''):
        
        """
        buildRDFGraph creates an RDFLib graph object.
        
        :param identifier: the identifier string for the graph
        :type identifier: unicode
        :rtype: rdflib.Graph.Graph
        """
        
        #construct graph to be returned

        if identifier:
            #override original identifier
            graph = Graph(identifier=identifier)
        else:
            #otherwise keep identifier
            graph = Graph(identifier=self.identifier)


        #loop over triples in KBComponent
        for k in self.triples.keys():
            
            for v in self.triples[k]:
                object=''

                if type(v[1]) is str: 
                    object=Literal(v[1])
                
                #else: object = v[1].getURI()
                else: object = URIRef(v[1].uri)
                

                #graph.add((k.getURI(),URIRef(v[0].uri),object))
                graph.add((URIRef(k.uri),URIRef(v[0].uri),object))

        #always used these conventional labels
        graph.bind('rdfs', RDFSNS)    
        graph.bind('rdf', RDF.RDFNS,override=True)    
        graph.bind('owl',OWLNS)
        graph.bind('gold', GOLDNS)

        return graph
Beispiel #27
0
def parseRDFa(dom,base,graph = None,options=None) :
	"""The standard processing of an RDFa DOM into a Graph. This method is aimed at the inclusion of
	the library in other RDF applications using RDFLib.

	@param dom: DOM node for the document element (as returned from an XML parser)
	@param base: URI for the default "base" value (usually the URI of the file to be processed)
	@keyword graph: a graph. If the value is None, the graph is created.
	@type graph: RDFLib Graph
	@keyword options: Options for the distiller (in case of C{None}, the default options are used)
	@type options: L{Options}
	@return: the graph
	@rtype: RDFLib Graph
	"""
	if graph == None :
		graph = Graph()
	if options == None :
		options = Options()

	html = dom.documentElement

	# Creation of the top level execution context

	# Perform the built-in and external transformations on the HTML tree. This is,
	# in simulated form, the hGRDDL approach of Ben Adida
	for trans in options.transformers + builtInTransformers :
		trans(html,options)

	# collect the initial state. This takes care of things
	# like base, top level namespace settings, etc.
	# Ensure the proper initialization
	state = ExecutionContext(html,graph,base=base,options=options)

	# The top level subject starts with the current document; this
	# is used by the recursion
	subject = URIRef(state.base)

	# parse the whole thing recursively and fill the graph
	parse_one_node(html,graph,subject,state,[])
	if options.warning_graph != None :
		for t in options.warning_graph : graph.add(t)

	# That is it...
	return Graph
 def _load_clues(self, semanticPath):
     sfl = SemanticFilesLoader(semanticPath)
     names = sfl.selectStations()
     graphs = {}
     sfl.loadGraphsJustOnce(names, graphs)
     
     clues = {}
     for node_name in graphs:
         if node_name not in ('ontology','ontology_expanded'):
             clues[node_name] = SchemaBasedClue()
             
             union = Graph()
             for g in graphs[node_name]:
                 for t in g.triples((None, None, None)):
                     union.add(t)
                     # arreglar lo de los namespaces en la union!
                
             clues[node_name].parseGraph(union)
             
     return clues
Beispiel #29
0
def runTest():
    g = Graph()
    from rdflib import Namespace
    FOAF = Namespace("http://xmlns.com/foaf/0.1/")
    g.parse("http://danbri.livejournal.com/data/foaf") 
    [g.add((s, FOAF['name'], n)) for s,_,n in g.triples((None, FOAF['member_name'], None))]
    import pdb
    pdb.set_trace()
    for row in g.query('SELECT * WHERE { ?a foaf:knows ?b . ?a ?cc ?aname . ?b ?dd ?bname . }', 
                       initNs=_getPrefixesDict()):
        print  row[0:1]
    def buildRDFGraph(self, identifier=''):
        """
        buildRDFGraph creates an RDFLib graph object.
        
        :param identifier: the identifier string for the graph
        :type identifier: unicode
        :rtype: rdflib.Graph.Graph
        """

        #construct graph to be returned

        if identifier:
            #override original identifier
            graph = Graph(identifier=identifier)
        else:
            #otherwise keep identifier
            graph = Graph(identifier=self.identifier)

        #loop over triples in KBComponent
        for k in self.triples.keys():

            for v in self.triples[k]:
                object = ''

                if type(v[1]) is str:
                    object = Literal(v[1])

                #else: object = v[1].getURI()
                else:
                    object = URIRef(v[1].uri)

                #graph.add((k.getURI(),URIRef(v[0].uri),object))
                graph.add((URIRef(k.uri), URIRef(v[0].uri), object))

        #always used these conventional labels
        graph.bind('rdfs', RDFSNS)
        graph.bind('rdf', RDF.RDFNS, override=True)
        graph.bind('owl', OWLNS)
        graph.bind('gold', GOLDNS)

        return graph
Beispiel #31
0
class RDFTestCase():
    backend = 'default'
    path = 'store'

    def setUp(self):
        self.store = Graph(store=self.backend)
        self.store.open(self.path)
        self.store.bind("dc", "http://http://purl.org/dc/elements/1.1/")
        self.store.bind("foaf", "http://xmlns.com/foaf/0.1/")
        return self.store

    def tearDown(self):
        self.store.close()
        print self.store.serialize()

    def addDonna(self):
        self.donna = donna = BNode()
        print 'Identificador:', donna.n3()
        self.store.add((donna, RDF.type, FOAF["Person"]))
        self.store.add((donna, FOAF["nick"], Literal("donna")))
        self.store.add((donna, FOAF["name"], Literal("Donna Fales")))
        return self.store

    def testRDFXML(self):
        self.addDonna()
        g = Graph()
        g.parse(StringInputSource(self.store.serialize(format="pretty-xml")))
        #self.assertEquals(self.store.isomorphic(g), True)
        print g.serialize()
class GraphTest(unittest.TestCase):
    backend = 'default'
    path = 'store'

    def setUp(self):
        self.store = Graph(store=self.backend)
        self.store.open(self.path)
        self.remove_me = (BNode(), RDFS.label, Literal("remove_me"))
        self.store.add(self.remove_me)

    def tearDown(self):
        self.store.close()

    def testAdd(self):
        subject = BNode()
        self.store.add((subject, RDFS.label, Literal("foo")))

    def testRemove(self):
        self.store.remove(self.remove_me)
        self.store.remove((None, None, None))

    def testTriples(self):
        for s, p, o in self.store:
            pass
Beispiel #33
0
class GraphTest(unittest.TestCase):
    backend = 'default'
    path = 'store'

    def setUp(self):
        self.store = Graph(store=self.backend)
        self.store.open(self.path)
        self.remove_me = (BNode(), RDFS.label, Literal("remove_me"))
        self.store.add(self.remove_me)

    def tearDown(self):
        self.store.close()

    def testAdd(self):
        subject = BNode()
        self.store.add((subject, RDFS.label, Literal("foo")))

    def testRemove(self):
        self.store.remove(self.remove_me)
        self.store.remove((None, None, None))

    def testTriples(self):
        for s, p, o in self.store:
            pass
Beispiel #34
0
class RDFTestCase(unittest.TestCase):
    backend = 'default'
    path = 'store'

    def setUp(self):
        self.store = Graph(store=self.backend)
        self.store.open(self.path)
        self.store.bind("dc", "http://http://purl.org/dc/elements/1.1/")
        self.store.bind("foaf", "http://xmlns.com/foaf/0.1/")

    def tearDown(self):
        self.store.close()

    def addDonna(self):
        self.donna = donna = BNode()
        self.store.add((donna, RDF.type, FOAF["Person"]))
        self.store.add((donna, FOAF["nick"], Literal("donna")))
        self.store.add((donna, FOAF["name"], Literal("Donna Fales")))

    def testRDFXML(self):
        self.addDonna()
        g = Graph()
        g.parse(StringInputSource(self.store.serialize(format="pretty-xml")))
        self.assertEquals(self.store.isomorphic(g), True)
Beispiel #35
0
            print 'processing %s ...' % license

            # add short code
            #code = code_from_uri(license)
            #print '  code is %s' % code
            #store.remove( (license, NS_DC.identifier, None) )
            #store.add( (license, NS_DC.identifier, Literal(code)) )

            # associate each License with a LicenseSelector
            #if 'sampling' in code:
            #    sel = 'http://creativecommons.org/license/sampling/'
            #elif code in ('GPL', 'LGPL'):
            #    sel = 'http://creativecommons.org/license/software'
            #elif code == 'publicdomain':
            #    sel = 'http://creativecommons.org/license/publicdomain/'
            #else:
            #    sel = 'http://creativecommons.org/license/'
            #print '  LicenseSelector is %s' % sel
            #store.remove( (license, NS_CC.licenseClass, None) )
            #store.add( (license, NS_CC.licenseClass, URIRef(sel)) )

            # add image uris
            store.remove( (license, NS_FOAF.logo, None) )
            for img in image_uris(license):
                print img
                store.add( (license, NS_FOAF.logo, img) )

        file(os.path.join(root, filename), 'w').write(
            store.serialize(format="pretty-xml", max_depth=1))

Beispiel #36
0
def main():
    g = Graph()
    Individual.factoryGraph = g
    g.bind('ex', EX_NS, override=False)

    isChildOf = Property(EX_NS.isChildOf)
    isMarriedTo = Property(EX_NS.isMarriedTo)

    woman = Class(EX_NS.Woman)
    man = Class(
        EX_NS.Man,
        subClassOf=[isMarriedTo | only | woman],
        # complementOf=woman
    )
    woman.subClassOf = [isMarriedTo | only | man]

    # Class(OWL_NS.Thing,subClassOf=[isMarriedTo|min|Literal(1)])

    man.extent = [EX_NS.John, EX_NS.Tim]
    woman.extent = [EX_NS.Kate, EX_NS.Mary]

    #Semantically equivalent to Abox assertion below
    # anon_cls1 = Class(
    #     subClassOf=[isMarriedTo|some|EnumeratedClass(members=[EX_NS.Mary])]
    # )
    # anon_cls1.extent = [EX_NS.John]
    g.add((EX_NS.John, isMarriedTo.identifier, EX_NS.Mary))

    #Semantically equivalent to Abox assertion below
    # anon_cls2 = Class(
    #     subClassOf=[isChildOf|some|EnumeratedClass(members=[EX_NS.John])]
    # )
    # anon_cls2.extent = [EX_NS.Kate]
    g.add((EX_NS.Kate, isChildOf.identifier, EX_NS.John))

    #Semantically equivalent to Abox assertion below
    # anon_cls3 = Class(
    #     subClassOf=[isChildOf|some|EnumeratedClass(members=[EX_NS.Mary])]
    # )
    # anon_cls3.extent = [EX_NS.Tim]
    g.add((EX_NS.Tim, isChildOf.identifier, EX_NS.Mary))

    print g.serialize(format='pretty-xml')

    rule_store, rule_graph, network = SetupRuleStore(makeNetwork=True)
    network.nsMap = {u'ex': EX_NS}

    # NormalFormReduction(g)
    dlp = network.setupDescriptionLogicProgramming(g,
                                                   addPDSemantics=False,
                                                   constructNetwork=False)
    for rule in dlp:
        print rule

    topDownStore = TopDownSPARQLEntailingStore(
        g.store,
        g,
        idb=dlp,
        DEBUG=True,
        derivedPredicates=[EX_NS.Man, EX_NS.Woman],
        nsBindings=network.nsMap,
        identifyHybridPredicates=True)
    targetGraph = Graph(topDownStore)
    rt = targetGraph.query("ASK { ex:Tim ex:isMarriedTo ex:John }",
                           initNs=network.nsMap)
    print rt.askAnswer[0]

    topDownStore.DEBUG = False

    for ind in g.query(
            "SELECT ?ind { ?ind a ?class FILTER(isUri(?ind) && ?class != owl:Class ) }"
    ):
        print "Individual: ", ind
        print "--- Children ---"
        for child in targetGraph.query(
                "SELECT ?child { ?child ex:isChildOf %s }" % ind.n3(),
                initNs=network.nsMap):
            print "\t- ", child
        print "----------------"
Beispiel #37
0
  if (s is None or
      p is None or
      o is None):
    continue

  s2 = getpuri(s, nsfrom, nsto, context, skippuris)
  p2 = getpuri(p, nsfrom, nsto, context, skippuris)
  o2 = getpuri(o, nsfrom, nsto, context, skippuris)

  if (s2 is None):
    s2 = s
  if (p2 is None):
    p2 = p
  if (o2 is None):
    o2 = o

  if (s is not s2 or
      p is not p2 or
      o is not o2):

    m.remove((s, p, o))
    m.add((s2, p2, o2))

  statusc = statusc + 1
  if (statusc % 1000 == 0):
    sys.stderr.write(".")


sys.stderr.write("\n")
m.serialize(destination=sys.stdout, format=options.to_format)
Beispiel #38
0
class GraphTestCase(unittest.TestCase):
    store_name = 'default'
    path = None
    slowtest = True

    def setUp(self):
        self.graph = Graph(store=self.store_name)
        a_tmp_dir = mkdtemp()
        self.path = self.path or a_tmp_dir
        self.graph.open(self.path)

        self.michel = URIRef(u'michel')
        self.tarek = URIRef(u'tarek')
        self.bob = URIRef(u'bob')
        self.likes = URIRef(u'likes')
        self.hates = URIRef(u'hates')
        self.pizza = URIRef(u'pizza')
        self.cheese = URIRef(u'cheese')

    def tearDown(self):
        self.graph.close()

    def addStuff(self):
        tarek = self.tarek
        michel = self.michel
        bob = self.bob
        likes = self.likes
        hates = self.hates
        pizza = self.pizza
        cheese = self.cheese

        self.graph.add((tarek, likes, pizza))
        self.graph.add((tarek, likes, cheese))
        self.graph.add((michel, likes, pizza))
        self.graph.add((michel, likes, cheese))
        self.graph.add((bob, likes, cheese))
        self.graph.add((bob, hates, pizza))
        self.graph.add((bob, hates, michel)) # gasp!

    def removeStuff(self):
        tarek = self.tarek
        michel = self.michel
        bob = self.bob
        likes = self.likes
        hates = self.hates
        pizza = self.pizza
        cheese = self.cheese

        self.graph.remove((tarek, likes, pizza))
        self.graph.remove((tarek, likes, cheese))
        self.graph.remove((michel, likes, pizza))
        self.graph.remove((michel, likes, cheese))
        self.graph.remove((bob, likes, cheese))
        self.graph.remove((bob, hates, pizza))
        self.graph.remove((bob, hates, michel)) # gasp!

    def testAdd(self):
        self.addStuff()

    def testRemove(self):
        self.addStuff()
        self.removeStuff()

    def testTriples(self):
        tarek = self.tarek
        michel = self.michel
        bob = self.bob
        likes = self.likes
        hates = self.hates
        pizza = self.pizza
        cheese = self.cheese
        asserte = self.assertEquals
        triples = self.graph.triples
        Any = None

        self.addStuff()

        # unbound subjects
        asserte(len(list(triples((Any, likes, pizza)))), 2)
        asserte(len(list(triples((Any, hates, pizza)))), 1)
        asserte(len(list(triples((Any, likes, cheese)))), 3)
        asserte(len(list(triples((Any, hates, cheese)))), 0)

        # unbound objects
        asserte(len(list(triples((michel, likes, Any)))), 2)
        asserte(len(list(triples((tarek, likes, Any)))), 2)
        asserte(len(list(triples((bob, hates, Any)))), 2)
        asserte(len(list(triples((bob, likes, Any)))), 1)

        # unbound predicates
        asserte(len(list(triples((michel, Any, cheese)))), 1)
        asserte(len(list(triples((tarek, Any, cheese)))), 1)
        asserte(len(list(triples((bob, Any, pizza)))), 1)
        asserte(len(list(triples((bob, Any, michel)))), 1)

        # unbound subject, objects
        asserte(len(list(triples((Any, hates, Any)))), 2)
        asserte(len(list(triples((Any, likes, Any)))), 5)

        # unbound predicates, objects
        asserte(len(list(triples((michel, Any, Any)))), 2)
        asserte(len(list(triples((bob, Any, Any)))), 3)
        asserte(len(list(triples((tarek, Any, Any)))), 2)

        # unbound subjects, predicates
        asserte(len(list(triples((Any, Any, pizza)))), 3)
        asserte(len(list(triples((Any, Any, cheese)))), 3)
        asserte(len(list(triples((Any, Any, michel)))), 1)

        # all unbound
        asserte(len(list(triples((Any, Any, Any)))), 7)
        self.removeStuff()
        asserte(len(list(triples((Any, Any, Any)))), 0)


    def testStatementNode(self):
        graph = self.graph

        from rdflib.Statement import Statement
        c = URIRef("http://example.org/foo#c")
        r = URIRef("http://example.org/foo#r")
        s = Statement((self.michel, self.likes, self.pizza), c)
        graph.add((s, RDF.value, r))
        self.assertEquals(r, graph.value(s, RDF.value))
        self.assertEquals(s, graph.value(predicate=RDF.value, object=r))

    def testGraphValue(self):
        from rdflib.Graph import GraphValue

        graph = self.graph

        alice = URIRef("alice")
        bob = URIRef("bob")
        pizza = URIRef("pizza")
        cheese = URIRef("cheese")

        g1 = Graph()
        g1.add((alice, RDF.value, pizza))
        g1.add((bob, RDF.value, cheese))
        g1.add((bob, RDF.value, pizza))

        g2 = Graph()
        g2.add((bob, RDF.value, pizza))
        g2.add((bob, RDF.value, cheese))
        g2.add((alice, RDF.value, pizza))

        gv1 = GraphValue(store=graph.store, graph=g1)
        gv2 = GraphValue(store=graph.store, graph=g2)
        graph.add((gv1, RDF.value, gv2))
        v = graph.value(gv1)
        #print type(v)
        self.assertEquals(gv2, v)
        #print list(gv2)
        #print gv2.identifier
        graph.remove((gv1, RDF.value, gv2))

    def testConnected(self):
        graph = self.graph
        self.addStuff()
        self.assertEquals(True, graph.connected())

        jeroen = URIRef("jeroen")
        unconnected = URIRef("unconnected")

        graph.add((jeroen,self.likes,unconnected))

        self.assertEquals(False, graph.connected())
def make_foaf_graph(starturi, steps=3):

    # Initialize the graph
    foafgraph = Graph()

    # Keep track of where we've already been
    visited = set()

    # Keep track of the current crawl queue
    current = set([starturi])

    # Crawl steps out
    for i in range(steps):
        nextstep = set()

        # Visit and parse every URI in the current set, adding it to the graph
        for uri in current:
            visited.add(uri)
            tempgraph = Graph()

            # Construct a request with an ACCEPT header
            # This tells pages you want RDF/XML
            try:
                reqObj = urllib2.Request(uri, None,
                                         {"ACCEPT": "application/rdf+xml"})
                urlObj = urllib2.urlopen(reqObj)
                tempgraph.parse(urlObj, format='xml')
                urlObj.close()
            except:
                print "Couldn't parse %s" % uri
                continue

            # Work around for FOAF's anonymous node problem
            # Map blank node IDs to their seeAlso URIs
            nm = dict([(str(s), n)
                       for s, _, n in tempgraph.triples((None, RDFS['seeAlso'],
                                                         None))])

            # Identify the root node (the one with an image for hi5, or the one called "me")
            imagelist = list(tempgraph.triples((None, FOAF['img'], None)))
            if len(imagelist) > 0:
                nm[imagelist[0][0]] = uri
            else:
                nm[''], nm['#me'] = uri, uri

            # Now rename the blank nodes as their seeAlso URIs
            for s, p, o in tempgraph:
                if str(s) in nm: s = nm[str(s)]
                if str(o) in nm: o = nm[str(o)]
                foafgraph.add((s, p, o))

            # Now look for the next step
            newfriends = tempgraph.query('SELECT ?burl ' +\
                                         'WHERE {?a foaf:knows ?b . ?b rdfs:seeAlso ?burl . }',
                                         initNs={'foaf':FOAF,'rdfs':RDFS})

            # Get all the people in the graph. If we haven't added them already, add them
            # to the crawl queue
            for friend in newfriends:
                if friend[0] not in current and friend[0] not in visited:
                    nextstep.add(friend[0])
                    visited.add(friend[0])

        # The new queue becomes the current queue
        current = nextstep
    return foafgraph
Beispiel #40
0
class LoadToRdf(object):
    def __init__(self):
        self.graph = Graph(store=IOMemory())
        # Bind a few prefix, namespace pairs.
        self.graph.bind('dc', 'http://http://purl.org/dc/elements/1.1/')
        self.graph.bind('foaf', 'http://xmlns.com/foaf/0.1/')

    def json_to_rdf(self):
        person_dict = {}

        dickens = self.add_person(DICKENS)
        self.graph.add((self.Letter, RDF.type, RDFS.Class))
        self.graph.add((self.Letter, RDFS.label, Literal('A postal letter')))
        for name, year in load_json():
            if name in person_dict:
                person = person_dict[name]
            else:
                person = self.add_person(name)
                person_dict[name] = person
            self.add_letter(person, dickens, year, id)
        self.graph.commit()
        print 'Writing RDF data to %s' % PATH
        self.graph.serialize(open(PATH, 'w'), format='nt')

    def add_person(self, name):
        nameid = name.lower().replace(' ', '_').replace('.', '')
        person = URIRef(base_uri + 'person#%s' % nameid)
        self.graph.add((person, RDF.type, FOAF['Person']))
        self.graph.add((person, FOAF['name'], Literal(name)))
        return person

    def add_letter(self, to, from_, date, id):
        letter = URIRef(base_uri + 'letter#%s' % id)
        self.graph.add((letter, RDF.type, self.Letter))
        self.graph.add((letter, letter_ns['to'], to))
        self.graph.add((letter, letter_ns['from'], from_))
        self.graph.add((letter, letter_ns['date'], Literal(date)))
        return letter
def LowestCommonFMAAncestors(locusMap,fmaGraph, snctGraph, vizGraph):
    #subsumerMap / coverage : Aanc  -> [ Afma1, Afma2, ..., AfmaN ]
    #CA                     : Afma -> [ commonAncestor1, commonAncestor2, ... ] 
    coverage  = {}
    ca        = {}    
    terms     = set()
    labelToId = {}
    RIPGraph  = Graph()
    RIPGraph.bind('rip',RIP)
    RIPGraph.bind('rdfs',RDFS.RDFSNS)
    def LocusPropagationTraversal(node,graph):
        """
        node  - an FuXi.Syntax.InfixOWL.Class instance
        graph - an RDF graph
        
        (Transitively) traverses from the leaves towards the root of a 
        right identity axiom spanning tree 
        (for procedure and disease mechanism inferences). 
        Formed from an OWL / RDF graph of clinical medicine (via SNOMED-CT and FMA)
        
        """
        if node.identifier.find(FMA)+1:
            node.graph = fmaGraph
            if node.identifier == FMA['FMA_61775']:
                return
            #An FMA term - strict EL, and we are only concerned
            #with atomic concept inclusion and GCIs with 
            #existential role restrictions
            #Note: all FMA terms are interpreted WRT the FMA
            #OWL/RDF graph
            #assert node.isPrimitive()
            for parent in node.subClassOf:
                if OWL.Restriction in parent.type:
                    if (parent.identifier,
                        OWL.onProperty,
                        partOf) in graph:
                        parent = Restriction(partOf,
                                             fmaGraph,
                                             someValuesFrom='..',
                                             identifier=parent.identifier)
                        cls = Class(parent.restrictionRange,
                                    skipOWLClassMembership=True)
                        cls.prior = node
                        yield cls
                elif parent.identifier.find(FMA)+1:
                    parent.prior = node
                    yield parent
        else:
            #In O'snct-fma
            node = CastClass(node)
            if isinstance(node,BooleanClass):
                _list = [i for i in node]
            else:
                _list = [i for i in node.subClassOf]
            for parent in _list:
                parent = Class(classOrIdentifier(parent),
                               skipOWLClassMembership=True,
                               graph=classOrIdentifier(parent).find(FMA)+1 and \
                               fmaGraph or node.graph)
                if OWL.Restriction in parent.type:
                    if (parent.identifier,
                        OWL.onProperty,
                        partOf) in node.graph:
                        parent = Restriction(partOf,
                                             node.graph,
                                             someValuesFrom='..',
                                             identifier=parent.identifier)
                        link = parent.restrictionRange
                        cls = Class(link,
                                    skipOWLClassMembership=True,
                                    graph=link.find(FMA)+1 and fmaGraph or \
                                    node.graph)
                        cls.prior = node
                        yield cls
                else:
                    cls = Class(classOrIdentifier(parent),
                                skipOWLClassMembership=True,
                                graph=classOrIdentifier(parent).find(FMA)+1 and \
                                fmaGraph or graph)
                    cls.prior = node
                    yield  cls
    for cl,places in locusMap.items():
        assert isinstance(cl,URIRef)        
#        cls=Class(cl,graph=snctGraph)
#        assert not isinstance(cl,BNode),cls.__repr__(True)
        for fmaTerm in places:
            #For every
            if isinstance(fmaTerm,FMARestrictionQuery):
                for fmaTerm in fmaTerm(fmaGraph):
                    terms.add(Class(fmaTerm,
                                    skipOWLClassMembership=True,
                                    graph=fmaGraph))
                    RIPGraph.add((cl,
                                  RIP.locus,
                                  classOrIdentifier(fmaTerm)))                    
            else:
                terms.add(fmaTerm)
                RIPGraph.add((cl,
                              RIP.locus,
                              classOrIdentifier(fmaTerm)))
                
    for term in terms:
        leafLabel = mintNodeLabel(term,snctGraph,fmaGraph)
        AddNode(vizGraph,
                RIPGraph,
                leafLabel,
                term,
                OBSERVED_ANAT_RIP_NODE_TYPE)
        termId = classOrIdentifier(term)
        ontGraph = termId.find(FMA)+1 and fmaGraph or snctGraph
        #For each possibly observed anatomical sites
        term = Class(classOrIdentifier(term),
                     skipOWLClassMembership=True,
                     graph=ontGraph)
        def isSibling(_term):
            if _term == termId:
                return False
            _term = Class(_term,
                          skipOWLClassMembership=True,
                          graph=ontGraph)
            return _term in terms and termId not in coverage.get(_term,set())  
        commonSiblings = set()
        for ancestor in term.graph.transitiveClosure(LocusPropagationTraversal,
                                                     term):
            ancLabel    = mintNodeLabel(ancestor,snctGraph,fmaGraph)
            #update subsumer map

            coverage.setdefault(ancestor.identifier,
                                set()).add(term.identifier)
            #Is the original term a new member of the subsumer set for the ancestor?
            #If so, the ancestor is an LCA for all the terms subsumed by the ancestor
            #First we get all the (prior) terms subsumed by this ancestor that are
            #observed anatomy kinds 
            siblings = set(ifilter(isSibling,
                                   coverage.get(ancestor.identifier,set())))
            _type = NORMAL_RIP_NODE_TYPE
            #if siblings and not siblings.intersection(commonSiblings) and \
            if siblings and ancestor not in terms:
                #This is a (new) common anatomy ancestor of another
                #observed entity for and is not itself an observed entity
                commonSiblings.update(siblings)
                ca.setdefault(term.identifier,
                               set()).add(ancestor.identifier)
                _type = CA_RIP_NODE_TYPE
            if isinstance(ancestor.identifier,BNode):
                assert _type != CA_RIP_NODE_TYPE
                _type = ANON_RIP_NODE_TYPE
            priorLabel = mintNodeLabel(ancestor.prior,snctGraph,fmaGraph)
            AddNode(vizGraph,RIPGraph,ancLabel,ancestor,_type)
            AddEdge(vizGraph,
                    RIPGraph,
                    ancestor.prior,
                    priorLabel,
                    ancestor,
                    ancLabel)
    return RIPGraph, coverage
Beispiel #42
0
class RDFWriter(_CaseWriter):
    """ Writes cases as RDF/XML.
    """
    def __init__(self, case):
        super(RDFWriter, self).__init__(case)

        self.store = Graph()

        # Map of Bus objects to BNodes.
        self.bus_map = {}

    def _write_data(self, file):
        super(RDFWriter, self)._write_data(file)

        NS_PYLON = Namespace("http://rwl.github.com/pylon/")

        self.store.bind("pylon", "http://rwl.github.com/pylon/")

        for bus in self.case.buses:
            bus_node = BNode()
            #            bus_node = URIRef(id(bus))
            self.bus_map[bus] = bus_node
            self.store.add((bus_node, RDF.type, NS_PYLON["Bus"]))
            for attr in BUS_ATTRS:
                self.store.add(
                    (bus_node, NS_PYLON[attr], Literal(getattr(bus, attr))))

        for branch in self.case.branches:
            branch_node = BNode()
            self.store.add((branch_node, RDF.type, NS_PYLON["Branch"]))

            #            self.store.add((branch_node, NS_PYLON["from_bus"],
            #                            self.bus_map[branch.from_bus]))

            for attr in BRANCH_ATTRS:
                self.store.add((branch_node, NS_PYLON[attr],
                                Literal(getattr(branch, attr))))

        for generator in self.case.generators:
            g_node = BNode()
            self.store.add((g_node, RDF.type, NS_PYLON["Generator"]))
            for attr in GENERATOR_ATTRS:
                self.store.add(
                    (g_node, NS_PYLON[attr], Literal(getattr(generator,
                                                             attr))))

        file.write(self.store.serialize(format="pretty-xml", max_depth=3))
Beispiel #43
0
    def prep_annotation_file(self,basefile):
        print "prep_annotation_file"



        baseline = self.ranked_set_baseline(basefile)
        
        # goldstandard = self.ranked_set_goldstandard(basefile)
        rs2 = self.ranked_set_fake2(basefile)
        rs3 = self.ranked_set_fake3(basefile)
        rs4 = self.ranked_set_fake4(basefile)
        # goldstandard = {'1': ['62009J0014','62009J0197','62009J0357','62009J0403','62009A0027']}
        # self.calculate_map(rs1,goldstandard)

        goldstandard = {'1': [['62009J0014',u'Genc v Land Berlin (100%)'],
                              ['62009J0197',u'Agence européenne des médicaments (90%)'],
                              ['62009J0357',u'Huchbarov (80%)'],
                              ['62009J0403',u'Jasna Deticke (70%)'],
                              ['62009A0027',u'Stella Kunststofftechnik(60%)']]}
        
        sets = [{'label':'Baseline',
                 'data':baseline},
                {'label':'Gold standard',
                 'data':goldstandard}]
        
        g = Graph()
        g.bind('dct',self.ns['dct'])
        g.bind('rinfoex',self.ns['rinfoex'])
        
        XHT_NS = "{http://www.w3.org/1999/xhtml}"
        tree = ET.parse(self.parsed_path(basefile))
        els = tree.findall("//"+XHT_NS+"div")
        articles = []
        for el in els:
            if 'typeof' in el.attrib and el.attrib['typeof'] == "eurlex:Article":
                article = unicode(el.attrib['id'][1:])
                articles.append(article)
        for article in articles:
            print "Results for article %s" % article
            articlenode = URIRef("http://rinfo.lagrummet.se/extern/celex/12008E%03d" % int(article))
            resultsetcollectionnode = BNode()
            g.add((resultsetcollectionnode, RDF.type, RDF.List))
            rc = Collection.Collection(g,resultsetcollectionnode)
            g.add((articlenode, DCT["relation"], resultsetcollectionnode))
            for s in sets:
                resultsetnode = BNode()
                listnode = BNode()
                rc.append(resultsetnode)
                g.add((resultsetnode, RDF.type, RINFOEX["RelatedContentCollection"]))
                g.add((resultsetnode, DCT["title"], Literal(s["label"])))
                g.add((resultsetnode, DCT["hasPart"], listnode))
                c = Collection.Collection(g,listnode)
                g.add((listnode, RDF.type, RDF.List))
                if article in s['data']:
                    print "    Set %s" % s['label']
                    for result in s['data'][article]:
                        resnode = BNode()
                        g.add((resnode, DCT["references"], Literal(result[0])))
                        g.add((resnode, DCT["title"], Literal(result[1])))
                        c.append(resnode)
                        print "        %s" % result[1]
                    
        # self.graph_to_image(g,"png",self.annotation_path(basefile)+".png")
        return self.graph_to_annotation_file(g, basefile)
Beispiel #44
0
class ReteNetwork:
    """
    The Rete network.  The constructor takes an N3 rule graph, an identifier (a BNode by default), an 
    initial Set of Rete tokens that serve as the 'working memory', and an rdflib Graph to 
    add inferred triples to - by forward-chaining via Rete evaluation algorithm), 
    """
    def __init__(self,ruleStore,name = None,
                 initialWorkingMemory = None, 
                 inferredTarget = None,
                 nsMap = {},
                 graphVizOutFile=None,
                 dontFinalize=False,
                 goal=None,
                 rulePrioritizer=None,
                 alphaNodePrioritizer=None):
        self.leanCheck = {}
        self.goal = goal        
        self.nsMap = nsMap
        self.name = name and name or BNode()
        self.nodes = {}
        self.alphaPatternHash = {}
        self.ruleSet = set()
        for alphaPattern in xcombine(('1','0'),('1','0'),('1','0')):
            self.alphaPatternHash[tuple(alphaPattern)] = {}
        if inferredTarget is None:
            self.inferredFacts = Graph()
            namespace_manager = NamespaceManager(self.inferredFacts)
            for k,v in nsMap.items():
                namespace_manager.bind(k, v)    
            self.inferredFacts.namespace_manager = namespace_manager    
        else:            
            self.inferredFacts = inferredTarget
        self.workingMemory = initialWorkingMemory and initialWorkingMemory or set()
        self.proofTracers = {}
        self.terminalNodes  = set()
        self.instanciations = {}        
        start = time.time()
        self.ruleStore=ruleStore
        self.justifications = {}
        self.dischargedBindings = {}
        if not dontFinalize:
            self.ruleStore._finalize()
        self.filteredFacts = Graph()
        self.rulePrioritizer      = rulePrioritizer
        self.alphaNodePrioritizer = alphaNodePrioritizer
        
        #'Universal truths' for a rule set are rules where the LHS is empty.  
        # Rather than automatically adding them to the working set, alpha nodes are 'notified'
        # of them, so they can be checked for while performing inter element tests.
        self.universalTruths = []
        from FuXi.Horn.HornRules import Ruleset
        self.rules=set()
        self.negRules = set()
        for rule in Ruleset(n3Rules=self.ruleStore.rules,nsMapping=self.nsMap):
            import warnings
            warnings.warn(
          "Rules in a network should be built *after* construction via "+
          " self.buildNetworkClause(HornFromN3(n3graph)) for instance",
                          DeprecationWarning,2)            
            self.buildNetworkFromClause(rule)
        self.alphaNodes = [node for node in self.nodes.values() if isinstance(node,AlphaNode)]
        self.alphaBuiltInNodes = [node for node in self.nodes.values() if isinstance(node,BuiltInAlphaNode)]
        self._setupDefaultRules()
        if initialWorkingMemory:            
            start = time.time()          
            self.feedFactsToAdd(initialWorkingMemory)
            print >>sys.stderr,"Time to calculate closure on working memory: %s m seconds"%((time.time() - start) * 1000)            
        if graphVizOutFile:
            print >>sys.stderr,"Writing out RETE network to ", graphVizOutFile
            renderNetwork(self,nsMap=nsMap).write(graphVizOutFile)
                        
    def getNsBindings(self,nsMgr):
        for prefix,Uri in nsMgr.namespaces():
            self.nsMap[prefix]=Uri

    def buildFilterNetworkFromClause(self,rule):
        lhs = BNode()
        rhs = BNode()
        builtins=[]
        for term in rule.formula.body:
            if isinstance(term, N3Builtin):
                #We want to move builtins to the 'end' of the body
                #so they only apply to the terminal nodes of 
                #the corresponding network
                builtins.append(term)
            else:
                self.ruleStore.formulae.setdefault(lhs,Formula(lhs)).append(term.toRDFTuple())
        for builtin in builtins:
            self.ruleStore.formulae.setdefault(lhs,Formula(lhs)).append(builtin.toRDFTuple())
        nonEmptyHead=False
        for term in rule.formula.head:
            nonEmptyHead=True
            assert not hasattr(term,'next')
            assert isinstance(term,Uniterm)
            self.ruleStore.formulae.setdefault(rhs,Formula(rhs)).append(term.toRDFTuple())
        assert nonEmptyHead,"Filters must conclude something!"
        self.ruleStore.rules.append((self.ruleStore.formulae[lhs],self.ruleStore.formulae[rhs]))
        tNode = self.buildNetwork(iter(self.ruleStore.formulae[lhs]),
                             iter(self.ruleStore.formulae[rhs]),
                             rule,
                             aFilter=True)
        self.alphaNodes = [node for node in self.nodes.values() if isinstance(node,AlphaNode)]
        self.rules.add(rule)
        return tNode
                        
    def buildNetworkFromClause(self,rule):
        lhs = BNode()
        rhs = BNode()
        builtins=[]
        for term in rule.formula.body:
            if isinstance(term, N3Builtin):
                #We want to move builtins to the 'end' of the body
                #so they only apply to the terminal nodes of 
                #the corresponding network
                builtins.append(term)
            else:
                self.ruleStore.formulae.setdefault(lhs,Formula(lhs)).append(term.toRDFTuple())
        for builtin in builtins:
            self.ruleStore.formulae.setdefault(lhs,Formula(lhs)).append(builtin.toRDFTuple())
        nonEmptyHead=False
        for term in rule.formula.head:
            nonEmptyHead=True
            assert not hasattr(term,'next')
            assert isinstance(term,Uniterm)
            self.ruleStore.formulae.setdefault(rhs,Formula(rhs)).append(term.toRDFTuple())
        if not nonEmptyHead:
            import warnings
            warnings.warn(
          "Integrity constraints (rules with empty heads) are not supported!: %s"%rule,
                          SyntaxWarning,2)            
            return
        self.ruleStore.rules.append((self.ruleStore.formulae[lhs],self.ruleStore.formulae[rhs]))
        tNode = self.buildNetwork(iter(self.ruleStore.formulae[lhs]),
                             iter(self.ruleStore.formulae[rhs]),
                             rule)
        self.alphaNodes = [node for node in self.nodes.values() if isinstance(node,AlphaNode)]
        self.rules.add(rule)
        return tNode
        
    def calculateStratifiedModel(self,database):
        """
        Stratified Negation Semantics for DLP using SPARQL to handle the negation
        """
        if not self.negRules:
            return
        from FuXi.DLP.Negation import StratifiedSPARQL
        from FuXi.Rete.Magic import PrettyPrintRule
        import copy
        noNegFacts = 0
        for i in self.negRules:
            #Evaluate the Graph pattern, and instanciate the head of the rule with 
            #the solutions returned
            nsMapping = dict([(v,k) for k,v in self.nsMap.items()])
            sel,compiler=StratifiedSPARQL(i,nsMapping)
            query=compiler.compile(sel)
            i.stratifiedQuery=query
            vars = sel.projection
            unionClosureG = self.closureGraph(database)
            for rt in unionClosureG.query(query):
                solutions={}
                if isinstance(rt,tuple):
                    solutions.update(dict([(vars[idx],i) for idx,i in enumerate(rt)]))
                else:
                    solutions[vars[0]]=rt
                i.solutions=solutions
                head=copy.deepcopy(i.formula.head)
                head.ground(solutions)
                fact=head.toRDFTuple()
                self.inferredFacts.add(fact)
                self.feedFactsToAdd(generateTokenSet([fact]))
                noNegFacts += 1
        #Now we need to clear assertions that cross the individual, concept, relation divide
        toRemove=[]
        for s,p,o in self.inferredFacts.triples((None,RDF.type,None)):
            if s in unionClosureG.predicates() or\
               s in [_s for _s,_p,_o in 
                        unionClosureG.triples_choices(
                                            (None,
                                             RDF.type,
                                             [OWL_NS.Class,
                                              OWL_NS.Restriction]))]:
                self.inferredFacts.remove((s,p,o))
        return noNegFacts
                        
    def setupDescriptionLogicProgramming(self,
                                         owlN3Graph,
                                         expanded=[],
                                         addPDSemantics=True,
                                         classifyTBox=False,
                                         constructNetwork=True,
                                         derivedPreds=[],
                                         ignoreNegativeStratus=False,
                                         safety = DATALOG_SAFETY_NONE):
        rt=[rule 
                    for rule in MapDLPtoNetwork(self,
                                                owlN3Graph,
                                                complementExpansions=expanded,
                                                constructNetwork=constructNetwork,
                                                derivedPreds=derivedPreds,
                                                ignoreNegativeStratus=ignoreNegativeStratus,
                                                safety = safety)]
        if ignoreNegativeStratus:
            rules,negRules=rt
            rules = set(rules)
            self.negRules = set(negRules)
        else:
            rules=set(rt)
        if constructNetwork:
            self.rules.update(rules)
        additionalRules = set(AdditionalRules(owlN3Graph))
        if addPDSemantics:
            from FuXi.Horn.HornRules import HornFromN3
            additionalRules.update(HornFromN3(StringIO(non_DHL_OWL_Semantics)))
        
        if constructNetwork:
            for rule in additionalRules:
                self.buildNetwork(iter(rule.formula.body),
                                  iter(rule.formula.head),
                                  rule)
                self.rules.add(rule)
        else:
            rules.update(additionalRules)
            
        if constructNetwork:
            rules = self.rules
            
        noRules=len(rules)
        if classifyTBox:
            self.feedFactsToAdd(generateTokenSet(owlN3Graph))
#        print "##### DLP rules fired against OWL/RDF TBOX",self
        return rules
    
    def reportSize(self,tokenSizeThreshold=1200,stream=sys.stdout):
        for pattern,node in self.nodes.items():
            if isinstance(node,BetaNode):
                for largeMem in itertools.ifilter(
                                   lambda i:len(i) > tokenSizeThreshold,
                                   node.memories.itervalues()):
                    if largeMem:
                        print >>stream, "Large apha node memory extent: "
                        pprint(pattern)
                        print >>stream, len(largeMem)        
    
    def reportConflictSet(self,closureSummary=False,stream=sys.stdout):
        tNodeOrder = [tNode 
                        for tNode in self.terminalNodes 
                            if self.instanciations.get(tNode,0)]
        tNodeOrder.sort(key=lambda x:self.instanciations[x],reverse=True)
        for termNode in tNodeOrder:
            print >>stream,termNode
            print >>stream,"\t", termNode.clauseRepresentation()
            print >>stream,"\t\t%s instanciations"%self.instanciations[termNode]
        if closureSummary:        
            print >>stream ,self.inferredFacts.serialize(destination=stream,format='turtle')
                
    def parseN3Logic(self,src):
        store=N3RuleStore(additionalBuiltins=self.ruleStore.filters)
        Graph(store).parse(src,format='n3')
        store._finalize()
        assert len(store.rules),"There are no rules passed in!"
        from FuXi.Horn.HornRules import Ruleset
        for rule in Ruleset(n3Rules=store.rules,
                            nsMapping=self.nsMap):
            self.buildNetwork(iter(rule.formula.body),
                              iter(rule.formula.head),
                              rule)
            self.rules.add(rule)
        self.alphaNodes = [node for node in self.nodes.values() if isinstance(node,AlphaNode)]
        self.alphaBuiltInNodes = [node for node in self.nodes.values() if isinstance(node,BuiltInAlphaNode)]        

    def __repr__(self):
        total = 0 
        for node in self.nodes.values():
            if isinstance(node,BetaNode):
                total+=len(node.memories[LEFT_MEMORY])
                total+=len(node.memories[RIGHT_MEMORY])
        
        return "<Network: %s rules, %s nodes, %s tokens in working memory, %s inferred tokens>"%(len(self.terminalNodes),len(self.nodes),total,len(self.inferredFacts))
        
    def closureGraph(self,sourceGraph,readOnly=True,store=None):
        if readOnly:
            if store is None and not sourceGraph:
                store = Graph().store
            store = store is None and sourceGraph.store or store
            roGraph=ReadOnlyGraphAggregate([sourceGraph,self.inferredFacts],
                                           store=store)
            roGraph.namespace_manager = NamespaceManager(roGraph)
            for srcGraph in [sourceGraph,self.inferredFacts]:
                for prefix,uri in srcGraph.namespaces():
                    roGraph.namespace_manager.bind(prefix,uri)
            return roGraph
        else:
            cg=ConjunctiveGraph()
            cg+=sourceGraph
            cg+=self.inferredFacts
            return cg        

    def _setupDefaultRules(self):
        """
        Checks every alpha node to see if it may match against a 'universal truth' (one w/out a LHS)
        """
        for node in self.nodes.values():
            if isinstance(node,AlphaNode):                
                node.checkDefaultRule(self.universalTruths)
        
    def clear(self):
        self.nodes = {}
        self.alphaPatternHash = {}
        self.rules = set()
        for alphaPattern in xcombine(('1','0'),('1','0'),('1','0')):
            self.alphaPatternHash[tuple(alphaPattern)] = {}
        self.proofTracers = {}
        self.terminalNodes  = set()
        self.justifications = {}
        self._resetinstanciationStats()
        self.workingMemory = set()
        self.dischargedBindings = {}
        
    def reset(self,newinferredFacts=None):
        "Reset the network by emptying the memory associated with all Beta Nodes nodes"
        for node in self.nodes.values():
            if isinstance(node,BetaNode):
                node.memories[LEFT_MEMORY].reset()
                node.memories[RIGHT_MEMORY].reset()
        self.justifications = {}
        self.proofTracers = {}
        self.inferredFacts = newinferredFacts if newinferredFacts is not None else Graph()
        self.workingMemory = set()
        self._resetinstanciationStats()

    def handleInferredTriple(self,
                             inferredTriple,
                             tokens,
                             termNode,
                             binding,
                             debug=False,
                             executeFn=None):
        inferredToken=ReteToken(inferredTriple)
        self.proofTracers.setdefault(inferredTriple,[]).append(binding)
        self.justifications.setdefault(inferredTriple,set()).add(termNode)
        if termNode.filter and inferredTriple not in self.filteredFacts:
            self.filteredFacts.add(inferredTriple)
        if inferredTriple not in self.inferredFacts and inferredToken not in self.workingMemory:
            if debug:
                lit = BuildUnitermFromTuple(inferredTriple,
                    newNss=self.nsMap)
                print "Inferred triple: ", lit, " from ",termNode.clauseRepresentation()
                inferredToken.debug = True
            if executeFn:
                #The indicated execute action is supposed to be triggered
                #when the indicates RHS triple is inferred for the
                #first time
                executeFn(termNode,inferredTriple,tokens,binding,debug)
            if self.goal is not None and self.goal == inferredTriple:#in self.inferredFacts:
                for binding in tokens.bindings:
                    rt=None#self.bfp.extractProof(binding,termNode.ruleNo,inferredTriple,applyBindings=True)
                    def depth(l):
                        assert isinstance(l,(tuple))
                        if isinstance(l,tuple):
                            depths = [depth(item) for item in l[2]]
                            return 1 + max(depths) if depths else 1
                            #self.bfp.proof = -1,rt#depth(rt),rt
                if debug:
                    "Proved goal " + repr(self.goal)
                raise InferredGoal("Proved goal " + repr(self.goal))
            self.inferredFacts.add(inferredTriple)
            self.addWME(inferredToken)
            currIdx = self.instanciations.get(termNode,0)
            currIdx+=1
            self.instanciations[termNode] = currIdx

        else:
            if debug:
                lit = BuildUnitermFromTuple(inferredTriple,
                    newNss=self.nsMap)
                print "Inferred triple skipped: ", lit
            if executeFn:
                #The indicated execute action is supposed to be triggered
                #when the indicates RHS triple is inferred for the
                #first time
                executeFn(termNode,inferredTriple,tokens,binding,debug)

    def fireConsequent(self,tokens,termNode,debug=False):
        """
        
        "In general, a p-node also contains a specifcation of what production it corresponds to | the
        name of the production, its right-hand-side actions, etc. A p-node may also contain information
        about the names of the variables that occur in the production. Note that variable names
        are not mentioned in any of the Rete node data structures we describe in this chapter. This is
        intentional |it enables nodes to be shared when two productions have conditions with the same
        basic form, but with different variable names."        
        
        
        Takes a set of tokens and the terminal Beta node they came from
        and fires the inferred statements using the patterns associated
        with the terminal node.  Statements that have been previously inferred
        or already exist in the working memory are not asserted
        """
        if debug:
            print "%s from %s"%(tokens,termNode)

        newTokens = []
        termNode.instanciatingTokens.add(tokens)
        def iterCondition(condition):
            if isinstance(condition,Exists):
                return condition.formula
            return isinstance(condition,SetOperator) and condition or iter([condition])
        def extractVariables(term,existential=True):
            if isinstance(term,existential and BNode or Variable):
                yield term
            elif isinstance(term,Uniterm):
                for t in term.toRDFTuple():
                    if isinstance(t,existential and BNode or Variable):
                        yield t
        
        #replace existentials in the head with new BNodes!
        BNodeReplacement = {}
        for rule in termNode.rules:
            if isinstance(rule.formula.head,Exists):
                for bN in rule.formula.head.declare:
                    if not isinstance(rule.formula.body,Exists) or \
                       bN not in rule.formula.body.declare:
                       BNodeReplacement[bN] = BNode()
        consequents = self.rulePrioritizer(termNode.consequent
            ) if self.rulePrioritizer else termNode.consequent
        for rhsTriple in consequents:
            if BNodeReplacement:
                rhsTriple = tuple([BNodeReplacement.get(term,term) for term in rhsTriple])
            if debug:
                if not tokens.bindings:
                    tokens._generateBindings()
            key = tuple(map(lambda item: None if isinstance(item,BNode) else item,rhsTriple))
            override,executeFn = termNode.executeActions.get(key,(None,None))
            
            if override:
                #There is an execute action associated with this production
                #that is attaced to the given consequent triple and
                #is meant to perform all of the production duties
                #(bypassing the inference of triples, etc.)
                executeFn(termNode,None,tokens,None,debug)                    
            else:                
                for inferredTriple,binding in _mulPatternWithSubstitutions(tokens,rhsTriple,termNode):
                    if [term for term in inferredTriple if isinstance(term,Variable)]:
                        #Unfullfilled bindings (skip non-ground head literals)
                        if executeFn:
                            #The indicated execute action is supposed to be triggered
                            #when the indicates RHS triple is inferred for the
                            #(even if it is not ground)
                            executeFn(termNode,inferredTriple,tokens,binding,debug)                                        
                        continue
                    self.handleInferredTriple(inferredTriple,
                                              tokens,
                                              termNode,
                                              binding,
                                              debug,
                                              executeFn)

    def addWME(self,wme):
        """
        procedure add-wme (w: WME) exhaustive hash table versiong
            let v1, v2, and v3 be the symbols in the three fields of w
            alpha-mem = lookup-in-hash-table (v1,v2,v3)
            if alpha-mem then alpha-memory-activation (alpha-mem, w)
            alpha-mem = lookup-in-hash-table (v1,v2,*)
            if alpha-mem then alpha-memory-activation (alpha-mem, w)
            alpha-mem = lookup-in-hash-table (v1,*,v3)
            if alpha-mem then alpha-memory-activation (alpha-mem, w)
            ...
            alpha-mem = lookup-in-hash-table (*,*,*)
            if alpha-mem then alpha-memory-activation (alpha-mem, w)
        end        
        """
#        print wme.asTuple()
        #If the user provided a function that enforces an ordering in the
        # evaluation of alpha nodes, then we use this ordering or the 'natural'
        # ordering otherwise
        aNodes = reduce(
            lambda l,r:l+r,
            filter(lambda i:i,
                   map(lambda (termComb,termDict): termDict.get(wme.alphaNetworkHash(termComb),[]) ,
                       iteritems(self.alphaPatternHash))),[])
        sortedANodes = self.alphaNodePrioritizer(aNodes) if self.alphaNodePrioritizer else aNodes
        for alphaNode in sortedANodes:
#            print "\t## Activated AlphaNode ##"
#            print "\t\t",termComb,wme.alphaNetworkHash(termComb)
#            print "\t\t",alphaNode
            alphaNode.activate(wme.unboundCopy())

    def feedFactsToAdd(self,tokenIterator):
        """
        Feeds the network an iterator of facts / tokens which are fed to the alpha nodes 
        which propagate the matching process through the network
        """
        for token in tokenIterator:
            self.workingMemory.add(token)
            #print token.unboundCopy().bindingDict
            self.addWME(token)
    
    def _findPatterns(self,patternList):
        rt = []
        for betaNodePattern, alphaNodePatterns in \
            [(patternList[:-i],patternList[-i:]) for i in xrange(1,len(patternList))]:
            assert isinstance(betaNodePattern,HashablePatternList)
            assert isinstance(alphaNodePatterns,HashablePatternList)            
            if betaNodePattern in self.nodes:                                
                rt.append(betaNodePattern)
                rt.extend([HashablePatternList([aPattern]) for aPattern in alphaNodePatterns])
                return rt
        for alphaNodePattern in patternList:
            rt.append(HashablePatternList([alphaNodePattern]))
        return rt            
    
    def createAlphaNode(self,currentPattern):
        """
        """
        if isinstance(currentPattern,N3Builtin):
            node = BuiltInAlphaNode(currentPattern)
        else:
            node = AlphaNode(currentPattern,self.ruleStore.filters)
        self.alphaPatternHash[node.alphaNetworkHash()].setdefault(node.alphaNetworkHash(groundTermHash=True),[]).append(node)
        if not isinstance(node,BuiltInAlphaNode) and node.builtin:
            s,p,o = currentPattern
            node = BuiltInAlphaNode(N3Builtin(p,self.ruleStore.filters[p](s,o),s,o))
        return node
    
    def _resetinstanciationStats(self):
        self.instanciations = dict([(tNode,0) for tNode in self.terminalNodes])
        
    def checkDuplicateRules(self):
        checkedClauses={}
        for tNode in self.terminalNodes:
            for rule in tNode.rules:
                collision = checkedClauses.get(rule.formula)
                assert collision is None,"%s collides with %s"%(tNode,checkedClauses[rule.formula])
                checkedClauses.setdefault(tNode.rule.formula,[]).append(tNode)

    def registerReteAction(self,headTriple,override,executeFn):
        """
        Register the given execute function for any rule with the
        given head using the override argument to determine whether or
        not the action completely handles the firing of the rule.

        The signature of the execute action is as follows:

        def someExecuteAction(tNode, inferredTriple, token, binding):
            .. pass ..
        """
        for tNode in self.terminalNodes:
            for rule in tNode.rules:
                if not isinstance(rule.formula.head, (Exists,Uniterm)):
                    continue
                headTriple = GetUterm(rule.formula.head).toRDFTuple()
                headTriple = tuple(
                    map(lambda item: None if isinstance(item,BNode) else item,
                        headTriple))
                tNode.executeActions[headTriple] = (override,executeFn)
    
    def buildNetwork(self,lhsIterator,rhsIterator,rule,aFilter=False):
        """
        Takes an iterator of triples in the LHS of an N3 rule and an iterator of the RHS and extends
        the Rete network, building / reusing Alpha 
        and Beta nodes along the way (via a dictionary mapping of patterns to the built nodes)
        """
        matchedPatterns   = HashablePatternList()
        attachedPatterns = []
        hasBuiltin = False
        LHS = []
        while True:
            try:
                currentPattern = lhsIterator.next()
                #The LHS isn't done yet, stow away the current pattern
                #We need to convert the Uniterm into a triple
                if isinstance(currentPattern,Uniterm):
                    currentPattern = currentPattern.toRDFTuple()
                LHS.append(currentPattern)
            except StopIteration:                
                #The LHS is done, need to initiate second pass to recursively build join / beta
                #nodes towards a terminal node                
                
                #We need to convert the Uniterm into a triple
                consequents = [isinstance(fact,Uniterm) and fact.toRDFTuple() or fact for fact in rhsIterator]
                if matchedPatterns and matchedPatterns in self.nodes:
                    attachedPatterns.append(matchedPatterns)
                elif matchedPatterns:
                    rt = self._findPatterns(matchedPatterns)
                    attachedPatterns.extend(rt)
                if len(attachedPatterns) == 1:
                    node = self.nodes[attachedPatterns[0]]
                    if isinstance(node,BetaNode):
                        terminalNode = node
                    else:
                        paddedLHSPattern = HashablePatternList([None])+attachedPatterns[0]                    
                        terminalNode = self.nodes.get(paddedLHSPattern)
                        if terminalNode is None:
                            #New terminal node
                            terminalNode = BetaNode(None,node,aPassThru=True)
                            self.nodes[paddedLHSPattern] = terminalNode    
                            node.connectToBetaNode(terminalNode,RIGHT_MEMORY)                            
                    terminalNode.consequent.update(consequents)
                    terminalNode.rules.add(rule)
                    terminalNode.antecedent = rule.formula.body
                    terminalNode.network    = self
                    terminalNode.headAtoms.update(rule.formula.head)
                    terminalNode.filter = aFilter
                    self.terminalNodes.add(terminalNode)                    
                else:              
                    moveToEnd = []
                    endIdx = len(attachedPatterns) - 1
                    finalPatternList = []
                    for idx,pattern in enumerate(attachedPatterns):
                        assert isinstance(pattern,HashablePatternList),repr(pattern)                    
                        currNode = self.nodes[pattern]
                        if (isinstance(currNode,BuiltInAlphaNode) or 
                            isinstance(currNode,BetaNode) and currNode.fedByBuiltin):
                            moveToEnd.append(pattern)
                        else:
                            finalPatternList.append(pattern)
                    terminalNode = self.attachBetaNodes(chain(finalPatternList,moveToEnd))
                    terminalNode.consequent.update(consequents)
                    terminalNode.rules.add(rule)
                    terminalNode.antecedent = rule.formula.body                    
                    terminalNode.network    = self
                    terminalNode.headAtoms.update(rule.formula.head)
                    terminalNode.filter = aFilter
                    self.terminalNodes.add(terminalNode)
                    self._resetinstanciationStats()                        
                #self.checkDuplicateRules()
                return terminalNode
            if HashablePatternList([currentPattern]) in self.nodes:
                #Current pattern matches an existing alpha node
                matchedPatterns.append(currentPattern)
            elif matchedPatterns in self.nodes:
                #preceding patterns match an existing join/beta node
                newNode = self.createAlphaNode(currentPattern)
                if len(matchedPatterns) == 1 and HashablePatternList([None])+matchedPatterns in self.nodes:
                    existingNode = self.nodes[HashablePatternList([None])+matchedPatterns]
                    newBetaNode = BetaNode(existingNode,newNode)     
                    self.nodes[HashablePatternList([None])+matchedPatterns+HashablePatternList([currentPattern])] = newBetaNode
                    matchedPatterns = HashablePatternList([None])+matchedPatterns+HashablePatternList([currentPattern])
                else:
                    existingNode = self.nodes[matchedPatterns]                
                    newBetaNode = BetaNode(existingNode,newNode)     
                    self.nodes[matchedPatterns+HashablePatternList([currentPattern])] = newBetaNode
                    matchedPatterns.append(currentPattern)
                
                self.nodes[HashablePatternList([currentPattern])] = newNode
                newBetaNode.connectIncomingNodes(existingNode,newNode)
                #Extend the match list with the current pattern and add it
                #to the list of attached patterns for the second pass                
                attachedPatterns.append(matchedPatterns)
                matchedPatterns = HashablePatternList()
            else:
                #The current pattern is not in the network and the match list isn't
                #either.  Add an alpha node 
                newNode = self.createAlphaNode(currentPattern)
                self.nodes[HashablePatternList([currentPattern])] = newNode
                #Add to list of attached patterns for the second pass
                attachedPatterns.append(HashablePatternList([currentPattern]))
                
    def attachBetaNodes(self,patternIterator,lastBetaNodePattern=None):
        """
        The second 'pass' in the Rete network compilation algorithm:
        Attaches Beta nodes to the alpha nodes associated with all the patterns
        in a rule's LHS recursively towards a 'root' Beta node - the terminal node
        for the rule.  This root / terminal node is returned
        """
        try:
            nextPattern = patternIterator.next()
        except StopIteration:
            assert lastBetaNodePattern
            if lastBetaNodePattern:
                return self.nodes[lastBetaNodePattern]
            else:
                assert len(self.universalTruths),"should be empty LHSs"
                terminalNode = BetaNode(None,None,aPassThru=True)
                self.nodes[HashablePatternList([None])] = terminalNode                
                return terminalNode#raise Exception("Ehh. Why are we here?")
        if lastBetaNodePattern:
            firstNode = self.nodes[lastBetaNodePattern]
            secondNode = self.nodes[nextPattern]
            newBNodePattern = lastBetaNodePattern + nextPattern
            newBetaNode = BetaNode(firstNode,secondNode)        
            self.nodes[newBNodePattern] = newBetaNode            
        else:            
            firstNode  = self.nodes[nextPattern]
            oldAnchor = self.nodes.get(HashablePatternList([None])+nextPattern)
            if not oldAnchor: 
                if isinstance(firstNode,AlphaNode):
                    newfirstNode = BetaNode(None,firstNode,aPassThru=True) 
                    newfirstNode.connectIncomingNodes(None,firstNode)
                    self.nodes[HashablePatternList([None])+nextPattern] = newfirstNode
                else:
                    newfirstNode = firstNode
            else:                
                newfirstNode = oldAnchor
            firstNode = newfirstNode
            secondPattern = patternIterator.next()
            secondNode = self.nodes[secondPattern]
            newBetaNode = BetaNode(firstNode,secondNode)                         
            newBNodePattern = HashablePatternList([None]) + nextPattern + secondPattern                    
            self.nodes[newBNodePattern] = newBetaNode

        newBetaNode.connectIncomingNodes(firstNode,secondNode)
        return self.attachBetaNodes(patternIterator,newBNodePattern)
Beispiel #45
0
    def addStuff(self):
        tarek = self.tarek
        michel = self.michel
        bob = self.bob
        likes = self.likes
        hates = self.hates
        pizza = self.pizza
        cheese = self.cheese
        c1 = self.c1
        graph = Graph(self.graph.store, c1)

        graph.add((tarek, likes, pizza))
        graph.add((tarek, likes, cheese))
        graph.add((michel, likes, pizza))
        graph.add((michel, likes, cheese))
        graph.add((bob, likes, cheese))
        graph.add((bob, hates, pizza))
        graph.add((bob, hates, michel))  # gasp!
Beispiel #46
0
            print 'processing %s ...' % license

            # add short code
            #code = code_from_uri(license)
            #print '  code is %s' % code
            #store.remove( (license, NS_DC.identifier, None) )
            #store.add( (license, NS_DC.identifier, Literal(code)) )

            # associate each License with a LicenseSelector
            #if 'sampling' in code:
            #    sel = 'http://creativecommons.org/license/sampling/'
            #elif code in ('GPL', 'LGPL'):
            #    sel = 'http://creativecommons.org/license/software'
            #elif code == 'publicdomain':
            #    sel = 'http://creativecommons.org/license/publicdomain/'
            #else:
            #    sel = 'http://creativecommons.org/license/'
            #print '  LicenseSelector is %s' % sel
            #store.remove( (license, NS_CC.licenseClass, None) )
            #store.add( (license, NS_CC.licenseClass, URIRef(sel)) )

            # add image uris
            store.remove((license, NS_FOAF.logo, None))
            for img in image_uris(license):
                print img
                store.add((license, NS_FOAF.logo, img))

        file(os.path.join(root, filename),
             'w').write(store.serialize(format="pretty-xml", max_depth=1))
Beispiel #47
0
def main():
    g = Graph()
    Individual.factoryGraph = g
    g.bind('ex', EX_NS, override=False)

    isChildOf   = Property(EX_NS.isChildOf)
    isMarriedTo = Property(EX_NS.isMarriedTo)


    woman = Class(EX_NS.Woman)
    man   = Class(EX_NS.Man,
                  subClassOf=[isMarriedTo|only|woman],
                  # complementOf=woman
    )
    woman.subClassOf = [isMarriedTo|only|man]

    # Class(OWL_NS.Thing,subClassOf=[isMarriedTo|min|Literal(1)])

    man.extent = [EX_NS.John,EX_NS.Tim]
    woman.extent = [EX_NS.Kate,EX_NS.Mary]

    #Semantically equivalent to Abox assertion below
    # anon_cls1 = Class(
    #     subClassOf=[isMarriedTo|some|EnumeratedClass(members=[EX_NS.Mary])]
    # )
    # anon_cls1.extent = [EX_NS.John]
    g.add((EX_NS.John,isMarriedTo.identifier,EX_NS.Mary))

    #Semantically equivalent to Abox assertion below
    # anon_cls2 = Class(
    #     subClassOf=[isChildOf|some|EnumeratedClass(members=[EX_NS.John])]
    # )
    # anon_cls2.extent = [EX_NS.Kate]
    g.add((EX_NS.Kate,isChildOf.identifier,EX_NS.John))

    #Semantically equivalent to Abox assertion below
    # anon_cls3 = Class(
    #     subClassOf=[isChildOf|some|EnumeratedClass(members=[EX_NS.Mary])]
    # )
    # anon_cls3.extent = [EX_NS.Tim]
    g.add((EX_NS.Tim,isChildOf.identifier,EX_NS.Mary))

    print g.serialize(format='pretty-xml')

    rule_store, rule_graph, network = SetupRuleStore(makeNetwork=True)
    network.nsMap = { u'ex' : EX_NS }

    # NormalFormReduction(g)
    dlp=network.setupDescriptionLogicProgramming(
                             g,
                             addPDSemantics=False,
                             constructNetwork=False
    )
    for rule in dlp:
        print rule

    topDownStore=TopDownSPARQLEntailingStore(
                    g.store,
                    g,
                    idb=dlp,
                    DEBUG=True,
                    derivedPredicates=[EX_NS.Man,EX_NS.Woman],
                    nsBindings=network.nsMap,
                    identifyHybridPredicates = True)
    targetGraph = Graph(topDownStore)
    rt=targetGraph.query("ASK { ex:Tim ex:isMarriedTo ex:John }",
                         initNs=network.nsMap)
    print rt.askAnswer[0]

    topDownStore.DEBUG = False

    for ind in g.query("SELECT ?ind { ?ind a ?class FILTER(isUri(?ind) && ?class != owl:Class ) }"):
        print "Individual: ", ind
        print "--- Children ---"
        for child in targetGraph.query("SELECT ?child { ?child ex:isChildOf %s }"%ind.n3(),
                                       initNs=network.nsMap):
            print "\t- ", child
        print "----------------"
Beispiel #48
0
def graphFromTriples(triples):
    g = Graph()
    for stmt in triples:
        g.add(stmt)
    return g
 def testI(self):
     g = Graph()
     g.add((self.uriref, RDF.value, self.literal))
     g.add((self.uriref, RDF.value, self.uriref))
     self.assertEqual(len(g), 2)
Beispiel #50
0
    stmt.say(RDF.object, obj)
    return stmt


print 'Dumping assertions.'
for stem1, predtype, stem2, text1, text2, frame_id, language_id, creator_id, score, sentence in Assertion.useful.filter(
        language='en').values_list('stem1__text', 'predtype__name',
                                   'stem2__text', 'text1', 'text2', 'frame_id',
                                   'language_id', 'creator_id', 'score',
                                   'sentence__text').iterator():
    stmt = add(concept[stem1], reltype[predtype], concept[stem2])
    stmt.say(b('LeftText'), Literal(text1))
    stmt.say(b('RightText'), Literal(text2))
    stmt.say(b('FrameId'), frame[str(frame_id)])
    stmt.say(b('Language'), language[str(language_id)])
    stmt.say(b('Creator'), user[str(creator_id)])
    stmt.say(b('Score'), Literal(score))
    stmt.say(b('Sentence'), Literal(sentence))

g.commit()
print 'Dumping frames.'
for id, predtype, text, goodness in Frame.objects.filter(
        language='en').values_list('id', 'predtype__name', 'text',
                                   'goodness').iterator():
    ff = frame[str(id)]
    g.add((ff, b('RelationType'), reltype[predtype]))
    g.add((ff, b('FrameText'), Literal(text)))
    g.add((ff, b('FrameGoodness'), Literal(str(goodness))))

g.commit()
Beispiel #51
0
                if not result:
                    results.add((test, RDF.type, RESULT["PassingRun"]))
                else:
                   results.add((test, RDF.type, RESULT["FailingRun"]))
                total += 1
                num_failed += result
        self.assertEquals(num_failed, 0, "Failed: %s of %s." % (num_failed, total))

RESULT = Namespace("http://www.w3.org/2002/03owlt/resultsOntology#")
FOAF = Namespace("http://xmlns.com/foaf/0.1/")


results = Graph()

system = BNode("system")
results.add((system, FOAF["homepage"], URIRef("http://rdflib.net/")))
results.add((system, RDFS.label, Literal("RDFLib")))
results.add((system, RDFS.comment, Literal("")))


if __name__ == "__main__":
    manifest = Graph()
    manifest.load("http://www.w3.org/2000/10/rdf-tests/rdfcore/Manifest.rdf")
    import sys, getopt
    try:
        optlist, args = getopt.getopt(sys.argv[1:], 'h:', ["help"])
    except getopt.GetoptError, msg:
        write(msg)
        usage()

    try:
Beispiel #52
0
def add(subj, type, obj):
    stmt = SuperNode()
    stmt.say(RDF.subject, subj)
    stmt.say(RDF.predicate, type)
    stmt.say(RDF.object, obj)
    return stmt

print 'Dumping assertions.'
for stem1, predtype, stem2, text1, text2, frame_id, language_id, creator_id, score, sentence in Assertion.useful.filter(language='en').values_list('stem1__text', 'predtype__name', 'stem2__text',
                                                                                                 'text1', 'text2', 'frame_id', 'language_id', 'creator_id', 'score', 'sentence__text').iterator():
    stmt = add(concept[stem1], reltype[predtype], concept[stem2])
    stmt.say(b('LeftText'), Literal(text1))
    stmt.say(b('RightText'), Literal(text2))
    stmt.say(b('FrameId'), frame[str(frame_id)])
    stmt.say(b('Language'), language[str(language_id)])
    stmt.say(b('Creator'), user[str(creator_id)])
    stmt.say(b('Score'), Literal(score))
    stmt.say(b('Sentence'), Literal(sentence))

g.commit()
print 'Dumping frames.'
for id, predtype, text, goodness in Frame.objects.filter(language='en').values_list('id', 'predtype__name', 'text', 'goodness').iterator():
    ff = frame[str(id)]
    g.add((ff, b('RelationType'), reltype[predtype]))
    g.add((ff, b('FrameText'), Literal(text)))
    g.add((ff, b('FrameGoodness'), Literal(str(goodness))))


g.commit()
Beispiel #53
0
    def testGraphValue(self):
        from rdflib.Graph import GraphValue

        graph = self.graph

        alice = URIRef("alice")
        bob = URIRef("bob")
        pizza = URIRef("pizza")
        cheese = URIRef("cheese")

        g1 = Graph()
        g1.add((alice, RDF.value, pizza))
        g1.add((bob, RDF.value, cheese))
        g1.add((bob, RDF.value, pizza))

        g2 = Graph()
        g2.add((bob, RDF.value, pizza))
        g2.add((bob, RDF.value, cheese))
        g2.add((alice, RDF.value, pizza))

        gv1 = GraphValue(store=graph.store, graph=g1)
        gv2 = GraphValue(store=graph.store, graph=g2)
        graph.add((gv1, RDF.value, gv2))
        v = graph.value(gv1)
        #print type(v)
        self.assertEquals(gv2, v)
        #print list(gv2)
        #print gv2.identifier
        graph.remove((gv1, RDF.value, gv2))
class TriXHandler(handler.ContentHandler):
    """An Sax Handler for TriX. See http://swdev.nokia.com/trix/TriX.html"""

    def __init__(self, store):
        self.store = store
        self.preserve_bnode_ids = False
        self.reset()

    def reset(self):
        self.bnode = {}
        self.graph=self.store
        self.triple=None
        self.state=0
        self.lang=None
        self.datatype=None

    # ContentHandler methods

    def setDocumentLocator(self, locator):
        self.locator = locator

    def startDocument(self):
        pass

    def startPrefixMapping(self, prefix, namespace):
        pass

    def endPrefixMapping(self, prefix):
        pass

    def startElementNS(self, name, qname, attrs):
    
        if name[0]!=TRIXNS:
            self.error("Only elements in the TriX namespace are allowed.")

        if name[1]=="TriX":
            if self.state==0:
                self.state=1
            else:
                self.error("Unexpected TriX element")

        elif name[1]=="graph":
            if self.state==1:
                self.state=2
            else:
                self.error("Unexpected graph element")

        elif name[1]=="uri":
            if self.state==2:
                # the context uri
                self.state=3
            elif self.state==4:
                # part of a triple
                pass
            else:
                self.error("Unexpected uri element")

        elif name[1]=="triple":
            if self.state==2:
                # start of a triple
                self.triple=[]
                self.state=4
            else:
                self.error("Unexpected triple element")

        elif name[1]=="typedLiteral":
            if self.state==4:
                # part of triple
                self.lang=None
                self.datatype=None

                try:
                    self.lang=attrs.getValueByQName("lang")
                except:
                    # language not required - ignore
                    pass
                try: 
                    self.datatype=attrs.getValueByQName("datatype")
                except KeyError:
                    self.error("No required attribute 'datatype'")
            else:
                self.error("Unexpected typedLiteral element")
                
        elif name[1]=="plainLiteral":
            if self.state==4:
                # part of triple
                self.lang=None
                self.datatype=None
                try:
                    self.lang=attrs.getValueByQName("lang")
                except:
                    # language not required - ignore
                    pass

            else:
                self.error("Unexpected plainLiteral element")

        elif name[1]=="id":
            if self.state==2:
                # the context uri
                self.state=3

            elif self.state==4:
                # part of triple
                pass
            else:
                self.error("Unexpected id element")
        
        else:
            self.error("Unknown element %s in TriX namespace"%name[1])

        self.chars=""

    
    def endElementNS(self, name, qname):
        if name[0]!=TRIXNS:
            self.error("Only elements in the TriX namespace are allowed.")

        if name[1]=="uri":
            if self.state==3:
                self.graph=Graph(store=self.store.store, identifier=URIRef(self.chars.strip()))
                self.state=2
            elif self.state==4:
                self.triple+=[URIRef(self.chars.strip())]
            else:
                self.error("Illegal internal self.state - This should never happen if the SAX parser ensures XML syntax correctness")

        if name[1]=="id":
            if self.state==3:
                self.graph=Graph(self.store.store,identifier=self.get_bnode(self.chars.strip()))
                self.state=2
            elif self.state==4:
                self.triple+=[self.get_bnode(self.chars.strip())]
            else:
                self.error("Illegal internal self.state - This should never happen if the SAX parser ensures XML syntax correctness")

        if name[1]=="plainLiteral" or name[1]=="typedLiteral":
            if self.state==4:
                self.triple+=[Literal(self.chars, lang=self.lang, datatype=self.datatype)]
            else:
                self.error("This should never happen if the SAX parser ensures XML syntax correctness")

        if name[1]=="triple":
            if self.state==4:
                if len(self.triple)!=3:
                    self.error("Triple has wrong length, got %d elements: %s"%(len(self.triple),self.triple))

                self.graph.add(self.triple)
                #self.store.store.add(self.triple,context=self.graph)
                #self.store.addN([self.triple+[self.graph]])
                self.state=2
            else:
                self.error("This should never happen if the SAX parser ensures XML syntax correctness")

        if name[1]=="graph":
            self.state=1

        if name[1]=="TriX":
            self.state=0


    def get_bnode(self,label):
        if self.preserve_bnode_ids:
            bn=BNode(label)
        else:
            if label in self.bnode:
                bn=self.bnode[label]
            else: 
                bn=BNode(label)
                self.bnode[label]=bn
        return bn
                

    def characters(self, content):
        self.chars+=content

    
    def ignorableWhitespace(self, content):
        pass

    def processingInstruction(self, target, data):
        pass

    
    def error(self, message):
        locator = self.locator
        info = "%s:%s:%s: " % (locator.getSystemId(),
                            locator.getLineNumber(), locator.getColumnNumber())
        raise ParserError(info + message)
Beispiel #55
0
def make_foaf_graph(starturi, steps=3):

    # Initialize the graph
    foafgraph = Graph()

    # Keep track of where we've already been
    visited = set()

    # Keep track of the current crawl queue
    current = set([starturi])

    # Crawl steps out
    for i in range(steps):
        nextstep = set()

        # Visit and parse every URI in the current set, adding it to the graph
        for uri in current:
            visited.add(uri)
            tempgraph = Graph()

            # Construct a request with an ACCEPT header
            # This tells pages you want RDF/XML
            try:
                reqObj = urllib2.Request(uri, None, {"ACCEPT": "application/rdf+xml"})
                urlObj = urllib2.urlopen(reqObj)
                tempgraph.parse(urlObj, format="xml")
                urlObj.close()
            except:
                print "Couldn't parse %s" % uri
                continue

            # Work around for FOAF's anonymous node problem
            # Map blank node IDs to their seeAlso URIs
            nm = dict([(str(s), n) for s, _, n in tempgraph.triples((None, RDFS["seeAlso"], None))])

            # Identify the root node (the one with an image for hi5, or the one called "me")
            imagelist = list(tempgraph.triples((None, FOAF["img"], None)))
            if len(imagelist) > 0:
                nm[imagelist[0][0]] = uri
            else:
                nm[""], nm["#me"] = uri, uri

            # Now rename the blank nodes as their seeAlso URIs
            for s, p, o in tempgraph:
                if str(s) in nm:
                    s = nm[str(s)]
                if str(o) in nm:
                    o = nm[str(o)]
                foafgraph.add((s, p, o))

            # Now look for the next step
            newfriends = tempgraph.query(
                "SELECT ?burl " + "WHERE {?a foaf:knows ?b . ?b rdfs:seeAlso ?burl . }",
                initNs={"foaf": FOAF, "rdfs": RDFS},
            )

            # Get all the people in the graph. If we haven't added them already, add them
            # to the crawl queue
            for friend in newfriends:
                if friend[0] not in current and friend[0] not in visited:
                    nextstep.add(friend[0])
                    visited.add(friend[0])

        # The new queue becomes the current queue
        current = nextstep
    return foafgraph
Beispiel #56
0
for s, p, o in list(m.triples((None, None, None))):
    s2 = None
    p2 = None
    o2 = None

    if (s is None or p is None or o is None):
        continue

    s2 = getpuri(s, nsfrom, nsto, context, skippuris)
    p2 = getpuri(p, nsfrom, nsto, context, skippuris)
    o2 = getpuri(o, nsfrom, nsto, context, skippuris)

    if (s2 is None):
        s2 = s
    if (p2 is None):
        p2 = p
    if (o2 is None):
        o2 = o

    if (s is not s2 or p is not p2 or o is not o2):

        m.remove((s, p, o))
        m.add((s2, p2, o2))

    statusc = statusc + 1
    if (statusc % 1000 == 0):
        sys.stderr.write(".")

sys.stderr.write("\n")
m.serialize(destination=sys.stdout, format=options.to_format)
 def query_wildcard(self, subject, predicate, obj):
     ret = Graph()
     for t in self.graphs.triples((subject, predicate, obj)):
         ret.add(t)
     return ret if len(ret)>0 else None
Beispiel #58
0
class RDFObjectGraph:
    """
    The RDFObjectGraph caches object values for populating RDFObject values.
    """
    def __init__(self, connection, ontology):
        self._connection = connection
        self._ontology = ontology
        self._rdfObjects = {}
        self._graph = Graph()
        self._added = Graph()
        self._removed = Graph()

    def get(self, uri):
        """
        Gets an RDFObject for the specified URI.
        """
        if uri not in self._rdfObjects:
            self._load(uri)
            self._rdfObjects[uri] = RDFObject(uri, self)
        return self._rdfObjects[uri]

    def _load(self, uri):
        """
        This method ensures that the data for a uri is loaded into
        the local graph.
        """
        if uri not in self._rdfObjects:
            self._connection.query(
            "construct { <" + uri + "> ?p ?o . " +
            "?rs ?rp <" + uri + "> .} where { " +
            "OPTIONAL { <" + uri + "> ?p ?o } " +
            "OPTIONAL { ?rs ?rp <" + uri + "> } }", self._graph)

    def _subjects(self, prop, uri):
        """
        Retrieves all subjects for a property and object URI.
        """
        for triple in self._graph.triples((None, prop, uri)):
            if triple not in self._removed:
                yield triple[0]
        for triple in self._added.triples((None, prop, uri)):
            yield triple[0]

    def _objects(self, uri, prop):
        """
        Retrieves all objects for a subject URI and property.
        """
        for triple in self._graph.triples((uri, prop, None)):
            if triple not in self._removed:
                yield triple[2]
        for triple in self._added.triples((uri, prop, None)):
            yield triple[2]

    def _predicates(self, subject=None, object=None):
        """
        Retrieves all unique predicates for a subject or object URI.
        """
        result = set()
        for triple in self._graph.triples((subject, None, object)):
            if triple not in self._removed:
                result.add(triple[1])
        for triple in self._added.triples((subject, None, object)):
            result.add(triple[1])
        return result

    def _setSubjects(self, values, prop, uri):
        """
        Sets all subjects for a property and uri.
        """
        newValues = set(values)
        existingValues = set(self._graph.subjects(prop, uri))
        for value in existingValues - newValues:
            removed = (value, prop, uri)
            self._added.remove(removed)
            self._removed.add(removed)
        for value in newValues - existingValues:
            added = (value, prop, uri)
            self._removed.remove(added)
            self._added.add(added)

    def _setObjects(self, uri, prop, values):
        """
        Sets all objects for a uri and property.
        """
        newValues = set(values)
        existingValues = set(self._graph.objects(uri, prop))
        for value in existingValues - newValues:
            removed = (uri, prop, value)
            self._added.remove(removed)
            self._removed.add(removed)
        for value in newValues - existingValues:
            added = (uri, prop, value)
            self._removed.remove(added)
            self._added.add(added)

    def commit(self):
        """
        Commits changes to the remote graph and flushes local caches.
        """
        self._connection.update(add=self._added, remove=self._removed)
        self._rdfObjects = {}
        self._graph = Graph()
        self._added = Graph()
        self._removed = Graph()