如何从rdfs:标签获取OntClass?

时间:2013-08-01 09:45:15

标签: java jena ontology

我正在使用Jena / SPARQL,无法从标签中获取类。

我正在使用一个本体,其中所有类都有不同的rdfs:label,并尝试通过在标签上发出请求从我的模型中获取URI或OntClass。

我让我的代码看起来像这样:

------- --------编辑

@Joshua:我尝试了你的代码,不幸的是我仍然没有得到任何结果,这是我改变的代码:

        String labelOfTheClassIWant = "Quantity";

        String queryString = 
            "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 owl: <http://www.w3.org/2002/07/owl#>" +
            "PREFIX dul: <"+RDF.getURI()+">" +

                "SELECT ?c" +
                "WHERE  { ?c rdfs:label '" + labelOfTheClassIWant + "' }";

        Query query = QueryFactory.create(queryString);

        QueryExecution qe = QueryExecutionFactory.create(query, model);
        ResultSet results = qe.execSelect();

        for ( ; results.hasNext() ; ){
            final QuerySolution solution = results.nextSolution();

            System.out.println( "the class I want to find :"+solution.get("c"));}

以下是我的Ontology中为我的代码所需的几行:

<owl:Class rdf:about="#Amount">
<rdfs:label xml:lang="en">Quantity</rdfs:label>

如果您的代码正常工作,我真的不明白我的错误:/ 我将重新检查我的代码,以确保没有任何愚蠢的错误,但我到目前为止我不这么认为。

1 个答案:

答案 0 :(得分:0)

当我们面前没有完整的问题(我们没有数据)时,很难确切地知道问题出在哪里,但是这里有一个包含两个类的小本体,每个类都有一个标签:

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:owl="http://www.w3.org/2002/07/owl#"
    xmlns="http://example.org/labelled-classes#"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
  <owl:Ontology rdf:about="http://example.org/labelled-classes"/>
  <owl:Class rdf:about="http://example.org/labelled-classes#B">
    <rdfs:label>Class B</rdfs:label>
  </owl:Class>
  <owl:Class rdf:about="http://example.org/labelled-classes#A">
    <rdfs:label>Class A</rdfs:label>
  </owl:Class>
</rdf:RDF>

当我运行此查询时

prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>

select ?class where { 
  ?class rdfs:label "Class A"
}

使用Jena的命令行ARQ对数据,我得到了这些结果:

$ arq --data labelled-classes.owl --query classByLabel.sparql
-------------------------------------------
| class                                   |
===========================================
| <http://example.org/labelled-classes#A> |
-------------------------------------------

所以查询看起来没问题。现在问题是我们在耶拿做了什么?以下是一些对相同数据执行相同查询的Java代码:

import java.io.ByteArrayInputStream;

import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;

public class ClassByLabel {
    public static void main(String[] args) {
        final String rdfxmlContent = ""+
                "<rdf:RDF\n" +
                "    xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n" +
                "    xmlns:owl=\"http://www.w3.org/2002/07/owl#\"\n" +
                "    xmlns=\"http://example.org/labelled-classes#\"\n" +
                "    xmlns:xsd=\"http://www.w3.org/2001/XMLSchema#\"\n" +
                "    xmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\">\n" +
                "  <owl:Ontology rdf:about=\"http://example.org/labelled-classes\"/>\n" +
                  "<owl:Class rdf:about=\"http://example.org/labelled-classes#B\">\n" +
                "    <rdfs:label>Class B</rdfs:label>\n" +
                "  </owl:Class>\n" +
                "  <owl:Class rdf:about=\"http://example.org/labelled-classes#A\">\n" +
                "    <rdfs:label>Class A</rdfs:label>\n" +
                "  </owl:Class>\n" +
                "</rdf:RDF>\n" +
                "";
        final String queryString = "" +
                "prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" +
                "\n" +
                "select ?class where {\n" +
                "  ?class rdfs:label \"Class A\"\n" +
                "}\n" +
                "";

        final Model model = ModelFactory.createDefaultModel();
        model.read( new ByteArrayInputStream( rdfxmlContent.getBytes()), null, "RDF/XML" );

        final ResultSet results = QueryExecutionFactory.create( queryString, model ).execSelect();
        while ( results.hasNext() ) {
            final QuerySolution solution = results.nextSolution();
            System.out.println( "?class:\t"+solution.get( "class" ));
            System.out.println( "?o1:\t"+solution.get( "o1" ));
        }
    }
}

输出结果为:

?class: http://example.org/labelled-classes#A
?o1:    null

您的数据可能有些奇怪,但我希望您检索的是o1而不是c。请注意,检索未绑定的变量不会产生错误,只会返回null