Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

Prefix declarations



The SparqlPrefixDeclarations class specifies the prefixes to include in the SPARQL requests.

Including the prefix declarations in the SPARQL result

By default SPARQL requests created by the framework do not include the prefix declarations.

For example suppose the following code:
   SparqlRequest request = new SparqlRequest("sitac");
   request.addSelect("zone");
   request.addType("zone", "TaskZone");
   request.addPropertyValue("zone", "isTasked", true);
   String sparql = request.getSPARQL();
The result will be:
   SELECT ?zone
   WHERE {
     ?zone rdf:type sitac:TaskZone .
     ?zone sitac:isTasked "true" .
   }
To include the prefix declarations in the resulting SPARQL request, you must add a SparqlPrefixDeclarations as an argument to the request constructor.

For example with the following code:
   SparqlPrefixDeclarations prefixDecl = new SparqlPrefixDeclarations();
   prefixDecl.addPrefix("sitac", "http://example.com/");      
      
   SparqlRequest request = new SparqlRequest("sitac", prefixDecl);
   request.addSelect("zone");
   request.addType("zone", "TaskZone");
   request.addPropertyValue("zone", "isTasked", true);
   String sparql = request.getSPARQL();
The result will be:
   PREFIX owl: <http://www.w3.org/2002/07/owl#>
   PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
   PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
   PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
   PREFIX sitac: <http://example.com/>
   SELECT ?zone
   WHERE {
     ?zone rdf:type sitac:TaskZone .
     ?zone sitac:isTasked "true" .
   }

Adding prefixes to the prefixes declarations

To bind prefixes to IRIs to the prefixes declarations, you can use the following methods: For example with the following code:
   SparqlPrefixDeclarations prefixDecl = new SparqlPrefixDeclarations();
   prefixDecl.addPrefix("sitac", "http://example.com/");      
      
   SparqlRequest request = new SparqlRequest("sitac", prefixDecl);
   request.addSelect("zone");
   request.addType("zone", "TaskZone");
   request.addPropertyValue("zone", "isTasked", true);
   String sparql = request.getSPARQL();
The result will be:
   PREFIX owl: <http://www.w3.org/2002/07/owl#>
   PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
   PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
   PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
   PREFIX sitac: <http://example.com/>
   SELECT ?zone
   WHERE {
     ?zone rdf:type sitac:TaskZone .
     ?zone sitac:isTasked "true" .
   }

Adding specific default prefixes to the prefixes declarations

Some methods allow to add specific default prefixes to the prefixes declarations, you can use of of the following methods: For example with the following code:
   SparqlPrefixDeclarations prefixDecl = new SparqlPrefixDeclarations();
   prefixDecl.addPrefix("sitac", "http://example.com/");    
   prefixDecl.addGeoSparqlPrefix();
   prefixDecl.addOwlTimePrefix();        
      
   SparqlRequest request = new SparqlRequest("sitac", prefixDecl);
   request.addSelect("zone");
   request.addType("zone", "TaskZone");
   request.addPropertyValue("zone", "isTasked", true);
   String sparql = request.getSPARQL();
The result will be:
   PREFIX owl: <http://www.w3.org/2002/07/owl#>
   PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
   PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
   PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
   PREFIX geo: <http://www.opengis.net/ont/geosparql#>
   PREFIX geof: <http://www.opengis.net/def/function/geosparql/>
   PREFIX sitac: <http://example.com/>
   PREFIX time: <http://www.w3.org/2006/time#>
   PREFIX unit: <http://qudt.org/vocab/unit#>
   PREFIX uom: <http://www.opengis.net/def/uom/OGC/1.0/#>
   SELECT ?zone
   WHERE {
     ?zone rdf:type sitac:TaskZone .
     ?zone sitac:isTasked "true" .
   }      

Default prefix declarations

Main Article: Library configuration

It is possible to define a default prefix declarations by calling the SparqlHelperConfig.setDefaultPrefixDeclarations(SparqlPrefixDeclarations decl) method.

For example with the following code:
   SparqlPrefixDeclarations prefixDecl = new SparqlPrefixDeclarations();      
   prefixDecl.addPrefix("sitac", "http://example.com/");    
   prefixDecl.addGeoSparqlPrefix();
   prefixDecl.addOwlTimePrefix();      
   SparqlHelperConfig.getInstance().setDefaultPrefixDeclarations(prefixDecl);  
      
   SparqlRequest request = new SparqlRequest("sitac");
   request.addSelect("zone");
   request.addType("zone", "TaskZone");
   request.addPropertyValue("zone", "isTasked", true);
   String sparql = request.getSPARQL();
The result will be:
   PREFIX owl: <http://www.w3.org/2002/07/owl#>
   PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
   PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
   PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
   PREFIX geo: <http://www.opengis.net/ont/geosparql#>
   PREFIX geof: <http://www.opengis.net/def/function/geosparql/>
   PREFIX sitac: <http://example.com/>
   PREFIX time: <http://www.w3.org/2006/time#>
   PREFIX unit: <http://qudt.org/vocab/unit#>
   PREFIX uom: <http://www.opengis.net/def/uom/OGC/1.0/#>
   SELECT ?zone
   WHERE {
     ?zone rdf:type sitac:TaskZone .
     ?zone sitac:isTasked "true" .
   }      

See also


Categories: Requestcreation

Copyright 2025 Dassault Aviation. All Rights Reserved. Documentation and source under the LGPL v3 licence