Beispiel #1
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 #2
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()
Beispiel #3
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 #4
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 #5
0
class NegationOfAtomicConcept(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 testAtomicNegation(self):
        bar = EX.Bar
        baz = ~bar
        baz.identifier = EX_NS.Baz
        ruleStore, ruleGraph, network = SetupRuleStore(makeNetwork=True)
        individual = BNode()
        individual2 = BNode()
        (EX.OtherClass).extent = [individual]
        bar.extent = [individual2]
        NormalFormReduction(self.ontGraph)
        self.assertEqual(repr(baz), "Class: ex:Baz DisjointWith ex:Bar\n")
        posRules, negRules = CalculateStratifiedModel(network, self.ontGraph, [EX_NS.Foo])
        self.failUnless(not posRules, "There should be no rules in the 0 strata!")
        self.failUnless(len(negRules) == 1, "There should only be one negative rule in a higher strata")
        self.assertEqual(repr(negRules[0]), "Forall ?X ( ex:Baz(?X) :- not ex:Bar(?X) )")
        baz.graph = network.inferredFacts
        self.failUnless(individual in baz.extent, "%s should be a member of ex:Baz" % individual)
        self.failUnless(individual2 not in baz.extent, "%s should *not* be a member of ex:Baz" % individual2)
Beispiel #6
0
class NegationOfAtomicConcept(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 testAtomicNegation(self):
        bar=EX.Bar
        baz=~bar
        baz.identifier = EX_NS.Baz
        ruleStore,ruleGraph,network=SetupRuleStore(makeNetwork=True)
        individual=BNode()
        individual2=BNode()
        (EX.OtherClass).extent = [individual]
        bar.extent = [individual2]
        NormalFormReduction(self.ontGraph)
        self.assertEqual(repr(baz),
                         "Class: ex:Baz DisjointWith ex:Bar\n")
        posRules,negRules=CalculateStratifiedModel(network,self.ontGraph,[EX_NS.Foo])
        self.failUnless(not posRules,"There should be no rules in the 0 strata!")
        self.failUnless(len(negRules)==1,"There should only be one negative rule in a higher strata")
        self.assertEqual(repr(negRules[0]),
                         "Forall ?X ( ex:Baz(?X) :- not ex:Bar(?X) )")        
        baz.graph = network.inferredFacts
        self.failUnless(individual in baz.extent,
                        "%s should be a member of ex:Baz"%individual)
        self.failUnless(individual2 not in baz.extent,
                        "%s should *not* be a member of ex:Baz"%individual2)
Beispiel #7
0
def reason_func(resource_name):
    famNs = Namespace('file:///code/ganglia/metric.n3#')
    nsMapping = {'mtc': famNs}
    rules = HornFromN3('ganglia/metric/metric_rule.n3')
    factGraph = Graph().parse('ganglia/metric/metric.n3', format='n3')
    factGraph.bind('mtc', famNs)
    dPreds = [famNs.relateTo]

    topDownStore = TopDownSPARQLEntailingStore(factGraph.store,
                                               factGraph,
                                               idb=rules,
                                               derivedPredicates=dPreds,
                                               nsBindings=nsMapping)
    targetGraph = Graph(topDownStore)
    targetGraph.bind('ex', famNs)
    #get list of the related resource
    r_list = list(
        targetGraph.query('SELECT ?RELATETO { mtc:%s mtc:relateTo ?RELATETO}' %
                          resource_name,
                          initNs=nsMapping))

    res_list = []
    for res in r_list:
        res_list.append(str(res).split("#")[1])
    return res_list
Beispiel #8
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 #9
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 #10
0
class NegatedDisjunctTest(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 testStratified(self):
        bar=EX.Bar
        baz=EX.Baz
        noBarOrBaz = ~(bar|baz)
        omega = EX.Omega
        foo = omega & noBarOrBaz
        foo.identifier = EX_NS.Foo
        ruleStore,ruleGraph,network=SetupRuleStore(makeNetwork=True)
        individual=BNode()
        omega.extent = [individual]
        NormalFormReduction(self.ontGraph)
        self.assertEqual(repr(foo),
                         "ex:Omega that ( not ex:Bar ) and ( not ex:Baz )")
        posRules,negRules=CalculateStratifiedModel(network,self.ontGraph,[EX_NS.Foo])
        foo.graph = network.inferredFacts
        self.failUnless(not posRules,"There should be no rules in the 0 strata!")
        self.assertEqual(repr(negRules[0]),"Forall ?X ( ex:Foo(?X) :- And( ex:Omega(?X) not ex:Bar(?X) not ex:Baz(?X) ) )")
        self.failUnless(len(negRules)==1,"There should only be one negative rule in a higher strata")
        self.failUnless(individual in foo.extent,
                        "%s should be a member of ex:Foo"%individual)
Beispiel #11
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 #12
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 #13
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 #14
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 #15
0
class NegatedDisjunctTest(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 testStratified(self):
        bar = EX.Bar
        baz = EX.Baz
        noBarOrBaz = ~(bar | baz)
        omega = EX.Omega
        foo = omega & noBarOrBaz
        foo.identifier = EX_NS.Foo
        ruleStore, ruleGraph, network = SetupRuleStore(makeNetwork=True)
        individual = BNode()
        omega.extent = [individual]
        NormalFormReduction(self.ontGraph)
        self.assertEqual(repr(foo), "ex:Omega that ( not ex:Bar ) and ( not ex:Baz )")
        posRules, negRules = CalculateStratifiedModel(network, self.ontGraph, [EX_NS.Foo])
        foo.graph = network.inferredFacts
        self.failUnless(not posRules, "There should be no rules in the 0 strata!")
        self.assertEqual(
            repr(negRules[0]), "Forall ?X ( ex:Foo(?X) :- And( ex:Omega(?X) not ex:Bar(?X) not ex:Baz(?X) ) )"
        )
        self.failUnless(len(negRules) == 1, "There should only be one negative rule in a higher strata")
        self.failUnless(individual in foo.extent, "%s should be a member of ex:Foo" % individual)
Beispiel #16
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 #17
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 #18
0
class NegatedExistentialRestrictionTest(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 testInConjunct(self):
        contains = Property(EX_NS.contains)
        testCase2 = (
            EX.Operation
            & ~(contains | some | EX.IsolatedCABGConcomitantExclusion)
            & (contains | some | EX.CoronaryArteryBypassGrafting)
        )
        testCase2.identifier = EX_NS.IsolatedCABGOperation
        NormalFormReduction(self.ontGraph)
        self.assertEqual(
            repr(testCase2),
            "ex:Operation that ( ex:contains some ex:CoronaryArteryBypassGrafting ) and ( not ( ex:contains some ex:IsolatedCABGConcomitantExclusion ) )",
        )
        ruleStore, ruleGraph, network = SetupRuleStore(makeNetwork=True)
        op = BNode()
        (EX.Operation).extent = [op]
        grafting = BNode()
        (EX.CoronaryArteryBypassGrafting).extent = [grafting]
        testCase2.graph.add((op, EX_NS.contains, grafting))
        CalculateStratifiedModel(network, testCase2.graph, [EX_NS.Foo, EX_NS.IsolatedCABGOperation])
        testCase2.graph = network.inferredFacts
        self.failUnless(op in testCase2.extent, "%s should be in ex:IsolatedCABGOperation's extent" % op)

    def testGeneralConceptInclusion(self):
        #        Some Class
        #            ## Primitive Type  ##
        #            SubClassOf: Class: ex:NoExclusion  .
        #            DisjointWith ( ex:contains some ex:IsolatedCABGConcomitantExclusion )
        contains = Property(EX_NS.contains)
        testClass = ~(contains | some | EX.Exclusion)
        testClass2 = EX.NoExclusion
        testClass2 += testClass
        NormalFormReduction(self.ontGraph)
        individual1 = BNode()
        individual2 = BNode()
        contains.extent = [(individual1, individual2)]
        ruleStore, ruleGraph, network = SetupRuleStore(makeNetwork=True)
        posRules, negRules = CalculateStratifiedModel(network, self.ontGraph, [EX_NS.NoExclusion])
        self.failUnless(not posRules, "There should be no rules in the 0 strata!")
        self.assertEqual(len(negRules), 2, "There should be 2 'negative' rules")
        Individual.factoryGraph = network.inferredFacts
        targetClass = Class(EX_NS.NoExclusion, skipOWLClassMembership=False)
        self.failUnless(
            individual1 in targetClass.extent,
            "There is a BNode that bears the contains relation with another individual that is not a member of Exclusion!",
        )
        self.assertEquals(len(list(targetClass.extent)), 1, "There should only be one member in NoExclusion")
Beispiel #19
0
def graph():
    """Return an empty graph with common namespaces defined."""

    store = Graph()
    store.bind("cc", "http://creativecommons.org/ns#")
    store.bind("dc", "http://purl.org/dc/elements/1.1/")
    store.bind("dcq", "http://purl.org/dc/terms/")
    store.bind("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
    store.bind("foaf", "http://xmlns.com/foaf/0.1/")

    return store
Beispiel #20
0
def graph():
    """Return an empty graph with common namespaces defined."""

    store = Graph()
    store.bind("cc", "http://creativecommons.org/ns#")
    store.bind("dc", "http://purl.org/dc/elements/1.1/")
    store.bind("dcq", "http://purl.org/dc/terms/")
    store.bind("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
    store.bind("foaf", "http://xmlns.com/foaf/0.1/")

    return store
Beispiel #21
0
def main(rss_url, blog_uri):
    store, schema_store = Graph(), Graph()
    store.parse(rss_url)
    store.bind('rss', 'http://purl.org/rss/1.0/')
    schema_store.parse('file:rss_schema.xml')
    Thing = ThingFactory(store, schema_store)
    
    blog = Thing(URI(blog_uri))
    for item in blog.rss_items:
        print "*", item.rss_title
        print indent(item.rss_description)
Beispiel #22
0
 def renderHTTP(self, ctx):
     request = inevow.IRequest(ctx)
     if ctx.arg('type') == 'n3':
         request.setHeader('Content-Type', 'text/plain')
         graph = Graph()
         graph.parse(StringInputSource(self.xml), format='xml')
         graph.bind('station', STATION)
         graph.bind('train', TRAIN)
         return graph.serialize(format='n3')
     else:
         request.setHeader("Content-Type", "application/rdf+xml")
         return self.xml
Beispiel #23
0
 def renderHTTP(self, ctx):
     request = inevow.IRequest(ctx)
     if ctx.arg('type') == 'n3':
         request.setHeader('Content-Type', 'text/plain')
         graph = Graph()
         graph.parse(StringInputSource(self.xml), format='xml')
         graph.bind('station', STATION)
         graph.bind('train', TRAIN)
         return graph.serialize(format='n3')
     else:
         request.setHeader("Content-Type", "application/rdf+xml")
         return self.xml
Beispiel #24
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 #25
0
def makeOutputGraph():
    graph = Graph()
    graph.bind('pre', 'http://bigasterisk.com/pre/general/')
    graph.bind('local', 'http://bigasterisk.com/pre/drew/') # todo
    graph.bind('ad', 'http://bigasterisk.com/pre/general/accountDataType/')
    graph.bind('mt', 'http://bigasterisk.com/pre/general/messageType/')
    return graph
Beispiel #26
0
    def MagicOWLProof(self,goals,rules,factGraph,conclusionFile):
        progLen = len(rules)
        magicRuleNo = 0
        dPreds = []
        for rule in AdditionalRules(factGraph):
            rules.append(rule)            
        if not GROUND_QUERY:
            goalDict = dict([((Variable('SUBJECT'),goalP,goalO),goalS) 
                        for goalS,goalP,goalO in goals])
            goals = goalDict.keys()
        assert goals

        topDownStore=TopDownSPARQLEntailingStore(
                        factGraph.store,
                        factGraph,
                        idb=rules,
                        DEBUG=DEBUG,
                        identifyHybridPredicates=True,
                        nsBindings=nsMap)
        targetGraph = Graph(topDownStore)
        for pref,nsUri in nsMap.items():
            targetGraph.bind(pref,nsUri)
        start = time.time()

        for goal in goals:
            queryLiteral = EDBQuery([BuildUnitermFromTuple(goal)],
                                    factGraph,
                                    None if GROUND_QUERY else [goal[0]])
            query = queryLiteral.asSPARQL()
            print "Goal to solve ", query
            rt=targetGraph.query(query,initNs=nsMap)
            if GROUND_QUERY:
                self.failUnless(rt.askAnswer[0],"Failed top-down problem")
            else:
                if (goalDict[goal]) not in rt or DEBUG:
                    for network,_goal in topDownStore.queryNetworks:
                        print network,_goal
                        network.reportConflictSet(True)
                    for query in topDownStore.edbQueries:
                        print query.asSPARQL()
                    print "Missing", goalDict[goal]
                self.failUnless((goalDict[goal]) in rt,
                                "Failed top-down problem")
        sTime = time.time() - start
        if sTime > 1:
            sTimeStr = "%s seconds"%sTime
        else:
            sTime = sTime * 1000
            sTimeStr = "%s milli seconds"%sTime
        return sTimeStr
Beispiel #27
0
def makeOutputGraph():
    graph = Graph()
    graph.bind('pre', 'http://bigasterisk.com/pre/general/')
    graph.bind('local', 'http://bigasterisk.com/pre/drew/')  # todo
    graph.bind('ad', 'http://bigasterisk.com/pre/general/accountDataType/')
    graph.bind('mt', 'http://bigasterisk.com/pre/general/messageType/')
    return graph
Beispiel #28
0
class NegatedExistentialRestrictionTest(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 testInConjunct(self):
        contains=Property(EX_NS.contains)
        testCase2 = EX.Operation & ~ (contains|some|EX.IsolatedCABGConcomitantExclusion) &\
                                          (contains|some|EX.CoronaryArteryBypassGrafting)
        testCase2.identifier = EX_NS.IsolatedCABGOperation        
        NormalFormReduction(self.ontGraph)
        self.assertEqual(repr(testCase2),
                        "ex:Operation that ( ex:contains some ex:CoronaryArteryBypassGrafting ) and ( not ( ex:contains some ex:IsolatedCABGConcomitantExclusion ) )")
        ruleStore,ruleGraph,network=SetupRuleStore(makeNetwork=True)
        op=BNode()
        (EX.Operation).extent = [op]
        grafting=BNode()
        (EX.CoronaryArteryBypassGrafting).extent = [grafting]
        testCase2.graph.add((op,EX_NS.contains,grafting))        
        CalculateStratifiedModel(network,testCase2.graph,[EX_NS.Foo,EX_NS.IsolatedCABGOperation])
        testCase2.graph = network.inferredFacts 
        self.failUnless(op in testCase2.extent,
                        "%s should be in ex:IsolatedCABGOperation's extent"%op)        
        

    def testGeneralConceptInclusion(self):
#        Some Class 
#            ## Primitive Type  ##
#            SubClassOf: Class: ex:NoExclusion  . 
#            DisjointWith ( ex:contains some ex:IsolatedCABGConcomitantExclusion )
        contains=Property(EX_NS.contains)
        testClass = ~(contains|some|EX.Exclusion)
        testClass2 = EX.NoExclusion
        testClass2 += testClass
        NormalFormReduction(self.ontGraph)
        individual1 = BNode()
        individual2 = BNode()
        contains.extent = [(individual1,individual2)]
        ruleStore,ruleGraph,network=SetupRuleStore(makeNetwork=True)
        posRules,negRules=CalculateStratifiedModel(network,self.ontGraph,[EX_NS.NoExclusion])
        self.failUnless(not posRules,"There should be no rules in the 0 strata!")
        self.assertEqual(len(negRules),2,"There should be 2 'negative' rules")
        Individual.factoryGraph = network.inferredFacts
        targetClass = Class(EX_NS.NoExclusion,skipOWLClassMembership=False)
        self.failUnless(individual1 in targetClass.extent,
        "There is a BNode that bears the contains relation with another individual that is not a member of Exclusion!")
        self.assertEquals(len(list(targetClass.extent)),1,
                          "There should only be one member in NoExclusion")
Beispiel #29
0
    def MagicOWLProof(self, goals, rules, factGraph, conclusionFile):
        progLen = len(rules)
        magicRuleNo = 0
        dPreds = []
        for rule in AdditionalRules(factGraph):
            rules.append(rule)
        if not GROUND_QUERY:
            goalDict = dict([((Variable('SUBJECT'), goalP, goalO), goalS)
                             for goalS, goalP, goalO in goals])
            goals = goalDict.keys()
        assert goals

        topDownStore = TopDownSPARQLEntailingStore(
            factGraph.store,
            factGraph,
            idb=rules,
            DEBUG=DEBUG,
            identifyHybridPredicates=True,
            nsBindings=nsMap)
        targetGraph = Graph(topDownStore)
        for pref, nsUri in nsMap.items():
            targetGraph.bind(pref, nsUri)
        start = time.time()

        for goal in goals:
            queryLiteral = EDBQuery([BuildUnitermFromTuple(goal)], factGraph,
                                    None if GROUND_QUERY else [goal[0]])
            query = queryLiteral.asSPARQL()
            print "Goal to solve ", query
            rt = targetGraph.query(query, initNs=nsMap)
            if GROUND_QUERY:
                self.failUnless(rt.askAnswer[0], "Failed top-down problem")
            else:
                if (goalDict[goal]) not in rt or DEBUG:
                    for network, _goal in topDownStore.queryNetworks:
                        print network, _goal
                        network.reportConflictSet(True)
                    for query in topDownStore.edbQueries:
                        print query.asSPARQL()
                    print "Missing", goalDict[goal]
                self.failUnless((goalDict[goal]) in rt,
                                "Failed top-down problem")
        sTime = time.time() - start
        if sTime > 1:
            sTimeStr = "%s seconds" % sTime
        else:
            sTime = sTime * 1000
            sTimeStr = "%s milli seconds" % sTime
        return sTimeStr
class FlatteningTest(unittest.TestCase):
    def setUp(self):
        self.ontGraph = Graph()
        self.ontGraph.bind('ex', EX_NS)
        self.ontGraph.bind('owl', OWL_NS)
        Individual.factoryGraph = self.ontGraph
        nestedConjunct = EX.omega & EX.gamma
        self.topLevelConjunct = EX.alpha & nestedConjunct
        
    def testFlatenning(self):
        self.assertEquals(repr(self.topLevelConjunct),
                          "ex:alpha that ( ex:omega and ex:gamma )")
        ConjunctionFlattener().transform(self.ontGraph)
        self.assertEquals(repr(self.topLevelConjunct),
                          "( ex:alpha and ex:omega and ex:gamma )")
Beispiel #31
0
class FlatteningTest(unittest.TestCase):
    def setUp(self):
        self.ontGraph = Graph()
        self.ontGraph.bind('ex', EX_NS)
        self.ontGraph.bind('owl', OWL_NS)
        Individual.factoryGraph = self.ontGraph
        nestedConjunct = EX.omega & EX.gamma
        self.topLevelConjunct = EX.alpha & nestedConjunct

    def testFlatenning(self):
        self.assertEquals(repr(self.topLevelConjunct),
                          "ex:alpha that ( ex:omega and ex:gamma )")
        ConjunctionFlattener().transform(self.ontGraph)
        self.assertEquals(repr(self.topLevelConjunct),
                          "( ex:alpha and ex:omega and ex:gamma )")
class UniversalComplementXFormTest(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 testUniversalInversion(self):
        testClass1 = EX.omega & (Property(EX_NS.someProp)|only|~EX.gamma)
        testClass1.identifier = EX_NS.Foo
        self.assertEquals(repr(testClass1),
                          "ex:omega that ( ex:someProp only ( not ex:gamma ) )")
        NormalFormReduction(self.ontGraph)
        self.assertEquals(repr(testClass1),
                          "ex:omega that ( not ( ex:someProp some ex:gamma ) )")
Beispiel #33
0
class ReductionTestA(unittest.TestCase):
    def setUp(self):
        self.ontGraph = Graph()
        self.ontGraph.bind('ex', EX_NS)
        self.ontGraph.bind('owl', OWL_NS)
        Individual.factoryGraph = self.ontGraph
        partition = EnumeratedClass(
            EX_NS.part,
            members=[EX_NS.individual1, EX_NS.individual2, EX_NS.individual3])
        subPartition = EnumeratedClass(EX_NS.partition,
                                       members=[EX_NS.individual1])
        partitionProp = Property(EX_NS.propFoo, range=partition)
        self.foo = EX.foo
        self.foo.subClassOf = [partitionProp | only | subPartition]

    def testUnivInversion(self):
        UniversalNominalRangeTransformer().transform(self.ontGraph)
        self.failUnlessEqual(len(list(self.foo.subClassOf)), 1,
                             "There should still be one subsumed restriction")
        subC = CastClass(first(self.foo.subClassOf))
        self.failUnless(not isinstance(subC, Restriction),
                        "subclass of a restriction")
        self.failUnless(subC.complementOf is not None,
                        "Should be a complement!")
        innerC = CastClass(subC.complementOf)
        self.failUnless(isinstance(innerC, Restriction),
                        "complement of a restriction, not %r" % innerC)
        self.failUnlessEqual(innerC.onProperty, EX_NS.propFoo,
                             "restriction on propFoo")
        self.failUnless(
            innerC.someValuesFrom,
            "converted to an existential restriction not %r" % innerC)
        invertedC = CastClass(innerC.someValuesFrom)
        self.failUnless(isinstance(invertedC, EnumeratedClass),
                        "existencial restriction on enumerated class")
        self.assertEqual(
            len(invertedC), 2,
            "existencial restriction on enumerated class of length 2")
        self.assertEqual(repr(invertedC), "{ ex:individual2 ex:individual3 }",
                         "The negated partition should exclude individual1")
        NominalRangeTransformer().transform(self.ontGraph)
        DemorganTransformer().transform(self.ontGraph)

        subC = CastClass(first(self.foo.subClassOf))
        self.assertEqual(
            repr(subC),
            "( ( not ( ex:propFoo value ex:individual2 ) ) and ( not ( ex:propFoo value ex:individual3 ) ) )"
        )
class ReductionTestA(unittest.TestCase):
    def setUp(self):
        self.ontGraph = Graph()
        self.ontGraph.bind('ex', EX_NS)
        self.ontGraph.bind('owl', OWL_NS)
        Individual.factoryGraph = self.ontGraph
        partition = EnumeratedClass(EX_NS.part,
                                    members=[EX_NS.individual1,
                                             EX_NS.individual2,
                                             EX_NS.individual3])
        subPartition = EnumeratedClass(EX_NS.partition,members=[EX_NS.individual1])
        partitionProp = Property(EX_NS.propFoo,
                                 range=partition)
        self.foo = EX.foo
        self.foo.subClassOf = [partitionProp|only|subPartition] 
        
    def testUnivInversion(self):
        UniversalNominalRangeTransformer().transform(self.ontGraph)
        self.failUnlessEqual(len(list(self.foo.subClassOf)),
                             1,
                             "There should still be one subsumed restriction")
        subC = CastClass(first(self.foo.subClassOf))
        self.failUnless(not isinstance(subC,Restriction),
                        "subclass of a restriction")
        self.failUnless(subC.complementOf is not None,"Should be a complement!")
        innerC = CastClass(subC.complementOf)
        self.failUnless(isinstance(innerC,Restriction),
                        "complement of a restriction, not %r"%innerC)
        self.failUnlessEqual(innerC.onProperty,
                             EX_NS.propFoo,
                             "restriction on propFoo")
        self.failUnless(innerC.someValuesFrom,"converted to an existential restriction not %r"%innerC)
        invertedC = CastClass(innerC.someValuesFrom)
        self.failUnless(isinstance(invertedC,EnumeratedClass),
                        "existencial restriction on enumerated class")
        self.assertEqual(len(invertedC),
                         2,
                        "existencial restriction on enumerated class of length 2")
        self.assertEqual(repr(invertedC),
                         "{ ex:individual2 ex:individual3 }",
                         "The negated partition should exclude individual1")
        NominalRangeTransformer().transform(self.ontGraph)
        DemorganTransformer().transform(self.ontGraph)
        
        subC = CastClass(first(self.foo.subClassOf))
        self.assertEqual(repr(subC),
                        "( ( not ( ex:propFoo value ex:individual2 ) ) and ( not ( ex:propFoo value ex:individual3 ) ) )")
Beispiel #35
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 #36
0
class UniversalComplementXFormTest(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 testUniversalInversion(self):
        testClass1 = EX.omega & (Property(EX_NS.someProp) | only | ~EX.gamma)
        testClass1.identifier = EX_NS.Foo
        self.assertEquals(
            repr(testClass1),
            "ex:omega that ( ex:someProp only ( not ex:gamma ) )")
        NormalFormReduction(self.ontGraph)
        self.assertEquals(
            repr(testClass1),
            "ex:omega that ( not ( ex:someProp some ex:gamma ) )")
Beispiel #37
0
def infer_ontlg(resource_name):
    resNs = Namespace('file:///code/metric.n3#')
    nsMapping = {'mtc' : resNs}
    rules = HornFromN3('metric_rule.n3')
    factGraph = Graph().parse('metric.n3',format='n3')
    factGraph.bind('mtc',resNs)
    dPreds = [resNs.relateTo]

    topDownStore=TopDownSPARQLEntailingStore(factGraph.store,factGraph,idb=rules,derivedPredicates = dPreds,nsBindings=nsMapping)
    targetGraph = Graph(topDownStore)
    targetGraph.bind('ex',resNs)
    #get list of the related resource 
    r_list = list(targetGraph.query('SELECT ?RELATETO { mtc:%s mtc:relateTo ?RELATETO}' % resource_name,initNs=nsMapping))
    
    res_list = []
    for res in r_list:
        res_list.append(str(res).split("#")[1])
    return res_list
Beispiel #38
0
def build_graph():
    """
    build factgraph from ontology, and build inferedGraph from rules and factgraph
    """
    famNs = Namespace('http://cetc/onto.n3#')
    nsMapping = {'': famNs}
    rule_store, rule_graph, network = SetupRuleStore(makeNetwork=True)
    closureDeltaGraph=Graph()
    closureDeltaGraph.bind('', famNs)
    network.inferredFacts = closureDeltaGraph

    for rule in HornFromN3('openstack/ontology/rule.n3'):
        network.buildNetworkFromClause(rule)

    factGraph = Graph().parse('openstack/ontology/resource.n3', format='n3')
    factGraph.bind('', famNs)
    network.feedFactsToAdd(generateTokenSet(factGraph))

    return factGraph, closureDeltaGraph, nsMapping
class ReductionTestB(unittest.TestCase):
    def setUp(self):
        self.ontGraph = Graph()
        self.ontGraph.bind('ex', EX_NS)
        self.ontGraph.bind('owl', OWL_NS)
        Individual.factoryGraph = self.ontGraph
        disjunct = (~ EX.alpha) | (~ EX.omega)
        self.foo = EX.foo
        disjunct+=self.foo 
        
    def testHiddenDemorgan(self):
        NormalFormReduction(self.ontGraph)
        self.failUnless(first(self.foo.subClassOf).complementOf,
                        "should be the negation of a boolean class")
        innerC = CastClass(first(self.foo.subClassOf).complementOf)
        self.failUnless(isinstance(innerC,BooleanClass) and \
                        innerC._operator == OWL_NS.intersectionOf,
                        "should be the negation of a conjunct")        
        self.assertEqual(repr(innerC),"( ex:alpha and ex:omega )")
Beispiel #40
0
def load_graph(filename):
    """Load the specified filename; return a graph."""

    store = Graph()
    store.bind("cc", "http://creativecommons.org/ns#")
    store.bind("dc", "http://purl.org/dc/elements/1.1/")
    store.bind("dcq", "http://purl.org/dc/terms/")
    store.bind("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#")

    store.load(filename)

    return store
Beispiel #41
0
def load_graph(filename):
    """Load the specified filename; return a graph."""

    store = Graph()
    store.bind("cc", "http://creativecommons.org/ns#")
    store.bind("dc", "http://purl.org/dc/elements/1.1/")
    store.bind("dcq","http://purl.org/dc/terms/")
    store.bind("rdf","http://www.w3.org/1999/02/22-rdf-syntax-ns#")

    store.load(filename)

    return store
Beispiel #42
0
class ReductionTestB(unittest.TestCase):
    def setUp(self):
        self.ontGraph = Graph()
        self.ontGraph.bind('ex', EX_NS)
        self.ontGraph.bind('owl', OWL_NS)
        Individual.factoryGraph = self.ontGraph
        disjunct = (~EX.alpha) | (~EX.omega)
        self.foo = EX.foo
        disjunct += self.foo

    def testHiddenDemorgan(self):
        NormalFormReduction(self.ontGraph)
        self.failUnless(
            first(self.foo.subClassOf).complementOf,
            "should be the negation of a boolean class")
        innerC = CastClass(first(self.foo.subClassOf).complementOf)
        self.failUnless(isinstance(innerC,BooleanClass) and \
                        innerC._operator == OWL_NS.intersectionOf,
                        "should be the negation of a conjunct")
        self.assertEqual(repr(innerC), "( ex:alpha and ex:omega )")
Beispiel #43
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 testQueryMemoization(self):
     topDownStore = TopDownSPARQLEntailingStore(
         self.owlGraph.store,
         self.owlGraph,
         idb=self.program,
         DEBUG=False,
         nsBindings=nsMap,
         decisionProcedure=BFP_METHOD,
         identifyHybridPredicates=True,
     )
     targetGraph = Graph(topDownStore)
     for pref, nsUri in nsMap.items():
         targetGraph.bind(pref, nsUri)
     goal = (Variable("SUBJECT"), RDF.type, EX.C)
     queryLiteral = EDBQuery([BuildUnitermFromTuple(goal)], self.owlGraph, [Variable("SUBJECT")])
     query = queryLiteral.asSPARQL()
     rt = targetGraph.query(query, initNs=nsMap)
     #        if len(topDownStore.edbQueries) == len(set(topDownStore.edbQueries)):
     #            pprint(topDownStore.edbQueries)
     print "Queries dispatched against EDB"
     for query in self.owlGraph.queriesDispatched:
         print query
     self.failUnlessEqual(len(self.owlGraph.queriesDispatched), 4, "Duplicate query")
    def testQueryMemoization(self):
        topDownStore=TopDownSPARQLEntailingStore(
                        self.owlGraph.store,
                        self.owlGraph,
                        idb=self.program,
                        DEBUG=False,
                        nsBindings=nsMap,
                        decisionProcedure = BFP_METHOD,
                        identifyHybridPredicates = True)
        targetGraph = Graph(topDownStore)
        for pref,nsUri in nsMap.items():
            targetGraph.bind(pref,nsUri)
        goal = (Variable('SUBJECT'),RDF.type,EX.C)
        queryLiteral = EDBQuery([BuildUnitermFromTuple(goal)],
                                self.owlGraph,
                                [Variable('SUBJECT')])
        query = queryLiteral.asSPARQL()
        rt=targetGraph.query(query,initNs=nsMap)
#        if len(topDownStore.edbQueries) == len(set(topDownStore.edbQueries)):
#            pprint(topDownStore.edbQueries)
        print "Queries dispatched against EDB"
        for query in self.owlGraph.queriesDispatched:
            print query
        self.failUnlessEqual(len(self.owlGraph.queriesDispatched),4,"Duplicate query")
Beispiel #46
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 #47
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
    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 #49
0
def datasetInfo():
    from optparse import OptionParser
    usage = '''usage: %prog [options] <DB Type>'''
    op = OptionParser(usage=usage)
    op.add_option('-c', '--connection', help='Database connection string')
    op.add_option('-i', '--id', help='Database table set identifier')
    (options, args) = op.parse_args()

    store = plugin.get(args[0], Store)(options.id)
    store.open(options.connection)
    dataset = ConjunctiveGraph(store)
    sdGraph = Graph()

    SD_NS = Namespace('http://www.w3.org/ns/sparql-service-description#')
    SCOVO = Namespace('http://purl.org/NET/scovo#')
    VOID  = Namespace('http://rdfs.org/ns/void#')
    
    sdGraph.bind(u'sd',SD_NS)
    sdGraph.bind(u'scovo',SCOVO)
    sdGraph.bind(u'void',VOID)

    service = BNode()
    datasetNode = BNode()
    sdGraph.add((service,RDF.type,SD_NS.Service))
    sdGraph.add((service,SD_NS.defaultDatasetDescription,datasetNode))
    sdGraph.add((datasetNode,RDF.type,SD_NS.Dataset))
    for graph in dataset.contexts():
        graphNode  = BNode()
        graphNode2 = BNode()
        sdGraph.add((datasetNode,SD_NS.namedGraph,graphNode))
        sdGraph.add((graphNode,SD_NS.name,URIRef(graph.identifier)))
        sdGraph.add((graphNode,SD_NS.graph,graphNode2))
        sdGraph.add((graphNode2,RDF.type,SD_NS.Graph))
        statNode = BNode()
        sdGraph.add((graphNode2,SD_NS.statItem,statNode))
        sdGraph.add((statNode,SCOVO.dimension,VOID.numberOfTriples))
        noTriples = Literal(len(graph))
        sdGraph.add((statNode,RDF.value,noTriples))
    print sdGraph.serialize(format='pretty-xml')
Beispiel #50
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 #51
0
    "dbpedia-owl":
    Namespace("http://dbpedia.org/ontology/"),
    "ssn":
    Namespace("http://purl.oclc.org/NET/ssnx/ssn#"),
    "DUL":
    Namespace("http://www.loa-cnr.it/ontologies/DUL.owl#"),
    "time":
    Namespace("http://www.w3.org/2006/time#"),
    "sw":
    Namespace("http://sweet.jpl.nasa.gov/2.1/sweetAll.owl#"),
    "id-semsorgrid":
    Namespace("http://id.semsorgrid.ecs.soton.ac.uk/"),
})

g = Graph()

for short, long in ns.iteritems():
    g.bind(short, long)

g.parse(observationsURI)
if len(g) < 1:
    print >> sys.stderr, "failed to load any triples from '%s'" % observationsURI
    sys.exit(1)

for s, p, o in g.triples((None, ns["rdf"]["type"], ns["ssn"]["Observation"])):
    pprint.pprint(s)
    pprint.pprint(p)
    pprint.pprint(o)

print g.query("SELECT * WHERE { ?s ?p ?o . }")
Beispiel #52
0
class AdditionalDescriptionLogicTests(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 testGCIConDisjunction(self):
        conjunct = EX.Foo & (EX.Omega | EX.Alpha)
        (EX.Bar) += conjunct
        ruleStore,ruleGraph,network=SetupRuleStore(makeNetwork=True)
        rules=network.setupDescriptionLogicProgramming(
                              self.ontGraph,
                              derivedPreds=[EX_NS.Bar],
                              addPDSemantics=False,
                              constructNetwork=False)
        self.assertEqual(repr(rules),
                         "[Forall ?X ( ex:Bar(?X) :- And( ex:Foo(?X) ex:Alpha(?X) ) ), Forall ?X ( ex:Bar(?X) :- And( ex:Foo(?X) ex:Omega(?X) ) )]")
        self.assertEqual(len(rules),
                         2,
                        "There should be 2 rules")

#    def testMalformedUnivRestriction(self):
#        someProp = Property(EX_NS.someProp)
#        conjunct = EX.Foo & (someProp|only|EX.Omega)
#        conjunct.identifier = EX_NS.Bar
#        ruleStore,ruleGraph,network=SetupRuleStore(makeNetwork=True)
#        self.failUnlessRaises(MalformedDLPFormulaError, 
#                              network.setupDescriptionLogicProgramming,
#                              self.ontGraph,
#                              derivedPreds=[EX_NS.Bar],
#                              addPDSemantics=False,
#                              constructNetwork=False)

    def testBasePredicateEquivalence(self):
        (EX.Foo).equivalentClass = [EX.Bar]
        self.assertEqual(repr(Class(EX_NS.Foo)),"Class: ex:Foo EquivalentTo: ex:Bar")
        ruleStore,ruleGraph,network=SetupRuleStore(makeNetwork=True)
        rules=network.setupDescriptionLogicProgramming(
                              self.ontGraph,
                              addPDSemantics=False,
                              constructNetwork=False)
        self.assertEqual(repr(rules),
                         "[Forall ?X ( ex:Bar(?X) :- ex:Foo(?X) ), Forall ?X ( ex:Foo(?X) :- ex:Bar(?X) )]")
        self.assertEqual(len(rules),
                         2,
                        "There should be 2 rules")

    def testExistentialInRightOfGCI(self):
        someProp = Property(EX_NS.someProp)
        existential = someProp|some|EX.Omega
        existential += EX.Foo
        self.assertEqual(repr(Class(EX_NS.Foo)),"Class: ex:Foo SubClassOf: ( ex:someProp some ex:Omega )")
        ruleStore,ruleGraph,network=SetupRuleStore(makeNetwork=True)
        rules=network.setupDescriptionLogicProgramming(
                              self.ontGraph,
                              addPDSemantics=False,
                              constructNetwork=False)
#        self.assertEqual(len(rules),
#                         1,
#                        "There should be 1 rule: %s"%rules)
#        rule=rules[0]
#        self.assertEqual(repr(rule.formula.body),
#                         "ex:Foo(?X)")             
#        self.assertEqual(len(rule.formula.head.formula),
#                         2)
        
    def testValueRestrictionInLeftOfGCI(self):
        someProp = Property(EX_NS.someProp)
        leftGCI = (someProp|value|EX.fish) & EX.Bar
        foo = EX.Foo
        foo+=leftGCI
        self.assertEqual(repr(leftGCI),
                         "ex:Bar that ( ex:someProp value ex:fish )")
        ruleStore,ruleGraph,network=SetupRuleStore(makeNetwork=True)
        rules=network.setupDescriptionLogicProgramming(
                              self.ontGraph,
                              addPDSemantics=False,
                              constructNetwork=False)
        self.assertEqual(repr(rules),
                         "[Forall ?X ( ex:Foo(?X) :- And( ex:someProp(?X ex:fish) ex:Bar(?X) ) )]")
        
    def testNestedConjunct(self):
        nestedConj = (EX.Foo & EX.Bar) & EX.Baz
        (EX.Omega)+= nestedConj
        ruleStore,ruleGraph,network=SetupRuleStore(makeNetwork=True)
        rules=network.setupDescriptionLogicProgramming(
                              self.ontGraph,
                              addPDSemantics=False,
                              constructNetwork=False)
        for rule in rules:
            if rule.formula.head.arg[-1]==EX_NS.Omega:
                self.assertEqual(len(rule.formula.body),
                                 2)
                skolemPredicate = [term.arg[-1]
                         for term in rule.formula.body
                                if term.arg[-1].find(SKOLEMIZED_CLASS_NS)!=-1]
                self.assertEqual(len(skolemPredicate),
                                 1,
                                "Couldn't find skolem unary predicate!")
            else:
                self.assertEqual(len(rule.formula.body),
                                 2)
                skolemPredicate = rule.formula.head.arg[-1]
                self.failUnless(skolemPredicate.find(SKOLEMIZED_CLASS_NS)!=-1,
                                "Head should be a unary skolem predicate")
                skolemPredicate=skolemPredicate[0]
                
    def testOtherForm(self):
        contains   = Property(EX_NS.contains)
        locatedIn  = Property(EX_NS.locatedIn)
        topConjunct = (EX.Cath & 
                         (contains|some|
                            (EX.MajorStenosis & (locatedIn|value|EX_NS.LAD)))  &
                         (contains|some|
                            (EX.MajorStenosis & (locatedIn|value|EX_NS.RCA))))
        (EX.NumDisV2D)+=topConjunct
        from FuXi.DLP.DLNormalization import NormalFormReduction
        NormalFormReduction(self.ontGraph)
        ruleStore,ruleGraph,network=SetupRuleStore(makeNetwork=True)
        rules=network.setupDescriptionLogicProgramming(
                              self.ontGraph,
                              derivedPreds=[EX_NS.NumDisV2D],
                              addPDSemantics=False,
                              constructNetwork=False)
        from FuXi.Rete.Magic import PrettyPrintRule
        for rule in rules:
            PrettyPrintRule(rule)
            
    def testOtherForm2(self):
        hasCoronaryBypassConduit   = Property(EX_NS.hasCoronaryBypassConduit)

        ITALeft = EX.ITALeft
        ITALeft += (hasCoronaryBypassConduit|some|
                    EnumeratedClass(
                       members=[EX_NS.CoronaryBypassConduit_internal_thoracic_artery_left_insitu,
                                EX_NS.CoronaryBypassConduit_internal_thoracic_artery_left_free])) 
        from FuXi.DLP.DLNormalization import NormalFormReduction
        self.assertEquals(repr(Class(first(ITALeft.subSumpteeIds()))),"Some Class SubClassOf: Class: ex:ITALeft ")
        NormalFormReduction(self.ontGraph)
        self.assertEquals(repr(Class(first(ITALeft.subSumpteeIds()))),
                          "Some Class SubClassOf: Class: ex:ITALeft  . EquivalentTo: ( ( ex:hasCoronaryBypassConduit value ex:CoronaryBypassConduit_internal_thoracic_artery_left_insitu ) or ( ex:hasCoronaryBypassConduit value ex:CoronaryBypassConduit_internal_thoracic_artery_left_free ) )")
Beispiel #53
0
    def test(self, debug=debug):
        if debug:
            print testName, label, named_graphs
        query = urlopen(queryFile).read()
        try:
            parsedQuery = parse(query)
        except ParseException:
            return

        assertion = BNode()
        result_node = BNode()
        test_graph.add((result_node, RDF.type, EARL.TestResult))
        test_graph.add(
            (result_node, DC['date'], Literal(datetime.date.today())))
        test_graph.add((assertion, RDF.type, EARL.Assertion))
        test_graph.add((assertion, EARL.assertedBy, MY_FOAF.chime))
        test_graph.add((assertion, EARL.subject,
                        URIRef('http://metacognition.info/software/fuxi')))
        test_graph.add((assertion, EARL.test, TEST[testName]))
        test_graph.add((assertion, EARL.result, result_node))

        if named_graphs:
            g = ConjunctiveGraph()
        else:
            g = Graph()

        if debug:
            print "Source graph ", rdfDoc
        g.parse(urlopen(rdfDoc), publicID=rdfDoc, format='n3')

        for sourceUri, graphIri in named_graphs:
            g.parse(urlopen(sourceUri), publicID=graphIri, format='n3')
        if named_graphs:
            factGraph = Graph(g.store, identifier=rdfDoc)
        else:
            factGraph = g

        if ENT.RIF in regime:
            rules = []
        else:
            from FuXi.DLP.CompletionReasoning import GetELHConsequenceProcedureRules
            rules = [i for i in self.rdfs_rules] if ENT.RDFS in regime else []
            rules.extend(
                self.network.setupDescriptionLogicProgramming(
                    factGraph, addPDSemantics=True, constructNetwork=False))
            if query.find('subClassOf') + 1 and (ENT.RDFS not in regime or
                                                 testName in COMPLETION_RULES):
                if debug:
                    print "Added completion rules for EL TBox reasoning"
                rules.extend(GetELHConsequenceProcedureRules(factGraph))
                facts2add = []
                for owl_class in factGraph.subjects(RDF.type, OWLNS.Class):
                    facts2add.append(
                        (owl_class, RDFS.subClassOf, owl_class, factGraph))
                factGraph.addN(facts2add)
            if debug:
                pprint(list(rules))
        if debug:
            print query
        topDownStore = TopDownSPARQLEntailingStore(
            factGraph.store,
            factGraph,
            idb=rules,
            DEBUG=debug,
            nsBindings=nsMap,
            #hybridPredicates = [RDFS.subClassOf],
            identifyHybridPredicates=True,
            templateMap={STRING.contains: "REGEX(%s,%s)"})
        targetGraph = Graph(topDownStore)
        for pref, nsUri in (setdict(nsMap) | setdict(
                parsedQuery.prolog.prefixBindings)).items():
            targetGraph.bind(pref, nsUri)
        rt = targetGraph.query('', parsedQuery=parsedQuery)
        if rt.askAnswer:
            actualSolns = rt.askAnswer[0]
            expectedSolns = parseResults(urlopen(result).read())
        else:
            actualSolns = [
                ImmutableDict([(k, v) for k, v in d.items()])
                for d in parseResults(rt.serialize(format='xml'))
            ]
            expectedSolns = [
                ImmutableDict([(k, v) for k, v in d.items()])
                for d in parseResults(urlopen(result).read())
            ]
            actualSolns.sort(key=lambda d: hash(d))
            expectedSolns.sort(key=lambda d: hash(d))

            actualSolns = set(actualSolns)
            expectedSolns = set(expectedSolns)

        if actualSolns == expectedSolns:
            test_graph.add((result_node, EARL.outcome, EARL['pass']))
        else:
            test_graph.add((result_node, EARL.outcome, EARL['fail']))
        self.failUnless(
            actualSolns == expectedSolns,
            "Answers don't match %s v.s. %s" % (actualSolns, expectedSolns))
        if debug:
            for network, goal in topDownStore.queryNetworks:
                pprint(goal)
                network.reportConflictSet(True)
Beispiel #54
0
from rdflib import Namespace, Literal
from rdflib.Graph import Graph

NS_FOAF = Namespace("http://xmlns.com/foaf/0.1/")
NS_EXIF = Namespace("http://www.w3.org/2003/12/exif/ns#")

index = Graph()
index.bind("cc", "http://creativecommons.org/ns#")
index.bind("dc", "http://purl.org/dc/elements/1.1/")
index.bind("dcq","http://purl.org/dc/terms/")
index.bind("rdf","http://www.w3.org/1999/02/22-rdf-syntax-ns#")
index.bind("foaf","http://xmlns.com/foaf/0.1/")
index.load('./rdf/index.rdf')

output = Graph()
output.bind("foaf","http://xmlns.com/foaf/0.1/")
output.bind("exif","http://www.w3.org/2003/12/exif/ns#")

for img in index.objects(None, NS_FOAF.logo):
    print img
    width, height = img[:-len('.png')].split('/')[-1].split('x')
    output.add( (img, NS_EXIF.width, Literal(width)) )
    output.add( (img, NS_EXIF.height, Literal(height)) )

file('./rdf/images.rdf', 'w').write(
    output.serialize(format="pretty-xml", max_depth=2))
Beispiel #55
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 #56
0
class CeltxRDFProject:

    def __init__(self, source):
        if isinstance(source, ZipFile):
            source = StringIO.StringIO(source.read('project.rdf'))
        elif isinstance(source, basestring) and os.path.isdir(source):
            source = os.path.join(source, 'project.rdf')

        self.g = Graph()
        self.g.parse(source)

    def projectid(self):
        return self.g.subjects(RDF.type, CX.Project).next()

    def projectname(self):
        return self.g.objects(self.projectid(), DC['title']).next()

    def fileinfo(self, fileid):
        """ Returns an object with file information """

        file = dict(self.g.predicate_objects(fileid))
        title = DC['title'] in file and file[DC['title']] or ''
        doctype = CX.doctype in file and rsplit(file[CX.doctype], '/', 1)[1] or ''
        if not doctype:
            doctype = CX.projectRoot in file and 'Project' or ''
        localfile = CX.localFile in file and file[CX.localFile] or ''
        return MicroMock(title=title, doctype=doctype, localfile=localfile)

    def filelist(self):
        """ Returns a list of files within a project """

        base = self.g.seq(list(self.g.objects(self.projectid(), CX.components))[0])
        return list(self._filelist(base))


    def _filelist(self, seq):
        for pred in seq:
            r = self.fileinfo(pred)

            s = self.g.seq(pred)
            if s:
                r.filelist = list(self._filelist(s))

            yield r

    def fileid(self, filename):
        """ Returns the id of a file """

        if isinstance(filename, URIRef):
            return filename
        else:
            return list(self.g.subjects(CX.localFile, Literal(filename)))[0]

    def save(self, source):
        """ Saves the project to a file """

        if isinstance(source, basestring) and os.path.isdir(source):
            source = os.path.join(source, 'project.rdf')

        self.g.bind("RDF", RDF)
        self.g.bind("sx", SX)
        self.g.serialize(source, format='pretty-xml')
Beispiel #57
0
def image_uris(uri):
    """Given a CC license URI, return a sequence of image URIs."""
    base = 'http://creativecommons.org/licenses/'
    img_base = 'http://i.creativecommons.org/l/'
    img_uri = img_base + uri[len(base):]
    if not img_uri.endswith('/'):
        img_uri = img_uri + '/'
    return (img_uri + '88x31.png', img_uri + '80x15.png')


for root, dirs, files in os.walk('./license_rdf'):
    for filename in files:
        if filename[-4:] != '.rdf':
            continue
        store = Graph()
        store.bind("cc", "http://creativecommons.org/ns#")
        store.bind("dc", "http://purl.org/dc/elements/1.1/")
        store.bind("dcq", "http://purl.org/dc/terms/")
        store.bind("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
        store.bind("foaf", "http://xmlns.com/foaf/0.1/")
        store.load(os.path.join(root, filename))

        for license in store.subjects(NS_RDF.type, NS_CC.License):

            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)) )
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 #59
0
def image_uris(uri):
    """Given a CC license URI, return a sequence of image URIs."""
    base = 'http://creativecommons.org/licenses/'
    img_base = 'http://i.creativecommons.org/l/'
    img_uri = img_base + uri[len(base):]
    if not img_uri.endswith('/'):
        img_uri = img_uri  + '/'
    return (img_uri + '88x31.png', img_uri + '80x15.png')

for root, dirs, files in os.walk('./license_rdf'):
    for filename in files:
        if filename[-4:] != '.rdf':
            continue
        store = Graph()
        store.bind("cc", "http://creativecommons.org/ns#")
        store.bind("dc", "http://purl.org/dc/elements/1.1/")
        store.bind("dcq","http://purl.org/dc/terms/")
        store.bind("rdf","http://www.w3.org/1999/02/22-rdf-syntax-ns#")
        store.bind("foaf","http://xmlns.com/foaf/0.1/")
        store.load(os.path.join(root, filename))

        for license in store.subjects(NS_RDF.type, NS_CC.License):

            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)) )
Beispiel #60
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)