You need to enable JavaScript to run this app. 如何编写SPARQL查询从OWL文件中提取hasConcept关联的资源数据
优惠活动
大模型
产品
解决方案
定价
更多

如何编写SPARQL查询从OWL文件中提取hasConcept关联的资源数据

1. 编写SPARQL查询语句

从你给出的OWL文件内容来看,hasConcept属于本体命名空间http://www.semanticweb.org/ontologies/5/test-ontology-2#下的属性,只需要在SPARQL中预先定义该命名空间的前缀,就可以直接调用该属性作为查询条件。

1.1 查询所有hasConcept关联的完整资源IRI

# 定义命名空间前缀
PREFIX ont: <http://www.semanticweb.org/ontologies/5/test-ontology-2#>

SELECT ?concept WHERE {
  # 匹配所有主语通过ont:hasConcept关联的宾语资源
  ?s ont:hasConcept ?concept .
}

查询结果会返回http://www.semanticweb.org/ontologies/5/test-ontology-2#Cong、http://www.semanticweb.org/ontologies/5/test-ontology-2#Func两个完整IRI。

1.2 直接提取#后面的后缀名

如果只需要Cong、Func这类#后的内容,用SPARQL内置的strafter函数做字符串提取即可:

PREFIX ont: <http://www.semanticweb.org/ontologies/5/test-ontology-2#>

SELECT ?localName WHERE {
  ?s ont:hasConcept ?concept .
  # 提取?concept的字符串形式中#后面的部分,赋值给?localName
  BIND(strafter(str(?concept), "#") AS ?localName)
}

1.3 同时查询hasConcept和hasTopic对应的资源

如果要把你提到的Time也一起提取,只需要增加谓词的过滤条件即可:

PREFIX ont: <http://www.semanticweb.org/ontologies/5/test-ontology-2#>

SELECT ?localName WHERE {
  ?s ?p ?resource .
  # 过滤谓词后缀为hasConcept或hasTopic的三元组
  FILTER(strafter(str(?p), "#") IN ("hasConcept", "hasTopic"))
  BIND(strafter(str(?resource), "#") AS ?localName)
}

这个语句的查询结果会同时返回Cong、Func、Time三个值。


2. Jena运行查询的示例代码

你可以直接用以下代码运行上述查询,替换为你本地的OWL文件路径即可:

import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryExecutionFactory;
import org.apache.jena.query.QuerySolution;
import org.apache.jena.query.ResultSet;
import java.io.FileInputStream;
import java.io.IOException;

public class TestQuery {
    public static void main(String[] args) throws IOException {
        // 加载本地OWL文件
        Model model = ModelFactory.createDefaultModel();
        try (FileInputStream fis = new FileInputStream("你的OWL文件本地路径,比如D:/test.owl")) {
            model.read(fis, null);
        }

        // 填入上面写好的SPARQL语句
        String sparqlStr = "PREFIX ont: <http://www.semanticweb.org/ontologies/5/test-ontology-2#>\n" +
                "SELECT ?localName WHERE {\n" +
                "  ?s ?p ?resource .\n" +
                "  FILTER(strafter(str(?p), \"#\") IN (\"hasConcept\", \"hasTopic\"))\n" +
                "  BIND(strafter(str(?resource), \"#\") AS ?localName)\n" +
                "}";

        // 执行查询并输出结果
        Query query = QueryFactory.create(sparqlStr);
        try (QueryExecution qExec = QueryExecutionFactory.create(query, model)) {
            ResultSet rs = qExec.execSelect();
            while (rs.hasNext()) {
                QuerySolution sol = rs.next();
                System.out.println(sol.get("localName").toString());
            }
        }
    }
}

内容的提问来源于stack exchange,提问作者Micheal Nguyen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.11 13:27:01