新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论Semantic Web(语义Web,语义网或语义万维网, Web 3.0)及相关理论,如:Ontology(本体,本体论), OWL(Web Ontology Langauge,Web本体语言), Description Logic(DL, 描述逻辑),RDFa,Ontology Engineering等。
    [返回] 中文XML论坛 - 专业的XML技术讨论区W3CHINA.ORG讨论区 - Web新技术讨论『 Semantic Web(语义Web)/描述逻辑/本体 』 → 使用Jena对OWL本体进行查询推理 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 101163 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: 使用Jena对OWL本体进行查询推理 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     lipeiqiang1997 帅哥哟,离线,有人找我吗?
      
      
      等级:大二期末(汇编考了97分!)
      文章:84
      积分:418
      门派:XML.ORG.CN
      注册:2008/11/18

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给lipeiqiang1997发送一个短消息 把lipeiqiang1997加入好友 查看lipeiqiang1997的个人资料 搜索lipeiqiang1997在『 Semantic Web(语义Web)/描述逻辑/本体 』的所有贴子 引用回复这个贴子 回复这个贴子 查看lipeiqiang1997的博客21
    发贴心情 

    楼主能不能把你的OWL文件发给大家,让我们也好好学习一下。分享知识,共同提高。希望楼主能和我们分享一下你的资料。
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2009/4/11 22:11:00
     
     lixiaoming 帅哥哟,离线,有人找我吗?
      
      
      等级:大二期末(数据结构考了98分!)
      文章:78
      积分:409
      门派:XML.ORG.CN
      注册:2008/12/8

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给lixiaoming发送一个短消息 把lixiaoming加入好友 查看lixiaoming的个人资料 搜索lixiaoming在『 Semantic Web(语义Web)/描述逻辑/本体 』的所有贴子 引用回复这个贴子 回复这个贴子 查看lixiaoming的博客22
    发贴心情 
    同样希望楼主可以把这个owl给发一下,让我们试验一下
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2009/4/22 21:03:00
     
     shalen2008 帅哥哟,离线,有人找我吗?金牛座1986-5-16
      
      
      等级:大一(猛啃高等数学)
      文章:16
      积分:190
      门派:W3CHINA.ORG
      注册:2008/1/26

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给shalen2008发送一个短消息 把shalen2008加入好友 查看shalen2008的个人资料 搜索shalen2008在『 Semantic Web(语义Web)/描述逻辑/本体 』的所有贴子 点击这里发送电邮给shalen2008 引用回复这个贴子 回复这个贴子 查看shalen2008的博客23
    发贴心情 

    ----------------------------------------------
    喜欢编程,热爱游戏事业

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2009/4/23 10:18:00
     
     cxgnj 帅哥哟,离线,有人找我吗?
      
      
      等级:大一(猛啃高等数学)
      文章:12
      积分:114
      门派:XML.ORG.CN
      注册:2008/10/19

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给cxgnj发送一个短消息 把cxgnj加入好友 查看cxgnj的个人资料 搜索cxgnj在『 Semantic Web(语义Web)/描述逻辑/本体 』的所有贴子 引用回复这个贴子 回复这个贴子 查看cxgnj的博客24
    发贴心情 
    试了一下,得到了结果,多谢楼主!
    有个问题想请教下,本人对查询语句的语法不了解,如果想查询某个专家(假设某个专家的名字为abc)的研究学科,下面的查询语句
    /*查询语句*/
        String queryString = "PREFIX Expert:<http://www.owl-ontologies.com/Expert.owl#> " +
        "SELECT ?expert ?subject " +
        "WHERE {?expert Expert:familiar_with ?subject} ";
    该如何修改?
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2009/5/3 18:20:00
     
     shalen2008 帅哥哟,离线,有人找我吗?金牛座1986-5-16
      
      
      等级:大一(猛啃高等数学)
      文章:16
      积分:190
      门派:W3CHINA.ORG
      注册:2008/1/26

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给shalen2008发送一个短消息 把shalen2008加入好友 查看shalen2008的个人资料 搜索shalen2008在『 Semantic Web(语义Web)/描述逻辑/本体 』的所有贴子 点击这里发送电邮给shalen2008 引用回复这个贴子 回复这个贴子 查看shalen2008的博客25
    发贴心情 
    关于本体的数据库存储我前面就已经写了http://bbs.w3china.org/dispbbs.asp?boardID=2&ID=60494

    ----------------------------------------------
    喜欢编程,热爱游戏事业

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2009/5/4 21:02:00
     
     shalen2008 帅哥哟,离线,有人找我吗?金牛座1986-5-16
      
      
      等级:大一(猛啃高等数学)
      文章:16
      积分:190
      门派:W3CHINA.ORG
      注册:2008/1/26

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给shalen2008发送一个短消息 把shalen2008加入好友 查看shalen2008的个人资料 搜索shalen2008在『 Semantic Web(语义Web)/描述逻辑/本体 』的所有贴子 点击这里发送电邮给shalen2008 引用回复这个贴子 回复这个贴子 查看shalen2008的博客26
    发贴心情 
    我以前写的代码
    package utils;

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;

    import struts.bean.Bean;
    import struts.bean.Book;
    import struts.bean.Encouragement;
    import struts.bean.Expert;
    import struts.bean.Paper;
    import struts.bean.Patent;
    import struts.bean.Project;
    import struts.bean.SearchListBean;

    import com.hp.hpl.jena.db.IDBConnection;
    import com.hp.hpl.jena.ontology.DatatypeProperty;
    import com.hp.hpl.jena.ontology.Individual;
    import com.hp.hpl.jena.ontology.ObjectProperty;
    import com.hp.hpl.jena.ontology.OntClass;
    import com.hp.hpl.jena.ontology.OntModel;
    import com.hp.hpl.jena.ontology.OntModelSpec;
    import com.hp.hpl.jena.ontology.OntProperty;
    import com.hp.hpl.jena.query.Query;
    import com.hp.hpl.jena.query.QueryExecution;
    import com.hp.hpl.jena.query.QueryExecutionFactory;
    import com.hp.hpl.jena.query.QueryFactory;
    import com.hp.hpl.jena.query.QuerySolution;
    import com.hp.hpl.jena.query.ResultSet;
    import com.hp.hpl.jena.query.ResultSetFormatter;
    import com.hp.hpl.jena.rdf.model.InfModel;
    import com.hp.hpl.jena.rdf.model.Model;
    import com.hp.hpl.jena.rdf.model.ModelFactory;
    import com.hp.hpl.jena.rdf.model.ModelMaker;
    import com.hp.hpl.jena.reasoner.Reasoner;
    import com.hp.hpl.jena.reasoner.ReasonerRegistry;
    import com.hp.hpl.jena.reasoner.rulesys.GenericRuleReasoner;
    import com.hp.hpl.jena.reasoner.rulesys.Rule;
    import com.hp.hpl.jena.util.iterator.ExtendedIterator;

    public class JaneUtils {
        public static String subject = "Subject";

        /* 从文件装载模型 */
        public static OntModel loadModel(String filePath) {
            InputStreamReader in;
            OntModel model = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM,
                    null);
            try {
                FileInputStream file = new FileInputStream(filePath);
                in = new InputStreamReader(file, "UTF-8");// 处理中文
                model.read(in, null);
                in.close();
                System.out.println("从文件装载模型成功!");
            } catch (FileNotFoundException e) {
                System.out.println("无法打开本体文件,程序将终止");
                System.exit(0);
            } catch (IOException e) {
                e.printStackTrace();
                System.exit(0);
            }
            return model;
        }

        /* 本体中的各个class,同时将各个class的subclass和property */
        public static void readOntology(OntModel model) {
            // the class number
            int j = 0;

            // list classes
            for (Iterator i = model.listClasses(); i.hasNext();) {
                j++;
                OntClass c = (OntClass) i.next();
                String strClass = c.getModel().usePrefix(c.getURI());
                System.out.println(j + strClass.substring(1));

                // to list sub-classes for each class
                for (Iterator k = c.listSubClasses(true); k.hasNext();) {
                    System.out.print("  " + "hasSubClass");
                    OntClass subclass = (OntClass) k.next();
                    String strSubClass = subclass.getModel().usePrefix(
                            subclass.getURI());

                    System.out.println(strSubClass.substring(1));
                }

                // list property for each class
                for (Iterator y = c.listDeclaredProperties(true); y.hasNext();) {
                    OntProperty property = (OntProperty) y.next();
                    String strPropertyName = property.getModel().usePrefix(
                            property.getURI());

                    String strRange = property.getRange().toString();
                    String strRangeName = property.getModel().usePrefix(strRange);

                    // show just the "has" Properties
                    if (strPropertyName.substring(1).substring(0, 3).equals("has")) {
                        System.out.print("  ");
                        System.out.print(strPropertyName.substring(1));
                        System.out.println(strRangeName.substring(1));
                    }
                }
            }
        }

        /* 查询学科名称 */
        public static String getSujectName() {

            DBPool dbPool = DBPool.getInstance();

            IDBConnection con = dbPool.getConnection();
            OntModel model = JaneUtils.getModelFromDB(con, "expert");

            String NS = "http://www.owl-ontologies.com/Expert.owl#";
            OntClass c = model.getOntClass(NS + "Subject");

            subject = getSub(c);

            dbPool.freeConnection(con);

            return subject;
        }

        /* 查询研究方向名称 */
        public static String getResearchDirection() {
            String researchDirection = "";

            DBPool dbPool = DBPool.getInstance();

            IDBConnection con = dbPool.getConnection();
            OntModel model = JaneUtils.getModelFromDB(con, "expert");

            String NS = "http://www.owl-ontologies.com/Expert.owl#";
            OntClass c = model.getOntClass(NS + "ResearchDirection");

            for (Iterator i = c.listInstances(); i.hasNext();) {
                Individual ont = (Individual) i.next();
                researchDirection += "," + ont.getLocalName();
            }

            dbPool.freeConnection(con);

            researchDirection = researchDirection.substring(1, researchDirection
                    .length());
            return researchDirection;
        }

        /* 递归打印所有子类 */
        public static String getSub(OntClass c) {
            for (Iterator i = c.listSubClasses(false); i.hasNext();) {
                OntClass ont = (OntClass) i.next();
                if (!ont.isAnon()) {// 如果不是匿名类,则打印类的名字
                    // System.out.println(c.getLocalName());
                    subject += "," + ont.getLocalName();
                    getSub(ont);
                }
            }
            return subject;
        }

        /* 查询专家具体信息 */
        public static Expert getDetail(String name1) {
            DBPool dbPool = DBPool.getInstance();
            Expert expert = new Expert();
            IDBConnection con = dbPool.getConnection();
            OntModel model = JaneUtils.getModelFromDB(con, "expert");
            String NS = "http://www.owl-ontologies.com/Expert.owl#";

            Individual ont = model.getIndividual(NS + name1);

            DatatypeProperty ep = model.getDatatypeProperty(NS + "E_mail");
            
            if (ont.getPropertyValue(ep) != null) {
                String e_mail = ont.getPropertyValue(ep).toString();
                e_mail = e_mail.substring(0,e_mail.length()-3);
                System.out.println(e_mail);
                expert.setE_mail(e_mail);
            }

            DatatypeProperty name = model.getDatatypeProperty(NS + "name");
            if (ont.getPropertyValue(name) != null) {
                String name2 = ont.getPropertyValue(name).toString();
                name2 = name2.substring(0,name2.length()-3);
                System.out.println(name2);
                expert.setName(name2);
            }

            DatatypeProperty is_Academician = model.getDatatypeProperty(NS
                    + "is_Academician");
            if (ont.getPropertyValue(is_Academician) != null) {
                String is_Academician1 = ont.getPropertyValue(is_Academician)
                        .toString();
                
                if(is_Academician1.startsWith("true")){
                    expert.setIsAcademician("true");
                }else{
                    expert.setIsAcademician("false");
                }
                System.out.println(expert.getIsAcademician());
            }

            DatatypeProperty work_tel = model.getDatatypeProperty(NS + "work_tel");
            if (ont.getPropertyValue(work_tel) != null) {
                String work_tel1 = ont.getPropertyValue(work_tel).toString();
                work_tel1 = work_tel1.substring(0, work_tel1.length() - 3);
                System.out.println(work_tel1);
                expert.setWorkTel(work_tel1);
            }

            DatatypeProperty workplace = model
                    .getDatatypeProperty(NS + "workplace");
            if (ont.getPropertyValue(workplace) != null) {
                String workplace1 = ont.getPropertyValue(workplace).toString();
                workplace1 = workplace1.substring(0,workplace1.length()-3);
                System.out.println(workplace1);
                expert.setWorkplace(workplace1);
            }

            DatatypeProperty birthday = model.getDatatypeProperty(NS + "birthday");
            if (ont.getPropertyValue(birthday) != null) {
                String birthday1 = ont.getPropertyValue(birthday).toString();
                birthday1 = birthday1.substring(0, 10).trim();
                System.out.println(birthday1);
                
                expert.setBirthday(birthday1);
            }

            DatatypeProperty gender = model.getDatatypeProperty(NS + "gender");
            if (ont.getPropertyValue(gender) != null) {
                String gender1 = ont.getPropertyValue(gender).toString();
                if(gender1.startsWith("true")){
                    expert.setGender("male");
                }else{
                    expert.setGender("female");
                }
                System.out.println(expert.getGender());
            }

            DatatypeProperty Graduation_time = model.getDatatypeProperty(NS
                    + "Graduation_time");
            if (ont.getPropertyValue(Graduation_time) != null) {
                String Graduation_time1 = ont.getPropertyValue(Graduation_time)
                        .toString();
                Graduation_time1 = Graduation_time1.substring(0, 9).trim();
                System.out.println(Graduation_time1);
                expert.setGraduationTime(Graduation_time1);
            }

            DatatypeProperty ID_card = model.getDatatypeProperty(NS + "ID_card");
            if (ont.getPropertyValue(ID_card) != null) {
                String ID_card1 = ont.getPropertyValue(ID_card).toString();
                ID_card1 = ID_card1.substring(0, ID_card1.length() - 3);
                System.out.println(ID_card1);
                expert.setIDCard(ID_card1);
            }
            
            DatatypeProperty resume = model.getDatatypeProperty(NS + "resume");
            if (ont.getPropertyValue(resume) != null) {
                String resume1 = ont.getPropertyValue(resume).toString();
                System.out.println(resume1);
                expert.setResume(resume1);
            }

            DatatypeProperty home_address = model.getDatatypeProperty(NS
                    + "home_address");
            if (ont.getPropertyValue(home_address) != null) {
                String home_address1 = ont.getPropertyValue(home_address)
                        .toString();
                home_address1 = home_address1.substring(0,
                        home_address1.length() - 3);
                System.out.println(home_address1);
                expert.setHomeAddress(home_address1);
            }

            System.out.println(ont.getLocalName());

            ObjectProperty p = model.getObjectProperty(NS + "country_is");
            if (ont.getPropertyValue(p) != null) {
                String p1 = ont.getPropertyValue(p).toString();
                p1 = p1.substring(41,p1.length());
                System.out.println(p1);
                expert.setCountry(p1);
            }

            ObjectProperty research = model.getObjectProperty(NS + "research");
            List<String> SearchDirect = new ArrayList<String>();
            String research2 = "";
            if (ont.listPropertyValues(research) != null) {
                Iterator s = ont.listPropertyValues(research);
                while (s.hasNext()) {
                    research2 = s.next().toString();
                    research2 = research2.substring(41,research2.length());
                    System.out.println(research2);
                    SearchDirect.add(research2);
                }
                expert.setSearchDirect(SearchDirect);
            }
            
            List<Book> book = new ArrayList<Book>();
            String bookURL = "";
            ObjectProperty p2 = model.getObjectProperty(NS + "write_book");
            if (ont.listPropertyValues(p2) != null) {
                Iterator s = ont.listPropertyValues(p2);
                while (s.hasNext()) {
                    Book b = new Book();
                    bookURL = s.next().toString();
                    
                    Individual ont1 = model.getIndividual(bookURL);
                    
                    DatatypeProperty bookName = model.getDatatypeProperty(NS + "book_name");
                    if (ont1.getPropertyValue(bookName) != null) {
                        String bookName1 = ont1.getPropertyValue(bookName).toString();
                        bookName1 = bookName1.substring(0, bookName1.length() - 3);
                        System.out.println("Book:" + bookName1);
                        b.setName(bookName1);
                    }
                    
                    DatatypeProperty publisherTime = model.getDatatypeProperty(NS + "publish_time");
                    if (ont1.getPropertyValue(publisherTime) != null) {
                        String publisherTime1 = ont1.getPropertyValue(publisherTime).toString();
                        publisherTime1 = publisherTime1.substring(0, 10).trim();
                        System.out.println(publisherTime1);
                        b.setPublisherTime(publisherTime1);
                    }
                    
                    ObjectProperty publisher = model.getObjectProperty(NS + "publisher_is");
                    if (ont1.getPropertyValue(publisher) != null) {
                        String publisher1 = ont1.getPropertyValue(publisher).toString();
                        publisher1 = publisher1.substring(41,publisher1.length());
                        System.out.println(publisher1);
                        b.setPublisher(publisher1);
                    }
                    
                    ObjectProperty Property = model.getObjectProperty(NS + "bookProperty_is");
                    if (ont1.getPropertyValue(Property) != null) {
                        String Property1 = ont1.getPropertyValue(Property).toString();
                        Property1 = Property1.substring(41,Property1.length());
                        System.out.println(Property1);
                        b.setProperty(Property1);
                    }
                    
                    ObjectProperty author = model.getObjectProperty(NS + "book_author_is");
                    String author2 = "";
                    String author1 = "";
                    if (ont1.listPropertyValues(author) != null) {
                        Iterator s1 = ont1.listPropertyValues(author);
                        while (s1.hasNext()) {
                            author2 = s1.next().toString();
                            author2 = author2.substring(41,author2.length());                       
                            author1 = author1 + " " + author2;
                        }
                        System.out.println(author1.trim());
                        b.setAuthor(author1.trim());
                    }
                                
                    book.add(b);
                }
                expert.setBook(book);
            }

            List<Encouragement> encouragement = new ArrayList<Encouragement>();
            String encouragementURL = "";
            ObjectProperty encouragement1 = model.getObjectProperty(NS + "get_encouragement");
            if (ont.listPropertyValues(encouragement1) != null) {
                Iterator s = ont.listPropertyValues(encouragement1);
                while (s.hasNext()) {
                    Encouragement en = new Encouragement();
                    encouragementURL = s.next().toString();                
                    Individual ont1 = model.getIndividual(encouragementURL);
                    
                    ObjectProperty author = model.getObjectProperty(NS + "be_got");
                    String author2 = "";
                    String author1 = "";
                    if (ont1.listPropertyValues(author) != null) {
                        Iterator s1 = ont1.listPropertyValues(author);
                        while (s1.hasNext()) {
                            author2 = s1.next().toString();
                            author2 = author2.substring(41,author2.length());
                            System.out.println("encouragement:" + author2);
                            author1 = author1 + " " + author2;
                        }
                        en.setAuthor(author1);
                    }            
                    
                    DatatypeProperty id = model.getDatatypeProperty(NS + "encouragement_num");
                    if (ont1.getPropertyValue(id) != null) {
                        String id1 = ont1.getPropertyValue(id).toString();
                        id1 = id1.substring(0, id1.length() - 38);
                        System.out.println(id1);
                        en.setNumber(id1);
                    }
                    
                    ObjectProperty Property = model.getObjectProperty(NS + "type_is");
                    if (ont1.getPropertyValue(Property) != null) {
                        String Property1 = ont1.getPropertyValue(Property).toString();
                        Property1 = Property1.substring(41,Property1.length());
                        System.out.println(Property1);
                        en.setType(Property1);
                    }
                    encouragement.add(en);
                }
                expert.setEncouragement(encouragement);
            }
            

            List<Paper> paper = new ArrayList<Paper>();
            String paperURL = "";
            ObjectProperty paper2 = model.getObjectProperty(NS + "write");
            if (ont.listPropertyValues(paper2) != null) {
                Iterator s = ont.listPropertyValues(paper2);
                while (s.hasNext()) {
                    Paper paper1 = new Paper();
                    paperURL = s.next().toString();                
                    Individual ont1 = model.getIndividual(paperURL);
                    
                    DatatypeProperty bookName = model.getDatatypeProperty(NS + "paper_name");
                    if (ont1.getPropertyValue(bookName) != null) {
                        String bookName1 = ont1.getPropertyValue(bookName).toString();
                        bookName1 = bookName1.substring(0, bookName1.length() - 3);
                        System.out.println("paper:" + bookName1);
                        paper1.setPaperName(bookName1);
                    }
                    
                    DatatypeProperty indexNum = model.getDatatypeProperty(NS + "index_num");
                    if (ont1.getPropertyValue(indexNum) != null) {
                        String indexNum1 = ont1.getPropertyValue(indexNum).toString();
                        indexNum1 = indexNum1.substring(0, indexNum1.length() - 38);
                        System.out.println(indexNum1);
                        paper1.setIndexNum(indexNum1);
                    }
                    
                    DatatypeProperty is_domestic_paper = model.getDatatypeProperty(NS
                            + "is_domestic_paper");
                    if (ont1.getPropertyValue(is_domestic_paper) != null) {
                        String is_domestic_paper1 = ont1.getPropertyValue(is_domestic_paper)
                                .toString();
                        
                        if(is_domestic_paper1.startsWith("true")){
                            paper1.setDomesticPaper("true");
                        }else{
                            paper1.setDomesticPaper("false");
                        }
                        System.out.println(paper1.getIsDomesticPaper());
                    }
                    
                    ObjectProperty author = model.getObjectProperty(NS + "author_is");
                    String author2 = "";
                    String author1 = "";
                    if (ont1.listPropertyValues(author) != null) {
                        Iterator s1 = ont1.listPropertyValues(author);
                        while (s1.hasNext()) {
                            author2 = s1.next().toString();
                            author2 = author2.substring(41,author2.length());
                            author1 = author1 + " " + author2;
                        }
                        System.out.println(author1.trim());
                        paper1.setAuthor(author1.trim());
                    }
                    
                    ObjectProperty firstAuthor = model.getObjectProperty(NS + "first_author_is");
                    String firstAuthor2 = "";
                    String firstAuthor1 = "";
                    if (ont1.listPropertyValues(firstAuthor) != null) {
                        Iterator s1 = ont1.listPropertyValues(firstAuthor);
                        while (s1.hasNext()) {
                            firstAuthor2 = s1.next().toString();
                            firstAuthor2 = firstAuthor2.substring(41,firstAuthor2.length());
                            firstAuthor1 = firstAuthor1 + " " + firstAuthor2;
                        }
                        System.out.println(firstAuthor1.trim());
                        paper1.setFirstAuthor(firstAuthor1.trim());
                    }
                    
                    ObjectProperty Property = model.getObjectProperty(NS + "publication_is");
                    if (ont1.getPropertyValue(Property) != null) {
                        String Property1 = ont1.getPropertyValue(Property).toString();
                        Property1 = Property1.substring(41,Property1.length());
                        System.out.println(Property1);
                        paper1.setPublication(Property1);
                    }
                    
                    DatatypeProperty deliver_time = model.getDatatypeProperty(NS
                            + "deliver_time");
                    if (ont1.getPropertyValue(deliver_time) != null) {
                        String deliver_time1 = ont1.getPropertyValue(deliver_time)
                                .toString();
                        deliver_time1 = deliver_time1.substring(0, 10).trim();
                        System.out.println(deliver_time1);
                        paper1.setDeliverTime(deliver_time1);
                    }
                    paper.add(paper1);               
                }
                expert.setPaper(paper);
            }

            List<Patent> patent = new ArrayList<Patent>();
            String patentURL = "";
            ObjectProperty patent2 = model.getObjectProperty(NS + "create_patent");
            if (ont.listPropertyValues(patent2) != null) {
                Iterator s = ont.listPropertyValues(patent2);
                while (s.hasNext()) {
                    Patent patent1 = new Patent();
                    patentURL = s.next().toString();                
                    Individual ont1 = model.getIndividual(patentURL);
                    
                    DatatypeProperty bookName = model.getDatatypeProperty(NS + "patend_name");
                    if (ont1.getPropertyValue(bookName) != null) {
                        String bookName1 = ont1.getPropertyValue(bookName).toString();
                        bookName1 = bookName1.substring(0, bookName1.length() - 3);
                        System.out.println("Patent:" + bookName1);
                        patent1.setName(bookName1);
                    }
                    
                    DatatypeProperty id = model.getDatatypeProperty(NS + "patent_id");
                    if (ont1.getPropertyValue(id) != null) {
                        String id1 = ont1.getPropertyValue(id).toString();
                        id1 = id1.substring(0, id1.length() - 3);
                        System.out.println(id1);
                        patent1.setId(id1);
                    }
                    
                    DatatypeProperty publisherTime = model.getDatatypeProperty(NS + "pass_time");
                    if (ont1.getPropertyValue(publisherTime) != null) {
                        String publisherTime1 = ont1.getPropertyValue(publisherTime).toString();
                        publisherTime1 = publisherTime1.substring(0, 10).trim();
                        System.out.println(publisherTime1);
                        patent1.setPassTime(publisherTime1);
                    }
                    
                    ObjectProperty publisher = model.getObjectProperty(NS + "patent_type_is");
                    if (ont1.getPropertyValue(publisher) != null) {
                        String publisher1 = ont1.getPropertyValue(publisher).toString();
                        publisher1 = publisher1.substring(41,publisher1.length());
                        System.out.println(publisher1);
                        patent1.setType(publisher1);
                    }
                    
                    ObjectProperty author = model.getObjectProperty(NS + "be_created");
                    String author2 = "";
                    String author1 = "";
                    if (ont1.listPropertyValues(author) != null) {
                        Iterator s1 = ont1.listPropertyValues(author);
                        while (s1.hasNext()) {
                            author2 = s1.next().toString();
                            author2 = author2.substring(41,author2.length());
                            author1 = author1 + " " + author2;
                        }
                        System.out.println(author1.trim());
                        patent1.setAuthor(author1.trim());
                    }
                    patent.add(patent1);
                }
                expert.setPatent(patent);
            }

            List<Project> project = new ArrayList<Project>();
            String projectURL = "";
            ObjectProperty project2 = model.getObjectProperty(NS + "lead");
            if (ont.listPropertyValues(project2) != null) {
                Iterator s = ont.listPropertyValues(project2);
                while (s.hasNext()) {
                    Project project1 = new Project();
                    projectURL = s.next().toString();                
                    Individual ont1 = model.getIndividual(projectURL);
                    
                    DatatypeProperty bookName = model.getDatatypeProperty(NS + "project_name");
                    if (ont1.getPropertyValue(bookName) != null) {
                        String bookName1 = ont1.getPropertyValue(bookName).toString();
                        bookName1 = bookName1.substring(0, bookName1.length() - 3);
                        System.out.println("Project:" + bookName1);
                        project1.setProjectName(bookName1);
                    }
                    
                    ObjectProperty publisher = model.getObjectProperty(NS + "property_is");
                    if (ont1.getPropertyValue(publisher) != null) {
                        String publisher1 = ont1.getPropertyValue(publisher).toString();
                        publisher1 = publisher1.substring(41,publisher1.length());
                        System.out.println(publisher1);
                        project1.setProperty(publisher1);
                    }
                    
                    DatatypeProperty publisherTime = model.getDatatypeProperty(NS + "project_begin_time");
                    if (ont1.getPropertyValue(publisherTime) != null) {
                        String publisherTime1 = ont1.getPropertyValue(publisherTime).toString();
                        publisherTime1 = publisherTime1.substring(0, 10).trim();
                        System.out.println(publisherTime1);
                        project1.setProjectBeginTime(publisherTime1);
                    }
                    
                    DatatypeProperty project_end_time = model.getDatatypeProperty(NS + "project_end_time");
                    if (ont1.getPropertyValue(project_end_time) != null) {
                        String project_end_time1 = ont1.getPropertyValue(project_end_time).toString();
                        project_end_time1 = project_end_time1.substring(0, 10).trim();
                        System.out.println(project_end_time1);
                        project1.setProjectEndTime(project_end_time1);
                    }
                    
                    DatatypeProperty indexNum = model.getDatatypeProperty(NS + "Cost");
                    if (ont1.getPropertyValue(indexNum) != null) {
                        String indexNum1 = ont1.getPropertyValue(indexNum).toString();
                        indexNum1 = indexNum1.substring(0, indexNum1.length() - 38);
                        System.out.println(indexNum1);
                        project1.setCost(indexNum1);
                    }
                    
                    project.add(project1);
                }
                expert.setProject(project);
            }

            dbPool.freeConnection(con);
            
            return expert;
        }

        /* 简单读取本体中的各个class */
        public static void SimpleReadOntology(OntModel model) {
            for (Iterator i = model.listClasses(); i.hasNext();) {
                OntClass c = (OntClass) i.next();
                if (!c.isAnon()) {// 如果不是匿名类,则打印类的名字
                    System.out.println(c.getLocalName());
                }
            }
        }

        public static OntModelSpec getModelSpec(ModelMaker maker) {
            /*
             * create a spec for the new ont model that will use no inference over
             * models made by the given maker (which is where we get the persistent
             * models from)
             */
            OntModelSpec spec = new OntModelSpec(OntModelSpec.OWL_MEM);
            spec.setImportModelMaker(maker);
            return spec;
        }

        /* 从文件读取本体并将其存入数据库 */
        public static OntModel createDBModelFromFile(IDBConnection con,
                String name, String filePath) {
            ModelMaker maker = ModelFactory.createModelRDBMaker(con);
            Model base = maker.createModel(name);
            OntModel newmodel = ModelFactory.createOntologyModel(
                    getModelSpec(maker), base);
            newmodel.read(filePath);
            System.out.println("本体成功存入数据库!");
            return newmodel;
        }

        /* 从数据库中得到已存入本体 */
        public static OntModel getModelFromDB(IDBConnection con, String name) {
            ModelMaker maker = ModelFactory.createModelRDBMaker(con);
            Model base = maker.getModel(name);
            OntModel newmodel = ModelFactory.createOntologyModel(
                    getModelSpec(maker), base);
            System.out.println("本体从数据库读取成功!");
            return newmodel;
        }

        /*获取某学科类的实例名*/
        public static String getSubInstance(String sub){
            String si = "";
            DBPool dbPool = DBPool.getInstance();
            IDBConnection con = dbPool.getConnection();
            OntModel model = JaneUtils.getModelFromDB(con, "expert");
            String NS = "http://www.owl-ontologies.com/Expert.owl#";
            
            OntClass c = model.getOntClass(NS + sub);
            for (Iterator i = c.listInstances(); i.hasNext();) {
                Individual ont = (Individual) i.next();
                si = ont.getLocalName();
            }

            System.out.println(si);
            
            dbPool.freeConnection(con);
            return si;
        }
        
        /* 获取个体及其属性值 */
        public static void getInstance(OntModel model) {
            String NS = "http://www.owl-ontologies.com/Expert.owl#";
            /* 得到本体中的Expert类 */
            OntClass expert = model.getOntClass(NS + "Expert");
            // 打印类名
            System.out.println(expert.getLocalName());

            // 获得其所以个体
            ExtendedIterator it = expert.listInstances();
            // 打印其个体
            while (it.hasNext()) {
                Individual oi = (Individual) it.next();
                System.out.println(oi.getLocalName());

                for (Iterator ipp = expert.listDeclaredProperties(); ipp.hasNext();) {
                    OntProperty p = (OntProperty) ipp.next();
                    System.out.println("  associated property: " + p.getLocalName()
                            + " : " + oi.getPropertyValue(p));
                }// property ends
            }
        }

        /* 使用sparql对本体进行查询 */
        public static List<SearchListBean> search(Bean bean) {
            DBPool dbPool = DBPool.getInstance();

            IDBConnection con = dbPool.getConnection();
            OntModel model = JaneUtils.getModelFromDB(con, "expert");

            String NS = "http://www.owl-ontologies.com/Expert.owl#";

            /* 设置规则 */
            String rule = "[rule1:(?x http://www.owl-ontologies.com/Expert.owl#research ?y), "
                    + " (?s http://www.owl-ontologies.com/Expert.owl#has_research_direction ?y), "
                    + " (?s http://www.owl-ontologies.com/Expert.owl#associate ?z) "
                    + " ->(?x http://www.owl-ontologies.com/Expert.owl#familiar_with ?z)] " +
                            "[rule2:(?a http://www.owl-ontologies.com/Expert.owl#familiar_with ?b), " +
                            "(?b http://www.owl-ontologies.com/Expert.owl#is_a ?c) " +
                            "->(?a http://www.owl-ontologies.com/Expert.owl#familiar_with ?c)] ";

            System.out.println(rule);
            
            /* 查询语句 */
            // String queryString = "PREFIX
            // Expert:<http://www.owl-ontologies.com/Expert.owl#> " +
            // "SELECT ?expert ?ResearchDirection " +
            // "WHERE {?expert Expert:research ?ResearchDirection} ";
            String queryString1 = "PREFIX Expert:<http://www.owl-ontologies.com/Expert.owl#> "
                    + "SELECT ?name ?resume "
                    + "WHERE {?expert Expert:resume ?resume. ?expert Expert:name ?name.";
            
            String queryString2 = "PREFIX Expert:<http://www.owl-ontologies.com/Expert.owl#> "
                + "SELECT ?name ?resume "
                + "WHERE {?expert Expert:resume ?resume. ?expert Expert:name ?name.}";
            
            String queryString = "PREFIX Expert:<http://www.owl-ontologies.com/Expert.owl#> "
                + "SELECT ?name ?gender ?TechnicalTitle ?research ?resume "
                + "WHERE {?expert Expert:resume ?resume. " +
                        "?expert Expert:name ?name." +
                        "?expert Expert:gender ?gender." +
                        "?expert Expert:title_code_is ?TechnicalTitle." +
                        "?expert Expert:research ?research.}";
            
            //?expert Expert:familiar_with ?subject.
            //?expert Expert:research ?research.
            // "WHERE {?expert Expert:resume ?resume.?expert Expert:name
            // ?name.?expert Expert:work_tel \"8869565\"}";
            // "WHERE {?expert Expert:resume ?resume.?expert Expert:name ?name.
            // FILTER(?name=\"ChenGuoda\")}";

            if(!bean.getName().equals("")){
                queryString1 += "?expert Expert:name \""+ bean.getName()+"\"@en.";             
            }
            if(!bean.getGender().equals("")){
                queryString1 += "?expert Expert:gender "+ bean.getGender()+".";             
            }
            if(!bean.getTechnicalTitle().equals("other")){
                queryString1 += "?expert Expert:title_code_is Expert:"+ bean.getTechnicalTitle()+".";             
            }
            if(!bean.getResearchDirection().equals("")){
                queryString1 += "?expert Expert:research Expert:"+ bean.getResearchDirection()+".";             
            }
            if(!bean.getSubject().equals("")){
                queryString1 += "?expert Expert:familiar_with Expert:"+ getSubInstance(bean.getSubject())+".";             
            }
            queryString1 += "}";
            
            System.out.println(queryString1);

            /* 创建推理机 */
            Reasoner reasoner2 = new GenericRuleReasoner(Rule.parseRules(rule));

            InfModel inf = ModelFactory.createInfModel(reasoner2, model);
            Query query = QueryFactory.create(queryString1);

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

            List<SearchListBean> searchList = new ArrayList<SearchListBean>();

            while (results.hasNext()) {
                QuerySolution qs = (QuerySolution) results.next();
                searchList.add(new SearchListBean(
                        qs.getLiteral("name").getString(), qs.getLiteral("resume")
                                .getString()));
                System.out.println(qs.getLiteral("name").getString());
            }


            /* 打印结果 */
            //ResultSetFormatter.out(System.out, results, query);
            qe.close();
            dbPool.freeConnection(con);

            return searchList;
        }

        /* 使用sparql对本体进行查询 */
        public static void searchOnto(OntModel model) {
            Reasoner reasoner = ReasonerRegistry.getOWLMicroReasoner();

            // String rules = "[Rule1:(?x research ?y)(?y associat ?z)->(?x
            // familiar_with ?z)]";
            // reasoner.bind(Rule.parseRule(rules));

            String queryString = "PREFIX Expert:<http://www.owl-ontologies.com/Expert.owl#> "
                    + "SELECT ?expert ?subject "
                    + "WHERE {?expert Expert:familiar_with ?subject} ";

            InfModel inf = ModelFactory.createInfModel(reasoner, model);
            Query query = QueryFactory.create(queryString);

            // Execute the query and obtain results
            QueryExecution qe = QueryExecutionFactory.create(query, inf);
            ResultSet results = qe.execSelect();
            ResultSetFormatter.out(System.out, results, query);
            qe.close();
        }

        /* 使用sparql对本体进行查询 */
        public static void searchOnto1(OntModel model) {
            /* 设置规则 */
            String rule = "[rule1:(?x http://www.owl-ontologies.com/Expert.owl#research ?y) "
                    + "(?y http://www.owl-ontologies.com/Expert.owl#associate ?z) "
                    + "->(?x http://www.owl-ontologies.com/Expert.owl#familiar_with ?z)]";

            /* 查询语句 */
            String queryString = "PREFIX Expert:<http://www.owl-ontologies.com/Expert.owl#> "
                    + "SELECT ?expert ?subject "
                    + "WHERE {?expert Expert:familiar_with ?subject} ";

            /* 创建推理机 */
            Reasoner reasoner2 = new GenericRuleReasoner(Rule.parseRules(rule));

            InfModel inf = ModelFactory.createInfModel(reasoner2, model);
            Query query = QueryFactory.create(queryString);

            QueryExecution qe = QueryExecutionFactory.create(query, inf);
            ResultSet results = qe.execSelect();
            /* 打印结果 */
            ResultSetFormatter.out(System.out, results, query);
            qe.close();
        }
    }

    ----------------------------------------------
    喜欢编程,热爱游戏事业

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2009/5/4 21:05:00
     
     fairyxiaojun 美女呀,离线,快来找我吧!
      
      
      等级:大一新生
      文章:4
      积分:69
      门派:XML.ORG.CN
      注册:2009/4/9

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给fairyxiaojun发送一个短消息 把fairyxiaojun加入好友 查看fairyxiaojun的个人资料 搜索fairyxiaojun在『 Semantic Web(语义Web)/描述逻辑/本体 』的所有贴子 引用回复这个贴子 回复这个贴子 查看fairyxiaojun的博客27
    发贴心情 
    请问要查询expert的实例的属性值name该怎么改呢?
    比如要查询ChenGuoda 熟悉的subject代码该怎么改呢?
    是不是
    "WHERE {?expert Expert:familiar_with ?subject.?expert Expert:name
           ?name.?expert Expert:name \"ChenGuoda\"}";

    十分感谢!!

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2009/5/18 16:58:00
     
     jsupeng 帅哥哟,离线,有人找我吗?
      
      
      等级:大一新生
      文章:4
      积分:85
      门派:XML.ORG.CN
      注册:2006/9/27

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给jsupeng发送一个短消息 把jsupeng加入好友 查看jsupeng的个人资料 搜索jsupeng在『 Semantic Web(语义Web)/描述逻辑/本体 』的所有贴子 引用回复这个贴子 回复这个贴子 查看jsupeng的博客28
    发贴心情 
    太好了。
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2009/11/10 21:45:00
     
     binyu3008 帅哥哟,离线,有人找我吗?
      
      
      等级:大一(猛啃高等数学)
      文章:16
      积分:108
      门派:XML.ORG.CN
      注册:2009/8/19

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给binyu3008发送一个短消息 把binyu3008加入好友 查看binyu3008的个人资料 搜索binyu3008在『 Semantic Web(语义Web)/描述逻辑/本体 』的所有贴子 引用回复这个贴子 回复这个贴子 查看binyu3008的博客29
    发贴心情 
    你好,我按照你的代码持久化存储了我的本体,通过通用规则推理机进行了推理,得到了新知识,可是数据库却没有得到更新,查不到新知识,请问这个问题你有没有遇到过,怎么样修改的?
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2010/3/20 15:50:00
     
     angleangleangle 美女呀,离线,快来找我吧!
      
      
      等级:大二(研究汇编)
      文章:42
      积分:271
      门派:XML.ORG.CN
      注册:2010/3/23

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给angleangleangle发送一个短消息 把angleangleangle加入好友 查看angleangleangle的个人资料 搜索angleangleangle在『 Semantic Web(语义Web)/描述逻辑/本体 』的所有贴子 引用回复这个贴子 回复这个贴子 查看angleangleangle的博客30
    发贴心情 
    请问楼主查询语言是在什么环境下实现的呢?
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2010/4/19 15:42:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 Semantic Web(语义Web)/描述逻辑/本体 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/4/28 22:35:13

    本主题贴数47,分页: [1] [2] [3] [4] [5]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    3,140.625ms