Jena对描述逻辑构造的支持

前言

本文依据"The Description Logic Handbookd"中Appendxi1 Description Terminology中基本的描述逻辑构造,考察Jena对这些构造的支持,以自说明的代码片段予以说明。

内容

1 概念构造

2 关系构造

3 公理构造

1 概念构造

概念构造的具体语法

Jena对描述逻辑构造的支持

package constructor;

import java.util.List;

import com.hp.hpl.jena.ontology.AllValuesFromRestriction;
import com.hp.hpl.jena.ontology.CardinalityRestriction;
import com.hp.hpl.jena.ontology.ComplementClass;
import com.hp.hpl.jena.ontology.EnumeratedClass;
import com.hp.hpl.jena.ontology.HasValueRestriction;
import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.IntersectionClass;
import com.hp.hpl.jena.ontology.MaxCardinalityRestriction;
import com.hp.hpl.jena.ontology.MinCardinalityRestriction;
import com.hp.hpl.jena.ontology.ObjectProperty;
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.OntResource;
import com.hp.hpl.jena.ontology.SomeValuesFromRestriction;
import com.hp.hpl.jena.ontology.UnionClass;
import com.hp.hpl.jena.ontology.impl.OWLProfile;
import com.hp.hpl.jena.rdf.model.InfModel;
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.RDFList;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.ResIterator;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.reasoner.Reasoner;
import com.hp.hpl.jena.reasoner.ReasonerRegistry;
import com.hp.hpl.jena.vocabulary.OWL;
import com.hp.hpl.jena.vocabulary.OWL2;

public class ConceptConstruct {

    public static void main(String[] args) {
        String ns = "http://jena/demo/constructors/concept";
        OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM, null);
        // (1) Top
        System.out.println("(1)");
        OWLProfile owlProfile = new OWLProfile();
        Resource top = owlProfile.THING();
        System.out.println(top);

        // (2) Bottom
        System.out.println("(2)");
        Resource bottom = owlProfile.NOTHING();
        System.out.println(bottom);

        // (3) Intersection: C = A and B
        System.out.println("(3)");
        OntClass aClass = ontModel.createClass(ns + "A");
        System.out.println(aClass);
        OntClass bClass = ontModel.createClass(ns + "B");
        RDFNode[] nodes = new RDFNode[] { aClass, bClass };
        RDFList members = ontModel.createList(nodes);
        IntersectionClass intersection = ontModel.createIntersectionClass(ns + "C", members);
        System.out.println(intersection);
        List<? extends OntClass> operands = intersection.listOperands().toList();
        for (OntClass operand : operands) {
            System.out.println(operand);
        }
        Property operator = intersection.operator();
        System.out.println(operator);// http://www.w3.org/2002/07/owl#intersectionOf

        // (4) Union: D = A or B
        System.out.println("(4)");
        UnionClass union = ontModel.createUnionClass(ns + "D", ontModel.createList(new RDFNode[] { bClass, aClass }));
        System.out.println(union);
        operands = union.listOperands().toList();// BooleanClassDescription
        for (OntClass operand : operands) {
            System.out.println(operand);
        }
        operator = union.operator();
        System.out.println(operator);// http://www.w3.org/2002/07/owl#unionOf

        // (5) Negation: E = not A
        System.out.println("(5)");
        ComplementClass negation = ontModel.createComplementClass(ns + "E", aClass);
        System.out.println(negation);
        operands = negation.listOperands().toList();
        for (OntClass operand : operands) {
            System.out.println(operand);
        }
        operator = negation.operator();
        System.out.println(operator);// http://www.w3.org/2002/07/owl#complementOf

        // (6) Value restriction: G = all R.A
        System.out.println("(6)");
        ObjectProperty role = ontModel.createObjectProperty(ns + "R");
        System.out.println(role);
        AllValuesFromRestriction valueRestriction = ontModel.createAllValuesFromRestriction(ns + "G", role, aClass);
        System.out.println(valueRestriction);
        System.out.println(valueRestriction.getAllValuesFrom());
        System.out.println(valueRestriction.getOnProperty());
        // list all restrictions
        // List<Restriction> restrictions = ontModel.listRestrictions().toList();
        // for (Restriction restriction : restrictions) {
        // System.out.println(restriction);
        // }

        // (7) Limited existential quantification: H = some R
        System.out.println("(7)");
        SomeValuesFromRestriction limitedExistentialQuantification = ontModel.createSomeValuesFromRestriction(ns + "H", role, top);
        System.out.println(limitedExistentialQuantification);
        System.out.println(limitedExistentialQuantification.getSomeValuesFrom());
        System.out.println(limitedExistentialQuantification.getOnProperty());

        // (8) Existential quantification: I = some R A
        System.out.println("(8)");
        SomeValuesFromRestriction existentialQuantification = ontModel.createSomeValuesFromRestriction(ns + "I", role, aClass);
        System.out.println(existentialQuantification);
        System.out.println(existentialQuantification.getSomeValuesFrom());
        System.out.println(existentialQuantification.getOnProperty());

        // (9) At-least number restriction: J = at-least n R
        System.out.println("(9)");
        int cardinality = 2;
        MinCardinalityRestriction atLeastNumberRestriction = ontModel.createMinCardinalityRestriction(ns + "J", role, cardinality);
        System.out.println(atLeastNumberRestriction);
        System.out.println(atLeastNumberRestriction.getMinCardinality());
        System.out.println(atLeastNumberRestriction.getOnProperty());

        // (10) At-most number restriction: K = at-least n R
        System.out.println("(10)");
        MaxCardinalityRestriction atMosttNumberRestriction = ontModel.createMaxCardinalityRestriction(ns + "K", role, cardinality);
        System.out.println(atMosttNumberRestriction);
        System.out.println(atMosttNumberRestriction.getMaxCardinality());
        System.out.println(atMosttNumberRestriction.getOnProperty());

        // (11) Exact number restriction: L = exactly n R
        System.out.println("(11)");
        CardinalityRestriction exactNumberRestriction = ontModel.createCardinalityRestriction(ns + "L", role, cardinality);
        System.out.println(exactNumberRestriction);
        System.out.println(exactNumberRestriction.getCardinality());
        System.out.println(exactNumberRestriction.getOnProperty());

        // (12) Qualified at-least restriction : M = at-least n R A, OntModelSpec.OWL_DL_MEM不支持
        System.out.println("(12)");
        // (13) Qualified at-most restriction: N = at-most n R A, OntModelSpec.OWL_DL_MEM不支持
        System.out.println("(13)");
        // (14) Qualified exact restriction: O = exactly n R A, OntModelSpec.OWL_DL_MEM不支持
        System.out.println("(14)");

        // (15) Same-as, agreement: i1 = i2
        System.out.println("(15)");
        Individual i1 = ontModel.createIndividual(ns + "i1", aClass);
        Individual i2 = ontModel.createIndividual(ns + "i2", aClass);
        i1.addSameAs(i2);
        // navigate
        ResIterator sameIter = ontModel.listSubjectsWithProperty(OWL2.sameAs);
        while (sameIter.hasNext()) {
            Resource sameAsLeft = sameIter.next();
            System.out.println(sameAsLeft);
            System.out.println(sameAsLeft.as(Individual.class).getSameAs());
        }

        // (16) Role-value-map: subset R1 R2
        System.out.println("(16)");
        ObjectProperty role2 = ontModel.createObjectProperty(ns + "R2");
        role.addSuperProperty(role2);
        System.out.println(role.getSuperProperty());

        // (17) Role fillers: P = fillers R i1 i2
        System.out.println("(17)");
        HasValueRestriction hasValueRestriction = ontModel.createHasValueRestriction(null, role, i1);
        HasValueRestriction hasValueRestriction2 = ontModel.createHasValueRestriction(null, role, i2);
        OntClass pClass = ontModel.createClass(ns + "P");
        pClass.addEquivalentClass(ontModel.createIntersectionClass(null,
                ontModel.createList(new RDFNode[] { hasValueRestriction, hasValueRestriction2 })));
        System.out.println(pClass.getEquivalentClass());// 匿名

        // (18) One-of: Q = {i1, i2}
        System.out.println("(18)");
        EnumeratedClass oneOf = ontModel.createEnumeratedClass(ns + "Q", ontModel.createList(new RDFNode[] { i1, i2 }));
        List<? extends OntResource> instances = oneOf.listOneOf().toList();
        for (OntResource instance : instances) {
            System.out.println(instance);
        }
    }

    public static InfModel bindReasoner(Model model) {
        Reasoner reasoner = ReasonerRegistry.getOWLReasoner();
        reasoner = reasoner.bindSchema(model);
        return ModelFactory.createInfModel(reasoner, model);
    }

    public static OntClass createCardinalityQRestriction(OntModel model, String uri, Property prop, int cardinality, OntClass clas) {
        OntClass klass = model.createCardinalityRestriction(uri, prop, cardinality);
        klass.removeAll(OWL.cardinality);
        klass.addLiteral(OWL2.qualifiedCardinality, cardinality);
        klass.addProperty(OWL2.onClass, clas);
        return klass;
    }
}

Jena暂不支持存在量词约束(qualified at-least/at-most/exact)的直接构造,采用下述URL中方法支持该构造的RDF序列化

http://mail-archives.apache.org/mod_mbox/jena-users/201303.mbox/%3CCA+Q4Jn=bDM2wiPSh4DHj58hzxR_oWx2jJoQxnroFScac=E7t3Q@mail.gmail.com%3E

2 关系构造

关系构造的具体语法

Jena对描述逻辑构造的支持

package constructor;

import com.hp.hpl.jena.ontology.ObjectProperty;
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.TransitiveProperty;
import com.hp.hpl.jena.ontology.impl.OWLProfile;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;

/**
 * TransitiveProperty ,FunctionalProperty ,SymmetricProperty,InverseFunctionalProperty
 */
public class RoleConstruct {

    public static void main(String[] args) {
        String ns = "http://jena/demo/constructors/role#";
        OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM, null);
        // (1) Universal role
        System.out.println("(1)");
        OWLProfile owlProfile = new OWLProfile();
        Resource topObjectProperty = owlProfile.OBJECT_PROPERTY();
        Resource topDatatypeProperty = owlProfile.DATATYPE_PROPERTY();
        System.out.println(topObjectProperty);
        System.out.println(topDatatypeProperty);

        // (2) Intersection: r = atomR1 and atomR2
        System.out.println("(2)");

        // (3) Union
        System.out.println("(3)");

        // (4) Complement
        System.out.println("(4)");

        // (5) Inverse
        System.out.println("(5)");
        ObjectProperty r = ontModel.createObjectProperty(ns + "r");
        ObjectProperty inverse_r = ontModel.createObjectProperty(ns + "ir");
        r.addInverseOf(inverse_r);
        System.out.println(inverse_r);
        System.out.println(inverse_r.getInverse());
        System.out.println(r.getInverse());// null

        // (6) Composition
        System.out.println("(6)");

        // (7) Transitive closure
        System.out.println("(7)");
        TransitiveProperty tr = ontModel.createTransitiveProperty(ns + "tr");
        System.out.println(tr);
        System.out.println(tr.isTransitiveProperty());

        // (8) Reflexive-transitive closure
        System.out.println("(8)");

        // (9) Role Restriction
        System.out.println("(9)");
        ObjectProperty rrest = ontModel.createObjectProperty(ns + "rrest");
        OntClass aClass = ontModel.createClass(ns + "A");
        rrest.addRange(aClass);
        System.out.println(rrest);
        System.out.println(rrest.getRange());

        // (10) Identity
        System.out.println("(10)");

    }

}

同OWL规范一样,Jena将关系划分对象属性(ObjectProperty)和数值属性(DatatypeProperty),显式支持传递属性TransitiveProperty, 函数属性FunctionalProperty, 对称属性SymmetricProperty, 逆函数属性InverseFunctionalProperty。

不支持关系与/并/补、关系链(role chain)、类标识(Identity)构造。、

3 公理构造

公理的具体语法

Jena对描述逻辑构造的支持

package constructor;

import java.util.List;

import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.ObjectProperty;
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.OntResource;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Statement;

public class AxiomConstruct {

    public static void main(String[] args) {
        String ns = "http://jena/demo/constructors/axiom#";
        OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM, null);
        // (1) Concept definition
        System.out.println("(1)");
        OntClass aClass1 = ontModel.createClass(ns + "A1");
        OntClass cClass1 = ontModel.createClass(ns + "C1");
        aClass1.addEquivalentClass(cClass1);
        System.out.println(aClass1.getEquivalentClass());
        System.out.println(cClass1.getEquivalentClass());

        // (2) Primitive concept introduction
        System.out.println("(2)");
        OntClass aClass2 = ontModel.createClass(ns + "A2");
        OntClass cClass2 = ontModel.createClass(ns + "C2");
        aClass2.addSuperClass(cClass2);
        System.out.println(aClass2.getSuperClass());
        System.out.println(cClass2.getSubClass());

        // (3) General inclusion axiom
        System.out.println("(3)");
        // mostly like (2)

        // (4) Role definition
        System.out.println("(4)");
        ObjectProperty r11 = ontModel.createObjectProperty(ns + "r11");
        ObjectProperty r12 = ontModel.createObjectProperty(ns + "r12");
        r11.addEquivalentProperty(r12);
        System.out.println(r11.getEquivalentProperty());
        System.out.println(r12.getEquivalentProperty());

        // (5) Primitive role introduction
        System.out.println("(5)");
        ObjectProperty r21 = ontModel.createObjectProperty(ns + "r21");
        ObjectProperty r22 = ontModel.createObjectProperty(ns + "r22");
        r21.addSuperProperty(r22);
        System.out.println(r21.getSuperProperty());
        System.out.println(r22.getSubProperty());

        // (6) Concept assertion
        System.out.println("(6)");
        OntClass concept = ontModel.createClass(ns + "C");
        Individual indv = concept.createIndividual(ns + "c1");
        System.out.println(indv.getOntClass());
        List<? extends OntResource> indvs = concept.listInstances().toList();
        for (OntResource ind : indvs) {
            System.out.println(ind);
        }

        // (7) Role assertion
        System.out.println("(7)");
        ObjectProperty role = ontModel.createObjectProperty(ns + "R");
        OntClass roleDomain = ontModel.createClass(ns + "Domain");
        Individual d1 = roleDomain.createIndividual(ns + "domain1");
        OntClass roleRange = ontModel.createClass(ns + "Range");
        Individual r1 = roleRange.createIndividual(ns + "range1");
        role.addDomain(roleDomain);
        role.addRange(roleRange);
        Statement stmt = ontModel.createStatement(d1, role, r1);
        System.out.println(stmt);
        ontModel.add(stmt);
    }

}

Jena可以构建描述逻辑公理构造。

上一篇:学习算法-基数排序(radix sort)卡片分类(card sort) C++数组实现


下一篇:SpringBoot Jpa 自定义查询