Semweb4j/filesQueries/step5

From semanticweb.org

Jump to: navigation, search

Up | Previous step | Next step

[edit] Step 5: SPARQL CONSTRUCT

This step takes (almost) the same setup as step 4, so we don't repeat the URI definitions, naming and tagging statements, etc..

We define additionally:

PlainLiteral tagComputers = model.createPlainLiteral("Computers");

[edit] CONSTRUCT queries

A sample CONSTRUCT query string is

CONSTRUCT { ?subject <http://some-other-relation-B> ?object } WHERE { ?subject <http://some-relation-A ?object }

and returns a graph where all A related resources are now B related (and no not-A-related resources from the original graph).

CONSTRUCT queries are always executed via model.sparqlConstruct (not sparqlSelect!). sparqlConstruct returns a ClosableIterable of Statements, no QueryRowTable - it is a RDF graph instead.

Now suggest tagJava "implies" tagComputers:

	System.out.println("Query 1:");
	String queryString = "CONSTRUCT { ?resource <"+hasTag+"> "+tagComputers.toSPARQL()+" } WHERE { ?resource <"+hasTag+"> "+tagJava.toSPARQL()+" }";
	ClosableIterable<? extends Statement> results = model.sparqlConstruct(queryString);
	for(Statement result : results) {
		System.out.println(result);
	}

We will construct a graph where resources without age get an age of 0. Then we will add this graph to our default model, so every resource should have an age after this:

	System.out.println("Query 2:");
	queryString =
		"CONSTRUCT { ?resource <"+hasAge+"> 0 } " +
		"WHERE { ?resource ?p ?o . " +
				"OPTIONAL { ?resource <"+hasAge+"> ?age } . " +
				"FILTER(!bound(?age)) }";
	results = model.sparqlConstruct(queryString);
	for(Statement result : results) {
		System.out.println(result);
	}
	model.addAll(results.iterator());

The "FILTER(!bound(?age))" filters all resources where "bound(?age)" is False, that are all resources where "OPTIONAL { ?resource <"+hasAge+"> ?age }" couldn't select "?age" because there was no such statement.

The model.addAll adds the constructed graph to the default model.

The last CONSTRUCT constructs blank nodes with age and name, sort by age:

	System.out.println("Query 3:");
	queryString = 
		"CONSTRUCT { _:r <"+hasName+"> ?name ." +
			"_:r <"+hasAge+"> ?age } " +
		"WHERE { ?resource <"+hasAge+"> ?age ." +
			"?resource <"+hasName+"> ?name } " +
		"ORDER BY DESC(?age)";
	results = model.sparqlConstruct(queryString);
	for(Statement result : results) {
		System.out.println(result);
	}

where _:r is a blank node identifier (in SPARQL syntax).

[edit] output

Query 1:
http://example.com/persons#james--http://example.com/relations#hasTag--Computers
http://xam.de/foaf.rdf.xml#i--http://example.com/relations#hasTag--Computers
http://example.com/persons#konrad--http://example.com/relations#hasTag--Computers
Query 2:
http://example.com/persons#james--http://example.com/relations#age--0^^http://www.w3.org/2001/XMLSchema#integer
http://example.com/persons#guido--http://example.com/relations#age--0^^http://www.w3.org/2001/XMLSchema#integer
Query 3:
3f110d13:110dae3895d:-7fff--http://example.com/relations#age--29^^http://www.w3.org/2001/XMLSchema#integer
3f110d13:110dae3895d:-7fff--http://xmlns.com/foaf/0.1/#term_name--Max Völkel
3f110d13:110dae3895d:-7ffe--http://example.com/relations#age--19^^http://www.w3.org/2001/XMLSchema#integer
3f110d13:110dae3895d:-7ffe--http://xmlns.com/foaf/0.1/#term_name--Konrad Völkel
3f110d13:110dae3895d:-7ffd--http://xmlns.com/foaf/0.1/#term_name--Guido van Rossum
3f110d13:110dae3895d:-7ffd--http://example.com/relations#age--0^^http://www.w3.org/2001/XMLSchema#integer
3f110d13:110dae3895d:-7ffc--http://example.com/relations#age--0^^http://www.w3.org/2001/XMLSchema#integer
3f110d13:110dae3895d:-7ffc--http://xmlns.com/foaf/0.1/#term_name--James Gosling

3f110d13:110dae3895d:-7ffc is a blank node identifier, to avoid having only one blank node instead of four.

Personal tools