Ejemplo n.º 1
0
 def enriched(self, graph):
     """
     Find if the graph is enriched with FOAF
     
     @param graph: graph
     @return: graph enriched (True/False)
     """
     
     sparqlGr = sparqlGraph.SPARQLGraph(graph)
     select = ('?foaf')
     where = GraphPattern(
                                  [('?user', RDF['type'], SIOC['User']),
                                   ('?user', RDFS['seeAlso'], '?foaf')])
     foafs = sparqlGr.query(select, where)
     
     return (len(foafs) > 0)
Ejemplo n.º 2
0
    def __getGraph(self, foaf):
        """
        A simple mechanism to cache foaf graph
        """

        #tip to set socket timeout global var
        import socket
        socket.setdefaulttimeout(10)  #timeout in seconds

        if (self.__actualFoaf != foaf or self.__graph == None):
            self.__actualFoaf = foaf
            self.__graph = sparqlGraph.SPARQLGraph()
            try:
                self.__graph.parse(foaf)
            except:
                self.__graph = None

        return self.__graph
Ejemplo n.º 3
0
    def parse_xinclude(self, http_res, sparql=""):

        (fd, fname) = tempfile.mkstemp()
        fh = codecs.open(fname, "w", "utf-8")
        fh.write(http_res.read())
        fh.close()

        if not sparql:

            try:
                self.__graph.load(fname, format="n3")
                os.unlink(fname)
                return 1
            except:
                os.unlink(fname)
                return

        #

        try:
            new_graph = Graph()
            new_graph.load(fname, format="n3")
            os.unlink(fname)
        except:
            os.unlink(fname)
            return

        sparql_graph = sparqlGraph.SPARQLGraph(graph=new_graph)

        # re to parse 'sparql'

        select = ()
        pattern = []

        try:
            where = GraphPattern(pattern)
            result = sparql_graph.query(select, where)
        except:
            return

        for spo in result:
            self.__graph.add(spo)

        return 1
Ejemplo n.º 4
0
def run(modName):
    # Import the python module
    defs = None
    (fl, realpath, descr) = imp.find_module(modName, ["."])
    mod = imp.load_module(modName, fl, realpath, descr)
    defs = mod.__dict__

    ##################################################
    # Two ways of identifying the RDF data:
    # 1. A Triple Store generated in the module
    graph = None
    try:
        graph = defs["graph"]
    except:
        pass
    # 2. Directly in the test module as a string
    rdfData = None
    try:
        rdfData = defs["rdfData"]
    except:
        pass

    # Get the final of the triple store...
    if graph == None:
        stream = FileInputSource(StringIO.StringIO(rdfData))
        graph = sparqlGraph.SPARQLGraph()
        graph.parse(stream, format="xml")

    ###############################################
    # Retrive the query data
    pattern = defs["pattern"]
    optPattern = defs["optional"]
    construct = defs["construct"]

    ###############################################
    print "\n============= Test Module: %s =============" % modName

    results = graph.queryObject(pattern, optPattern)
    graph = results.construct(construct)
    graph.serialize("output.rdf")

    print "=== generated RDF file (output.rdf):\n"
    for l in file("output.rdf"):
        sys.stdout.write(l)
Ejemplo n.º 5
0
    def process(self, input, output=None):
        """
        Process
        
        @param input: input file
        @param output: output file
        """

        if (output == None):
            output = '.'.join(input.split('.')[:-1]) + '.kml'

        graph = self.parse(input)

        #sparql query
        sparqlGr = sparqlGraph.SPARQLGraph(graph)
        select = ('?name', '?lat', '?lon', '?pic')
        where = GraphPattern([('?x', RDF['type'], SIOC['User']),
                              ('?x', SIOC['name'], '?name'),
                              ('?x', FOAF['based_near'], "?y"),
                              ('?y', GEO['long'], '?lon'),
                              ('?y', GEO['lat'], '?lat')])
        opt = GraphPattern([('?x', SIOC['avatar'], "?pic")])
        users = sparqlGr.query(select, where, opt)

        n = len(users)
        if (n > 0):
            kml = KML()

            #create places
            for (name, lat, lon, pic) in users:
                kml.addPlace(lat, lon, str(name), pic)

            #and dump to disk
            try:
                kml_file = open(output, 'w+')
                kml.write(kml_file)
                kml_file.flush()
                kml_file.close()
                print 'new KML file created in', output, 'with', n, 'points'
            except IOError, detail:
                print 'Error exporting coordinates to KML: ' + str(detail)
Ejemplo n.º 6
0
 def query(self):
     """
     Make a SPARQL query
     
     @return: posts result
     """
     
     try:    
         sparqlGr = sparqlGraph.SPARQLGraph(self.graph)
         select = ('?post', '?postTitle', '?date', '?userName', '?content', '?parent')            
         where  = GraphPattern([('?post',    RDF['type'],            SIOC['Post']),
                                       ('?post',    DC['title'],            '?postTitle'),
                                       ('?post',    DCTERMS['created'],     '?date'),
                                       ('?post',    SIOC['content'],        '?content'),
                                       ('?post',    SIOC['has_creator'],    '?user'),
                                       ('?user',    SIOC['name'],           '?userName')])
         opt    = GraphPattern([('?post',    SIOC['reply_of'],       '?parent')])
         posts  = sparqlGr.query(select, where, opt)
         return self.orderByDate(posts)
     except Exception, details:
         print 'parsing exception:', str(details)
         return None
Ejemplo n.º 7
0
 def __listPosts(self):
     """
     List post at cache
     """
     
     try:    
         sparqlGr = sparqlGraph.SPARQLGraph(self.graph)
         select = ('?post', '?title')            
         where  = GraphPattern([('?post', RDF['type'],   SIOC['Post']),
                                       ('?post', DC['title'], '?title')])
         posts  = sparqlGr.query(select, where)
         
         print len(posts), 'posts:'
         
         for post, title in posts:
             print post, 
             try:
                 print title
             except:
                 print '(bad formed title)'
             
     except Exception, details:
         print 'parsing exception:', str(details)
         return None
Ejemplo n.º 8
0
def Evaluate(graph, query, passedBindings={}, DEBUG=False):
    """
    Takes:
        1. a rdflib.Graph.Graph instance 
        2. a SPARQL query instance (parsed using the BisonGen parser)
        3. A dictionary of initial variable bindings (varName -> .. rdflib Term .. )
        4. DEBUG Flag

    Returns a list of tuples - each a binding of the selected variables in query order
    """
    if query.prolog:
        query.prolog.DEBUG = DEBUG
    if query.query.dataSets:
        graphs = []
        for dtSet in query.query.dataSets:
            if isinstance(dtSet, NamedGraph):
                graphs.append(Graph(graph.store, dtSet))
            else:
                memStore = plugin.get('IOMemory', Store)()
                memGraph = Graph(memStore)
                try:
                    memGraph.parse(dtSet, format='n3')
                except:
                    #Parse as RDF/XML instead
                    memGraph.parse(dtSet)
                graphs.append(memGraph)
        tripleStore = sparqlGraph.SPARQLGraph(ReadOnlyGraphAggregate(graphs))
    else:
        tripleStore = sparqlGraph.SPARQLGraph(graph)

    if isinstance(query.query, SelectQuery) and query.query.variables:
        query.query.variables = [
            convertTerm(item, query.prolog) for item in query.query.variables
        ]
    else:
        query.query.variables = []

    #Interpret Graph Graph Patterns as Named Graphs
    graphGraphPatterns = categorizeGroupGraphPattern(
        query.query.whereClause.parsedGraphPattern)[0]
    #    rt = categorizeGroupGraphPattern(query.query.whereClause.parsedGraphPattern)[0]
    #    print rt[0], rt[1]
    if graphGraphPatterns:
        graphGraphP = graphGraphPatterns[0].nonTripleGraphPattern
        if isinstance(graphGraphP.name, Variable):
            if graphGraphP.name in passedBindings:
                tripleStore = sparqlGraph.SPARQLGraph(
                    Graph(graph.store, passedBindings[graphGraphP.name]))
            else:
                #print graphGraphP
                #raise Exception("Graph Graph Patterns can only be used with variables bound at the top level or a URIRef or BNode term")
                tripleStore = sparqlGraph.SPARQLGraph(
                    graph, graphVariable=graphGraphP.name)
        else:
            graphName = isinstance(graphGraphP.name,
                                   Variable) and passedBindings[
                                       graphGraphP.name] or graphGraphP.name
            graphName = convertTerm(graphName, query.prolog)
            if isinstance(graph, ReadOnlyGraphAggregate) and not graph.store:
                targetGraph = [
                    g for g in graph.graphs if g.identifier == graphName
                ]
                assert len(targetGraph) == 1
                targetGraph = targetGraph[0]
            else:
                targetGraph = Graph(graph.store, graphName)
            tripleStore = sparqlGraph.SPARQLGraph(targetGraph)

    gp = reorderGroupGraphPattern(query.query.whereClause.parsedGraphPattern)
    validateGroupGraphPattern(gp)
    basicPatterns, optionalPatterns = sparqlPSetup(gp, query.prolog)

    if DEBUG:
        print "## Select Variables ##\n", query.query.variables
        print "## Patterns ##\n", basicPatterns
        print "## OptionalPatterns ##\n", optionalPatterns

    result = queryObject(tripleStore, basicPatterns, optionalPatterns,
                         passedBindings)
    if result == None:
        # generate some proper output for the exception :-)
        msg = "Errors in the patterns, no valid query object generated; "
        msg += ("pattern:\n%s\netc..." % basicPatterns[0])
        raise SPARQLError(msg)

    if isinstance(query.query, AskQuery):
        return result.ask()

    elif isinstance(query.query, SelectQuery):
        orderBy = None
        orderAsc = None
        if query.query.solutionModifier.orderClause:
            orderBy = []
            orderAsc = []
            for orderCond in query.query.solutionModifier.orderClause:
                # is it a variable?
                if isinstance(orderCond, Variable):
                    orderBy.append(orderCond)
                    orderAsc.append(ASCENDING_ORDER)
                # is it another expression, only variables are supported
                else:
                    expr = orderCond.expression
                    assert isinstance(
                        expr, Variable
                    ), "Support for ORDER BY with anything other than a variable is not supported: %s" % expr
                    orderBy.append(expr)
                    orderAsc.append(orderCond.order == ASCENDING_ORDER)

        limit = query.query.solutionModifier.limitClause and int(
            query.query.solutionModifier.limitClause) or None

        offset = query.query.solutionModifier.offsetClause and int(
            query.query.solutionModifier.offsetClause) or 0
        return result.select(query.query.variables, query.query.distinct,
                             limit, orderBy, orderAsc,
                             offset), _variablesToArray(
                                 query.query.variables,
                                 "selection"), result._getAllVariables(
                                 ), orderBy, query.query.distinct
    else:
        raise NotImplemented(CONSTRUCT_NOT_SUPPORTED, repr(query))
Ejemplo n.º 9
0
 def process(self, input, output=None):
     """
     Enrichement process
     
     @param input: input file
     @param output: output file
     """
     
     graph = self.parse(input)
     
     if not self.enriched(graph):
         
         if (output == None):
             output = '.'.join(input.split('.')[:-1]) + '.foaf.enrichment.rdf'
         
         #sparql query
         sparqlGr = sparqlGraph.SPARQLGraph(graph)
         select = ('?user', '?email_sha1sum')
         where = GraphPattern(
             [('?user', RDF['type'], SIOC['User']),
              ('?user', SIOC['email_sha1sum'], '?email_sha1sum')])
         users = sparqlGr.query(select, where)
         
         if (len(users) > 0):
             foafserv = FOAFS()
             n = 0
             
             graph.bind('foaf', FOAF)
             graph.bind('sioc', SIOC)
             graph.bind('geo', GEO)
             graph.bind('rdfs', RDFS)
             
             for (user, email_sha1sum) in users:
                 foaf = foafserv.getFoafFromSha(email_sha1sum)
                 if (foaf != None):
                     n += 1
                     
                     graph.add((user, RDFS['seeAlso'], URIRef(foaf)))
                     
                     lat, lon = foafserv.getGeoPosition(foaf, email_sha1sum)
                     if (lat != None and lon != None):                        
                         geo = BNode()
                         graph.add((user, FOAF['based_near'], geo))
                         graph.add((geo, RDF.type, GEO['Point']))        
                         graph.add((geo, GEO['lat'], Literal(lat)))
                         graph.add((geo, GEO['long'], Literal(lon)))
                 
                     pic = foafserv.getPic(foaf, email_sha1sum)
                     if (pic != None):
                         graph.add((user, SIOC['avatar'], URIRef(pic)))
 
                     
             #and dump to disk
             try:
                 rdf_file = open(output, 'w+')
                 graph.serialize(destination=rdf_file, format="pretty-xml")
                 rdf_file.flush()
                 rdf_file.close()
                 print 'new subscriber RDF file created in', output, 'enriched with', n, 'FOAF files'
             except IOError, detail:
                 print 'Error exporting subscriber to RDF: ' + str(detail)
                 
         else:
             print 'Nobody with FOAF description available in', input