如何使用Apache Jena考虑SPARQL中的子类关系?

时间:2019-06-28 18:22:31

标签: java sparql jena ontology

我想向现有的本体添加一个新的子类。通过使用Apache Jena和ru.avicomp.ontapi的以下JAVA代码,我已经可以加载本体并处理数据了。

import java.io.File;

import org.apache.jena.update.UpdateAction;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyStorageException;

import ru.avicomp.ontapi.OntManagers;
import ru.avicomp.ontapi.OntologyManager;
import ru.avicomp.ontapi.OntologyModel;
import ru.avicomp.ontapi.jena.model.OntGraphModel;

public class OntologyManipulator {

    OntGraphModel _base;
    private OntologyManager manager;
    private OntologyModel _ontology;

    private String _uri = "http://www.co-ode.org/ontologies/pizza/2.0.0";
    String _ns = _uri+"#";
    String _prefixRDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
    String _prefixEX = _ns;

    public OntologyManipulator() {

    }

    // reads the ontology into jena
    public void loadOntology(String path) {
        manager = OntManagers.createONT();
        try {
            _ontology = manager.loadOntologyFromOntologyDocument(new File(path));

            _base = _ontology.asGraphModel();

        } catch (OWLOntologyCreationException e) {
            e.printStackTrace();
        }
    }

    private void execSPARQLquery(String _query) {
        UpdateAction.parseExecute(_query, _base);

        _ontology.axioms().forEach(System.out::println);
        try {
            // write update to .owl ontology file
            _ontology.saveOntology();
        } catch (OWLOntologyStorageException e) {
            e.printStackTrace();
        }

        System.out.println("Statement successfully executed.");
    }

    // adds a new Individual to existing pizza.owl ontology
    public void addItemToOntology() {
        String toolName = "Sweden";
        String title = "DescribingTheTitle";
        String description = "DescribingTheIndividual";
        String parentitem = "Country";

        //check if tool exists in current ontology, otherwise delete it
        String deleteclause = "";

        // SPARQL Query:
        String _query = "PREFIX rdf:     <" + _prefixRDF + "> \n" + "PREFIX owl:     <http://www.w3.org/2002/07/owl#>\n"
                + "PREFIX ex:      <" + _prefixEX + "> \n" + deleteclause + "INSERT {ex:" + toolName + " ex:title \""
                + title + "\" ;" + "ex:description \"" + description + "\" ;" + "rdf:type owl:NamedIndividual; rdfs:subClassOf rdf:resource=\"#Country\";"
                + "  .  }\n" + "WHERE { }";

        System.out.println("QUERY: " + _query);
        // parse and execute SPARQL query
        execSPARQLquery(_query);

    }

}

当前,我的SPARQL看起来像这样并且可以正常工作:

PREFIX rdf:     <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 
PREFIX owl:     <http://www.w3.org/2002/07/owl#>
PREFIX ex:      <http://www.co-ode.org/ontologies/pizza/pizza.owl#> 
INSERT {ex:Sweden ex:title "DescribingTheTitle" ;ex:description "DescribingTheIndividual" ;rdf:type owl:NamedIndividual;  .  }
WHERE { }

Protégé识别出插入,并在执行该语句后显示该插入。不幸的是,字体不像其他“个人”字体那样以粗体显示,因为没有子类关系,例如:

  

Protégé(IST)   Protege_NOW

我如何在我的SPARQL中考虑子类关系,所以后言在Protégé中看起来像这样?

  

Protégé(SOLL)   Protege_NTH

我也尝试过输入

rdfs:subClassOf rdf:resource=\"#Country\" 

进入我的INSERT子句。这样做时,我只会得到以下异常:

org.apache.jena.query.QueryException: Line 4, column 122: Unresolved prefixed name: rdfs:subClassOf
    at org.apache.jena.sparql.lang.ParserSPARQL11Update._parse(ParserSPARQL11Update.java:75)
    at org.apache.jena.sparql.lang.ParserSPARQL11Update.parse$(ParserSPARQL11Update.java:40)
    at org.apache.jena.sparql.lang.UpdateParser.parse(UpdateParser.java:39)
    at org.apache.jena.update.UpdateFactory.make(UpdateFactory.java:87)
    at org.apache.jena.update.UpdateFactory.create(UpdateFactory.java:78)
    at org.apache.jena.update.UpdateFactory.create(UpdateFactory.java:56)
    at org.apache.jena.update.UpdateFactory.create(UpdateFactory.java:46)
    at org.apache.jena.update.UpdateAction.parseExecute(UpdateAction.java:136)
    at org.apache.jena.update.UpdateAction.parseExecute(UpdateAction.java:118)
    at org.apache.jena.update.UpdateAction.parseExecute(UpdateAction.java:109)
    at OntologyManipulator.execSPARQLquery(OntologyManipulator.java:27)

我已经阅读了其他一些与SPARQL相关的问题,但是似乎没有人适合我的查询模板,而且官方的SPARQL在INSERT操作方面似乎也很差。为什么?

如果有人能帮助我理解如何在SPARQL中考虑子类关系,我将不胜感激。

0 个答案:

没有答案