显示在jena中的protege中创建的本体的数据类型属性值

时间:2014-09-18 07:03:42

标签: java jena owl protege

我已经在protege中创建了一个本体,并希望显示对象属性的值并将值保存在一个数组中,以便我可以使用该值来执行推理。问题是我无法仅检索数据类型值域和范围正在显示,但没有错误请帮助找到解决方案

      import java.io.IOException;
 import com.hp.hpl.jena.ontology.DatatypeProperty;
 import com.hp.hpl.jena.ontology.EnumeratedClass;
 import com.hp.hpl.jena.ontology.Individual;
 import com.hp.hpl.jena.ontology.OntClass;
 import com.hp.hpl.jena.ontology.OntModel;
 import com.hp.hpl.jena.ontology.OntResource;
 import com.hp.hpl.jena.rdf.model.Model;
 import com.hp.hpl.jena.rdf.model.ModelFactory;
 import com.hp.hpl.jena.rdf.model.RDFNode;
 import com.hp.hpl.jena.util.FileManager;
 import com.hp.hpl.jena.util.iterator.ExtendedIterator;
 public class GetStartedSemWeb {
 static String defaultNameSpace ="http://semanticweb.org/ontologies#";
 Model schema = null;
  public static void main(String[] args) throws IOException 
  {
    GetStartedSemWeb getsemweb = new GetStartedSemWeb();
    System.out.println(" Adding  student ontology ");
    getsemweb.loadontology();
    }
   private void loadontology() throws IOException
    {
   schema = ModelFactory.createOntologyModel();
      java.io.InputStream inschema =                    FileManager.get().open("C:/Users/Desktop/Documents/Extracting knowledge from ontology               using jena/getstarted.owl");
   schema.read(inschema,defaultNameSpace);
    System.out.println("new  ontology added");

   ExtendedIterator it = ((OntModel) schema).listClasses();
   while(it.hasNext()) 
     {
           OntClass cls= (OntClass)it.next();
            System.out.println("URI of classes of Merged University Ontology                are "+cls.getURI());
            ExtendedIterator pinstance = ((OntClass)cls).listInstances();
            while(pinstance.hasNext()) 
            {
                Individual pinstance1= (Individual)pinstance.next();
                        System.out.println("Individual of          "                        +cls.getLocalName()  +      pinstance1.getLocalName());
            ExtendedIterator dp = ((OntModel)                                                                                 schema).listDatatypeProperties();
                         while(dp.hasNext())
                        {
                            DatatypeProperty p = (DatatypeProperty) dp.next();
                            if (p.isDatatypeProperty() && p.getDomain()!=null &&                        p.getRange()!=null)
                            {
                                System.out.println("Data Property Name: "+                          p.getLocalName());
                                System.out.println("Domain: "+                                  p.getDomain().getLocalName());
                                EnumeratedClass e = null;
                                ExtendedIterator i = null;
                                if(p.getRange().asClass().isEnumeratedClass())
                                {
                                    e = p.getRange().asClass().asEnumeratedClass();
                                    i = e.getOneOf().iterator();

                                   RDFNode prop = null;
                                     String s=null;
                                    System.out.println("Range: ");
                                    while(i.hasNext())
                                    {
                                        prop = (RDFNode) i.next();
             s=((Object)   prop).getClass().toString().split("\\^\\^")[0];

             RDFNode propvalue = ((OntResource) prop).getPropertyValue(p);                                  
        System.out.println(" Property value is"         +propvalue);
                                        System.out.println(s);
                                    }
                                }
                                else
                    System.out.println("Range: "+ p.getRange().getLocalName());
                                }

                                System.out.println("\n");
                            }

                        }

            }

}               
schema.close();

}               }

        OWL file content

          // OWL file content
                      <rdf:RDFxml:base="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl"><owl:Ontology rdf:about="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl"><owl:versionIRI rdf:resource="http://www.semanticweb.org/ontologies/2013/9/Ontology1382504980350.owl"/></owl:Ontology><!-- 
///////////////////////////////////////////////////////////////////////////////////////
//
// Datatypes
//
///////////////////////////////////////////////////////////////////////////////////////
 --><!-- 
///////////////////////////////////////////////////////////////////////////////////////
//
// Object Properties
//
///////////////////////////////////////////////////////////////////////////////////////
 --><!-- http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#hasaAcquired -   -><owl:ObjectProperty    rdf:about="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#hasaAcquired"> <rdfs:range rdf:resource="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#Academic"/><rdfs:domain rdf:resource="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#Student"/></owl:ObjectProperty><!-- http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#isAcquiredBy --><owl:ObjectProperty rdf:about="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#isAcquiredBy"><rdfs:domain rdf:resource="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#Academic"/><rdfs:range rdf:resource="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#Student"/><owl:inverseOf rdf:resource="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#hasaAcquired"/></owl:ObjectProperty><!-- 
///////////////////////////////////////////////////////////////////////////////////////
//
// Data properties
//
///////////////////////////////////////////////////////////////////////////////////////
 --><!-- http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#hasAge --> <owl:DatatypeProperty r df:about="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#hasAge"><rdfs:domain rdf:resource="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#Student"/><rdfs:range rdf:resource="http://www.w3.org/2002/07/owl#real"/></owl:DatatypeProperty><!-- http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#hasAggregate --><owl:DatatypeProperty rdf:about="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#hasAggregate"><rdfs:domain rdf:resource="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#Student"/><rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#integer"/></owl:DatatypeProperty><!-- http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#hasEmailaddress --><owl:DatatypeProperty rdf:about="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#hasEmailaddress"><rdfs:domain rdf:resource="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#Student"/><rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#string"/></owl:DatatypeProperty><!-- http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#hasGender --><owl:DatatypeProperty rdf:about="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#hasGender"><rdfs:domain rdf:resource="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#Student"/><rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#string"/></owl:DatatypeProperty><!-- http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#hasName --><owl:DatatypeProperty rdf:about="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#hasName"><rdfs:domain rdf:resource="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#Student"/><rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#string"/></owl:DatatypeProperty><!-- http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#hasPostaladress --><owl:DatatypeProperty rdf:about="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#hasPostaladress"><rdfs:domain rdf:resource="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#Student"/><rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#string"/></owl:DatatypeProperty><!-- 
///////////////////////////////////////////////////////////////////////////////////////
//
// Classes
//
///////////////////////////////////////////////////////////////////////////////////////
 --><!-- http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#Academic --><owl:Class rdf:about="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#Academic"><rdfs:subClassOf rdf:resource="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#Student"/></owl:Class><!-- http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#General --><owl:Class rdf:about="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#General"><rdfs:subClassOf rdf:resource="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#Student"/></owl:Class><!-- http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#Personal --><owl:Class rdf:about="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#Personal"><rdfs:subClassOf rdf:resource="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#Student"/></owl:Class><!-- http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#Student --><owl:Class rdf:about="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#Student"/><!-- 
///////////////////////////////////////////////////////////////////////////////////////
//
// Individuals
//
///////////////////////////////////////////////////////////////////////////////////////
 --><!-- http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#jack --><owl:NamedIndividual rdf:about="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#jack"><hasGender>male</hasGender><hasAggregate>50</hasAggregate><hasAge>20</hasAge><hasPostaladress>illonious chicago</hasPostaladress><hasName>jack</hasName><hasEmailaddress>jackid</hasEmailaddress></owl:NamedIndividual><!-- http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#jill_ --><owl:NamedIndividual rdf:about="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#jill_"><hasGender>male</hasGender><hasEmailaddress>jillid</hasEmailaddress><hasName>jill</hasName><hasAggregate>34</hasAggregate><hasPostaladress>chicago</hasPostaladress><hasAge>21</hasAge></owl:NamedIndividual><!-- http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#jim --><owl:NamedIndividual rdf:about="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#jim"><hasEmailaddress>jimid</hasEmailaddress><hasAggregate>70</hasAggregate><hasName>jim</hasName><hasGender>male</hasGender><hasAge>23</hasAge><hasPostaladress>chicago</hasPostaladress></owl:NamedIndividual><!-- 
///////////////////////////////////////////////////////////////////////////////////////
//
// General axioms
//
///////////////////////////////////////////////////////////////////////////////////////
 --><rdf:Description><rdf:type rdf:resource="http://www.w3.org/2002/07/owl#AllDisjointClasses"/><owl:members rdf:parseType="Collection"><rdf:Description rdf:about="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#Academic"/><rdf:Description rdf:about="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#General"/><rdf:Description rdf:about="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#Personal"/></owl:members></rdf:Description></rdf:RDF>

1 个答案:

答案 0 :(得分:0)

请注意,DatatypeProperty的范围在逻辑上不能是EnumeratedClass。枚举类是一个owl类,定义为固定的个人集合。那些人不可能是文字。 DatatypeProperty范围内的值只能是Literal

您想使用DataRange,它代表Literal值的枚举。将.asClass().isEnumeratedClass()的测试替换为.isDataRange()

在您的数据中,您希望使用owl:DataRangeowl:oneOf as demonstrated here来制作您的媒体资源的范围。

仅当您希望探索owl:DatatypeProperty的允许值的理论空间时才适用,其范围被枚举为owl:DataRange。虽然您的代码暗示这是您的目标,但您的问题的措辞提出了另一个目标:您有一些owl:DatatypePropertyex:p,并且您希望识别该属性中存在的所有对象你的模特。

如果这是你的目标,那么你可以简单地向模型询问文字值:

final ExtendedIterator<Literal> observedRange =
        new NiceIterator<RDFNode>()
        .andThen(Iter.distinct(model.listObjectsOfProperty(p)))
        .mapWith(new Map1<RDFNode,Literal>(){
            @Override
            public Literal map1(final RDFNode o) {
                return o.asLiteral();
            }});