我遇到的问题是 com.hp.hpl.jena.query.QueryParseException: Encountered " <PNAME_LN> "rdfs:domain.
这就是我所做的。我使用 OWL API 读取 xml/owl 文档并使用 Pellet 进行推理。因为除了原因我还想做一些 SPARQL 查询,我使用 Jena 2.10.0 与 Pellet 2.3.0 一起工作。由于兼容性,我无法更改 Jena 的版本。这是代码:
package loadOnto;
import java.io.File;
import org.mindswap.pellet.KnowledgeBase;
import org.mindswap.pellet.jena.PelletInfGraph;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import com.clarkparsia.pellet.owlapiv3.PelletReasoner;
import com.clarkparsia.pellet.owlapiv3.PelletReasonerFactory;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.rdf.model.InfModel;
import com.hp.hpl.jena.rdf.model.ModelFactory;
public class load {
public static void main(String[] args) throws OWLOntologyCreationException {
// Get hold of an ontology manager
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
// We can also load ontologies from files. Create a file object that points to the local copy
File file = new File("G:/Protege/owlfiles/Before_Gather.owl");
// Load the local copy
OWLOntology loadMODIS = manager.loadOntologyFromOntologyDocument(file);
PelletReasoner reasoner = PelletReasonerFactory.getInstance().createNonBufferingReasoner( loadMODIS );
//System.out.println("CheckParsing.should() " + reasoner.isConsistent());
KnowledgeBase kb = reasoner.getKB();
PelletInfGraph graph = new org.mindswap.pellet.jena.PelletReasoner().bind( kb );
InfModel model = ModelFactory.createInfModel( graph );
String PREFIX = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" +
"PREFIX owl: <http://www.w3.org/2002/07/owl#>" +
"PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>" +
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"+
"PREFIX seaice: <http://www.semanticweb.org/SeaIceOntology#>" +
"PREFIX repr: <http://sweet.jpl.nasa.gov/2.3/reprDataFormat.owl#>" +
"PREFIX realmCryo: <http://sweet.jpl.nasa.gov/2.3/realmCryo.owl#>" +
"PREFIX relaMath: <http://sweet.jpl.nasa.gov/2.3/relaMath.owl#>";
String SELECT = "select ?domain ";
String WHERE = "where {" +
"?dataset seaice:hasSpatialResolution" + "\"4.0\"^^xsd:float." +
"?dataset seaice:hasDataFormat repr:GeoTIFF."+
"?dataset seaice:recordedDuring seaice:Day."+
"seaice:describe rdfs:domain ?domain."+
//"seaice:describe rdfs:range realmCryo:SeaIce."+
//"?dataset rdf:type ?domain." +
"}" ;
Query queryStr = QueryFactory.create(PREFIX + SELECT + WHERE);
QueryExecution qe = QueryExecutionFactory.create(queryStr, model);
ResultSet rs = qe.execSelect();
ResultSetFormatter.out(System.out,rs);
rs = null; qe.close();
reasoner.dispose();
//System.out.println("Loaded ontology: " + loadMODIS);
}
}
该代码在前 3 个查询中运行良好,但在添加 后"seaice:describe rdfs:domain ?domain."
,问题立即出现。该代码也适用于seaice:describe rdfs:domain ?domain.
单独的“”。错误如下:
Exception in thread "main" com.hp.hpl.jena.query.QueryParseException: Encountered " <PNAME_LN> "rdfs:domain "" at line 1, column 582.
Was expecting one of:
"values" ...
"graph" ...
"optional" ...
"minus" ...
"bind" ...
"service" ...
"filter" ...
"{" ...
"}" ...
";" ...
"," ...
"." ...
at com.hp.hpl.jena.sparql.lang.ParserSPARQL11.perform(ParserSPARQL11.java:102)
at com.hp.hpl.jena.sparql.lang.ParserSPARQL11.parse$(ParserSPARQL11.java:53)
at com.hp.hpl.jena.sparql.lang.SPARQLParser.parse(SPARQLParser.java:37)
at com.hp.hpl.jena.query.QueryFactory.parse(QueryFactory.java:156)
at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:79)
at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:52)
at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:40)
at loadOnto.load.main(load.java:65)