使用OWL Api或Jena api将原始类转换为已定义的类

时间:2014-01-07 05:37:19

标签: class owl primitive protege defined

我正在使用OWL API和Jena API。

我需要将原始类转换为已定义的类。

我正在使用Protege,我想这样做:

(我是stackoverflow中的新手,我无法发布图片,但我把链接。)

我有一个“TestClassInstan”类,它是“TestClass”的子类,并且有一个限制“hasURL”,如图[1]所示:

[one] https://fbcdn-sphotos-c-a.akamaihd.net/hphotos-ak-ash3/1526873_1383572248568109_2144507550_n.jpg https://fbcdn-sphotos-c-a.akamaihd.net/hphotos-ak-ash3/1526873_1383572248568109_2144507550_n.jpg

我想将此类和此属性转换为已定义的类。然后得到这个结果,一个具有等价性的定义类,如图[2]所示:

[two] https://fbcdn-sphotos-g-a.akamaihd.net/hphotos-ak-ash3/1488649_1383580128567321_1363049007_n.jpg enter image description here

感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

在OWL API中,不需要任何特殊转换来转换已定义的原始概念。只需添加定义您的类的公理。例如,如果A是您的类,并且您希望将其定义为与some r C等存在性限制等效,则只需创建一个公理:

Set<OWLClassExpression> arguments=new HashSet<OWLClassExpression>();
arguments.add(A);
arguments.add(dataFactory.getOWLObjectSomeValuesFrom(r, C);
OWLAxiom axiom = dataFactory.getOWLEquivalentClassesAxiom(arguments);

并将此公理添加到本体。 A现在将是一个定义的概念。

注意:上面的代码不会修改本体文件。要保存本体,请使用

ontologyManager.saveOntology(ontology)

答案 1 :(得分:1)

问题中的图片有点难以理解,所以要清楚,首先,有两个公理

  

TestClassInstan subClassOf hasURL "http"
   TestClassInstan subClassOf TestClass

你想要删除这两个公理并用一个公理替换它们

  

TestClassInstan equivalentClass TestClass hasURL value < / strong> "http"))

Ignazio's answer已经展示了如何使用OWLAPI执行此操作。这个答案将展示如何使用Jena来做到这一点。 OntClassOntModel接口实际上提供了您需要的所有方法。唯一棘手的一点可能是构造构成交集的类的RDFList,尽管在这个例子中它并不难(它只是 TestClassInstan 的原始超类列表)。这是完成转换的Java代码。这还包括在代码中重建本体,因为您没有提供实际的本体代码。答案没有必要,但它可能会有所帮助,它使这段代码独立存在。

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.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.RDFList;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;

public class ConvertSubclassesToEquivalentIntersection {
    final static String NS = "https://stackoverflow.com/a/21001185/1281433/";

    public static void main(String[] args) {
        // Get the original model, and show it (just for good measure, so 
        // that we can compare them).
        final OntModel model = createOriginalModel();
        model.write( System.out, "RDF/XML-ABBREV" );

        // Get the class that we want to convert.
        OntClass testClassInstan = model.createClass( NS+"TestClassInstan" );

        // To create an intersection class, we need an RDFList of the 
        // superclasses, and we can make one of those from the superclass
        // iterator.
        final RDFList superclasses = model.createList( testClassInstan.listSuperClasses( true ));
        testClassInstan.addEquivalentClass( model.createIntersectionClass( null, superclasses ));

        // We still need to remove those superclasses though.
        for ( ExtendedIterator<OntClass> sups = testClassInstan.listSuperClasses( true ); sups.hasNext(); ) {
            testClassInstan.removeSuperClass( sups.next() );
        }

        // Write out the new model.  You can write to a file instead of System.out, 
        // of course, and that's what you'd do to save the model back to a file.
        model.write( System.out, "RDF/XML-ABBREV" );
    }

    /**
     * <p>Recreate the original ontology that had the following axioms (and
     * appropriate class and property declarations):</p>
     * <pre>
     * TestClassInstan subClassOf TestClass
     * TestClassInstan subClassOf (hasURL value "http")
     * </pre>  
     * @return the ontology model
     */
    private static OntModel createOriginalModel() { 
        final OntModel model = ModelFactory.createOntologyModel( OntModelSpec.OWL_DL_MEM );

        final OntClass testClass = model.createClass( NS+"TestClass" );
        final OntClass testClassInstan = model.createClass( NS+"TestClassInstan" );
        testClassInstan.addSuperClass( testClass );

        final OntProperty hasURL = model.createDatatypeProperty( NS+"hasURL" );
        final OntClass hasURLValueHttp = model.createHasValueRestriction( null, hasURL, model.createLiteral( "http" ));
        testClassInstan.addSuperClass( hasURLValueHttp );

        return model;
    }
}

原始本体论是:

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:owl="http://www.w3.org/2002/07/owl#"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
  <owl:Class rdf:about="https://stackoverflow.com/a/21001185/1281433/TestClass"/>
  <owl:Class rdf:about="https://stackoverflow.com/a/21001185/1281433/TestClassInstan">
    <rdfs:subClassOf>
      <owl:Restriction>
        <owl:hasValue>http</owl:hasValue>
        <owl:onProperty>
          <owl:DatatypeProperty rdf:about="https://stackoverflow.com/a/21001185/1281433/hasURL"/>
        </owl:onProperty>
      </owl:Restriction>
    </rdfs:subClassOf>
    <rdfs:subClassOf rdf:resource="https://stackoverflow.com/a/21001185/1281433/TestClass"/>
  </owl:Class>
</rdf:RDF>

original ontology

转换后的本体是:

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:owl="http://www.w3.org/2002/07/owl#"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
  <owl:Class rdf:about="https://stackoverflow.com/a/21001185/1281433/TestClass"/>
  <owl:Class rdf:about="https://stackoverflow.com/a/21001185/1281433/TestClassInstan">
    <owl:equivalentClass>
      <owl:Class>
        <owl:intersectionOf rdf:parseType="Collection">
          <owl:Restriction>
            <owl:hasValue>http</owl:hasValue>
            <owl:onProperty>
              <owl:DatatypeProperty rdf:about="https://stackoverflow.com/a/21001185/1281433/hasURL"/>
            </owl:onProperty>
          </owl:Restriction>
          <owl:Class rdf:about="https://stackoverflow.com/a/21001185/1281433/TestClass"/>
        </owl:intersectionOf>
      </owl:Class>
    </owl:equivalentClass>
  </owl:Class>
</rdf:RDF>

after conversion