2011-10-15 34 views

回答

9

這是我的解決方案。我剛打印出你要的字符串,但希望你可以從這個看到如何使用耶拿OntAPI遍歷本體圖,並挑選出你感興趣的東西。

package examples; 
import java.util.Iterator; 
import com.hp.hpl.jena.ontology.*; 
import com.hp.hpl.jena.rdf.model.ModelFactory; 
import com.hp.hpl.jena.rdf.model.Resource; 

public class PizzaExample 
{ 
    /***********************************/ 
    /* Constants      */ 
    /***********************************/ 
    public static String BASE = "http://www.co-ode.org/ontologies/pizza/pizza.owl"; 
    public static String NS = BASE + "#"; 

    /***********************************/ 
    /* External signature methods  */ 
    /***********************************/ 

    public static void main(String[] args) { 
     new PizzaExample().run(); 
    } 

    public void run() { 
     OntModel m = getPizzaOntology(); 
     OntClass american = m.getOntClass(NS + "American"); 

     for (Iterator<OntClass> supers = american.listSuperClasses(); supers.hasNext();) { 
      displayType(supers.next()); 
     } 
    } 

    /***********************************/ 
    /* Internal implementation methods */ 
    /***********************************/ 

    protected OntModel getPizzaOntology() { 
     OntModel m = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM); 
     m.read(BASE); 
     return m; 
    } 

    protected void displayType(OntClass sup) { 
     if (sup.isRestriction()) { 
      displayRestriction(sup.asRestriction()); 
     } 
    } 

    protected void displayRestriction(Restriction sup) { 
     if (sup.isAllValuesFromRestriction()) { 
      displayRestriction("all", sup.getOnProperty(), sup.asAllValuesFromRestriction().getAllValuesFrom()); 
     } 
     else if (sup.isSomeValuesFromRestriction()) { 
      displayRestriction("some", sup.getOnProperty(), sup.asSomeValuesFromRestriction().getSomeValuesFrom()); 
     } 
    } 

    protected void displayRestriction(String qualifier, OntProperty onP, Resource constraint) { 
     String out = String.format("%s %s %s", 
            qualifier, renderURI(onP), renderConstraint(constraint)); 
     System.out.println("american pizza: " + out); 
    } 

    protected Object renderConstraint(Resource constraint) { 
     if (constraint.canAs(UnionClass.class)) { 
      UnionClass uc = constraint.as(UnionClass.class); 
      // this would be so much easier in ruby ... 
      String r = "union{ "; 
      for (Iterator<? extends OntClass> i = uc.listOperands(); i.hasNext();) { 
       r = r + " " + renderURI(i.next()); 
      } 
      return r + "}"; 
     } 
     else { 
      return renderURI(constraint); 
     } 
    } 

    protected Object renderURI(Resource onP) { 
     String qName = onP.getModel().qnameFor(onP.getURI()); 
     return qName == null ? onP.getLocalName() : qName; 
    } 
} 

將會產生下面的輸出:

american pizza: some pizza:hasTopping pizza:MozzarellaTopping 
american pizza: some pizza:hasTopping pizza:PeperoniSausageTopping 
american pizza: some pizza:hasTopping pizza:TomatoTopping 
american pizza: all pizza:hasTopping union{ pizza:MozzarellaTopping pizza:TomatoTopping pizza:PeperoniSausageTopping} 
0

查閱(用Apache-耶拿3.xx的)有一個可能的問題org.apache.jena.ontology.OntModel和pizza.owl該耶拿-ONT-API在支持OWL- 1只,而比薩則是OWL-2本體。 儘管對於上面的例子來說並不重要(對於OWL1和OWL2來說,'存在量化'的限制看起來是一樣的),但是一般情況下,你不能像使用OntModel一樣容易地處理本體。 作爲一個選項,有一個OntModel替代品,名稱爲ru.avicomp.ontapi.jena.model.OntGraphModel,來自ont-api。它基於與jena OntModel相同的原則。也許這對某人會有所幫助。

用法的例子(只獲得對象的一些值-從限制):

String uri = "http://www.co-ode.org/ontologies/pizza/pizza.owl"; 
    String ns = uri + "#"; 

    OntGraphModel m = ru.avicomp.ontapi.jena.OntModelFactory.createModel(); 
    try (InputStream in = ExamplePizza.class.getResourceAsStream("/pizza.ttl")) { 
     m.read(in, uri, "ttl"); 
    } 
    ru.avicomp.ontapi.jena.model.OntClass clazz = m.getOntEntity(OntClass.class, ns + "American"); 
    OntNOP prop = m.getOntEntity(OntNOP.class, ns + "hasTopping"); 
    clazz.subClassOf() 
      .filter(c -> c.canAs(OntCE.ObjectSomeValuesFrom.class)) 
      .map(c -> c.as(OntCE.ObjectSomeValuesFrom.class)) 
      .filter(c -> prop.equals(c.getOnProperty())) 
      .map(OntCE.Value::getValue) 
      .forEach(System.out::println); 

輸出:

http://www.co-ode.org/ontologies/pizza/pizza.owl#PeperoniSausageTopping(OntClass) 
http://www.co-ode.org/ontologies/pizza/pizza.owl#TomatoTopping(OntClass) 
http://www.co-ode.org/ontologies/pizza/pizza.owl#MozzarellaTopping(OntClass)