以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 Semantic Web(语义Web)/描述逻辑/本体 』  (http://bbs.xml.org.cn/list.asp?boardid=2)
----  求助:关于jena中GenericRuleReasoner的结果返回  (http://bbs.xml.org.cn/dispbbs.asp?boardid=2&rootid=&id=64391)


--  作者:skyhui
--  发布时间:7/5/2008 4:47:00 PM

--  求助:关于jena中GenericRuleReasoner的结果返回
各位高人:
        小弟又来求助了。
       我应用层叠推理机(即先用Pellet进行初步推理,在此推理结果基础上,再用自定义规则推理机进行推理)。本体,规则,和两个推理模块如附件中所示。但问题是:运行之后,GenericRuleReasoner推出的结果只能打印(即在eclipse下面显示),但不能进行传递。。。。。。。。
        盼高人指点迷津!
        谢先。
--  作者:skyhui
--  发布时间:7/5/2008 8:44:00 PM

--  
等高人援手!
实现搞不出来了。。。。。
--  作者:jpz6311whu
--  发布时间:7/7/2008 8:19:00 PM

--  
请楼主把你的程序源代码贴出来看看
--  作者:skyhui
--  发布时间:7/7/2008 8:42:00 PM

--  
外挂推理机模块

package jena.examples.ontology.persistentOntology;

import java.util.ArrayList;
import java.util.List;

import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;

import com.hp.hpl.jena.reasoner.ReasonerRegistry;
import com.hp.hpl.jena.reasoner.dig.DIGReasoner;
import com.hp.hpl.jena.reasoner.dig.DIGReasonerFactory;

import com.hp.hpl.jena.vocabulary.ReasonerVocabulary;

public class DIGExample
{

public static void main( String[] args ) {
 
   digExam();

  }
  
  
  public static  List digExam(){
    // set up a configuration resource to connect to the reasoner
    // on port 2004 on the local system
   String xmlns = "http://www.owl-ontologies.com/manufacturing_ontology.owl#";
   String rdfs ="http://www.w3.org/2000/01/rdf-schema#";
   String rdf = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
   String owl = "http://www.w3.org/2002/07/owl#";
   
   
    Model cModel = ModelFactory.createDefaultModel();
    Resource conf = cModel.createResource();
    conf.addProperty( ReasonerVocabulary.EXT_REASONER_URL,
                      cModel.createResource( "http://localhost:8081" ) );
       
    // create the reasoner factory and the reasoner
    DIGReasonerFactory drf = (DIGReasonerFactory) ReasonerRegistry.theRegistry()
                                                                  .getFactory( DIGReasonerFactory.URI );
    DIGReasoner r = (DIGReasoner) drf.create( conf );
        
    // now make a model
    OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_DL_MEM );
    spec.setReasoner( r );
    OntModel m = ModelFactory.createOntologyModel( spec, null );
        
    // load an input document
    m.read("file:D:/manufacturing_ontology.owl");
        
//  list the inconsistent classes
    
   
    
    
    //list what you want

 
       
  Resource Subject = m.getResource(xmlns+"Supplier");
 
//    Property isproducted = m.getProperty(xmlns+"isProducted");
  //   Property product = m.getProperty(xmlns+"Product");
  Property subClassOf = m.getProperty(rdfs+"subClassOf");
  Property type = m.getProperty(rdf+"type");
  Property equivalentClass = m.getProperty(owl+"equivalentClass");
 
  
//  System.out.println("Infer Start:");
//  System.out.println("1111111111111111111111111111111111");
  List list = new ArrayList();
 
 // System.out.println("22222222222");
 
 
//选择本位类且以其为主语 
//  System.out.println("本位类 /主语");
  for ( StmtIterator i=
   //    m.listStatements(null,null,MargheritaPizza);
    m.listStatements(Subject, null,RDFnodes(null));
    i.hasNext();)
     {
       Statement stmt = i.nextStatement();
       list.add(stmt);
    //  System.out.println("/////////////////////////////");
      //  System.out.println(stmt);
     }
 
// 选择本位类且以其为宾语 
//  System.out.println("本位类 /宾语");
  for ( StmtIterator i=
   //    m.listStatements(null,null,MargheritaPizza);
    m.listStatements(null, null,Subject);
    i.hasNext();)
     {
       Statement stmt = i.nextStatement();
       list.add(stmt);
    //  System.out.println("/////////////////////////////");
      //  System.out.println(stmt);
     }
 
 
 
 
 //选择同位类
//  System.out.println("选择同位类");
  for (StmtIterator i= m.listStatements(Subject,equivalentClass,RDFnodes(null));i.hasNext();)
  { 
 //  System.out.println("1111111111111111111111111111111111");
   Statement stmt = i.nextStatement();
      Resource Subject1 = (Resource) stmt.getObject(); 
 //     System.out.println(Subject1);
    //  System.out.println("555555555555555555555");
    for ( StmtIterator j=
      m.listStatements(Subject1,null,RDFnodes(null));j.hasNext();)  
    {
     
     Statement stmt2 = j.nextStatement();
        list.add(stmt2);
   //  System.out.println(stmt2);
    }      
  }
 
  //select 实例
//  System.out.println("实例");
  for (StmtIterator i= m.listStatements(null,type,Subject);i.hasNext();)
  { 
 //  System.out.println("1111111111111111111111111111111111");
   Statement stmt = i.nextStatement();
      Resource Subject2 = (Resource) stmt.getSubject(); 
  //    System.out.println(Subject2);
    //  System.out.println("555555555555555555555");
    for ( StmtIterator j=
      m.listStatements(Subject2,null,RDFnodes(null));j.hasNext();)  
    {
     
     Statement stmt2 = j.nextStatement();
        list.add(stmt2);
   //  System.out.println(stmt2);
    }      
  }
 
 
  //选择下位类
//  System.out.println("下位类");
  for (StmtIterator i= m.listStatements(null,subClassOf,Subject);i.hasNext();)
  { 
 //  System.out.println("1111111111111111111111111111111111");
   Statement stmt = i.nextStatement();
      Resource Subject2 = (Resource) stmt.getSubject(); 
  //    System.out.println(Subject2);
    //  System.out.println("555555555555555555555");
    for ( StmtIterator j=
      m.listStatements(Subject2,null,RDFnodes(null));j.hasNext();)  
    {
     
     Statement stmt2 = j.nextStatement();
        list.add(stmt2);
   //  System.out.println(stmt2);
    }      
  }
 
 
 
  //选择上位类
 // System.out.println("上位类");
  for (StmtIterator i= m.listStatements(Subject,subClassOf,RDFnodes(null));i.hasNext();)
  { 
 //  System.out.println("1111111111111111111111111111111111");
   Statement stmt = i.nextStatement();
      Resource Subject2 = (Resource) stmt.getSubject(); 
   //   System.out.println(Subject2);
    //  System.out.println("555555555555555555555");
    for ( StmtIterator j=
      m.listStatements(Subject2,null,RDFnodes(null));j.hasNext();)  
    {
     
     Statement stmt2 = j.nextStatement();
        list.add(stmt2);
  //   System.out.println(stmt2);
    }      
  }
 
    return list; 
  }
 
 
 

private static RDFNode RDFnodes(Object object) {
 // TODO Auto-generated method stub
 return null;
        }

  }
    

  


--  作者:skyhui
--  发布时间:7/7/2008 8:43:00 PM

--  
自定义规则模块

package jena.examples.ontology.persistentOntology;

import java.util.ArrayList;
import java.util.List;
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.Property;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.reasoner.rulesys.GenericRuleReasoner;
import com.hp.hpl.jena.reasoner.rulesys.Rule;


public class MakeRule {
 public static void main(String[] args)
    {   getRuleInf();
   
      }
   public static List  getRuleInf(){  
    String xmlns = "http://www.owl-ontologies.com/manufacturing_ontology.owl#";
    String rdfs="http://www.w3.org/2000/01/rdf-schema#";
    Model model = ModelFactory.createDefaultModel();
    List listResult = DIGExample.digExam();
    System.out.println("MakeRule Start:");
    Model data = null;
   for(int i = 0; i< listResult.size();i++)
   {
   Statement stmt = (Statement) listResult.get(i);
         data = model.add(stmt);
   }  
  // System.out.println("1111111111111");
  // data.write(System.out,"RDF/XML-ABBREV");
////  Create an (RDF) specification of a hybrid reasoner which
// // loads its data from an external file.
// Model m = ModelFactory.createDefaultModel();
// Resource configuration =  m.createResource();
// configuration.addProperty(ReasonerVocabulary.PROPruleMode, "hybrid");
// configuration.addProperty(ReasonerVocabulary.PROPruleSet,  "D:/Jena-2.5.4/etc/myrules.rules");
//
//   
////  Create an instance of such a reasoner
// Reasoner reasoner = GenericRuleReasonerFactory.theInstance().create(configuration);
//
////  Load test data
// Model data = FileManager.get().loadModel("file:D:/wine.owl");
// InfModel infModel = ModelFactory.createInfModel(reasoner, data);

 
//    //test 11111111111111
   //   Model data = FileManager.get().loadModel("file:D:/p.owl");
  
   System.out.println("1111111111111");
   List rules = Rule.rulesFromURL("file:D:/Jena-2.5.4/etc/myrules.rules");   
   GenericRuleReasoner reasoner = new GenericRuleReasoner(rules);
   System.out.println("11111111114444444444444444444111");
   reasoner.setOWLTranslation(true);   // not needed in RDFS case
      reasoner.setTransitiveClosureCaching(true);
   InfModel infModel = ModelFactory.createInfModel(reasoner,data);
    //   infModel.PROPenableFunctorFiltering(true);
   System.out.println("11111111115555555555555555555555555555111");
//   infModel.write(System.out,"N3");
 //  infModel.getDeductionsModel().write(System.out,"N3");
//   System.out.println("11111111113333333333333333333111");
//   Model date = ModelLoader.loadModel("file:D:/wine.owl");
//   String rules = "[rule: (?X locatedIn 'SonomaRegion') -> print('Its very good' ?X)]";
//   Reasoner reasoner = new GenericRuleReasoner(Rule.parseRules(rules));
//   reasoner=reasoner.bindSchema(date);
//   InfModel infModel = ModelFactory.createInfModel(reasoner,date);
   
   // test RuleExample
//  String ruleString="file:D:/Jena-2.5.4/etc/myrules.rules";
//   OntModel m = ModelFactory.createOntologyModel( OntModelSpec.OWL_DL_MEM_RULE_INF, null );
//      OntModel n = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM, null );
//   
//      List rawRule=((RuleReasoner)m.getReasoner()).getRules();
//   List rule=Rule.rulesFromURL(ruleString);
//   rawRule.addAll(rule);
//   ((RuleReasoner)m.getReasoner()).setRules(rawRule);
//   System.out.println(((RuleReasoner)m.getReasoner()).getRules());
   
   
   infModel.write(System.out, "RDF/XML-ABBREV");
 //  infModel.getDeductionsModel().write(System.out,"N3");
  Resource Margaux = infModel.getResource(xmlns+"Supplier");
//  Property subClass = infModel.getProperty(rdfs+"subClassOf");
//  Property locatedIn = infModel.getProperty(xmlns+"locatedIn");
//  System.out.println("Wine:");
  System.out.println("111111111666666666666666666666661111");
  List list = new ArrayList();
  System.out.println("111111117777777777777777777777711111");
  for ( StmtIterator i=
      infModel.listStatements(Margaux,null,RDFnodes(null));
  i.hasNext();)
  {
    System.out.println("12222222222222111");
    Statement stmt = i.nextStatement();
    list.add(stmt);
 //  System.out.println(stmt);
 
  }
 
  return list;
 
}

private static RDFNode RDFnodes(Object object) {
 // TODO Auto-generated method stub
 return null;
}


}


--  作者:skyhui
--  发布时间:7/7/2008 8:44:00 PM

--  
有劳 jpz6311whu大哥了
--  作者:skyhui
--  发布时间:7/8/2008 9:41:00 AM

--  
我将其改为“直接对原始本体进行自定义推理,然后调用推理项”,仍旧不行。请各位大哥帮忙啊。

自定义模块:
package jena.examples.ontology.persistentOntology;

import java.util.ArrayList;
import java.util.List;
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.Property;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.reasoner.rulesys.GenericRuleReasoner;
import com.hp.hpl.jena.reasoner.rulesys.Rule;
import com.hp.hpl.jena.util.ModelLoader;


public class MakeRule{
 public static void main(String[] args)
    {   getRuleInf();
   
      }
   public static List  getRuleInf(){  
    String xmlns = "http://www.owl-ontologies.com/manufacturing_ontology.owl#";
    String rdfs = "http://www.w3.org/2000/01/rdf-schema#";
    String file = "D:/manufacturing_ontology.owl";
    
    Model data = ModelLoader.loadModel(file);
    
    
 
   List rules = Rule.rulesFromURL("file:D:/Jena-2.5.4/etc/myrules.rules");   
   GenericRuleReasoner reasoner = new GenericRuleReasoner(rules);
 
   reasoner.setOWLTranslation(true);   // not needed in RDFS case
      reasoner.setTransitiveClosureCaching(true);
   InfModel infModel = ModelFactory.createInfModel(reasoner,data);
   


   

 
 
  List list = new ArrayList();
 
  for ( StmtIterator i=
      infModel.listStatements(null,null,RDFnodes(null));
  i.hasNext();)
  {
 
    Statement stmt = i.nextStatement();
    list.add(stmt);

 
  }
 
  return list;
 
}

private static RDFNode RDFnodes(Object object) {
 // TODO Auto-generated method stub
 return null;
}


}

调用模块

package jena.examples.ontology.persistentOntology;

import java.util.ArrayList;
import java.util.List;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;

public class Sparql {
 public static void main(String[] args){
  String xmlns = "http://www.owl-ontologies.com/manufacturing_ontology.owl#";
  String rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
  String rdfs="http://www.w3.org/2000/01/rdf-schema#";
  // List list = OWLReasoner.getInfter();
 
   List list = MakeRule.getRuleInf();
  
   for(int i = 0; i< list.size();i++){
   Statement stmt = (Statement) list.get(i);
   
   System.out.println(i);
 

  }
 
  
 } 
}


W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
125.000ms