Jena下本体的简单检索

http://bbs.w3china.org/dispbbs.asp?boardID=2&ID=69946

Jena下本体的简单检索

以下代码实现的具体功能为:
读入三个不同但相关联的本体,设定1个关键词(此关键词在某本体内为类名,在另一本体内为实体名),查询出跟这关键词相关的总共三个本体内的内容.
希望此代码能帮助初学者了解Jena下使用SPARQL实现本体的查询,以此举一反三.同时也欢迎交流与指错.
(注意:勿忘运行前设好Jena的classpath)
import com.hp.hpl.jena.query.*;
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.reasoner.*;
public class MyOntology{
  public static void main(String[] args) {
    OntModel text_ontmodel = ModelFactory.createOntologyModel();
       OntDocumentManager dm = text_ontmodel.getDocumentManager();
       dm.addAltEntry(" http://www.owl-ontologies.com/Ontology1241779535.owl#","file:" + "Family.owl");
       text_ontmodel.read("file:C:/Protege_3.4/Family.owl");
       String k=" base:Julia ";
       String prefix = "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  base:< http://www.owl-ontologies.com/Ontology1241779535.owl#> ";
       String slect =  "SELECT ?son ?daughter ?father ?mother ?sister ?brother ?marry_with ";
       String where =  "WHERE { "+
                         "OPTIONAL { "+ k + 
                                  " base:son ?son ."+
                         " }"+
                         "OPTIONAL { "+ k +
                                  " base:daughter ?daughter ."+
                         //for <daughter xml:lang="en">Lily</daughter> in <married rdf:ID="Julia">
                         " }"+
                         "OPTIONAL { "+ k +
                                  " base:father ?father ."+
                         " }"+
                         "OPTIONAL { "+ k +
                                  " base:mother ?mother ."+
                         " }"+     
                         "OPTIONAL { "+ k +        
                                  " base:sister ?sister ."+
                         " }"+ 
                         "OPTIONAL { "+ k +
                                  " base:brother ?brother ."+
                         " }"+ 
                         "OPTIONAL { "+ k +
                                  " base:marry_with ?marry_with ."+
                         " }"+
                               //can test "?x rdfs:subClassOf ?y ." +
                               //and "?y base:son \"Jack\"@en ."+
                        " }";
       Query query = QueryFactory.create(prefix + slect + where);
       Reasoner reasoner = ReasonerRegistry.getOWLReasoner();
       InfModel inf = ModelFactory.createInfModel(reasoner, text_ontmodel);
       QueryExecution qe = QueryExecutionFactory.create(query,inf);
       ResultSet results = qe.execSelect();
       ResultSetFormatter.out(System.out,results,query);
       qe.close();
       
       
       
       
       OntModel text_ontmodel2 = ModelFactory.createOntologyModel();
       OntDocumentManager dm2 = text_ontmodel.getDocumentManager();
       dm2.addAltEntry(" http://www.owl-ontologies.com/Ontology1241779535.owl#","file:" + "Family2.owl");
       text_ontmodel2.read("file:C:/Protege_3.4/Family2.owl");
       String prefix2 = "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  base:< http://www.owl-ontologies.com/Ontology1241779535.owl#> ";
       String slect2 =  "SELECT ?relationship ?with ";
       String where2 =  "WHERE { "+
                                "?x owl:onProperty ?relationship ." +
                                //here for <owl:ObjectProperty rdf:ID="daughter_is"/> in <owl:onProperty> in <owl:Restriction> in <owl:Class rdf:ID="Julia">
                                "?x owl:allValuesFrom ?with ."+ k +
                                " rdfs:subClassOf ?x ."+
                                
                        " }";
       Query query2 = QueryFactory.create(prefix2 + slect2 + where2);
       Reasoner reasoner2 = ReasonerRegistry.getOWLReasoner();
       InfModel inf2 = ModelFactory.createInfModel(reasoner2, text_ontmodel2);
       QueryExecution qe2 = QueryExecutionFactory.create(query2,inf2);
       ResultSet results2 = qe2.execSelect();
       ResultSetFormatter.out(System.out,results2,query2);
       qe2.close();
       
       
       
       

       OntModel text_ontmodel3 = ModelFactory.createOntologyModel();
       OntDocumentManager dm3 = text_ontmodel3.getDocumentManager();
       dm3.addAltEntry("http://www.owl-ontologies.com/Ontology1241772983.owl#","file:" + "human_being.owl");
       text_ontmodel3.read("file:C:/Protege_3.4/human_being.owl");
       String prefix3 = "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  base:<http://www.owl-ontologies.com/Ontology1241772983.owl#> ";
       String slect3 = "SELECT ?birthday ?firstname ?familyname ?nationality ";
       String where3 = "WHERE { "+
                         "OPTIONAL { "+ k + 
                                  " base:birthday ?birthday ."+
                         " }"+
                         "OPTIONAL { "+ k +
                                  " base:firstname ?firstname ."+
                         " }"+
                         "OPTIONAL { "+ k +
                                  " base:familyname ?familyname ."+
                         " }"+
                         "OPTIONAL { "+ k +
                                  " base:nationality ?nationality ."+
                         " }"+     
                       " }";
       Query query3 = QueryFactory.create(prefix3 + slect3 + where3);
       Reasoner reasoner3 = ReasonerRegistry.getOWLReasoner();
       InfModel inf3 = ModelFactory.createInfModel(reasoner3, text_ontmodel3);
       QueryExecution qe3 = QueryExecutionFactory.create(query3,inf3);
       ResultSet results3 = qe3.execSelect();
       ResultSetFormatter.out(System.out,results3,query3);
       qe3.close();       


  }
}

附件里面有本代码应用到的三个本体owl文件,另外还有4个关系图帮助理解三本体,其中Ktiiy应为Kitty
 
 
 
此主题相关图片如下:
按此在新窗口浏览图片


此主题相关图片如下:
按此在新窗口浏览图片
此主题相关图片如下:
按此在新窗口浏览图片
此主题相关图片如下:
按此在新窗口浏览图片


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值