Thank you for the answer but unfortunately i am not getting any result in the console, here i am attaching my code, please guide that where i am making a mistake.
FileManager.get().addLocatorClassLoader(test.class.getClassLoader());
Model model=FileManager.get().loadModel("H:/EclipseWorkplace/MuseumDatabaseRecommendation/src/data3.rdf");
String spr="prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"+
"prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"+
"prefix crm: <urn:x-stackoverflow:example#>\n"+
"\n"+
"SELECT * WHERE{\n"+
" [ crm:E21_Person/rdfs:label ?creator\n"+
" ; crm:P108i_was_produced_by [ crm:P126_employed [ rdfs:label ?material ]\n"+
" ; crm:P4_has_time-span [ crm:P82_at_some_time_within ?timespan ]\n"+
" ]\n"+
" ; crm:P3_has_note [ a crm:P102_has_title\n"+
" ; rdfs:label ?title\n"+
" ]\n"+
" ]\n"+
" FILTER( ?creator = \"Brett WHITELEY\" ).\n"+
"}";
Query query = QueryFactory.create(spr); //s2 = the query above
QueryExecution qExe = QueryExecutionFactory.create( query,model );
//QueryExecution qExe = QueryExecutionFactory.sparqlService( "http://dbpedia.org/sparql", query );
ResultSet results = qExe.execSelect();
ResultSetFormatter.out(System.out, results, query);
My current output is
-----------------------------------------
| creator | material | timespan | title |
=========================================
-----------------------------------------
Thanks for your time.
Input Data
In order to facilitate readability, I restructured your model into TURTLE
syntax for others to read.
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix crm: <http://www.cidoc-crm.org/cidoc-crm/> .
<http://phdprototype.tk/collectionimage /4D0BFF17-5810-4644-A550-D35EE090D4A8.png>
a "Painting" ;
rdfs:label "Brett WHITELEY" ;
crm:E21_Person [ a <E39_Actor> ;
rdfs:label "Brett WHITELEY"
] ;
crm:E62_String "Painting\n" ;
crm:P108i_was_produced_by [ a crm:E12_Production ;
crm:P126_employed [ a crm:E57_Material ;
rdfs:label "Oil"
] ;
crm:P4_has_time-span [ a crm:E52_Time-Span ;
crm:P82_at_some_time_within "\n 1976\n "
]
] ;
crm:P3_has_note [ a crm:P102_has_title ;
rdfs:label "Interior with Time Past"
] ;
crm:P7_took_place_at [ a crm:E53_Place ;
crm:E44_Place_Appellation " \n 5D\n "
] ;
crm:P91_has_unit [ a crm:E58_Measurement_Unit ;
rdfs:label "182.0 h * 200.0 w cm"
] .
There are some issues with this data. For example, your painting has rdf:type
the plain literal "painting"
, which is extremely bad. This is denoted by the a "painting"
property-object pair. We can execute a query over this data, but you may wish to restructure your ontology so that it uses terms like rdf:type
properly.
Query
For the sample data provided, the following query will extract what you are looking for:
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix crm: <http://www.cidoc-crm.org/cidoc-crm/>
SELECT * WHERE{
[ crm:E21_Person/rdfs:label ?creator
; crm:P108i_was_produced_by [ crm:P126_employed [ rdfs:label ?material ]
; crm:P4_has_time-span [ crm:P82_at_some_time_within ?timespan ]
]
; crm:P3_has_note [ a crm:P102_has_title
; rdfs:label ?title
]
]
FILTER( ?creator = "Brett WHITELEY" ).
}
will result in:
----------------------------------------------------------------------------------
| creator | material | timespan | title |
==================================================================================
| "Brett WHITELEY" | "Oil" | "\n 1976\n " | "Interior with Time Past" |
----------------------------------------------------------------------------------
Note that this is highly tailored to your particular sample data. You will need to adjust this query to match what structures are actually allowed. For (hypothetical) example, the TimeSpan
instance may sometimes have a crm:P82_at_some_time_span
property, or it may have some crm:exampleProperty
that relates it to another values. In that case, you'd need to modify the query to use a property path to match this.
Example Code
public static final String queryString = "prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"+
"prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"+
"prefix crm: <http://www.cidoc-crm.org/cidoc-crm/>\n"+
"\n"+
"SELECT * WHERE{\n"+
" [ crm:E21_Person/rdfs:label ?creator\n"+
" ; crm:P108i_was_produced_by [ crm:P126_employed [ rdfs:label ?material ]\n"+
" ; crm:P4_has_time-span [ crm:P82_at_some_time_within ?timespan ]\n"+
" ]\n"+
" ; crm:P3_has_note [ a crm:P102_has_title\n"+
" ; rdfs:label ?title\n"+
" ]\n"+
" ]\n"+
" FILTER( ?creator = \"Brett WHITELEY\" ).\n"+
"}";
public static final Query query = QueryFactory.create(queryString);
@Test
public void test() throws Exception {
final Model model = ModelFactory.createDefaultModel();
try( final InputStream in = this.getClass().getResourceAsStream("/so.rdf") ) {
model.read(in, null, "RDF/XML");
}
model.write(System.out, "TTL");
System.out.println("=================================================================");
System.out.println(queryString);
System.out.println("=================================================================");
try( final QueryExecution exec = QueryExecutionFactory.create(query, model) ) {
ResultSetFormatter.out(System.out, exec.execSelect(), query);
}
}