Exemplo n.º 1
0
    def writeFile(self, stmts, ctx, fileWords):
        outfile = "commentstore/post-%s.nt" % ("-".join(fileWords))
        graph = ConjunctiveGraph()

        graph.add(*stmts, **{'context' : ctx})
        graph.graph.serialize(outfile, format='n3')
        log.info("wrote new comment to %s", outfile)
Exemplo n.º 2
0
class Db(_shared):
    def __init__(self):
        self.lastTimes = []

    def getGraph(self):
        t1 = time.time()

        mtimes = []
        for f in (["/my/proj/openid_proxy/access.n3"] +
                  glob.glob("commentstore/*.nt")):
            mtimes.append(os.path.getmtime(f))

        if mtimes == self.lastTimes and hasattr(self, 'currentGraph'):
            return self.currentGraph
        self.lastTimes = mtimes

        tf = tempfile.NamedTemporaryFile()
        os.system("cat /my/proj/openid_proxy/access.n3 commentstore/*.nt > %s" % tf.name)
        self.currentGraph = ConjunctiveGraph()
        self.currentGraph.parse(tf.name, format="n3")
      
        log.info("reloaded comments from disk in %f sec" % (time.time() - t1))

        return self.currentGraph
        
    def writeFile(self, stmts, ctx, fileWords):
        outfile = "commentstore/post-%s.nt" % ("-".join(fileWords))
        graph = ConjunctiveGraph()

        graph.add(*stmts, **{'context' : ctx})
        graph.graph.serialize(outfile, format='n3')
        log.info("wrote new comment to %s", outfile)
Exemplo n.º 3
0
 def test_url(self):
     if self.html5lib_installed():
         g = ConjunctiveGraph()
         g.parse(location='http://oreilly.com/catalog/9780596516499/',
                 format='rdfa', 
                 lax=True)
         self.assertTrue(len(g) > 0)
Exemplo n.º 4
0
class TestSparqlJsonResults(unittest.TestCase):

    def setUp(self):
        self.graph = ConjunctiveGraph()
        self.graph.parse(StringIO(test_data), format="n3")

    def _query_result_contains(self, query, correct):
        results = self.graph.query(query)
        result_json = json.loads(results.serialize(format='json').decode('utf-8'))

        msg = "Expected:\n %s \n- to contain:\n%s" % (result_json, correct)
        self.assertEqual(sorted(result_json["head"], key=repr),
                         sorted(correct["head"], key=repr), msg)

        # Sort by repr - rather a hack, but currently the best way I can think
        # of to ensure the results are in the same order.
        result_bindings = sorted(result_json["results"]["bindings"], key=repr)
        correct_bindings = sorted(correct["results"]["bindings"], key=repr)
        msg = "Expected:\n %s \n- to contain:\n%s" % (result_bindings, correct_bindings)
        self.failUnless(result_bindings==correct_bindings, msg)

    testOptional = make_method('optional')

    testWildcard = make_method('wildcard')

    testUnion = make_method('union')

    testUnion3 = make_method('union3')

    testSelectVars = make_method('select_vars')
    
    testWildcardVars = make_method('wildcard_vars')
 def test4_DAWG_DATASET_COMPLIANCE_is_True(self):
     raise SkipTest("known DAWG_DATATSET_COMPLIANCE SPARQL issue")
     graph = Graph()
     graph.parse(data=test4data, format='n3')
     res = graph.query(test4query, dSCompliance=True)
     # print("json", res.serialize(format='json'))
     assert len(res) == 2
Exemplo n.º 6
0
 def has_correct_hash(self, resource):
     f = RdfUtils.get_format(resource.get_filename())
     cg = ConjunctiveGraph()
     cg.parse(data=resource.get_content(), format=f)
     quads = RdfUtils.get_quads(cg)
     h = RdfHasher.make_hash(quads, resource.get_hashstr())
     return resource.get_hashstr() == h
Exemplo n.º 7
0
def main():
    parser = argparse.ArgumentParser(
        description='OMIA integration test',
        formatter_class=argparse.RawTextHelpFormatter)

    parser.add_argument(
        '--input', '-i', type=str, required=True,
        help='Location of input ttl file')

    args = parser.parse_args()

    graph = ConjunctiveGraph()
    graph.parse(args.input, format=rdflib_util.guess_format(args.input))

    model_of = URIRef('http://purl.obolibrary.org/obo/RO_0003301')

    models = graph.subject_objects(model_of)
    model_len = len(list(models))

    if model_len < EXPECTED_PAIRS:
        logger.error("Not enough model_of predicates in graph:"
                     " {} expected {} check omia log for"
                     " warnings".format(model_len, EXPECTED_PAIRS))
        exit(1)

    omim_diseases = graph.objects(
        subject=URIRef('https://monarchinitiative.org/model/OMIA-breed:18'),
        predicate=model_of
    )

    if list(omim_diseases) != [URIRef('http://purl.obolibrary.org/obo/OMIM_275220')]:
        logger.error("Missing breed to omim triple for {}".format('OMIA-breed:18'))
        exit(1)
    
    logger.info("PASSED")
Exemplo n.º 8
0
class TestIssue06(unittest.TestCase):
    debug = False
    sparql = True

    def setUp(self):
        self.graph = ConjunctiveGraph()
        self.graph.parse(data=testgraph, publicID="testgraph")

    def test_issue_6(self):
        query = """
        PREFIX ex: <http://temp.example.org/terms/>
        PREFIX loc: <http://simile.mit.edu/2005/05/ontologies/location#>
        PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
        PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

        SELECT *
        WHERE {
            {?event ex:date ?date .
            FILTER (xsd:date(?date) >= xsd:date("2007-12-31") && xsd:date(?date) <= xsd:date("2008-01-11"))}

            UNION

            {?event ex:starts ?start; ex:finishes ?end .
             FILTER (xsd:date(?start) >= xsd:date("2008-01-02") && xsd:date(?end) <= xsd:date("2008-01-10"))}
        }
        ORDER BY ?event
        """
        self.graph.query(query, DEBUG=False)
Exemplo n.º 9
0
class GraphAggregates2(unittest.TestCase):

    known_issue = True
    sparql = True

    def setUp(self):
        memStore = plugin.get('IOMemory',Store)()
        self.graph1 = Graph(memStore,URIRef("http://example.com/graph1"))
        self.graph2 = Graph(memStore,URIRef("http://example.com/graph2"))
        self.graph3 = Graph(memStore,URIRef("http://example.com/graph3"))
    
        for n3Str,graph in [(testGraph1N3,self.graph1),
                            (testGraph2N3,self.graph2),
                            (testGraph3N3,self.graph3)]:
            graph.parse(StringIO(n3Str),format='n3')
    
        self.graph4 = Graph(memStore,RDFS)
        self.graph4.parse(RDFS.uri)
        self.G = ConjunctiveGraph(memStore)

    def testAggregateSPARQL(self):    
        print sparqlQ
        rt =  self.G.query(sparqlQ)
        assert len(rt) > 1
        #print rt.serialize(format='xml')
        LOG_NS = Namespace(u'http://www.w3.org/2000/10/swap/log#')
        rt=self.G.query(sparqlQ2,initBindings={u'?graph' : URIRef("http://example.com/graph3")})
        #print rt.serialize(format='json')
        assert rt.serialize('python')[0] == LOG_NS.N3Document,repr(list(rt.serialize('python')))
Exemplo n.º 10
0
class TestSearchAnnotations(unittest.TestCase):
    def test_search_for_uri(self):
        for url in annotation_urls:
            g, target, selector = specific_resource(self.canvas, 
                                                    res=URIRef(uuid.uuid4()), 
                                                    selector=URIRef(uuid.uuid4()))
            g, anno, body, target = annotation(g=g,
                                               anno=URIRef(uuid.uuid4()), 
                                               target=target,
                                               body=URIRef(uuid.uuid4()))
            response = self.client.post(url, data=g.serialize(), 
                                        content_type="text/xml")
            self.assertEqual(response.status_code, 201)

            for uri in [anno, body, target, selector, self.canvas]:
                response = self.client.get(url, {'uri': uri})
                self.assertEqual(response.status_code, 200)
                validate_return_content(self, response, g)

    def tearDown(self):
        pass

    def setUp(self):
        url = reverse('semantic_store_annotations', kwargs=dict())
        self.client = Client()
        fixture_filename = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                                        "semantic_store_test_fixture.xml")
        self.g = ConjunctiveGraph(rdfstore.rdfstore(),
                                  identifier=rdfstore.default_identifier)
        self.g.parse(fixture_filename)
        canvases = self.g.subjects(URIRef(NS.rdf['type']), URIRef(NS.dms['Canvas']))
        self.canvas = list(canvases)[0]
Exemplo n.º 11
0
 def test_file(self):
     if self.html5lib_installed():
         g = ConjunctiveGraph()
         g.parse(location='test/rdfa/oreilly.html',
                 format='rdfa',
                 lax=True)
         self.assertEqual(len(g), 77)
Exemplo n.º 12
0
 def test_01_query(self):
     g = ConjunctiveGraph(self.store)
     count = 0
     for statement in g.triples((None, None, None)):
         count += 1
         break
     assert count == 1, "Should have found at least one triple"
Exemplo n.º 13
0
    def check(kws):
        cg = ConjunctiveGraph()
        cg.parse(**kws)

        for g in cg.contexts():
            gid = g.identifier
            assert isinstance(gid, Identifier)
Exemplo n.º 14
0
class RecursionTests(unittest.TestCase):
    def setUp(self):
        self.graph = ConjunctiveGraph()
        self.graph.load(StringIO(testContent), format='n3')

    def test_simple_recursion(self):
        graph = ConjunctiveGraph()
        graph.load(StringIO(BASIC_KNOWS_DATA), format='n3')
        results = graph.query(KNOWS_QUERY,
                              DEBUG=False).serialize(format='python')
        results = set([tuple(result) for result in results])
        person1 = URIRef('ex:person.1')
        person2 = URIRef('ex:person.2')
        nose.tools.assert_equal(
          results,
          set([(person1, None), (person1, Literal('person 3')),
               (person2, Literal('person 3'))]))

    def test_secondary_recursion(self):
        graph = ConjunctiveGraph()
        graph.load(StringIO(SUBCLASS_DATA), format='n3')
        results = graph.query(SUBCLASS_QUERY,
                              DEBUG=False).serialize(format='python')
        results = set([tuple(result) for result in results])
        ob = URIRef('ex:ob')
        class1 = URIRef('ex:class.1')
        class2 = URIRef('ex:class.2')
        class3 = URIRef('ex:class.3')
        nose.tools.assert_equal(
          results,
          set([(ob, class1), (ob, class2), (ob, class3)]))
Exemplo n.º 15
0
class TestSparqlEquals(unittest.TestCase):

    PREFIXES = {
        'rdfs': "http://www.w3.org/2000/01/rdf-schema#"
    }

    def setUp(self):
        testContent = """
            @prefix rdfs: <%(rdfs)s> .
            <http://example.org/doc/1> rdfs:label "Document 1"@en .
            <http://example.org/doc/2> rdfs:label "Document 2"@en .
            <http://example.org/doc/3> rdfs:label "Document 3"@en .
        """ % self.PREFIXES
        self.graph = ConjunctiveGraph()
        self.graph.load(StringIO(testContent), format='n3')

    def test_uri_equals(self):
        uri = URIRef("http://example.org/doc/1")
        query = ("""
            PREFIX rdfs: <%(rdfs)s>

            SELECT ?uri WHERE {
                ?uri rdfs:label ?label .
                FILTER( ?uri = <"""+uri+"""> )
            }
        """) % self.PREFIXES
        res = self.graph.query(query)
        expected = [(uri,)]
        self.assertEqual(list(res),expected)
class GraphAggregates2(unittest.TestCase):
    # known_issue = True

    def setUp(self):
        memStore = plugin.get("SQLAlchemy", Store)(identifier="rdflib_test", configuration=Literal("sqlite://"))
        self.graph1 = Graph(memStore, URIRef("http://example.com/graph1"))
        self.graph2 = Graph(memStore, URIRef("http://example.com/graph2"))
        self.graph3 = Graph(memStore, URIRef("http://example.com/graph3"))

        for n3Str, graph in [(testGraph1N3, self.graph1), (testGraph2N3, self.graph2), (testGraph3N3, self.graph3)]:
            graph.parse(StringIO(n3Str), format="n3")
        self.graph4 = Graph(memStore, RDFS.uri)
        self.graph4.parse(RDFS.uri)
        self.G = ConjunctiveGraph(memStore)

    def testAggregateSPARQL(self):
        raise SkipTest("known_issue with SELECT from NAMED")
        rt = self.G.query(sparqlQ)
        assert len(rt) > 1
        rt = self.G.query(sparqlQ2, initBindings={u"?graph": URIRef(u"http://example.com/graph3")})
        try:
            import json

            assert json
        except ImportError:
            import simplejson as json
        res = json.loads(rt.serialize(format="json").decode("utf-8"))
        assert len(res["results"]["bindings"]) == 20, len(res["results"]["bindings"])
Exemplo n.º 17
0
    def test_issue_250(self):
        """

        https://github.com/RDFLib/rdflib/issues/250

        When I have a ConjunctiveGraph with the default namespace set,
        for example

        import rdflib
        g = rdflib.ConjunctiveGraph()
        g.bind(None, "http://defaultnamespace")

        then the Trix serializer binds the default namespace twice in its XML
        output, once for the Trix namespace and once for the namespace I used:

        print(g.serialize(format='trix').decode('UTF-8'))

        <?xml version="1.0" encoding="utf-8"?>
        <TriX
          xmlns:xml="http://www.w3.org/XML/1998/namespace"
          xmlns="http://defaultnamespace"
          xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
          xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
          xmlns="http://www.w3.org/2004/03/trix/trix-1/"
        />

        """

        graph = ConjunctiveGraph()
        graph.bind(None, "http://defaultnamespace")
        sg = graph.serialize(format='trix').decode('UTF-8')
        self.assertTrue(
            'xmlns="http://defaultnamespace"' not in sg, sg)
        self.assertTrue(
            'xmlns="http://www.w3.org/2004/03/trix/trix-1/' in sg, sg)
Exemplo n.º 18
0
    def fromuri(self, uri):

        self.uri = uri

        if not uri.startswith('http://rdf.freebase.com'):
            self.checkuri()
            try:
                g = ConjunctiveGraph()
                g.load(self.uri)

                if g:
                    logger.info("INFO process_rdf.py - returning graph for " + self.uri)
                    return g

                else:
                    raise Exception('Nothing was returned, probably caused URL serving no RDF or bad RDF (eg. Freebase): '
                                    '"No handlers could be found for logger "process_rdf.py" -- uri was ' + self.uri)

            except URLError as e:
                logger.error("URLError process_rdf.py - " + e.message)
                raise Exception('URLError, cause either bad URL or no internet connection - ' + e.message + '(uri was ' + self.uri + ')')
            except SAXParseException as e:
                logger.error("SAXParseException process_rdf.py - " + e.message + '(uri was' + self.uri + ')')
                raise Exception('SAXParseException')
            except AttributeError as e:
                logger.error("AttributeError process_rdf.py - " + e.message + '(uri was' + self.uri + ')')
                raise Exception('AttributeError')
        else:
            self.fromfreebaseuri()
class TestSparqlJsonResults(unittest.TestCase):

    def setUp(self):
        self.graph = ConjunctiveGraph()
        self.graph.parse(StringIO(test_data), format="n3")

    def _query_result_contains(self, query, correct):
        results = self.graph.query(query)
        result_json = json.loads(results.serialize(format='json'))

        msg = "Expected:\n %s \n- to contain:\n%s" % (result_json, correct)
        self.failUnless(result_json["head"]==correct["head"], msg)

        result_bindings = sorted(result_json["results"]["bindings"])
        correct_bindings = sorted(correct["results"]["bindings"])
        msg = "Expected:\n %s \n- to contain:\n%s" % (result_bindings, correct_bindings)
        self.failUnless(result_bindings==correct_bindings, msg)

    testOptional = make_method('optional')

    testWildcard = make_method('wildcard')

    testUnion = make_method('union')

    testUnion3 = make_method('union3')

    testSelectVars = make_method('select_vars')
    
    testWildcardVars = make_method('wildcard_vars')
class GraphAggregates2(unittest.TestCase):
    def setUp(self):
        memStore = plugin.get('SQLAlchemy', Store)(
            identifier="rdflib_test", configuration=Literal("sqlite://"))
        self.graph1 = Graph(memStore, URIRef("http://example.com/graph1"))
        self.graph2 = Graph(memStore, URIRef("http://example.com/graph2"))
        self.graph3 = Graph(memStore, URIRef("http://example.com/graph3"))

        for n3Str,graph in [(testGraph1N3, self.graph1),
                            (testGraph2N3, self.graph2),
                            (testGraph3N3, self.graph3)]:
            graph.parse(StringIO(n3Str), format='n3')

        self.graph4 = Graph(memStore, RDFS.uri)
        self.graph4.parse(RDFS.uri)
        self.G = ConjunctiveGraph(memStore)

    def testAggregateSPARQL(self):
        rt =  self.G.query(sparqlQ)
        assert len(rt) > 1
        rt = self.G.query(sparqlQ2, initBindings={u'?graph' : URIRef(u"http://example.com/graph3")})
        try:
            import json
        except ImportError:
            import simplejson as json
        res = json.loads(rt.serialize(format='json'))
        assert len(res['results']['bindings']) == 20, len(res['results']['bindings'])
Exemplo n.º 21
0
 def testParse(self):
     g = ConjunctiveGraph()
     try:
         g.parse("http://groups.csail.mit.edu/dig/2005/09/rein/examples/troop42-policy.n3", format="n3")
     except URLError:
         from nose import SkipTest
         raise SkipTest('No network to retrieve the information, skipping test')
Exemplo n.º 22
0
 def parse_n3(term_n3):
     ''' Disclaimer: Quick and dirty hack using the n3 parser. '''
     prepstr = ("@prefix  xsd: <http://www.w3.org/2001/XMLSchema#> .\n"
                "<urn:no_use> <urn:no_use> %s.\n" % term_n3)
     g = ConjunctiveGraph()
     g.parse(data=prepstr, format='n3')
     return [t for t in g.triples((None, None, None))][0][2]
Exemplo n.º 23
0
def main(fd, store_type=None, store_id=None, graph_id=None, gzipped=False):
    """
    Converts MARC21 data stored in fd to a RDFlib graph.
    """
    from rdflib import plugin

    if store_type:
        msg = "Need a {} identifier for a disk-based store."
        assert store_id, msg.format('store')
        assert graph_id, msg.format('graph')
        store = plugin.get(store_type, Store)(store_id)
    else:
        store = 'default'

    graph = Graph(store=store, identifier=graph_id)

    if gzipped:
        import gzip
        open = gzip.open

    try:
        records = MARCReader(open(fd))

        for i, triple in enumerate(process_records(records)):
            graph.add(triple)
            if i % 100 == 0:
                graph.commit()
            if i % 10000 == 0:
                print i
    finally:
        records.close()

    return graph
Exemplo n.º 24
0
class TestSparqlASK(unittest.TestCase):
    def setUp(self):
        self.graph = Graph()

        io = StringIO("""
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix : <http://goonmill.org/2007/skill.n3#> .

:Foo a rdfs:Class .

:bar a :Foo .
""")

        
        self.graph.load(io, format='n3')

        self.compliance_setting, algebra.DAWG_DATASET_COMPLIANCE = algebra.DAWG_DATASET_COMPLIANCE, False

    def tearDown(self):
        algebra.DAWG_DATASET_COMPLIANCE = self.compliance_setting

    def test_ask_true(self):
        """
        Ask for a triple that exists, assert that the response is True.
        """
        res = self.graph.query('ASK { <http://goonmill.org/2007/skill.n3#bar> a <http://goonmill.org/2007/skill.n3#Foo> } ')
        self.assertEquals(res.askAnswer, True, "The answer should have been that the triple was found")

    def test_ask_false(self):
        """
        Ask for a triple that does not exist, assert that the response is False.
        """
        res = self.graph.query('ASK { <http://goonmill.org/2007/skill.n3#baz> a <http://goonmill.org/2007/skill.n3#Foo> } ')
        self.assertEquals(res.askAnswer, False, "The answer should have been that the triple was not found")
Exemplo n.º 25
0
 def test_pretty_broken_xmlliteral(self):
     # given:
     g = ConjunctiveGraph()
     g.add((BNode(), RDF.value, Literal(u'''<p ''', datatype=RDF.XMLLiteral)))
     # when:
     xmlrepr = g.serialize(format='pretty-xml')
     # then:
     assert u'''<rdf:value rdf:datatype="http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral">&lt;p '''.encode('utf-8') in xmlrepr
Exemplo n.º 26
0
 def testLimit2(self):
     graph = ConjunctiveGraph(plugin.get('IOMemory',Store)())
     graph.parse(data=test_data2, format="n3")
     results = list(graph.query(test_query2,DEBUG=False))
     self.assertEqual(len(results), 1)
     for title,price in results:    
         self.assertTrue(title in [Literal("Java Tutorial"),
                                   Literal("COBOL Tutorial")])    
Exemplo n.º 27
0
 def test_pretty_xmlliteral(self):
     # given:
     g = ConjunctiveGraph()
     g.add((BNode(), RDF.value, Literal(u'''<p xmlns="http://www.w3.org/1999/xhtml">See also <a href="#aring">Å</a></p>''', datatype=RDF.XMLLiteral)))
     # when:
     xmlrepr = g.serialize(format='pretty-xml')
     # then:
     assert u'''<rdf:value rdf:parseType="Literal"><p xmlns="http://www.w3.org/1999/xhtml">See also <a href="#aring">Å</a></p></rdf:value>'''.encode('utf-8') in xmlrepr
Exemplo n.º 28
0
def build_network(rules):
    if isinstance(rules, basestring):
        rules = StringIO(rules)
    graph = ConjunctiveGraph()
    graph.load(rules, publicID="test", format="n3")
    network = NetworkFromN3(graph, additionalBuiltins={STRING_NS.startsWith: StringStartsWith})
    network.feedFactsToAdd(generateTokenSet(extractBaseFacts(graph)))
    return network
Exemplo n.º 29
0
class TestEmptyBase(unittest.TestCase):
    def setUp(self):
        self.graph = ConjunctiveGraph()
        self.graph.parse(StringIO(test_data),publicID=baseUri)

    def test_base_ref(self):        
        self.failUnless(len(self.graph) == 1,"There should be at least one statement in the graph")
        self.failUnless((baseUri,RDF.type,FOAF.Document) in self.graph,"There should be a triple with %s as the subject" % baseUri)
Exemplo n.º 30
0
class TestRelativeBase(unittest.TestCase):
    def setUp(self):
        self.graph = ConjunctiveGraph()
        self.graph.parse(StringIO(test_data2),publicID=baseUri2)

    def test_base_ref(self):        
        self.failUnless(len(self.graph) == 1,"There should be at least one statement in the graph")
        resolvedBase = URIRef('http://example.com/baz')
        self.failUnless((resolvedBase,RDF.type,FOAF.Document) in self.graph,"There should be a triple with %s as the subject" % resolvedBase)
Exemplo n.º 31
0
class TestRelativeBase(unittest.TestCase):
    def setUp(self):
        self.graph = ConjunctiveGraph()
        self.graph.parse(StringIO(test_data2), publicID=baseUri2, format="xml")

    def test_base_ref(self):
        self.assertTrue(len(self.graph),
                        "There should be at least one statement in the graph")
        resolvedBase = URIRef('http://example.com/baz')
        self.assertTrue((resolvedBase, RDF.type, FOAF.Document) in self.graph,
                        "There should be a triple with %s as the subject" % resolvedBase)
Exemplo n.º 32
0
 def _convertJSONLD(self):
     """
     Convert a RDF JSON-LDresult into an RDFLib triple store. This method can be overwritten
     in a subclass for a different conversion method.
     @return: converted result
     @rtype: RDFLib Graph
     """
     from rdflib import ConjunctiveGraph
     retval = ConjunctiveGraph()
     retval.load(self.response, format='json-ld', publicID=' ')
     return retval
Exemplo n.º 33
0
 def test_pretty_broken_xmlliteral(self):
     # given:
     g = ConjunctiveGraph()
     g.add((BNode(), RDF.value, Literal("""<p """,
                                        datatype=RDF.XMLLiteral)))
     # when:
     xmlrepr = g.serialize(format="pretty-xml")
     # then:
     assert (
         """<rdf:value rdf:datatype="http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral">&lt;p """
         in xmlrepr)
Exemplo n.º 34
0
    def testParse(self):
        g = ConjunctiveGraph()
        try:
            g.parse(
                "http://groups.csail.mit.edu/dig/2005/09/rein/examples/troop42-policy.n3",
                format="n3",
            )
        except URLError:
            from nose import SkipTest

            raise SkipTest("No network to retrieve the information, skipping test")
Exemplo n.º 35
0
def transform(args):
    filename = args[0]
    baseuristr = args[1]

    with open(filename, "r") as f:
        rdfFormat = RdfUtils.get_format(filename)
        cg = ConjunctiveGraph()
        cg.parse(data=f.read(), format=rdfFormat)
        baseuri = URIRef(baseuristr)
        outdir = os.path.abspath(os.path.join(str(filename), os.pardir))
        RdfTransformer.transform_to_file(cg, baseuri, outdir, filename)
Exemplo n.º 36
0
 def _get_graph():
     inputGraph = ConjunctiveGraph()
     contentType = request.headers['Content-Type']
     encoding = 'utf8' if not request.content_encoding else request.content_encoding
     content = str(request.data, encoding)
     fmt = sadi.mimeparse.best_match(
         [mt for mt in list(dataFormats.keys()) if mt is not None],
         contentType)
     if fmt in dataFormats:
         inputGraph.parse(data=content, format=dataFormats[fmt])
     return inputGraph
Exemplo n.º 37
0
 def test_html_decoded_entity_xhtml(self):
     if sys.version_info[0] == 3:
         raise SkipTest('html5lib not yet available for Python 3')
     if platform.system() == "Java":
         raise SkipTest('problem with HTML entities for html5lib in Jython')
     g = ConjunctiveGraph()
     g.parse(data=htmlentitydecode(html), format='rdfa')
     self.assertEqual(len(g), 1)
     self.assertTrue(
         g.value(
             URIRef("http://example.com"),
             URIRef("http://purl.org/dc/terms/title")).eq(u"Exampl\xe9"))
Exemplo n.º 38
0
 def test_html_entity_xhtml(self):
     if sys.version_info[0] == 3:
         raise SkipTest('minidom parser strips HTML entities in Python 3.2')
     if platform.system() == "Java":
         raise SkipTest('problem with HTML entities for html5lib in Jython')
     g = ConjunctiveGraph()
     warnings.simplefilter('ignore', UserWarning)
     g.parse(data=html, format='rdfa')
     self.assertEqual(len(g), 1)
     self.assertTrue(
         g.value(URIRef("http://example.com"),
                 URIRef("http://purl.org/dc/terms/title")).eq(u"Exampl"))
Exemplo n.º 39
0
def test_etc_files():
    assert p.exists(etc_dir)
    for root, dirs, fnames in os.walk(etc_dir):
        for fname in fnames:
            name, ext = p.splitext(fname)
            if ext != '.n3':
                continue
            n3_fpath = p.join(root, fname)
            expected_graph = ConjunctiveGraph()
            expected_graph.parse(n3_fpath, format='n3')
            for json_fpath in glob.glob(p.join(root, name+"*.json")):
                yield _run_case, json_fpath, expected_graph
Exemplo n.º 40
0
 def testBaseSerialize(self):
     g = Graph()
     g.add((
         URIRef("http://example.com/people/Bob"),
         URIRef("urn:knows"),
         URIRef("http://example.com/people/Linda"),
     ))
     s = g.serialize(base="http://example.com/", format="n3")
     self.assertTrue("<people/Bob>".encode("latin-1") in s)
     g2 = ConjunctiveGraph()
     g2.parse(data=s, publicID="http://example.com/", format="n3")
     self.assertEqual(list(g), list(g2))
Exemplo n.º 41
0
class TestSparqlOPT_FILTER(unittest.TestCase):
    def setUp(self):
        self.graph = ConjunctiveGraph()
        self.graph.load(StringIO(testContent), format='n3')

    def test_OPT_FILTER(self):
        xd3Objs = [
            o for o in self.graph.objects(subject=exNS.xd3, predicate=exNS.p)
        ]
        self.failUnless(
            xd3Objs[0].datatype == XSD.double,
            "Expecting %r, got instead : %r" % (double1, xd3Objs[0]))
Exemplo n.º 42
0
 def test_simple_recursion(self):
     graph = ConjunctiveGraph()
     graph.load(StringIO(BASIC_KNOWS_DATA), format='n3')
     results = graph.query(KNOWS_QUERY,
                           DEBUG=False).serialize(format='python')
     results = set([tuple(result) for result in results])
     person1 = URIRef('ex:person.1')
     person2 = URIRef('ex:person.2')
     nose.tools.assert_equal(
         results,
         set([(person1, None), (person1, Literal('person 3')),
              (person2, Literal('person 3'))]))
Exemplo n.º 43
0
 def test_secondary_recursion(self):
     graph = ConjunctiveGraph()
     graph.load(StringIO(SUBCLASS_DATA), format='n3')
     results = graph.query(SUBCLASS_QUERY,
                           DEBUG=False).serialize(format='python')
     results = set([tuple(result) for result in results])
     ob = URIRef('ex:ob')
     class1 = URIRef('ex:class.1')
     class2 = URIRef('ex:class.2')
     class3 = URIRef('ex:class.3')
     nose.tools.assert_equal(
         results, set([(ob, class1), (ob, class2), (ob, class3)]))
Exemplo n.º 44
0
class TestSparqlJsonResults(unittest.TestCase):

    sparql = True

    def setUp(self):
        self.graph = ConjunctiveGraph()
        self.graph.parse(StringIO(test_data), format="n3")

    def test_base_ref(self):
        rt = list(self.graph.query(test_query))
        self.failUnless(rt[0][0] == Literal("Alice"),
                        "Expected:\n 'Alice' \nGot:\n %s" % rt)
class TestEmptyBase(unittest.TestCase):
    def setUp(self):
        self.graph = ConjunctiveGraph()
        self.graph.parse(StringIO(test_data), publicID=baseUri)

    def test_base_ref(self):
        self.assertTrue(
            len(self.graph) == 1,
            "There should be at least one statement in the graph")
        self.assertTrue(
            (baseUri, RDF.type, FOAF.Document) in self.graph,
            "There should be a triple with %s as the subject" % baseUri)
Exemplo n.º 46
0
def bundle_helper(descriptor,
                  graph=None,
                  bundles_directory=None,
                  homedir=None,
                  **kwargs):
    '''
    Helper for creating bundles for testing.

    Uses `~owmeta_core.bundle.Installer` to lay out a bundle

    Parameters
    ----------
    descriptor : Descriptor
        Describes the bundle
    graph : rdflib.graph.ConjunctiveGraph, optional
        Graph from which the bundle contexts will be generated. If not provided, a graph
        will be created with the triple ``(ex:a, ex:b, ex:c)`` in a context named ``ex:ctx``,
        where ``ex:`` expands to ``http://example.org/``
    bundles_directory : str, optional
        The directory where the bundles should be installed. If not provided, creates a
        temporary directory to house the bundles and cleans them up afterwards
    homedir : str, optional
        Test home directory. If not provided, one will be created based on test directory
    '''
    res = BundleData()
    with tempfile.TemporaryDirectory(prefix=__name__ + '.') as testdir:
        res.testdir = testdir
        res.test_homedir = homedir or p(res.testdir, 'homedir')
        res.bundle_source_directory = p(res.testdir, 'bundle_source')
        res.bundles_directory = bundles_directory or p(res.test_homedir,
                                                       '.owmeta', 'bundles')
        if not homedir:
            os.mkdir(res.test_homedir)
        os.mkdir(res.bundle_source_directory)
        if not bundles_directory:
            os.makedirs(res.bundles_directory)

        # This is a bit of an integration test since it would be a PITA to maintain the bundle
        # format separately from the installer
        res.descriptor = descriptor
        if graph is None:
            graph = ConjunctiveGraph()
            ctxg = graph.get_context(URIRef('http://example.org/ctx'))
            ctxg.add((URIRef('http://example.org/a'),
                      URIRef('http://example.org/b'),
                      URIRef('http://example.org/c')))
        res.installer = Installer(res.bundle_source_directory,
                                  res.bundles_directory,
                                  graph=graph,
                                  **kwargs)
        res.bundle_directory = res.installer.install(res.descriptor)
        yield res
 def test_pretty_xmlliteral(self):
     # given:
     g = ConjunctiveGraph()
     g.add((
         BNode(), RDF.value,
         Literal(
             u'''<p xmlns="http://www.w3.org/1999/xhtml">See also <a href="#aring">Å</a></p>''',
             datatype=RDF.XMLLiteral)))
     # when:
     xmlrepr = g.serialize(format='pretty-xml')
     # then:
     assert u'''<rdf:value rdf:parseType="Literal"><p xmlns="http://www.w3.org/1999/xhtml">See also <a href="#aring">Å</a></p></rdf:value>'''.encode(
         'utf-8') in xmlrepr
Exemplo n.º 48
0
    def setUp(self):
        memStore = plugin.get('SQLAlchemy',
                              Store)(identifier="rdflib_test",
                                     configuration=Literal("sqlite://"))
        self.graph1 = Graph(memStore, URIRef("graph1"))
        self.graph2 = Graph(memStore, URIRef("graph2"))
        self.graph3 = Graph(memStore, URIRef("graph3"))

        for n3Str, graph in [(testGraph1N3, self.graph1),
                             (testGraph2N3, self.graph2),
                             (testGraph3N3, self.graph3)]:
            graph.parse(StringIO(n3Str), format='n3')
        self.G = ConjunctiveGraph(memStore)
Exemplo n.º 49
0
class TestIssue11(unittest.TestCase):
    debug = False
    sparql = True
    
    def setUp(self):
        NS = u"http://example.org/"
        self.graph = ConjunctiveGraph()
        self.graph.parse(data=testgraph, format="n3", publicID=NS)

    def testSPARQL_lessthan_filter_using_negative_integer(self):
        rt = self.graph.query(testquery, initNs={'rdf':RDF }, DEBUG=True)
        for row in rt:
            assert str(row[0]) == "http://example.org/bar"
Exemplo n.º 50
0
class TestSparqlOPT_FILTER(unittest.TestCase):
    def setUp(self):
        self.graph = ConjunctiveGraph()
        self.graph.load(StringIO(testContent), format='n3')

    def test_OPT_FILTER(self):
        results = self.graph.query(QUERY,
                                   DEBUG=False,
                                   initBindings={
                                       '?label': RDFS.label
                                   }).serialize(format='python')
        self.failUnless(
            list(results) == [doc2], "expecting : %s" % repr([doc2]))
Exemplo n.º 51
0
def parse_and_serialize(input_files,
                        input_format,
                        guess,
                        outfile,
                        output_format,
                        ns_bindings,
                        store_conn="",
                        store_type=None):

    if store_type:
        store = plugin.get(store_type, Store)()
        store.open(store_conn)
        graph = ConjunctiveGraph(store)
    else:
        store = None
        graph = ConjunctiveGraph()

    for prefix, uri in list(ns_bindings.items()):
        graph.namespace_manager.bind(prefix, uri, override=False)

    for fpath in input_files:
        use_format, kws = _format_and_kws(input_format)
        if fpath == '-':
            fpath = sys.stdin
        elif not input_format and guess:
            use_format = guess_format(fpath) or DEFAULT_INPUT_FORMAT
        graph.parse(fpath, format=use_format, **kws)

    if outfile:
        output_format, kws = _format_and_kws(output_format)
        kws.setdefault('base', None)
        graph.serialize(destination=outfile, format=output_format, **kws)

    if store:
        store.rollback()
Exemplo n.º 52
0
def get_conjunctive_graph(store_id=None):
    """
    Returns an open conjunctive graph.
    """
    if not store_id:
        store_id = DEFAULT_STORE

    store = DjangoStore(DEFAULT_STORE)
    graph = ConjunctiveGraph(store=store, identifier=store_id)
    if graph.open(None) != VALID_STORE:
        raise ValueError(
            "The store identified by {0} is not a valid store".format(
                store_id))
    return graph
Exemplo n.º 53
0
def load_cache(cache_filename):
    cache = {'urls': set(), 'g': ConjunctiveGraph()}
    if not cache_filename:
        return cache
    if not os.path.exists(cache_filename):
        return cache
    f = open(cache_filename, "rb")
    cache = pickle.load(f)
    g_serialized = cache['g']
    g = ConjunctiveGraph()
    g.parse(data=g_serialized)
    cache['g'] = g
    f.close()
    return cache
Exemplo n.º 54
0
def _mangled_copy(g):
    "Makes a copy of the graph, replacing all bnodes with the bnode ``_blank``."
    gcopy = ConjunctiveGraph()

    def isbnode(v): return isinstance(v, BNode)
    for s, p, o in g:
        if isbnode(s):
            s = _blank
        if isbnode(p):
            p = _blank
        if isbnode(o):
            o = _blank
        gcopy.add((s, p, o))
    return gcopy
Exemplo n.º 55
0
def test_escaping_of_triple_doublequotes():
    """
    Issue 186 - Check escaping of multiple doublequotes.
    A serialization/deserialization roundtrip of a certain class of 
    Literals fails when there are both, newline characters and multiple subsequent 
    quotation marks in the lexical form of the Literal. In this case invalid N3
    is emitted by the serializer, which in turn cannot be parsed correctly.
    """
    g = ConjunctiveGraph()
    g.add((URIRef('http://foobar'), URIRef('http://fooprop'),
           Literal('abc\ndef"""""')))
    # assert g.serialize(format='n3') == '@prefix ns1: <http:// .\n\nns1:foobar ns1:fooprop """abc\ndef\\"\\"\\"\\"\\"""" .\n\n'
    g2 = ConjunctiveGraph()
    g2.parse(data=g.serialize(format='n3'), format='n3')
    assert g.isomorphic(g2) is True
Exemplo n.º 56
0
    def deserialize(self, stream, rdf_format='trig', **kwargs):
        """
        Deserialize from the `Prov-O <https://www.w3.org/TR/prov-o/>`_
        representation to a :class:`~prov.model.ProvDocument` instance.

        :param stream: Input data.
        """
        newargs = kwargs.copy()
        newargs['format'] = rdf_format
        container = ConjunctiveGraph()
        container.parse(stream, **newargs)
        document = ProvDocument()
        self.document = document
        self.decode_document(container, document)
        return document
Exemplo n.º 57
0
 def __init__(self, *args, **kw):
     """Inicializa o agregador RDF.
     """
     super(RDFAggregator, self).__init__('csv', *args, **kw)
     self.aggregator = ConjunctiveGraph()
     self.aggregator.bind(u'owl', OWL)
     self.aggregator.bind(u'lic', LIC)
     self.aggregator.bind(u'siorg', SIORG)
     self.aggregator.bind(u'siafi', SIAFI)
     self.aggregator.bind(u'geo', GEO)
     self.aggregator.bind(u'dbpedia', DBPEDIA)
     self.aggregator.bind(u'dbprop', DBPROP)
     self.aggregator.bind(u'dbo', DBONT)
     self.aggregator.bind(u'void', VOID)
     self.aggregator.bind(u'foaf', FOAF)
     self.aggregator.bind(u'vcard', VCARD)
Exemplo n.º 58
0
    def _convertRDF(self):
        """
		Convert an RDF/XML result into an RDFLib triple store. This method can be overwritten
		in a subclass for a different conversion method.
		@return: converted result
		@rtype: RDFLib Graph
		"""
        try:
            from rdflib.graph import ConjunctiveGraph
        except ImportError:
            from rdflib import ConjunctiveGraph
        retval = ConjunctiveGraph()
        # this is a strange hack. If the publicID is not set, rdflib (or the underlying xml parser) makes a funny
        #(and, as far as I could see, meaningless) error message...
        retval.load(self.response, publicID=' ')
        return retval
Exemplo n.º 59
0
Arquivo: main.py Projeto: cbp44/whyis
 def update(nanopub_uri):
     '''gets called whenever there is a change in the knowledge graph.
     Performs a breadth-first knowledge expansion of the current change.'''
     #print "Updating on", nanopub_uri
     #if not app.nanopub_manager.is_current(nanopub_uri):
     #    print("Skipping retired nanopub", nanopub_uri)
     #    return
     nanopub = app.nanopub_manager.get(nanopub_uri)
     nanopub_graph = ConjunctiveGraph(nanopub.store)
     if 'inferencers' in self.config:
         for name, service in list(self.config['inferencers'].items()):
             service.app = self
             if service.query_predicate == self.NS.whyis.updateChangeQuery:
                 if service.getInstances(nanopub_graph):
                     print("invoking", name, nanopub_uri)
                     process_nanopub.apply_async(kwargs={
                         'nanopub_uri': nanopub_uri,
                         'service_name': name
                     },
                                                 priority=1)
         for name, service in list(self.config['inferencers'].items()):
             service.app = self
             if service.query_predicate == self.NS.whyis.globalChangeQuery:
                 process_resource.apply_async(
                     kwargs={'service_name': name}, priority=5)
Exemplo n.º 60
0
    def parse(self, source, sink, **kwargs):
        # TODO: docstring w. args and return value
        encoding = kwargs.get('encoding') or 'utf-8'
        if encoding not in ('utf-8', 'utf-16'):
            warnings.warn("JSON should be encoded as unicode. " +
                          "Given encoding was: %s" % encoding)

        base = kwargs.get('base') or sink.absolutize(
            source.getPublicId() or source.getSystemId() or "")
        context_data = kwargs.get('context')
        if not context_data and isinstance(source, URLInputSource):
            context_data = context_from_urlinputsource(source)
        produce_generalized_rdf = kwargs.get('produce_generalized_rdf', False)

        data = source_to_json(source)

        # NOTE: A ConjunctiveGraph parses into a Graph sink, so no sink will be
        # context_aware. Keeping this check in case RDFLib is changed, or
        # someone passes something context_aware to this parser directly.
        if not sink.context_aware:
            conj_sink = ConjunctiveGraph(store=sink.store,
                                         identifier=sink.identifier)
        else:
            conj_sink = sink

        to_rdf(data, conj_sink, base, context_data)