0
votes

Based on an initial URI from DBPedia (Ex: dbpedia.org/resource/Barack_Obama), I need to perform a depth search following these steps:

1 - Dereference a RDF link from DBPedia [OK]

2 - Take all triples that asserts this pattern [OK]

<givenURI> <property> <someObject> .

3 - Insert the result back into the initial graph [?]

4 - To navigate through new data, I need to find new triples that satisfy these two patterns [?]:

 <URI> owl:sameAs <resourceObject> . 
 <subjectResource> owl:sameAs <URI> .

5 - Than get back to first step, doing it recursively, saving visited URI's to avoid infity loops.

So, there is the resultSet from step 2. At this point, it's all property/Object from initial URI (dbpedia.org/resource/Barack_Obama).

select ?property ?resource where {
  <http://dbpedia.org/resource/Barack_Obama> ?property ?resource
}
property,resource
http://www.w3.org/1999/02/22-rdf-syntax-ns#type,http://xmlns.com/foaf/0.1/Person
http://www.w3.org/1999/02/22-rdf-syntax-ns#type,http://schema.org/Person
http://www.w3.org/1999/02/22-rdf-syntax-ns#type,http://dbpedia.org/class/yago/UnitedStatesSenatorsFromIllinois
http://www.w3.org/1999/02/22-rdf-syntax-ns#type,http://wikidata.dbpedia.org/resource/Q215627
http://www.w3.org/1999/02/22-rdf-syntax-ns#type,http://www.w3.org/2002/07/owl#Thing
http://www.w3.org/1999/02/22-rdf-syntax-ns#type,http://wikidata.dbpedia.org/resource/Q5
...

I'm really confused about how could I insert the resultSet from this query to initial graph, than deep search into it again..

There's the actual code on Java, using Jena and Sparql:

public class SemanticCrawlerImpl implements SemanticCrawler {
    public void search(Model graph, String resourceURI) {
        graph.read(resourceURI);

        ParameterizedSparqlString queryString = new ParameterizedSparqlString( "" +
                "select ?property ?resource where {\n" +
                "  <"+resourceURI+"> ?property ?resource\n" +
                "}" );

        System.out.println( queryString );

        QueryExecution exec = QueryExecutionFactory.sparqlService( "http://dbpedia.org/sparql", queryString.asQuery() );

        com.hp.hpl.jena.query.ResultSet results = ResultSetFactory.copyResults( exec.execSelect() );

        ResultSetFormatter.outputAsCSV( results );
    }
}

So, there it is. Any help would be appreciated!

1
You don't insert result sets, you insert triples. The first part would be something like insert { graph <graph> { <givenURI> ?p ?o } } where { service <http:/dbpedia.org/sparql> { <givenURI> ?p ?o } }. - Joshua Taylor
You don't actually need any repetitions here, if I understand you correctly. You just want all the triples that have <givenURI> as a subject, or anything that is owl:sameAs it, right? That's not too hard to do (writing an answer...). - Joshua Taylor

1 Answers

1
votes

It's not entirely clear from you question what it is that you're trying to follow, but it sounds like you want to start with dbpedia:Barack_Obama, pull all the triples with that as the subject, and then do the same for the objects of any triples that had the property owl:sameAs. That is, you're trying to get all the triples with a subject that's connected by an owl:sameAs chain to dbpedia:Barack_Obama. You don't need multiple queries to do that, though; you can just use property paths:

construct { ?s ?p ?o }
where {
  ?s ?p ?o 
  {
    select ?s { 
      ?s (owl:sameAs|^owl:sameAs)? dbpedia:Barack_Obama
    }
  }
}

SPARQL Results

That says to take as ?s the URI dbpedia:Barack_Obama, as well as any x such that either

x owl:sameAs dbpedia:Barack_Obama

or

dbpedia:Barack_Obama owl:sameAs x

and return all the triples ?s ?p ?o. (Ideally, you'd actually want to use

select ?s { 
  ?s (owl:sameAs|^owl:sameAs)* dbpedia:Barack_Obama
}

in the subquery to follow paths of any length, but DBpedia will complain about the possible memory usage.)

Now, that was a construct query, but it's easy enough to write a corresponding query that will update your graph ?g:

insert { graph <URIofGraph> { ?s ?p ?o } }
where {
  service <http://dbpedia.org/sparql> {
    ?s ?p ?o 
    {
      select ?s { 
        ?s (owl:sameAs|^owl:sameAs)? dbpedia:Barack_Obama
      }
    }
  }
}