i need a help for getting some information out of the RDF with Jena Framework.
I have an RDF content like this:
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:ts="http://www.test.com/testModel.owl#">
<ts:Entity rdf:ID="1234_test">
<....>
</ts>
</rdf:RDF>
Now my idea is to get out the ID from ts:Entity. This is my code:
Model model = ModelFactory.createDefaultModel();
InputStream requestBody = new ByteArrayInputStream(request.getBytes());
String BASE = "http://www.test.com/testModel.owl#";
model.read(requestBody,BASE);
requestBody.close();
StmtIterator iter = model.listStatements();
while (iter.hasNext()) {
Statement stmt = iter.nextStatement(); // get next statement
Resource subject = stmt.getSubject(); // get the subject
Property predicate = stmt.getPredicate(); // get the predicate
RDFNode object = stmt.getObject(); // get the object
System.out.println(subject + " | "+predicate);
}
The only problem, in that case, is that i have to scroll all the Statement. There is a way for getting direct the ID from ts:Entity ? Maybe before getting the resource and after the value of the ID related to that resource.
Thanks in advance for helping.
Sorry, i am again here, because i have a similar question, if for example i have this rdf:
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:ts="http://www.test.com/testModel.owl#">
<ts:Entity rdf:ID="1234_test">
<ts:Resource>
<ts:testProp rdf:datatype="http://www.w3.org/2001/XMLSchema#string">test_ID_test</ts:testProp>
</ts>
</ts>
</rdf:RDF>
How i can extract the value test_ID_test??? And if i want use SPARQL how can i do with jena???
You should be using SPARQL to query your model rather than iterating over all the statements. Jena provides a good tutorial on how to use SPARQL with their API.
How about:
Resource ENTITY_TYPE = model.getResource("http://www.test.com/testModel.owl#Entity");
StmtIterator iter = model.listStatements(null, RDF.type, ENTITY_TYPE);
while (iter.hasNext()) {
String entityID = iter.next().getSubject().getURI();
System.out.println(entityID);
}
That will get the URI of each entity.
Related
Apache Jena is not able to query RDF Star Triples that have a double data type in them. Here is the code for reproduction of the issue with Jena 3.17 (it can be reproduced on other versions too).
Dataset dataset = TDB2Factory.createDataset();
Model tempModel = ModelFactory.createDefaultModel();
StringReader reader = new StringReader("#prefix : <http://ex#> "
+ "#prefix xsd: <http://www.w3.org/2001/XMLSchema#> "
+ ":rk :val \"1.0\"^^xsd:double ."
+ "<<:rk :val \"1.0\"^^xsd:double>> :p_key 1");
RDFDataMgr.read(tempModel, reader, null, Lang.TURTLE);
dataset.begin(TxnType.WRITE);
Graph repositoryGraph = dataset.getNamedModel("RAW_MODEL").getGraph();
StmtIterator it = tempModel.listStatements();
while(it.hasNext()) {
repositoryGraph.add(it.nextStatement().asTriple());
}
dataset.commit()
dataset.end()
Now during query time, I am using the following code.
dataset.begin(TxnType.READ);
Query query = QueryFactory.create("SELECT ?s ?o ?id WHERE {"
+ "<<?s <http://ex#val> ?o>> <http://ex#p_key> ?id"
+ "}");
try (QueryExecution exec = QueryExecutionFactory.create(query, dataset.getUnionModel())) {
ResultSet result = exec.execSelect();
while (result.hasNext()) {
System.out.println(result.next().toString());
}
}
dataset.end()
The above query fails to fetch any result. However, if I just replace xsd:double with xsd:float or xsd:decimal the results are fetched. Hence, I am looking for help to understand what is causing this issue with xsd:double?
Note: You might think that I am not using the most optimal way to make insertions. However, this was due to other requirements in the code and reproduction of issue is possible through this route.
It works in Jena 4.0.0.
In 3.17.0 - SPARQL was more like the original RDF* in its use of indexing.
As a consequence, the non-canonical term map cause a problem.
Try a lexical form of "1.0e0"^^xsd:double or v 4.x.x.
I would like to avoid passing SPARQL queries around as Strings. Therefore I use Jena's API for creating my queries. Now I need a PropertyPath in my query, but I can't find any Java class supporting this. Can you give me a hint?
Here's some example code where I would like to insert this (Jena 3.0.1):
private Query buildQuery(final String propertyPath) {
ElementTriplesBlock triplesBlock = new ElementTriplesBlock();
triplesBlock.addTriple(
new Triple(NodeFactory.createURI(this.titleUri.toString()),
//How can I set a property path as predicate here?
NodeFactory.???,
NodeFactory.createVariable("o"))
);
final Query query = buildSelectQuery(triplesBlock);
return query;
}
private Query buildSelectQuery(final ElementTriplesBlock queryBlock) {
final Query query = new Query();
query.setQuerySelectType();
query.setQueryResultStar(true);
query.setDistinct(true);
query.setQueryPattern(queryBlock);
return query;
}
You can use PathFactory to create property paths
Consider the graph below:
#prefix dc: <http://purl.org/dc/elements/1.1/>.
#prefix ex: <http://example.com/>.
ex:Manager ex:homeOffice ex:HomeOffice
ex:HomeOffice dc:title "Home Office Title"
Suppose you want to create a pattern like:
?x ex:homeOffice/dc:title ?title
The code below achieves it:
//create the path
Path exhomeOffice = PathFactory.pathLink(NodeFactory.createURI("http://example.com/homeOffice"));
Path dcTitle = PathFactory.pathLink(NodeFactory.createURI("http://purl.org/dc/elements/1.1/title"));
Path fullPath = PathFactory.pathSeq(exhomeOffice,dcTitle);
TriplePath t = new TriplePath(Var.alloc("x"),fullPath,Var.alloc("title"));
I have a bunch of code that uses the Apache Jena querybuilder API (SelectBuilder class). I am trying to add a term like this to my existing SPARQL query:
(?a ?b ?c) :hasMagicProperty ?this .
I have verified that this query works in TopBraid, but I can't figure out how to represent (?a, ?b, ?c) in the Jena API. What do I have to do to convert this list of Vars into a valid Jena resource node?
I am willing to explore alternate SPARQL-building frameworks, if they have robust support for typed literals, IRIs, and filters, as well as this list construct. I have skimmed over several other frameworks for building up SPARQL queries, but none of them seem to have a list construct.
Edit
My query building code (in Groovy) looks something like this:
def selectBuilder = new SelectBuilder()
selectBuilder.addPrefixes(...)
def thisVar = Var.alloc('this')
selectBuilder.addOptional(thisVar, 'rdf:type', ':MyEntity')
def aVar = Var.alloc('a')
def bVar = Var.alloc('b')
def cVar = Var.alloc('c')
List<Var> abc = [aVar, bVar, cVar]
//this doesn't work!!!
selectBuilder.addWhere(abc, ':hasMagicProperty', thisVar)
selectBuilder.addWhere(aVar, ':hasACode', 'code A')
selectBuilder.addWhere(bVar, ':hasBCode', 'code B')
selectBuilder.addWhere(cVar, ':hasCCode', 'code C')
def sparqlQuery = selectBuilder.buildString()
I have spent a couple of hours trying to work with the RDFList class, and I haven't figured it out. I'll keep trying, and see if I can grok it. In the meantime, any help would be appreciated. :)
Edit
Here is an unsuccessful attempt to use RDFList:
//this code does not work!
def varNode = NodeFactory.createVariable('a')
def model = ModelFactory.createDefaultModel()
def rdfNode = model.asRDFNode(varNode)
def rdfList = new RDFListImpl(model.createResource().asNode(), model)
//this line throws an exception!!
rdfList.add(rdfNode)
selectBuilder.addWhere(rdfList, ':hasMagicProperty', thisVar)
//com.hp.hpl.jena.shared.PropertyNotFoundException: http://www.w3.org/1999/02/22-rdf-syntax-ns#rest
The following method is a workaround, using multiple triples to recursively build up the RDF list:
/*
* Jena querybuilder does not yet support RDF lists. See:
* http://www.w3.org/TR/2013/REC-sparql11-query-20130321/#collections
*/
private Node buildRdfCollection(SelectBuilder queryBuilder, List<?> itemList) {
if (itemList.isEmpty()) {
return RDF.nil.asNode()
}
def head = itemList.first()
def rest = buildRdfCollection(queryBuilder, itemList.subList(1, itemList.size()))
def listNode = NodeFactory.createAnon()
queryBuilder.addWhere(listNode, RDF.first, head)
queryBuilder.addWhere(listNode, RDF.rest, rest)
return listNode
}
...
def listNode = buildRdfCollection(queryBuilder, abc)
queryBuilder.addWhere(listNode, ':hasMagicProperty', thisVar)
The generated SPARQL code looks like this:
_:b0 rdf:first ?c ;
rdf:rest rdf:nil .
_:b1 rdf:first ?b ;
rdf:rest _:b0 .
_:b2 rdf:first ?a ;
rdf:rest _:b1 ;
:hasMagicProperty ?this .
This is a long-winded equivalent to:
(?a ?b ?c) :hasMagicProperty ?this .
I wrote the queryBuilder and I don't think that in it's current state it will do what you want. Query builder is based on (but does not yet fully implement) the w3c SPARQL 1.1 recommendation:
http://www.w3.org/TR/2013/REC-sparql11-query-20130321/#rQuery
However, I think you can create your query using the Jena QueryFactory
String queryString = "SELECT * WHERE { "+
" OPTIONAL { ?this a :MyEntity } ."+
" (?a ?b ?c) :hasMagicProperty ?result . "+
" ?a :hasACode 'code A' . "+
" ?b :hasACode 'code B' . "+
" ?c :hasACode 'code C' ."+
" }";
Query query = QueryFactory.create(queryString) ;
Unfortunately, I don't think this is what you really want. Notice that ?this is not bound to any of the other statements and so will produce a cross product of all :MyEntity type subjects with the ?a, ?b, ?c and `?result`` bindings.
If you can create the query with QueryFactory, I can ensure that QueryBuilder will support it.
UPDATE
I have updated QueryBuilder (the next Snapshot should contain the changes). You should now be able to do the following:
Var aVar = Var.alloc('a')
Var bVar = Var.alloc('b')
Var cVar = Var.alloc('c')
selectBuilder.addWhere(selectBuilder.list(aVar, bVar, cVar), ':hasMagicProperty', thisVar)
selectBuilder.addWhere(aVar, ':hasACode', 'code A')
selectBuilder.addWhere(bVar, ':hasBCode', 'code B')
selectBuilder.addWhere(cVar, ':hasCCode', 'code C')
If you can also simply add the standard text versions of values in the list parameters like:
selectBuilder.list( "<a>", "?b", "'c'" )
I am new to both Jena-TDB and SPARQL, so it might be a silly question. I am using tdb-0.9.0, on Windows XP.
I am creating the TDB model for my trail_1.rdf file. My understanding here(correct me if I am wrong) is that the following code will read the given rdf file in TDB model and also stores/load (not sure what's the better word) the model in the given directory D:\Project\Store_DB\data1\tdb:
// open TDB dataset
String directory = "D:\\Project\\Store_DB\\data1\\tdb";
Dataset dataset = TDBFactory.createDataset(directory);
Model tdb = dataset.getDefaultModel();
// read the input file
String source = "D:\\Project\\Store_DB\\tmp\\trail_1.rdf";
FileManager.get().readModel( tdb, source);
tdb.close();
dataset.close();
Is this understanding correct?
As per my understanding since now the model is stored at D:\Project\Store_DB\data1\tdb directory, I should be able to run query on it at some later point of time.
So to query the TDB Store at D:\Project\Store_DB\data1\tdb I tried following, but it prints nothing:
String directory = "D:\\Project\\Store_DB\\data1\\tdb" ;
Dataset dataset = TDBFactory.createDataset(directory) ;
Iterator<String> graphNames = dataset.listNames();
while (graphNames.hasNext()) {
String graphName = graphNames.next();
System.out.println(graphName);
}
I also tried this, which also did not print anything:
String directory = "D:\\Project\\Store_DB\\data1\\tdb" ;
Dataset dataset = TDBFactory.createDataset(directory) ;
String sparqlQueryString = "SELECT (count(*) AS ?count) { ?s ?p ?o }" ;
Query query = QueryFactory.create(sparqlQueryString) ;
QueryExecution qexec = QueryExecutionFactory.create(query, dataset) ;
ResultSet results = qexec.execSelect() ;
ResultSetFormatter.out(results) ;
What am I doing incorrect? Is there anything wrong with my understanding that I have mentioned above?
For part (i) of your question, yes, your understanding is correct.
For part (ii), the reason that listNames does not return any results is because you have not put your data into a named graph. In particular,
Model tdb = dataset.getDefaultModel();
means that you are storing data into TDB's default graph, i.e. the graph with no name. If you wish listNames to return something, change that line to:
Model tdb = dataset.getNamedGraph( "graph42" );
or something similar. You will, of course, then need to refer to that graph by name when you query the data.
If your goal is simply to test whether or not you have successfully loaded data into the store, try the command line tools bin/tdbdump (Linux) or bat\tdbdump.bat (Windows).
For part (iii), I tried your code on my system, pointing at one of my TDB images, and it works just as one would expect. So: either the TDB image you're using doesn't have any data in it (test with tdbdump), or the code you actually ran was different to the sample above.
The problem in your part 1 code is, I think, you are not committing the data .
Try with this version of your part 1 code:
String directory = "D:\\Project\\Store_DB\\data1\\tdb";
Dataset dataset = TDBFactory.createDataset(directory);
Model tdb = dataset.getDefaultModel();
// read the input file
String source = "D:\\Project\\Store_DB\\tmp\\trail_1.rdf";
FileManager.get().readModel( tdb, source);
dataset.commit();//INCLUDE THIS STAMEMENT
tdb.close();
dataset.close();
and then try with your part 3 code :) ....
In all dbpedia pages, e.g.
http://dbpedia.org/page/Ireland
there's a link to a RDF file.
In my application I need to analyse the rdf code and run some logic on it.
I could rely on the dbpedia SPARQL endpoint, but I prefer to download the rdf code locally and parse it, to have full control over it.
I installed JENA and I'm trying to parse the code and extract for example a property called: "geo:geometry".
I'm trying with:
StringReader sr = new StringReader( node.rdfCode )
Model model = ModelFactory.createDefaultModel()
model.read( sr, null )
How can I query the model to get the info I need?
For example, if I wanted to get the statement:
<rdf:Description rdf:about="http://dbpedia.org/resource/Ireland">
<geo:geometry xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" rdf:datatype="http://www.openlinksw.com/schemas/virtrdf#Geometry">POINT(-7 53)</geo:geometry>
</rdf:Description>
Or
<rdf:Description rdf:about="http://dbpedia.org/resource/Ireland">
<dbpprop:countryLargestCity xmlns:dbpprop="http://dbpedia.org/property/" xml:lang="en">Dublin</dbpprop:countryLargestCity>
</rdf:Description>
What is the right filter?
Many thanks!
Mulone
Once you have the file parsed in a Jena model you can iterate and filter with something like:
//Property to filter the model
Property geoProperty =
model. createProperty("http://www.w3.org/2003/01/geo/wgs84_pos#",
"geometry");
//Iterator based on a Simple selector
StmtIterator iter =
model.listStatements(new SimpleSelector(null, geoProperty, (RDFNode)null));
//Loop to traverse the statements that match the SimpleSelector
while (iter.hasNext()) {
Statement stmt = iter.nextStatement();
System.out.print(stmt.getSubject().toString());
System.out.print(stmt.getPredicate().toString());
System.out.println(stmt.getObject().toString());
}
The SimpleSelector allows you to pass any (subject,predicate,object) pattern to match statements in the model. In your case if you only care about a specific predicate then first and third parameters of the constructor are null.
Allowing filtering two different properties
To allow more complex filtering you can implement the selects method in the
SimpleSelector interface like here:
Property geoProperty = /* like before */;
Property countryLargestCityProperty =
model. createProperty("http://dbpedia.org/property/",
"countryLargestCity");
SimpleSelector selector = new SimpleSelector(null, null, (RDFNode)null) {
public boolean selects(Statement s)
{ return s.getPredicate().equals(geoProperty) ||
s.getPredicate().equals(countryLargestCityProperty) ;}
}
StmtIterator iter = model.listStatements(selector);
while(it.hasNext()) {
/* same as in the previous example */
}
Edit: including a full example
This code includes a full example that works for me.
import com.hp.hpl.jena.util.FileManager;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.SimpleSelector;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.rdf.model.Statement;
public class TestJena {
public static void main(String[] args) {
FileManager fManager = FileManager.get();
fManager.addLocatorURL();
Model model = fManager.loadModel("http://dbpedia.org/data/Ireland.rdf");
Property geoProperty =
model. createProperty("http://www.w3.org/2003/01/geo/wgs84_pos#",
"geometry");
StmtIterator iter =
model.listStatements(new SimpleSelector(null, geoProperty,(RDFNode) null));
//Loop to traverse the statements that match the SimpleSelector
while (iter.hasNext()) {
Statement stmt = iter.nextStatement();
if (stmt.getObject().isLiteral()) {
Literal obj = (Literal) stmt.getObject();
System.out.println("The geometry predicate value is " +
obj.getString());
}
}
}
}
This full example prints out:
The geometry predicate value is POINT(-7 53)
Notes on Linked Data
http://dbpedia.org/page/Ireland is the HTML document version of the resource http://dbpedia.org/resource/Ireland
In order to get the RDF you should resolve :
http://dbpedia.org/data/Ireland.rdf
or
http://dbpedia.org/resource/Ireland + Accept: application/rdfxml in the HTTP header.
With curl it'd be something like:
curl -L -H 'Accept: application/rdf+xml' http://dbpedia.org/resource/Ireland