The most complete tool class in history to use the Jena framework to operate OWL ontology files

A recent project related to knowledge graphs used the jena framework to operate OWL files, but no suitable tutorials were found on the entire network, so I slowly tried to learn by myself, and then encapsulated a tool class for operating OWL for easy use, if necessary Take it yourself.

wives town building

Put the dependency first

<dependency>
    <groupId>org.apache.jena</groupId>
    <artifactId>apache-jena-libs</artifactId>
    <type>pom</type>
    <version>3.6.0</version>
</dependency>

The specific tool class code is as follows

import org.apache.jena.ontology.*;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.RDFWriter;
import org.springframework.stereotype.Component;

import javax.swing.plaf.ViewportUI;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @BelongsProject: createowl
 * @BelongsPackage: com.zt.createowl.util
 * @Author: zt
 * @CreateTime: 2023-02-28 15:59
 * @Description:
 */

@Component
public class CoreOWLUtil {

    /*
     * @Description: Returns the resource beginning name of the OWL file
     * @Author: zt
     * @Date: 2023/2/28 16:23
     * @param: []
     * @return: java.lang.String
     **/
    public static String getSourceName(){
        return "http://www.semanticweb.org/ztyt/ontologies/2023/2/fkfdcore";
    }

    /*
     * @Description: Return the namespace of the OWL file (add # on the basis of the beginning name)
     * @Author: zt
     * @Date: 2023/2/28 16:26
     * @param: []
     * @return: java.lang.String
     **/
    public static String getNameSpace(){
        return CoreOWLUtil. getSourceName() + "#";
    }

    /*
     * @Description: Write the object of OntModel class as an OWL file
     * @Author: zt
     * @Date: 2023/2/28 16:37
     * @param: [ontModel]
     * @return: void
     **/
    public static void ontModel2Owl(OntModel ontModel) throws IOException {
        //Output the owl file to the file system
        String filepath = "src/main/resources/owl/core/CoreOntology.owl";
        FileOutputStream fileOS = new FileOutputStream(filepath);
// RDFWriter rdfWriter = baseOnt. getWriter("RDF/XML");
        RDFWriter rdfWriter = ontModel. getWriter("RDF/XML");
        rdfWriter.setProperty("showXMLDeclaration","true");
        rdfWriter.setProperty("showDoctypeDeclaration", "true");
        rdfWriter.write(ontModel, fileOS, null);
        //With writer, you don't need to use the following method
        //baseOnt.write(fileOS, "RDF/XML");
        fileOS. close();
    }

    /*
     * @Description: Read the OWL file at the specified location as an object of the OntModel class
     * @Author: zt
     * @Date: 2023/2/28 16:05
     * @param: []
     * @return: org.apache.jena.ontology.OntModel
     **/
    public static OntModel owl2OntModel(){
        //Set the namespace of the ontology
        String SOURCE = CoreOWLUtil. getSourceName();
        OntDocumentManager ontDocMgr = new OntDocumentManager();
        //Set the location of the ontology owl file
        ontDocMgr.addAltEntry(SOURCE, "file:src/main/resources/owl/core/CoreOntology.owl");
        OntModelSpec ontModelSpec = new OntModelSpec(OntModelSpec.OWL_MEM);
        ontModelSpec.setDocumentManager(ontDocMgr);
        // asserted ontology
        OntModel baseOnt = ModelFactory. createOntologyModel(ontModelSpec);
        baseOnt.read(SOURCE, "RDF/XML");
        return baseOnt;
    }

    /*
     * @Description: Add a new class
     * @Author: zt
     * @Date: 2023/2/28 16:38
     * @param: [ontModel reads the OntModel class object generated by the OWL file, className adds the name of the new class]
     * @return: org.apache.jena.ontology.OntClass
     **/
    public static OntClass createClass(OntModel ontModel, String className) throws IOException {
        String nameSpace = CoreOWLUtil. getNameSpace();
        OntClass newClass = ontModel. createClass(nameSpace + className);
        CoreOWLUtil.ontModel2Owl(ontModel);
        return newClass;
    }

    /*
     * @Description: Add a parent-child relationship to the incoming parent and child classes
     * @Author: zt
     * @Date: 2023/2/28 16:53
     * @param: [ontModel reads the OntModel class object generated from the OWL file, fatherClass parent class, sonClass subclass]
     * @return: void
     **/
    public static void addSubClass(OntModel ontModel, OntClass fatherClass, OntClass sonClass) throws IOException {
        fatherClass.addSubClass(sonClass);
        CoreOWLUtil.ontModel2Owl(ontModel);
    }

    /*
     * @Description: Add a relationship between the incoming head class and tail class, the name of the relationship is passed by the parameter
     * @Author: zt
     * @Date: 2023/2/28 17:11
     * @param: [ontModel reads the OntModel class object generated from the OWL file, sourceClass head class, targetClass tail class, relationName relation name]
     * @return: org.apache.jena.ontology.ObjectProperty
     **/
    public static ObjectProperty addRelation(OntModel ontModel, OntClass sourceClass, OntClass targetClass, String relationName) throws IOException {
        String nameSpace = CoreOWLUtil. getNameSpace();
        ObjectProperty newRelation = ontModel.createObjectProperty(nameSpace + relationName);
        newRelation. addDomain(sourceClass);
        newRelation. addRange(targetClass);
        CoreOWLUtil.ontModel2Owl(ontModel);
        return newRelation;
    }

    /*
     * @Description: Add attribute information to the incoming class, the attribute name is passed by the parameter
     * @Author: zt
     * @Date: 2023/2/28 17:23
     * @param: [ontModel reads the OntModel class object generated from the OWL file, ontClass the category that needs to be added, propertyName the property name]
     * @return: org.apache.jena.ontology.DatatypeProperty
     **/
    public static DatatypeProperty addProperty(OntModel ontModel, OntClass ontClass, String propertyName) throws IOException {
        String nameSpace = CoreOWLUtil. getNameSpace();
        DatatypeProperty newProperty = ontModel.createDatatypeProperty(nameSpace + propertyName);
        newProperty. addDomain(ontClass);
        CoreOWLUtil.ontModel2Owl(ontModel);
        return newProperty;
    }

    /*
     * @Description: Delete the attribute information in the specified class
     * @Author: zt
     * @Date: 2023/3/27 15:39
     * @param: [ontModel, ontClass, propertyName]
     * @return: void
     **/
    public static void removeProperty(OntModel ontModel, OntClass ontClass, String propertyName) throws IOException {
        String nameSpace = CoreOWLUtil. getNameSpace();
        DatatypeProperty newProperty = ontModel.createDatatypeProperty(nameSpace + propertyName);
        newProperty. removeDomain(ontClass);
        CoreOWLUtil.ontModel2Owl(ontModel);
    }

    /*
     * @Description: Delete the class with the specified name. If this class has subclasses, it cannot be deleted and an exception is thrown directly
     * @Author: zt
     * @Date: 2023/2/28 18:18
     * @param: [ontModel reads the OntModel class object generated by the OWL file, classname is the name of the class to be deleted]
     * @return: void
     **/
    public static void removeClass(OntModel ontModel, String classname) throws Exception {
        OntClass ontClass = CoreOWLUtil.createClass(ontModel, classname);
        ontClass. remove();
        CoreOWLUtil.ontModel2Owl(ontModel);
    }

    /*
     * @Description: Delete the specified domain and range in the relationship with the specified name
     * @Author: zt
     * @Date: 2023/3/7 10:42
     * @param: [ontModel, propertyName the name of the relationship, ontClass the class to remove from this relationship]
     * @return: void
     **/
    public static void removeRelationDomainAndRange(OntModel ontModel, String propertyName, OntClass ontClass) throws IOException {
        String nameSpace = CoreOWLUtil. getNameSpace();
        ObjectProperty relation = ontModel.createObjectProperty(nameSpace + propertyName);
        //As long as this class is the range or domain of this relationship, delete it
        if(relation.hasDomain(ontClass)){
            relation. removeDomain(ontClass);
        }
        if(relation.hasRange(ontClass)){
            relation. removeRange(ontClass);
        }
        CoreOWLUtil.ontModel2Owl(ontModel);
    }

    public static void removeRelation(OntModel ontModel, String propertyName, OntClass domain, OntClass range) throws IOException {
        String nameSpace = CoreOWLUtil. getNameSpace();
        ObjectProperty relation = ontModel.createObjectProperty(nameSpace + propertyName);
        if(relation.hasDomain(domain)){
            relation. removeDomain(domain);
        }
        if(relation.hasRange(range)){
            relation. removeRange(range);
        }
        CoreOWLUtil.ontModel2Owl(ontModel);
    }

}

Please modify the specific OWL file path and namespace according to your own situation.

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge Java skill treeHome pageOverview 110089 people are studying systematically