-- 作者: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; } }
|