用本体模型来创建本体,并获取本体实例的属性(易)

Posted on 2011-09-02 08:34  语义网博客  阅读(1306)  评论(0)    收藏  举报

对于我们学习的语义网来说,本体是至关重要的。能够熟练的掌握本体能给我们去认识语义网有很大的帮助。那我们去熟悉本体该如何让下手呢?当然,我们应该首先明白本体的结构。下面的例子我们将演示如何创建一个简单的本体,并将我们添加的实例属性读取出来。
1、使用本体模型创建本体
                     OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.DAML_MEM);
                FileInputStream inputStream = new FileInputStream(new File("D:\\OWL\\createTest.owl"));
                ontModel.read(inputStream,null,"RDF/XML");
               
                Resource r1 = ontModel.createResource(this.defaultNamespace+"gaolei");
                Individual i1 = ontModel.createIndividual(this.defaultNamespace+"gaolei",r1);
               
                DatatypeProperty name= ontModel.createDatatypeProperty(this.defaultNamespace+"name");
                DatatypeProperty age= ontModel.createDatatypeProperty(this.defaultNamespace+"age");
                DatatypeProperty sex= ontModel.createDatatypeProperty(this.defaultNamespace+"sex");
               
                i1.addProperty(name, "gaolei");
                i1.addProperty(age, "22");
                i1.addProperty(sex, "boy");
               
                String filepath = "D:\\OWL\\createOWL.owl";
                FileOutputStream outputStream = new FileOutputStream(filepath);
                OutputStreamWriter sw = new OutputStreamWriter(outputStream,"UTF-8");
                ontModel.write(sw);

2、获取本体实例的属性
         public CreateOWLTest(){
                String owlpath=null;
                String namespace=null;
               
                namespace="http://www.biview.org/2011/7/ontology";
                owlpath= "D:\\OWL\\createOWL.owl";
                readFileMode(this.model, namespace, owlpath);
        }
        public static void readFileMode(Model _model, String namespace,String owlpath) {

                InputStream inFoafInstance = FileManager.get().open(owlpath);
                _model.read(inFoafInstance, namespace);
               
                try {
                        inFoafInstance.close();
                } catch (IOException e) {
                        e.printStackTrace();
                }
        }
        private StringBuffer getQueryStr(){
               
                StringBuffer queryStr=new StringBuffer();
               
                queryStr.append(" PREFIX sta: <http://www.biview.org/2011/7/ontology/> ");
               
                queryStr.append(" select ?people ?name ?age ?sex " +
                                " where { " +
                                " ?people sta:name \"gaolei\" . " +
                                " ?people sta:name ?name . " +
                                " ?people sta:age ?age . " +
                                " ?people sta:sex ?sex . " +
                                " } ");
               
                return queryStr;
        }
       
        private void SparqlResult(StringBuffer queryStr) throws IOException{
               
                Query query=QueryFactory.create(queryStr.toString());
                QueryExecution qexec=QueryExecutionFactory.create(query,model);
                 try{
                         ResultSet response=qexec.execSelect();
                         
                         while(response.hasNext()){
                                 QuerySolution soln=response.nextSolution();
                                 System.out.println("people="+soln.get("?people").asNode().getLocalName());
                                 
                                 RDFNode name=soln.get("?name");
                                 RDFNode age=soln.get("?age");
                                 RDFNode sex=soln.get("?sex");
                                 
                                 if(name != null&&age != null&&sex != null){
                                         System.out.println(name.toString()+","+sex.toString()+",年龄:"+age.toString());
                                 }else{
                                         System.out.println("no result ............");
                                 }
                         }
                 }finally{qexec.close();}
        }

总结:虽然这只是一个简单的例子,但是从里面我们看出来本体的基本结构,我们可以通过自己手动创建本体来更好的认识本体里面的构造!

博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3