Пример #1
0
def readGraphs():
    g = ConjunctiveGraph()
    # this file should only be reread when it changes
    g.parse("../config.n3", format="n3")
    dl = []
    startTime = time.time()
    for uri in [
        "http://bang:9055/graph",
        "http://bang:9069/graph",
        "http://bang:9070/graph",
        "http://bang:9072/bang-9002/processStatus",
        "http://bang:9072/bang/processStatus",
        "http://bang:9072/dash/processStatus",
        "http://bang:9072/slash-11000/processStatus",
        "http://bang:9072/slash/processStatus",
        "http://bang:9072/star/processStatus",
        "http://bang:9075/graph",
        ]:
        # this ought to not reparse the ones that are 304 not modified
        d = getPage(uri)
        def done(trig, uri):
            g.addN(parseTrig(trig))
            print "%s done in %.02fms" % (uri, 1000 * (time.time() - startTime))
        d.addCallback(done, uri)
        dl.append(d)
    return defer.DeferredList(dl).addCallback(lambda result: g)
Пример #2
0
def getGraph():
    """
    get the main graph, including data from trains.n3 on disk
    """
    graph = ConjunctiveGraph()
    graph.parse("trains.n3", format="n3", publicID=TT['disk#context'])
    return graph
Пример #3
0
def main():
	"""Main Function
	Simple command-line procedure for web2rdf."""

	if len(sys.argv) != 2:
		print "Must call with a URI parameter."
		print "Usage: %s uriSrc" % sys.argv[0]
		return

	uri = sys.argv[1]

	# Get the RDF
	wrdf = Web2Rdf(uri)
	rdf = wrdf.getRdf()

	if not rdf:
		print "No RDF returned!"
		return False

	print "Got RDF..."
	rdf = rdfString(rdf)

	# Open Storage
	print "Opening store..."
	db = "./testdb.sqlite"
	rstore = RdfStore('sqlite', db)
	rstore.open()

	print "Storing..."
	graph = Graph(rstore.get(), identifier = URIRef("http://slashdot/Test2"))
	#graph.parse("example.rdf")
	graph.parse(rdf, publicID=uri)

	graph.commit()
Пример #4
0
    def __init__(self, path=None):
        self.__dict__ = self.__shared_state
        if (self.data == None):
            if (path == None):
                raise ValueError("djubby's configuration MUST be initialized a first time, read http://code.google.com/p/djubby/wiki/GettingStarted")
            else:
                self.path = os.path.abspath(path)
                logging.debug("Reading djubby's configuration from %s..." % self.path)
                if (not os.path.exists(self.path)):
                    raise ValueError("Not found a proper file at '%s' with a configuration for djubby. Please, provide a right path" % self.path)

                data = ConjunctiveGraph()
                data.bind("conf", ns.config) 
                try:
                    data.load(path, format='n3') 
                except Exception, e:
                    raise ValueError("Not found a proper N3 file at '%s' with a configuration for djubby. Please, provide a valid N3 file" % self.path)

                self.data = data
                try:
                    self.graph = self.get_value("sparqlDefaultGraph")
                    self.endpoint = self.get_value("sparqlEndpoint")
                except Exception, e:
                    raise ValueError("Not found the graph not the endpoint that it's supposed djubby have to query. Please, provide a right donfiguration")

                logging.info("Using <%s> as default graph to query the endpoint <%s>" % (self.graph, self.endpoint))
                self.__class__.__dict__['_Configuration__shared_state']["data"] = data #FIXME
Пример #5
0
 def parse(self, result):
     """
     Parse query result
     
     @param result: text result
     @return: rdf graph
     """        
     graph = ConjunctiveGraph()
     graph.parse(StringInputSource(result))
     return graph
Пример #6
0
    def to_rdf(self, format="settings"):
        """Convert the RdfSerializer store into RDF."""
        graph = Graph()
        for k, v in self.NAMESPACES.iteritems():
            graph.bind(k, v)

        for g in self.subgraphs:
            graph += g

        if format == "settings":
            format = settings.RDF_SERIALIZATION

        return graph.serialize(format=format)
Пример #7
0
def testDefaultGraph():
    memStore = plugin.get('IOMemory',Store)()
    graph1 = Graph(memStore,URIRef("graph1"))
    graph2 = Graph(memStore,URIRef("graph2"))
    graph3 = Graph(memStore,URIRef("graph3"))
    
    for n3Str,graph in [(testGraph1N3,graph1),
                        (testGraph2N3,graph2),
                        (testGraph3N3,graph3)]:
        graph.parse(StringIO(n3Str),format='n3')
    G = ConjunctiveGraph(memStore)
    #test that CG includes triples from all 3
    assert G.query(sparqlQ3),"CG as default graph should *all* triples"
    assert not graph2.query(sparqlQ3),"Graph as default graph should *not* include triples from other graphs"
Пример #8
0
class RdfParser(object):
	"""A basic wrapper for RdfLib's RDF parser.
	This class aims to accomplish easier parsing, extraction of Models,
	etc."""

	def __init__(self, rdf, format='guess'):
		"""Init the parser with the graph string."""
		self.graph = Graph()
		if format == 'guess':
			format = self.__guess_format(rdf)
			print 'RdfParser guesses format to be: %s' % format
		try:
			self.graph.load(StringIO(rdf), format=format)
		except:
			print "Failed to parse RDF:"
			print rdf[0:100]

	def extract(self, datatype):
		"""Extract all of the data of a given datatype."""
		data = []
		ns = RdfSerializer.NAMESPACES['sylph'] # TODO: Awkward.
		for sub in self.graph.subjects(RDF.type, ns[datatype]):
			idx = str(sub)
			item = {'uri': idx}
			for pred, obj in self.graph.predicate_objects(sub):
				if pred == RDF.type:
					continue
				if obj == ns['None']:
					obj = None
				elif type(obj) == URIRef:
					obj = unicode(obj)
				elif type(obj) == Literal:
					obj = obj.toPython()
					if type(obj) == Literal: # Don't be silly, RdfLib!
						obj = unicode(obj)

				predstr = str(pred).rpartition('#')[2].rpartition('/')[2]
				item[predstr] = obj
			data.append(item)
		return data

	@staticmethod
	def __guess_format(st):
		"""Guess the format of the input string."""
		# TODO: At present, it can only guess between XML and n3, even
		# then this is a vague heuristic.
		if st.startswith('<'):
			return 'xml'
		return 'n3'
Пример #9
0
 def testQueryingMore(self):
     for result in self.results:
         uri = result[0]
         g = ConjunctiveGraph()
         g.parse(uri)
         query = Parse("""
                             SELECT ?person
                             WHERE {
                                      <%s> foaf:primaryTopic ?person .
                                      ?person rdf:type foaf:Person . 
                                   }
                       """ % uri )
         queryResults = g.query(query, initNs=NSbindings).serialize('python')
         if (len(queryResults)>0):
             self.assertEquals(str(queryResults[0]), "http://www.wikier.org/foaf#wikier")
Пример #10
0
def testDefaultGraph():
    memStore = plugin.get('IOMemory', Store)()
    graph1 = Graph(memStore, URIRef("graph1"))
    graph2 = Graph(memStore, URIRef("graph2"))
    graph3 = Graph(memStore, URIRef("graph3"))

    for n3Str, graph in [(testGraph1N3, graph1), (testGraph2N3, graph2),
                         (testGraph3N3, graph3)]:
        graph.parse(StringIO(n3Str), format='n3')
    G = ConjunctiveGraph(memStore)
    #test that CG includes triples from all 3
    assert G.query(sparqlQ3), "CG as default graph should *all* triples"
    assert not graph2.query(
        sparqlQ3
    ), "Graph as default graph should *not* include triples from other graphs"
Пример #11
0
 def _discover_meta(self, homepage, candidate):
     self.triples.push_meta(homepage, candidate)
     self.stats.count_rdf()
     logging.debug("Analyzing '%s'" % candidate)
     # FIXME: Not a good idea, think about it.
     if re.match(r".*\.rdf$", candidate) is not None:
         graph = ConjunctiveGraph()
         try:
             graph.parse(candidate)
         except (SAXParseException, RdflibParserError), e:
             self.stats.count_invalidrdf()
             raise RDFDiscoveringMalformedError(str(e), candidate)
         except urllib2.URLError:
             self.stats.count_invalidrdf()
             raise RDFDiscoveringBrokenLinkError(candidate)
def commitData(triples):
    """
    Commits triples to RDF store
  """
    report("INFO: rdflibWrapper.commitData")
    #  default_graph_uri = "http://rdflib.net/rdfstore"

    graph = Graph(store)  # Other optional argument: identifier = rdflib.URIRef(default_graph_uri)
    triples = list(set(triples))  # Deduplication of triples
    report("INFO: adding %d triples" % (len(triples)))
    for triple in triples:
        report("S:%s, P:%s, O:%s" % (str(triple[0]), str(triple[1]), str(triple[2])))

    map(lambda triple: graph.add(triple), triples)  # Add triples to the graph

    graph.commit()  #  Commit newly added triples
Пример #13
0
    def __init__(self,
                 store,
                 edb,
                 derivedPredicates=None,
                 idb=None,
                 DEBUG=False,
                 nsBindings={},
                 templateMap=None,
                 identifyHybridPredicates=False,
                 hybridPredicates=None,
                 existentialsInHeads=False,
                 toldBNode=False,
                 addRIFFacts=False,
                 embedOWL=False):
        self.toldBNode = toldBNode
        self.existentialInHead = existentialsInHeads
        self.dataset = store
        hybridPredicates = hybridPredicates if hybridPredicates else []
        if hasattr(store, '_db'):
            self._db = store._db
        self.idb = idb if idb else set()
        self.edb = edb

        for rifUri in edb.query(RIF_REFERENCE_QUERY):
            try:
                from FuXi.Horn.RIFCore import RIFCoreParser
                print rifUri
                if rifUri in map(lambda i: i.identifier,
                                 ConjunctiveGraph(edb.store).contexts()):
                    if DEBUG:
                        print "RIF in RDF is in named graph %s" % rifUri.n3()
                    rif_parser = RIFCoreParser(graph=Graph(edb.store, rifUri),
                                               debug=DEBUG,
                                               owlEmbeddings=embedOWL)
                else:
                    if DEBUG:
                        print "RIF / XML is remote"
                    rif_parser = RIFCoreParser(location=rifUri,
                                               debug=DEBUG,
                                               owlEmbeddings=embedOWL)
                rules, facts = rif_parser.getRuleset()
                if addRIFFacts and facts:
                    #Add any ground facts in the referenced RIF graph
                    #to the edb
                    if DEBUG:
                        print "Added %s statements from RIF document" % len(
                            facts)
                        print map(BuildUnitermFromTuple, facts)
                    if isinstance(self.edb, ConjunctiveGraph):
                        for fact in facts:
                            self.edb.add(fact)
                    else:
                        self.edb.addN(map(lambda i: (i + (self.edb, )), facts))
                self.idb.update(rules)
            except ImportError, e:
                raise Exception(
                    "Missing 3rd party libraries for RIF processing: %s" % e)
            if DEBUG:
                pprint(list(self.idb))
Пример #14
0
 def testQueryingMore(self):
     for result in self.results:
         uri = result[0]
         g = ConjunctiveGraph()
         g.parse(uri)
         query = Parse("""
                             SELECT ?person
                             WHERE {
                                      <%s> foaf:primaryTopic ?person .
                                      ?person rdf:type foaf:Person . 
                                   }
                       """ % uri)
         queryResults = g.query(query,
                                initNs=NSbindings).serialize('python')
         if (len(queryResults) > 0):
             self.assertEquals(str(queryResults[0]),
                               "http://www.wikier.org/foaf#wikier")
Пример #15
0
class TestSPARQLToldBNodes(unittest.TestCase):
    def setUp(self):
        NS = u"http://example.org/"
        self.graph = ConjunctiveGraph()
        self.graph.parse(StringInputSource("""
           @prefix    : <http://example.org/> .
           @prefix rdf: <%s> .
           @prefix rdfs: <%s> .
           [ :prop :val ].
           [ a rdfs:Class ]."""%(RDF.RDFNS,RDFS.RDFSNS)), format="n3")
    def testToldBNode(self):
        for s,p,o in self.graph.triples((None,RDF.type,None)):
            pass
        query = """SELECT ?obj WHERE { %s ?prop ?obj }"""%s.n3()
        print query
        rt = self.graph.query(query)
        self.failUnless(len(rt) == 1,"BGP should only match the 'told' BNode by name (result set size: %s)"%len(rt))
Пример #16
0
def main(specloc, template, mode="spec"):
    """The meat and potatoes: Everything starts here."""

    m = Graph()
    m.parse(specloc)

#    m = RDF.Model()
#    p = RDF.Parser()
#    p.parse_into_model(m, specloc)
    
    classlist, proplist = specInformation(m)
    
    if mode == "spec":
        # Build HTML list of terms.
        azlist = buildazlist(classlist, proplist)
    elif mode == "list":
        # Build simple <ul> list of terms.
        azlist = build_simple_list(classlist, proplist)

    # Generate Term HTML
#    termlist = "<h3>Classes and Properties (full detail)</h3>"
    termlist = docTerms('Class',classlist,m)
    termlist += docTerms('Property',proplist,m)
    
    # Generate RDF from original namespace.
    u = urllib.urlopen(specloc)
    rdfdata = u.read()
    rdfdata = re.sub(r"(<\?xml version.*\?>)", "", rdfdata)
    rdfdata = re.sub(r"(<!DOCTYPE[^]]*]>)", "", rdfdata)
    rdfdata.replace("""<?xml version="1.0"?>""", "")
    
    # print template % (azlist.encode("utf-8"), termlist.encode("utf-8"), rdfdata.encode("ISO-8859-1"))
    #template = re.sub(r"^#format \w*\n", "", template)
    #template = re.sub(r"\$VersionInfo\$", owlVersionInfo(m).encode("utf-8"), template) 
    
    # NOTE: This works with the assumtpion that all "%" in the template are escaped to "%%" and it
    #       contains the same number of "%s" as the number of parameters in % ( ...parameters here... )

    print "AZlist",azlist
    print "Termlist",termlist
 
#xxx    template = template % (azlist.encode("utf-8"), termlist.encode("utf-8"));    
#    template += "<!-- specification regenerated at " + time.strftime('%X %x %Z') + " -->"
    
    return template
Пример #17
0
def testSPARQLNotEquals():
    NS = u"http://example.org/"
    graph = ConjunctiveGraph()
    graph.parse(StringInputSource("""
       @prefix    : <http://example.org/> .
       @prefix rdf: <%s> .
       :foo rdf:value 1.
       :bar rdf:value 2."""%RDF.RDFNS), format="n3")
    rt = graph.query("""SELECT ?node 
                        WHERE {
                                ?node rdf:value ?val.
                                FILTER (?val != 1)
                               }""",
                           initNs={'rdf':RDF.RDFNS},                           
                           DEBUG=False)
    for row in rt:        
        item = row[0]
        assert item == URIRef("http://example.org/bar")
Пример #18
0
 def setUp(self):
     NS = u"http://example.org/"
     self.graph = ConjunctiveGraph()
     self.graph.parse(StringInputSource("""
        @prefix    : <http://example.org/> .
        @prefix rdf: <%s> .
        @prefix rdfs: <%s> .
        [ :prop :val ].
        [ a rdfs:Class ]."""%(RDF.RDFNS,RDFS.RDFSNS)), format="n3")
Пример #19
0
    def parse(self, source, graph):
        # we're currently being handed a Graph, not a ConjunctiveGraph
        assert graph.store.context_aware # is this implied by formula_aware
        assert graph.store.formula_aware

        conj_graph = ConjunctiveGraph(store=graph.store)
        conj_graph.default_context = graph # TODO: CG __init__ should have a default_context arg
        # TODO: update N3Processor so that it can use conj_graph as the sink
        sink = Sink(conj_graph)
        if False:
            sink.quantify = lambda *args: True
            sink.flatten = lambda *args: True
        baseURI = graph.absolutize(source.getPublicId() or source.getSystemId() or "")
        p = N3Processor("nowhere", sink, baseURI=baseURI) # pass in "nowhere" so we can set data instead
        p.userkeys = True # bah
        p.data = source.getByteStream().read() # TODO getCharacterStream?
        p.parse()
        for prefix, namespace in p.bindings.items():
            conj_graph.bind(prefix, namespace)
Пример #20
0
def testSPARQLNotEquals():
    NS = u"http://example.org/"
    graph = ConjunctiveGraph()
    graph.parse(StringInputSource("""
       @prefix    : <http://example.org/> .
       @prefix rdf: <%s> .
       :foo rdf:value 1.
       :bar rdf:value 2.""" % RDF.RDFNS),
                format="n3")
    rt = graph.query("""SELECT ?node 
                        WHERE {
                                ?node rdf:value ?val.
                                FILTER (?val != 1)
                               }""",
                     initNs={'rdf': RDF.RDFNS},
                     DEBUG=False)
    for row in rt:
        item = row[0]
        assert item == URIRef("http://example.org/bar")
Пример #21
0
 def __init__(self):
     
     self.g = Graph('IOMemory')
     self.g.bind('dc', dublin_core)
     self.g.bind('foaf', FOAF)
     self.g.bind('time-entry', owl_time)
     self.g.bind('letter', letter_ns)
     self.g.bind('owl', owl)
     self.g.bind('ex', exam)
     self.g.bind('geo', geo)
     self.g.bind('base', base_uri)
Пример #22
0
 def __init__(self, size, prefix, base):
     self.prefix = "%s/%s" % (base, prefix)
     self.pool = [ConjunctiveGraph() for i in range(int(size))]
     for graph in self.pool:
         graph.bind("deb", DEB)
         graph.bind("xhv", XHV)
         graph.bind("earl", EARL)
         graph.bind("dc", DC)
         graph.bind("content", CONTENT)
         graph.bind("r", R)
         graph.bind("rss", RSS)
Пример #23
0
	def __init__(self, rdf, format='guess'):
		"""Init the parser with the graph string."""
		self.graph = Graph()
		if format == 'guess':
			format = self.__guess_format(rdf)
			print 'RdfParser guesses format to be: %s' % format
		try:
			self.graph.load(StringIO(rdf), format=format)
		except:
			print "Failed to parse RDF:"
			print rdf[0:100]
Пример #24
0
def testAggregateSPARQL():
    memStore = plugin.get('IOMemory', Store)()
    graph1 = Graph(memStore, URIRef("graph1"))
    graph2 = Graph(memStore, URIRef("graph2"))
    graph3 = Graph(memStore, URIRef("graph3"))

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

    graph4 = Graph(memStore, RDFS.RDFSNS)
    graph4.parse(RDFS.RDFSNS)
    G = ConjunctiveGraph(memStore)
    rt = 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 = G.query(sparqlQ2, initBindings={u'?graph': URIRef("graph3")})
    #print rt.serialize(format='json')
    assert rt.serialize('python')[0] == LOG_NS.N3Document, str(rt)
Пример #25
0
    def __init__(self):

        self.g = Graph('IOMemory')
        self.g.bind('dc', dublin_core)
        self.g.bind('foaf', FOAF)
        self.g.bind('time-entry', owl_time)
        self.g.bind('letter', letter_ns)
        self.g.bind('owl', owl)
        self.g.bind('ex', exam)
        self.g.bind('geo', geo)
        self.g.bind('base', base_uri)
Пример #26
0
def getRdfXml(rdf):
    n3 = ""
    
    # Append the RDF namespace and print the prefix namespace mappings
    rdf['namespaces']['xh1'] = "http://www.w3.org/1999/xhtml/vocab#"
    rdf['namespaces']['rdf'] = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    for prefix, uri in rdf['namespaces'].items():
        n3 += "@prefix %s: <%s> .\n" % (prefix, uri)
        
    # Print each subject-based triple to the screen
    triples = rdf['triples']
    processed = []

    # Get all of the non-bnode subjects
    nonBnodeSubjects = getNonBnodeSubjects(triples)

    # Get all of the bnode subjects
    bnodeSubjects = getBnodeSubjects(triples)

    for subject in nonBnodeSubjects:
        subjectTriples = getTriplesBySubject(subject, triples)
        #print "PROCESSING NB SUBJECT:", subjectTriples

        if(subject not in processed):
            n3 += tripleToN3(subjectTriples, processed, triples)
        processed.append(subject)

    for subject in bnodeSubjects:
        subjectTriples = getTriplesBySubject(subject, triples)
        #print "PROCESSING BN SUBJECT:", subject
        if(subject not in processed):
            n3 += bnodeToN3(subjectTriples, processed, triples)
            n3 += " .\n"

    #print n3

    g = ConjunctiveGraph()
    g.parse(StringIO(n3), format="n3")
    rdfxml = g.serialize()

    return rdfxml
Пример #27
0
def testAggregateSPARQL():
    memStore = plugin.get('IOMemory',Store)()
    graph1 = Graph(memStore,URIRef("graph1"))
    graph2 = Graph(memStore,URIRef("graph2"))
    graph3 = Graph(memStore,URIRef("graph3"))

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

    graph4 = Graph(memStore,RDFS.RDFSNS)
    graph4.parse(RDFS.RDFSNS)
    G = ConjunctiveGraph(memStore)
    rt =  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=G.query(sparqlQ2,initBindings={u'?graph' : URIRef("graph3")})
    #print rt.serialize(format='json')
    assert rt.serialize('python')[0] == LOG_NS.N3Document,str(rt)
Пример #28
0
def check_type_definitions(vocabfile):
    graph = Graph()
    try:
        graph.parse(vocabfile)
    except:
        return False
    all_definitions = True

    testo = vocab_type_definitions_test['rdfs']
    subjects = []
    subs = graph.subjects(namespaces['rdf']['type'], URIRef(testo))
    for s in subs:
        subjects.append(s)
    if subjects:
        objects = vocab_type_definitions_rdfs
    else:
        objects = vocab_type_definitions_owl
    for o in objects:
        subs = graph.subjects(namespaces['rdf']['type'], o)
        done = []
        for s in subs:
            if s in done:
                continue
            done.append(s)
            definition = False
            vals = graph.objects(s, namespaces['rdfs']['isDefinedBy'])
            for val in vals:
                definition = True
            all_definitions = all_definitions and definition
    return all_definitions
def check_type_definitions(vocabfile):
    graph = Graph()
    try:
        graph.parse(vocabfile)
    except:
        return False
    all_definitions = True
    
    testo = vocab_type_definitions_test['rdfs']
    subjects = []
    subs = graph.subjects(namespaces['rdf']['type'], URIRef(testo))
    for s in subs:
        subjects.append(s)
    if subjects:
        objects = vocab_type_definitions_rdfs
    else:
        objects = vocab_type_definitions_owl
    for o in objects: 
        subs = graph.subjects(namespaces['rdf']['type'], o)
        done = []
        for s in subs:
            if s in done:
               continue
            done.append(s) 
            definition = False
            vals = graph.objects(s, namespaces['rdfs']['isDefinedBy'])
            for val in vals:
               definition = True
            all_definitions = all_definitions and definition
    return all_definitions
Пример #30
0
    def parse(self, source, sink, **args):
        assert sink.store.context_aware
        g = ConjunctiveGraph(store=sink.store)

        self._parser = create_parser(g)
        content_handler = self._parser.getContentHandler()
        preserve_bnode_ids = args.get("preserve_bnode_ids", None)
        if preserve_bnode_ids is not None:
            content_handler.preserve_bnode_ids = preserve_bnode_ids
        # We're only using it once now
        #content_handler.reset()
        #self._parser.reset()
        self._parser.parse(source)
Пример #31
0
    def testModel(self):
        g = ConjunctiveGraph()
        g.parse(StringInputSource(input), format="n3")
        i = 0
        for s, p, o in g:
            if isinstance(s, Graph):
                i += 1
        self.assertEquals(i, 3)
        self.assertEquals(len(list(g.contexts())), 13)

        g.close()
Пример #32
0
    def __convert_to_triples(self, model):
        """Converts each model/query result into its own graph to be
		merged the graph returned by to_rdf()."""
        sylph = self.NAMESPACES["sylph"]

        graph = Graph()
        graphNum = len(self.subgraphs)

        node = URIRef(model.uri)  # The Resource
        graph.add((node, RDF.type, sylph[model.get_rdf_class()]))  # Datatype

        data = model.get_transportable()

        # Performs graph.add(sub, pred, obj)
        for k, v in data.iteritems():
            obj = None
            if k == "uri":
                continue  # already done

                # Blank values transported because we may be 'erasing' them
            if not v:
                if type(v) in [str, unicode]:
                    obj = Literal("")
                if type(v) is NoneType:
                    obj = sylph["None"]

            if isinstance(v, (models.Model, Resource, ResourceTree)):
                # TODO/XXX: This is slow as hell. Hits the database every
                # single time this codepath is reached.
                # For now, forget performance. Work on this later...
                obj = URIRef(v.uri)  # XXX: Wouldn't this work with just hasattr?

            if not obj:
                obj = Literal(v)  # Handles int, float, etc.

            graph.add((node, sylph[k], obj))

        self.subgraphs.append(graph)
Пример #33
0
def OpenGraph(storeType, configStr, graphUri, storeName='rdfstore'):
    # Get the mysql plugin. You may have to install the python mysql libraries
    store = plugin.get(storeType, Store)(storeName, debug=False, perfLog=True)

    # Open previously created store, or create it if it doesn't exist yet
    rt = store.open(configStr, create=False)
    assert rt != NO_STORE, "'%s' store '%s' not found using config string '%s!'" % (
        storeType, storeName, configStr)
    assert rt == VALID_STORE or rt is not None, "There underlying store is corrupted"

    #There is a store, use it; use ConjunctiveGraph to see everything!
    graph = ConjunctiveGraph(store, identifier=URIRef(graphUri))

    return graph
Пример #34
0
def store_ontology( default_graph_uri ):
      #  graph_local = Graph( identifier = URIRef( default_graph_uri ) )
        print "Preparing to read graph: " + default_graph_uri + "\n"
     #   graph_local.parse( default_graph_uri )
      #  print "Read Graph saving to database"
        configString = "host=dbclass,user=brandon,password=b2341x,db=portal"
        store = plugin.get( 'MySQL', Store)('rdflib')
        rt = store.open(configString, create=False)
        if rt == NO_STORE:
                store.open(configString, create=True)
        else:
                assert rt == VALID_STORE, "The underlying store is not valid"
        print "Opened Store"
        graph = Graph(store, identifier = URIRef( default_graph_uri ))
#        graph = Graph( identifier = URIRef( default_graph_uri ))       
        # for stmt in graph_local:
        #        graph.add( stmt ) 
        graph.parse( default_graph_uri )
        print "Committing Graph to database\n"
        graph.commit()
        print "Saved Graph to database\n"
        store.close()
        return len( graph )
Пример #35
0
def extendGraphFromFile(inGraph, graphFile, format='n3'):
    """
    Add all the triples in graphFile to inGraph

    This is done as if the loaded graph is the same context as this
    database's graph, which means <> from the loaded graph will be
    modified to mean <> in the new context
    """
    g2 = Graph()

    # Generate a random publicID, then later throw it away, by
    # replacing references to it with URIRef('').  extendGraphFromFile thus
    # treats the inserted file as if it were part of the original file
    publicID = randomPublicID()
    g2.load(graphFile, format=format, publicID=publicID)

    # add each triple
    # FIXME - this should use addN
    for s,v,o in g2:
        if s == URIRef(publicID):
            inGraph.add((URIRef(''), v, o))
        else:
            inGraph.add((s,v,o))
def del_vocab_from_creator(userid, vocab):
    if not os.path.isfile(os.path.join(ag.creatorsdir, '%s.rdf'%userid)):
        return False
    graph = Graph()
    graph.parse(os.path.join(ag.creatorsdir, '%s.rdf'%userid))
    vocab_uri = URIRef("http://vocab.ox.ac.uk/%s"%vocabprefix)
    for s, p, o in graph.triples((URIRef(vocab_uri), namespaces['dcterms']['mediator'], None)):
        graph.remove((s, p, o))
    rdf_str = None
    rdf_str = graph.serialize()
    f = codecs.open(creatorfile, 'w', 'utf-8')
    f.write(rdf_str)
    f.close()
    return True 
Пример #37
0
    def testModel(self):
        g = ConjunctiveGraph()
        g.parse(StringInputSource(input), format="n3")
        i = 0
        for s, p, o in g:
            if isinstance(s, Graph):
                i += 1
        self.assertEquals(i, 3)
        self.assertEquals(len(list(g.contexts())), 13)

        g.close()
Пример #38
0
    def open(self, filename, graphClass=None):
        """
        Load existing database at 'filename'.
        """
        if filename is None:
            if graphClass is None:
                self.graph = Graph()
            else:
                self.graph = graphClass()
        else:
            assert os.path.exists(filename), (
                    "%s must be an existing database" % (filename,))

            path, filename = os.path.split(filename)
            self.graph = sqliteBackedGraph(path, filename)

        self._open = True
Пример #39
0
def del_vocab_from_creator(userid, vocab):
    if not os.path.isfile(os.path.join(ag.creatorsdir, '%s.rdf' % userid)):
        return False
    graph = Graph()
    graph.parse(os.path.join(ag.creatorsdir, '%s.rdf' % userid))
    vocab_uri = URIRef("http://vocab.ox.ac.uk/%s" % vocabprefix)
    for s, p, o in graph.triples(
        (URIRef(vocab_uri), namespaces['dcterms']['mediator'], None)):
        graph.remove((s, p, o))
    rdf_str = None
    rdf_str = graph.serialize()
    f = codecs.open(creatorfile, 'w', 'utf-8')
    f.write(rdf_str)
    f.close()
    return True
Пример #40
0
 def closureGraph(self,sourceGraph,readOnly=True,store=None):
     if readOnly:
         if store is None and not sourceGraph:
             store = Graph().store
         store = store is None and sourceGraph.store or store
         roGraph=ReadOnlyGraphAggregate([sourceGraph,self.inferredFacts],
                                        store=store)
         roGraph.namespace_manager = NamespaceManager(roGraph)
         for srcGraph in [sourceGraph,self.inferredFacts]:
             for prefix,uri in srcGraph.namespaces():
                 roGraph.namespace_manager.bind(prefix,uri)
         return roGraph
     else:
         cg=ConjunctiveGraph()
         cg+=sourceGraph
         cg+=self.inferredFacts
         return cg        
Пример #41
0
 def testModel(self):
     print 'Probando la función testModel\n_____________________________'
     g = ConjunctiveGraph()
     g.parse(StringInputSource(input), format="n3")
     i = 0
     for s, p, o in g:
         if isinstance(s, Graph):
             i += 1
             print i
     #self.assertEquals(i, 3)
     #self.assertEquals(len(list(g.contexts())), 13)
     #print g.serialize()
     g.close()
Пример #42
0
def FixViewsString(configStr, storeName='rdfstore'):
    # Get the mysql plugin. You may have to install the python mysql libraries
    store = plugin.get('MySQL', Store)(storeName,debug=False,perfLog=True)
    
    # Open previously created store, or create it if it doesn't exist yet
    rt = store.open(configStr,create=False)
    if rt == NO_STORE:
        #There is no underlying MySQL infrastructure, create it
        #store.open(configStr,create=True)
        
        #TODO: could create store & load appropriate data here
        assert False, "'%s' store '%s' not found using config string '%s!'" % (storeType, storeName, configStr) 
    else:
        assert rt == VALID_STORE,"There underlying store is corrupted"
        
    #There is a store, use it; use ConjunctiveGraph to see everything!
    graph = ConjunctiveGraph(store, identifier = URIRef(graphUri))
    
    FixViewsGraph(graph)    
Пример #43
0
 def load_store(files):
     """
    Takes a directory of RDf files and loads them into the store.
    """
     try:
         store = plugin.get("MySQL", Store)("rdflib_db")
         store.open(config["rdflib.config"])
         graph = ConjunctiveGraph(store)
         # iterate through files and load them into the graph
         for fpath in fl:
             graph.parse(fpath, format=get_format(fpath), publicID=context_uri(fpath))
             print fpath + " loaded."
         # save triples to store
         graph.commit()
         graph.close()
     except:
         print "=== error opening RDF store ==="
         exit
fq = movie_graph.query("""SELECT ?film ?act ?perf ?an ?fn WHERE
                        {?film fb:film.film.performances ?perf .
                         ?perf fb:film.performance.actor ?act . 
                         ?act dc:title ?an.                              
                         ?film dc:title ?fn .                              
                         }""",
                       initNs={
                           'fb': FB,
                           'dc': DC
                       })

graphs = {}
for film, act, perf, an, fn in fq:
    filmid = fn.split(',')[0].replace(' ', '_') + '_' + str(film).split('=')[1]
    actid = an.replace(' ', '_') + '_' + str(act).split('=')[1]

    graphs.setdefault(filmid, ConjunctiveGraph())
    graphs.setdefault(actid, ConjunctiveGraph())

    graphs[filmid].add((film, FB['film.film.performance.actor'], act))
    graphs[filmid].add((act, RDFS['seeAlso'], actid + '.xml'))
    graphs[filmid].add((film, DC['title'], fn))

    graphs[actid].add((act, FB['film.actor.performance.film'], film))
    graphs[actid].add((film, RDFS['seeAlso'], filmid + '.xml'))
    graphs[actid].add((act, DC['title'], an))

for id, graph in graphs.items():
    graph.serialize('open_films/%s.xml' % id)
Пример #45
0
def localGraph():
    g = ConjunctiveGraph()
    g.add((EXP['dp'], EXP['firstName'], Literal('Drew')))
    g.add((EXP['labeled'], RDFS.label, Literal('Labeled')))
    return g
Пример #46
0
def testN3Store(store="default", configString=None):
    g = ConjunctiveGraph(store=store)
    if configString:
        g.destroy(configString)
        g.open(configString)
    g.parse(StringInputSource(testN3), format="n3")
    print g.store
    try:
        for s, p, o in g.triples((None, implies, None)):
            formulaA = s
            formulaB = o

        assert type(formulaA) == QuotedGraph and type(formulaB) == QuotedGraph
        a = URIRef('http://test/a')
        b = URIRef('http://test/b')
        c = URIRef('http://test/c')
        d = URIRef('http://test/d')
        v = Variable('y')

        universe = ConjunctiveGraph(g.store)

        #test formula as terms
        assert len(list(universe.triples((formulaA, implies, formulaB)))) == 1

        #test variable as term and variable roundtrip
        assert len(list(formulaB.triples((None, None, v)))) == 1
        for s, p, o in formulaB.triples((None, d, None)):
            if o != c:
                assert isinstance(o, Variable)
                assert o == v
        s = list(universe.subjects(RDF.type, RDFS.Class))[0]
        assert isinstance(s, BNode)
        assert len(list(universe.triples((None, implies, None)))) == 1
        assert len(list(universe.triples((None, RDF.type, None)))) == 1
        assert len(list(formulaA.triples((None, RDF.type, None)))) == 1
        assert len(list(formulaA.triples((None, None, None)))) == 2
        assert len(list(formulaB.triples((None, None, None)))) == 2
        assert len(list(universe.triples((None, None, None)))) == 3
        assert len(
            list(formulaB.triples((None, URIRef('http://test/d'), None)))) == 2
        assert len(
            list(universe.triples((None, URIRef('http://test/d'), None)))) == 1

        #context tests
        #test contexts with triple argument
        assert len(list(universe.contexts((a, d, c)))) == 1

        #Remove test cases
        universe.remove((None, implies, None))
        assert len(list(universe.triples((None, implies, None)))) == 0
        assert len(list(formulaA.triples((None, None, None)))) == 2
        assert len(list(formulaB.triples((None, None, None)))) == 2

        formulaA.remove((None, b, None))
        assert len(list(formulaA.triples((None, None, None)))) == 1
        formulaA.remove((None, RDF.type, None))
        assert len(list(formulaA.triples((None, None, None)))) == 0

        universe.remove((None, RDF.type, RDFS.Class))

        #remove_context tests
        universe.remove_context(formulaB)
        assert len(list(universe.triples((None, RDF.type, None)))) == 0
        assert len(universe) == 1
        assert len(formulaB) == 0

        universe.remove((None, None, None))
        assert len(universe) == 0

        g.store.destroy(configString)
    except:
        g.store.destroy(configString)
        raise
Пример #47
0
 def testParse(self):
     g = ConjunctiveGraph()
     g.parse("http://groups.csail.mit.edu/dig/2005/09/rein/examples/troop42-policy.n3", format="n3")
Пример #48
0
# Get the sqlite plugin. You may have to install the python sqlite libraries
store = plugin.get('SQLite', Store)('rdfstore.db')

# Open previously created store, or create it if it doesn't exist yet
try:
    rt = store.open(configString,create=False)
except OperationalError, e:
    try:
        # There is no underlying sqlite infrastructure, create it
        rt = store.open(configString,create=True)
        assert rt == VALID_STORE
    except OperationalError, e:
        raise
        import sys, pdb; pdb.post_mortem(sys.exc_info()[2])
 
# There is a store, use it
graph = Graph(store, identifier = URIRef(default_graph_uri))

print "Triples in graph before add: ", len(graph)

# Now we'll add some triples to the graph & commit the changes
rdflibNS = Namespace('http://rdflib.net/test/')
graph.add((rdflibNS['pic:1'], rdflibNS['name'], Literal('Jane & Bob')))
graph.add((rdflibNS['pic:2'], rdflibNS['name'], Literal('Squirrel in Tree')))
graph.commit()

print "Triples in graph after add: ", len(graph)

# display the graph in RDF/XML
print graph.serialize()
Пример #49
0
from rdflib.Graph import ConjunctiveGraph
from rdflib import Namespace, BNode, Literal, RDF, URIRef
import csv
import pysesame

JB = Namespace("http://semprog.com/schemas/jobboard#")
GEO = Namespace('http://www.w3.org/2003/01/geo/wgs84_pos#')

lg = ConjunctiveGraph()
lg.bind('geo', GEO)

for city, lat, long in csv.reader(file('city_locations.csv', 'U')):
    lg.add((JB[city], GEO['lat'], Literal(float(lat))))
    lg.add((JB[city], GEO['long'], Literal(float(long))))

data = lg.serialize(format='xml')
print data
c = pysesame.connection('http://semprog.com:8280/openrdf-sesame/')
c.use_repository('joblistings')
print c.postdata(data)
Пример #50
0
    def toRDF(self):
        """
        Print a message into RDF in XML format
        """

        #rdf graph
        store = ConjunctiveGraph()

        #namespaces
        store.bind('sioc', SIOC)
        store.bind('foaf', FOAF)
        store.bind('rdfs', RDFS)
        store.bind('dc', DC)
        store.bind('dct', DCT)

        #message node
        message = URIRef(self.getUri())
        store.add((message, RDF.type, SIOC["Post"]))

        #document node
        doc = URIRef(self.getUri() + '.rdf')
        store.add((doc, RDF.type, FOAF["Document"]))
        store.add((doc, FOAF["primaryTopic"], message))

        try:

            store.add((message, SIOC['id'], Literal(self.getSwamlId())))
            store.add((message, SIOC['link'], URIRef(self.getXhtmlUrl())))
            store.add((message, SIOC['has_container'],
                       URIRef(self.config.get('base') + 'forum')))
            store.add((message, SIOC["has_creator"],
                       URIRef(self.getSender().getUri())))
            store.add((message, DC['title'], Literal(self.getSubject())))
            store.add((message, DCT['created'],
                       Literal(self.getDate(), datatype=XSD[u'dateTime'])))

            parent = self.getParent()
            if (parent != None):
                store.add((message, SIOC['reply_of'], URIRef(parent)))

            if (len(self.childs) > 0):
                for child in self.childs:
                    store.add((message, SIOC['has_reply'], URIRef(child)))

            previous = self.getPreviousByDate()
            if (previous != None):
                store.add(
                    (message, SIOC['previous_by_date'], URIRef(previous)))

            next = self.getNextByDate()
            if (next != None):
                store.add((message, SIOC['next_by_date'], URIRef(next)))

            store.add((message, SIOC['content'], Literal(self.getBody())))

        except Exception, detail:
            print 'Error proccesing message ' + str(
                self.getId()) + ': ' + str(detail)
Пример #51
0
class rdf_transform:
    def __init__(self):

        self.g = Graph('IOMemory')
        self.g.bind('dc', dublin_core)
        self.g.bind('foaf', FOAF)
        self.g.bind('time-entry', owl_time)
        self.g.bind('letter', letter_ns)
        self.g.bind('owl', owl)
        self.g.bind('ex', exam)
        self.g.bind('geo', geo)
        self.g.bind('base', base_uri)

    def create_rdf_letter(self, letters):
        '''
          creates an rdf representation of letter used to load into the triple store
        '''
        for l in letters:
            correspondence = base_uri + "letters/resource/" + l.type + '/' + urllib.quote(
                l.correspondent) + '/' + str(l.id) + '/rdf'
            self.add_author(correspondence, "Charles Dickens")
            self.add_subject(correspondence, "letter")
            self.add_time(correspondence, str(l.letter_date) + 'T00:00:00')
            self.add_correspondent(correspondence, l.correspondent)

            #self.add_place(correspondence, parse_text.find_geographical(l.letter_text))
            place = ''
            try:
                place = str(l.letter_place)
            #unicode errors are text related
            except UnicodeError:
                pass

            if place is not '':
                self.add_place(correspondence, place)

            self.add_letter_text(correspondence, l.letter_text)
            self.add_salutation(correspondence, l.correspondent, l.salutation)

            #for line in l.letter_text.splitlines():
            #    if len(line.strip()) > 1:
            #        self.add_open(correspondence, parse_text.parse_salutation_line(line))
            #this section will parse for proper names in due course
            #commented out whilst code is being ported
            #letter_name = parse_text.parseProperNames(text)
            # print"names, ", letter_name

            #for name in letter_name:
            #    letter_rdf += "<letter:personReferred>%s</letter:personReferred>" %(name)

            letter_quotes = parse_text.parse_balanced_quotes(l.letter_text)
            for quote in letter_quotes:
                if str(quote[0:1]).isupper and "!" not in quote:
                    if quote == "ALL THE YEAR ROUND" or quote == "HOUSEHOLD WORDS" or quote == "Household Words":
                        self.add_magazine(correspondence,
                                          parse_text.stripPunc(quote))
                    else:
                        self.add_text(correspondence,
                                      parse_text.stripPunc(quote))

        letter_rdf = self.g.serialize(format="pretty-xml", max_depth=3)
        return letter_rdf

    def create_rdf_end(self):
        ''' function to create an endpoint in rdf/xml '''
        correspondence = base_uri

        letter = {}
        letter = dbase.get_endpoint_rdf()

        letter_items = letter.items()
        letter_items.sort()

        works = set()
        works = dbase.get_books()

        for url, text in letter_items:

            try:
                correspondence = base_uri + "letters/resource/dickens/" + urllib.quote(
                    str(text[1])) + '/' + str(url) + '/rdf'
                self.add_author(correspondence, "Charles Dickens")
                self.add_subject(correspondence, "letter")
                self.add_subject(correspondence, "Charles Dickens")
                self.add_subject(correspondence,
                                 parse_text.camel_case(str(text[1])))
                self.add_time(correspondence, str(text[3]) + 'T00:00:00')
                self.add_correspondent(correspondence, str(text[1]))
                self.add_salutation(correspondence, urllib.quote(str(text[1])),
                                    str(text[4]))
                place = str(text[5])
                #for line in str(text[2]).splitlines():
                #    self.add_open(correspondence, parse_text.parse_salutation_line(str(text[2])))
                letter = str(text[2])
            #unicode errors are text related
            except UnicodeError:
                pass
            if place is not None:
                self.add_place(correspondence, place)

            self.add_letter_text(correspondence, letter)

            #this section will parse for proper names in due course
            #commented out whilst code is being ported
            #letter_name = parse_text.parseProperNames(text)
            # print"names, ", letter_name

            letter_quotes = parse_text.parse_balanced_quotes(text[2])
            for quote in letter_quotes:
                work = parse_text.stripPunc(quote)

                #TODO: Normalise the text to reduce code repetition
                periodicals = set([
                    'All The Year Round', 'Household Words', 'The Daily News'
                ])
                #print "quote", parse_text.stripPunc(quote)
                if quote in periodicals:
                    self.add_magazine(correspondence, quote)

                if work in works:
                    if work == "Copperfield":
                        work = "David Copperfield"
                    elif work == "Nickleby":
                        work = "Nicholas Nickleby"
                    elif work == "Edwin Drood":
                        work = "The Mystery of Edwin Drood"
                    elif work == "Dombey":
                        work = "Dombey and Son"
                    elif work == "Tale of Two Cities":
                        work = "A Tale of Two Cities"
                    elif work == "Christmas Carol":
                        work = "A Christmas Carol"

                    self.add_text(correspondence, work)

        letter_rdf = self.g.serialize(format="pretty-xml", max_depth=3)
        return letter_rdf

    def create_correspondent(self, corr, letter_items):
        u_corr = unicode(corr)

        correspondence = base_uri + "correspondent/resource/" + urllib.quote(
            corr)
        self.add_subject(correspondence, "correspondent")
        #self.add_correspondent(correspondence, corr)

        for url, text in letter_items:
            if url is not None or url != '':
                self.add_salutation(correspondence, corr, str(url))
        #need rules to define relationships - family, authors
        if u_corr == "Miss Hogarth":
            self.add_subject(correspondence, "daughter")
            self.add_daughter(correspondence, "Charles Dickens")
            self.add_sameas(correspondence,
                            "http://dbpedia.org/page/Georgina_Hogarth")

        letter_rdf = self.g.serialize(format="pretty-xml", max_depth=3)

        return letter_rdf

    def create_publication(self, title, type):
        books_set = {}
        start = ''
        end = ''
        abstract = ''
        uri_str = ''
        source = ''

        books = dbase.get_book_rdf(title)
        book_items = books.items()
        book_items.sort()

        for u, book in book_items:

            title = u
            start = book[0]
            end = book[1]
            abstract = book[2]
            uri_str = book[3]
            source = book[4]
            #create a books dictionary as a list of records to build a list of uris from
            # title => uri string
            books_set[u] = uri_str

            if ":" in u:
                for bk in u.split(":"):
                    books_set[bk[0]] = uri_str

            if "The " in u or "A " in u:
                aka = u.replace("The ", "").replace("A ", "")
                books_set[aka] = uri_str

        correspondence = base_uri + type + "/resource/" + title.strip(
        ).replace(" ", "_")
        self.add_subject(correspondence, type)
        self.add_subject(correspondence, "Charles Dickens")
        self.add_author(correspondence, "Charles Dickens")
        self.add_time(correspondence, start)
        self.add_time(correspondence, end)
        self.add_title(correspondence, title)

        self.add_abstract(correspondence, abstract)
        uri = u"http://dbpedia.org/page/" + uri_str
        self.add_sameas(correspondence, uri)

        if type == "book":
            source_uri = "http://gutenberg.org/ebooks/" + source
            self.add_sameas(correspondence, source_uri)

        letter_rdf = self.g.serialize(format="pretty-xml", max_depth=3)

        return letter_rdf

    def create_place(self, placeobj):
        (lat, long, place_name, source) = ('', '', '', '')

        for location in placeobj:
            place_name = location.placeid
            lat = location.latitude
            long = location.longitude
            source = location.source

        correspondence = base_uri + "place/resource/" + urllib.quote(
            place_name) + "/rdf"
        self.add_latitude(correspondence, lat)
        self.add_longitude(correspondence, long)
        self.add_place_name(correspondence, place_name)
        #self.add_description(correspondence, place_abstract)
        self.add_sameas(correspondence, source)

        letter_rdf = self.g.serialize(format="pretty-xml", max_depth=3)

        return letter_rdf

    def create_author(self, data):
        '''
           function to return an author graph in rdf
        '''
        author = u"Charles Dickens"
        subject = u"author"
        born = u"1812-02-07"
        died = u"1870-06-09"
        abstract = u"Charles John Huffam Dickens, pen-name 'Boz', was the most popular English novelist of the Victorian era, and one of the most popular of all time, responsible for some of English literature's most iconic characters. Many of his novels, with their recurrent theme of social reform, first appeared in periodicals and magazines in serialised form, a popular format for fiction at the time. Unlike other authors who completed entire novels before serial production began, Dickens often wrote them while they were being serialized, creating them in the order in which they were meant to appear. The practice lent his stories a particular rhythm, punctuated by one 'cliffhanger' after another to keep the public looking forward to the next installment. The continuing popularity of his novels and short stories is such that they have never gone out of print. His work has been praised for its mastery of prose and unique personalities by writers such as George Gissing and G. K. Chesterton, though the same characteristics prompted others, such as Henry James and Virginia Woolf, to criticize him for sentimentality and implausibility."
        author_url = u"http://en.wikipedia.org/wiki/Charles_Dickens"

        correspondence = base_uri + "author/resource/" + author
        self.add_subject(correspondence, "Charles Dickens")
        self.add_subject(correspondence, "author")
        self.add_nick(correspondence, "Boz")
        self.add_time(correspondence, born)
        self.add_time(correspondence, died)
        self.add_abstract(correspondence, abstract)
        self.add_sameas(correspondence, author_url)

        letter_rdf = self.g.serialize(format="pretty-xml", max_depth=3)

        return letter_rdf

    def add_author(self, correspondence, name):
        ''' function to add author to graph '''
        dc_author = urllib.quote(name)
        lauthor = URIRef(base_uri + 'author/resource/%s' % dc_author) + "/rdf"
        self.g.add((correspondence, dublin_core['creator'], lauthor))
        #self.g.add((correspondence, dublin_core['creator'], Literal(name)))
        return lauthor

    def add_salutation(self, correspondence, author, name):
        ''' function to add salutation to graph '''
        nameid = urllib.quote(author)
        person = URIRef(base_uri +
                        'correspondent/resource/%s' % nameid) + "/rdf"
        #self.g.add((person, RDF.type, FOAF['nick']))
        self.g.add((correspondence, FOAF['nick'], Literal(name)))

        return person

    def add_correspondent(self, correspondence, name):
        ''' function to add correspondent to graph '''
        nameid = urllib.quote(name)
        person = URIRef(base_uri +
                        'correspondent/resource/%s' % nameid) + "/rdf"
        self.g.add((correspondence, letter_ns["correspondent"], person))
        #self.g.add((person, Letter, Literal(name)))

        return person

    def add_magazine(self, correspondence, name):
        ''' function to add magazine to graph '''
        nameid = urllib.quote(name)
        magazine = URIRef(base_uri + 'magazine/resource/%s' % nameid) + "/rdf"
        self.g.add((correspondence, letter_ns['textReferred'], magazine))
        #self.g.add((person, Letter, Literal(name)))

        return magazine

    def add_text(self, correspondence, textname):
        ''' function to add referred text to the graph'''
        textid = base_uri + "book/resource/" + textname.replace(
            "\n", "_").replace(" ", "_") + "/rdf"
        return self.g.add(
            (correspondence, letter_ns['textReferred'], URIRef(textid)))

    def add_author_text(self, correspondence, textname):
        ''' function to add author referred text to the graph'''
        textid = urllib.quote(textname)
        self.g.add((correspondence, letter_ns['textAuthorReferred'],
                    Literal(textname)))
        return book

    def add_place(self, correspondence, place):
        return self.g.add(
            (correspondence, dublin_core['date'], Literal(str(time))))

    def add_subject(self, correspondence, subject):
        return self.g.add(
            (correspondence, dublin_core['subject'], Literal(subject)))

    def add_sameas(self, correspondence, link):
        return self.g.add((correspondence, owl['sameAs'], URIRef(link)))

    def add_time(self, correspondence, time):
        ''' function to add time '''
        return self.g.add(
            (correspondence, dublin_core['date'], Literal(str(time))))

    def add_title(self, correspondence, title):
        return self.g.add(
            (correspondence, dublin_core['title'], Literal(title)))

    def add_nick(self, correspondence, nick):
        return self.g.add((correspondence, FOAF['nick'], Literal(nick)))

    def add_place(self, correspondence, place):
        return self.g.add((correspondence, dublin_core['title'],
                           URIRef(base_uri + "place/resource/" +
                                  urllib.quote(place) + "/rdf")))

    def add_daughter(self, correspondence, author):
        return self.g.add(
            (correspondence, exam['daughter'],
             URIRef(base_uri + "author/resource/" + author + "/rdf")))

    def add_letter_text(self, correspondence, letter_text):
        return self.g.add(
            (correspondence, letter_ns['Text'], Literal(letter_text)))

    def add_longitude(self, correspondence, long):
        return self.g.add((correspondence, geo['long'], Literal(long)))

    def add_latitude(self, correspondence, lat):
        return self.g.add((correspondence, geo['lat'], Literal(lat)))

    def add_description(self, correspondence, abstract):
        return self.g.add((correspondence, geo['desc'], Literal(abstract)))

    def add_place_name(self, correspondence, name):
        return self.g.add((correspondence, geo['name'], Literal(name)))

    def add_abstract(self, correspondence, letters):
        return self.g.add(
            (correspondence, letter_ns['text'], Literal(letters)))

    def add_open(self, correspondence, letters):
        return self.g.add(
            (correspondence, letter_ns['open'], Literal(letters)))

    def add_close(self, correspondence, letters):
        return self.g.add(
            (correspondence, letter_ns['close'], Literal(letters)))
Пример #52
0
def schemadoc(uris): 
   G = Graph()
   for uri in uris: 
      G.parse(uri)

   print """
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Schema Documentation</title>
<style type="text/css">
body { margin: 1em; font-family: Georgia, sans-serif; }
h1 { font-family: Tahoma, sans-serif; }
h2, h3, h4, h5, h6 { font-family: Arial, sans-serif; }
a { font-weight: bold; color: #036; }
dt.class { margin-top: 0.75em; }
dt.property { margin-top: 0.75em; }
address { padding-top: 0.35em; border-top: 2px solid #369; }
</style>
</head>
<body>
<h1>Schema Documentation</h1>
"""
   classes = []
   for metaclass in [RDFS.Class, OWL.Class]: 
      for uri in G.subjects(RDF.type, metaclass): 
         if not isinstance(uri, URIRef): continue

         c = Class(uri)
         c.classes = [Class(u) for u in G.objects(uri, RDFS.subClassOf)
                      if isinstance(u, URIRef)]
         for prop in G.subjects(RDFS.domain, uri): 
            p = Property(prop)
            ranges = [Class(u) for u in G.objects(prop, RDFS.range)]
            c.properties.append((p, ranges))
         # c.properties = [Property(u) for u in G.subjects(RDFS.domain, uri)]
         c.comments = [str(s) for s in G.objects(uri, RDFS.comment)]
         classes.append(c)

   print '<h2>Classes</h2>'
   print '<ul>'
   for c in sorted(classes): 
      print '<li>'
      print '<dl>'
      print '<dt class="class">'
      sys.stdout.write(c.name())

      if c.classes: 
         o = ', '.join(cls.name(format='text') for cls in sorted(c.classes))
         print '(' + o + ')'
      else: print
      print '</dt>'

      for comment in c.comments: 
         print '<dd>'
         print comment
         print '</dd>'

      for prop, ranges in sorted(c.properties): 
         print '<dd>'
         print '   ' + prop.name()
         if ranges: 
            print ' => ' + ', '.join(range.name() for range in ranges)
         print '</dd>'
      print '</dt>'
      print '</li>'
   print '</ul>'

   print '<h2>Properties</h2>'
   properties = []
   print '<dl>'
   for propclass in [RDF.Property, OWL.FunctionalProperty,
                     OWL.InverseFunctionalProperty]: 
      for uri in G.subjects(RDF.type, propclass): 
         if not isinstance(uri, URIRef): continue

         p = Property(uri)
         properties.append(p)
         p.kind = Class(propclass)
         p.domains = [Class(u) for u in G.objects(uri, RDFS.domain)
                      if isinstance(u, URIRef)]
         p.ranges = [Class(u) for u in G.objects(uri, RDFS.range) 
                     if isinstance(u, URIRef)]
         p.comments = [str(s) for s in G.objects(uri, RDFS.comment)]

   for p in sorted(properties): 
      print '<dt class="property">'
      print p.name() + ' (' + p.kind.name(format='text') + ')'
      print '</dt>'

      for comment in p.comments: 
         print '<dd>'
         print comment
         print '</dd>'

      if p.domains: 
         print '<dd>domain: '
         print ', '.join(domain.name() for domain in p.domains)
         print '</dd>'

      if p.ranges: 
         print '<dd>range: '
         print ', '.join(range.name() for range in p.ranges)
         print '</dd>'
   print '</dl>'

   print '<address>'
   print 'Generated by <a href="http://inamidst.com/proj/sdoc/"'
   print '>Schemadoc</a>'
   print '</address>'
   print '</body>'
   print '</html>'
Пример #53
0
 def setUp(self):
     super(WithGraph, self).setUp()
     self.graph = ConjunctiveGraph()
     self.graph.add((CMD['c2'], RDF.type, CMD['Bright']))
     self.cl = CommandLog(Graph2(self.graph, initNs=INITNS))
Пример #54
0
class sparql_funcs():
    def __init__(self):
        self.g = Graph('IOMemory')
        #self.endpoint = "http://www.opencorrespondence.org/data/endpoint/rdf"
        #self.g.bind('geo', geo)

    def find_places(self):
        '''
            Function to get the distinct locations mentioned in the headers of the letters. 
            These are the locations from which Dickens wrote. 
            TODO: Parsing the letters to get the places mentioned in them
        '''
        row = set()
        o = OFS()

        for b in o.list_buckets():
            endpoint = o.get_stream(b, "endpoint")

        self.g.parse(endpoint)

        for s, _, n in self.g.triples((None, dublin_core['title'], None)):
            loc_key = urllib.unquote(
                n.replace("http://www.opencorrespondence.org/place/resource/",
                          "").replace("/rdf", ""))
            row.add(self.tidy_location(loc_key))

        return row

    def tidy_location(self, location):
        '''
           Function to tidy up some of the places where they refer to the same place
           TODO: prob need some language processing to make this scalable
        '''
        ret_location = ''
        if location == 'Office Of "household Words,':
            ret_location = "Household Words"
        elif location == '"household Words" Office':
            ret_location = "Household Words"
        elif location == '"household Words"':
            ret_location = "Household Words"
        elif location == 'H. W. Office':
            ret_location = "Household Words"
        elif location == '"household Words,':
            ret_location = "Household Words"
        elif location == '"all The Year Round" Office':
            ret_location = "All The Year Round"
        elif location == 'Office Of "all The Year Round,':
            ret_location = "All The Year Round"
        elif location == "Gad's Hill Place":
            ret_location = "Gads Hill"
        elif location == "Gad's Hill":
            ret_location = "Gads Hill"
        elif location == "Gad's Hill Place, Higham":
            ret_location = "Gads Hill"
        elif location == "Tavistock House, Tavistock Square":
            ret_location = "Tavistock House"
        elif location == "London, Tavistock House":
            ret_location = "Tavistock House"
        elif location == "Tavistock House, London":
            ret_location = "Tavistock House"
        else:
            if "U.s." in location:
                location = str(location).replace("U.s", "")
            ret_location = str(location).replace(".", "")

        return ret_location

    def find_correspondents(self):
        '''
            Function to get the distinct locations mentioned in the headers of the letters. 
            These are the locations from which Dickens wrote. 
            TODO: Parsing the letters to get the places mentioned in them
        '''
        row = set()
        self.g.parse(self.endpoint)

        for s, _, n in self.g.triples((None, letter['correspondent'], None)):
            loc_key = urllib.unquote(
                n.replace(
                    "http://www.opencorrespondence.org/correspondent/resource/",
                    "").replace("/rdf", ""))
            row.add(loc_key)

        return row

    def get_abstract(self, resource_id):

        self.g.parse('http://dbpedia.org/resource/'.resource_id)
        q = '''
          SELECT *
                WHERE 
                {
                ?x dbpedia:abstract ?abstract .
                FILTER (lang(?abstract) = 'en')
                }
        '''
        for row in self.g.query(
                q,
                initNs=dict(dbpedia=Namespace("http://dbpedia.org/ontology/")),
                initBindings={}):
            return row[1]

    def query_dates(self, author):
        '''query to identify individual dates to a correspondent'''
        q = '''
        SELECT ?date
        FROM <http://localhost:5000/data/endpoint/rdf>
        WHERE {
            ?r dc:subject  \'''' + author + '''\' .  
            ?r dc:date  ?date.  
        }
        '''
        dates = []
        for row in self.g.query(
                q,
                initNs=dict(letter=Namespace(
                    "http://www.opencorrespondence.org/schema#"),
                            dc=Namespace("http://purl.org/dc/elements/1.1/")),
                initBindings={}):

            date = str(row[0]).split('-')

            if date[0][1:].isdigit():
                dates.append(date[0])
        print dates
        dic = {}

        for dt in dates:
            dic[dt] = dates.count(dt)

        return dic
Пример #55
0
from rdflib.Graph import ConjunctiveGraph
from rdflib import Namespace, BNode, Literal, RDF, URIRef
import csv
import pysesame

JOBS = Namespace(
    "http://www.medev.ac.uk/interoperability/rss/1.0/modules/jobs/rss1.0jobsmodule#"
)
DC = Namespace("http://purl.org/dc/elements/1.1/")
JB = Namespace("http://semprog.com/schemas/jobboard#")
COMPANY = Namespace("http://purl.org/rss/1.0/modules/company/")
RDFS = Namespace('http://www.w3.org/2000/01/rdf-schema#')

jg = ConjunctiveGraph()
jg.bind('jobs', JOBS)
jg.bind('dc', DC)
jg.bind('jobboard', JB)
jg.bind('company', COMPANY)
jg.bind('rdfs', RDFS)

# Incremental counter for vacancy IDs
vid = 0

for title, salary, location, company, crunchbase, ticker in csv.reader(
        file('joblist.csv')):
    # Create the vacancy
    vid += 1
    vacancy = JB[str(vid)]
    jg.add((vacancy, RDF.type, JOBS['Vacancy']))
    jg.add((vacancy, DC['title'], Literal(title)))
Пример #56
0
 def __init__(self):
     self.g = Graph('IOMemory')
Пример #57
0
     rdf:type      foaf:PersonalProfileDocument .

# Named graph: http://example.org/foaf/bobFoaf
@prefix  foaf:  <http://xmlns.com/foaf/0.1/> .
@prefix  rdf:    <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix  rdfs:   <http://www.w3.org/2000/01/rdf-schema#> .

_:z  foaf:mbox     <mailto:[email protected]> .
_:z  rdfs:seeAlso  <http://example.org/foaf/bobFoaf> .
_:z  foaf:nick     "Robert" .
<http://example.org/foaf/bobFoaf>
     rdf:type      foaf:PersonalProfileDocument .

"""

graph = ConjunctiveGraph(plugin.get('IOMemory', Store)())
graph.parse(StringIO(text), format="n3")
print graph.serialize(format='xml')

test_query = """
PREFIX  data:  <http://example.org/foaf/>
PREFIX  foaf:  <http://xmlns.com/foaf/0.1/>
PREFIX  rdfs:  <http://www.w3.org/2000/01/rdf-schema#>

SELECT ?mbox ?nick ?ppd
WHERE
{
  GRAPH data:aliceFoaf
  {
    ?alice foaf:mbox <mailto:[email protected]> ;
           foaf:knows ?whom .
Пример #58
0
    def resolveURI(self, uri):
        return _urljoin(self.baseuri or '', uri)

    def _popStacks(self, event, node):
        # check abouts
        if len(self.abouts) <> 0:
            about, aboutnode = self.abouts[-1]
            if aboutnode == node:
                self.abouts.pop()

        # keep track of nodes going out of scope
        self.elementStack.pop()

        # track xml:base and xml:lang going out of scope
        if self.xmlbases:
            self.xmlbases.pop()
            if self.xmlbases and self.xmlbases[-1]:
                self.baseuri = self.xmlbases[-1]

        if self.langs:
            self.langs.pop()
            if self.langs and self.langs[-1]:
                self.lang = self.langs[-1]


if __name__ == "__main__":
    store = ConjunctiveGraph()
    store.load(sys.argv[1], format="rdfa")
    print store.serialize(format="pretty-xml")