Skip to content

3. Example: Constructing a Descriptor

Luca Buoncompagni edited this page Apr 8, 2024 · 2 revisions

Example of a Descriptor that implements a single ClassExpression

The following Java class InstanceClassDesc, is an example of a simple Class Descriptor. It can be found in the path: owloop/src/main/java/it/emarolab/owloop/descriptor/utility/classDescriptor/.

This class extends the abstract class ClassGround and implements 1 ClassExpression interface (i.e., Instance). Note that, when using Descriptor build(), the return type of the Instance is a compound Individual Descriptor called LinkIndividualDesc which implements 2 IndividualExpression interfaces (i.e., ObjectLink and DataLink).

public class InstanceClassDesc
        extends ClassGround
        implements ClassExpression.Instance<LinkIndividualDesc> {
        
}

Inside the class; first, we create an object of the class EntitySet.Individual. This set can hold the OWLNamedIndividuals associated to the ground of type OWLCLass via the expression Instance.

private DescriptorEntitySet.Individuals classAssertedIndividuals = new DescriptorEntitySet.Individuals();

Second, we add the constructors inherited from the abstract class ClassGround.

public InstanceClassDesc(OWLClass instance, OWLReferences onto) {
    super(instance, onto);
}
public InstanceClassDesc(String instanceName, OWLReferences onto) {
    super(instanceName, onto);
}
public InstanceClassDesc(OWLClass instance, String ontoName) {
    super(instance, ontoName);
}
public InstanceClassDesc(OWLClass instance, String ontoName, String filePath, String iriPath) {
    super(instance, ontoName, filePath, iriPath);
}
public InstanceClassDesc(OWLClass instance, String ontoName, String filePath, String iriPath, boolean bufferingChanges) {
    super(instance, ontoName, filePath, iriPath, bufferingChanges);
}
public InstanceClassDesc(String instanceName, String ontoName) {
    super(instanceName, ontoName);
}
public InstanceClassDesc(String instanceName, String ontoName, String filePath, String iriPath) {
    super(instanceName, ontoName, filePath, iriPath);
}
public InstanceClassDesc(String instanceName, String ontoName, String filePath, String iriPath, boolean bufferingChanges) {
    super(instanceName, ontoName, filePath, iriPath, bufferingChanges);
}

Third, we override methods in Class and ClassExpression interface. These methods enable manipulation of axioms, (i) within the internal state of the Descriptor and (ii) between the Descriptor(s) and ontology file(s).

// To read axioms from an ontology
@Override 
public List<MappingIntent> readExpressionAxioms() {
    return Instance.super.readExpressionAxioms();
}
// To read axioms from an ontology
@Override 
public List<MappingIntent> writeExpressionAxioms() {
    return Instance.super.writeExpressionAxioms();
}

// Is used by the descriptors's build() method. It's possible to change the return type based on need.
@Override 
public LinkIndividualDesc getIndividualDescriptor(OWLNamedIndividual instance, OWLReferences ontology) {
    return new LinkIndividualDesc( instance, ontology);
}
// It returns the Individuals from the EntitySet (after being read from the ontology)
@Override
public DescriptorEntitySet.Individuals getIndividualInstances() {
    return classAssertedIndividuals;
}

Finally, we override the default method toString() in the class Object, to be able to print some interesting knowledge within the Descriptor when toString() is called.

// To show internal state of the Descriptor
@Override
public String toString() {
    return getClass().getSimpleName() + "{" + "\n" +
            "\n" +
            "\t" + getGround() + ":" + "\n" +
            "\n" +
            "\t\t⇐ " + individuals + "\n" +
            "}" + "\n";
}

Example of a Descriptor that implements six ClassExpressions

The following Java class FullClassDesc, is an example of a compound Class Descriptor. It can be found in the path: owloop/src/main/java/it/emarolab/owloop/descriptor/utility/ClassDescriptor/.

This class FullClassDesc extends the abstract class ClassGround and implements 6 ClassExpression interfaces (i.e., Equivalent, Disjoint, Sub, Super, Instance, Definition). Note that, when using Descriptor build(), the return type of the Instance is a compound Individual Descriptor called LinkIndividualDesc which implements 2 IndividualExpression interfaces (i.e., ObjectLink and DataLink). Whereas, the return type of the remaining ClassExpression interfaces (except for Definition) is FullConpectDesc.

public class InstanceClassDesc
        extends ClassGround
        implements ClassExpression.Definition,
        ClassExpression.Disjoint<FullClassDesc>,
        ClassExpression.Equivalent<FullClassDesc>,
        ClassExpression.Sub<FullClassDesc>,
        ClassExpression.Super<FullClassDesc>,
        ClassExpression.Instance<LinkIndividualDesc> {

}

Inside the class; first, we create EntitySet objects for all the types of OWL entities we would like the descriptor FullClassDesc to handle.

private DescriptorEntitySet.Restrictions classRestrictions = new DescriptorEntitySet.Restrictions();
private DescriptorEntitySet.Classes disjointClasses = new DescriptorEntitySet.Classes();
private DescriptorEntitySet.Classes equivalentClasses = new DescriptorEntitySet.Classes();
private DescriptorEntitySet.Classes subClasses = new DescriptorEntitySet.Classes();
private DescriptorEntitySet.Classes superClasses = new DescriptorEntitySet.Classes();
private DescriptorEntitySet.Individuals classAssertedIndividuals = new DescriptorEntitySet.Individuals();

Second, we add the constructors inherited from the abstract class ClassGround.

public FullClassDesc(OWLClass instance, OWLReferences onto) {
    super(instance, onto);
}
public FullClassDesc(String instanceName, OWLReferences onto) {
    super(instanceName, onto);
}
public FullClassDesc(OWLClass instance, String ontoName) {
    super(instance, ontoName);
}
public FullClassDesc(OWLClass instance, String ontoName, String filePath, String iriPath) {
    super(instance, ontoName, filePath, iriPath);
}
public FullClassDesc(OWLClass instance, String ontoName, String filePath, String iriPath, boolean bufferingChanges) {
    super(instance, ontoName, filePath, iriPath, bufferingChanges);
}
public FullClassDesc(String instanceName, String ontoName) {
    super(instanceName, ontoName);
}
public FullClassDesc(String instanceName, String ontoName, String filePath, String iriPath) {
    super(instanceName, ontoName, filePath, iriPath);
}
public FullClassDesc(String instanceName, String ontoName, String filePath, String iriPath, boolean bufferingChanges) {
    super(instanceName, ontoName, filePath, iriPath, bufferingChanges);
}

Third, we override methods in Class and ClassExpression interface. These methods enable manipulation of axioms, (i) within the internal state of the Descriptor and (ii) between the Descriptor(s) and ontology file(s).

// To read axioms from an ontology
@Override
public List<MappingIntent> readExpressionAxioms() {
    List<MappingIntent> r = ClassExpression.Disjoint.super.readExpressionAxioms();
    r.addAll( ClassExpression.Equivalent.super.readExpressionAxioms());
    r.addAll( Definition.super.readExpressionAxioms()); // call this before Sub or Super !!!
    r.addAll( ClassExpression.Sub.super.readExpressionAxioms());
    r.addAll( ClassExpression.Super.super.readExpressionAxioms());
    r.addAll( Instance.super.readExpressionAxioms());
    return r;
}
// To write axioms to an ontology
@Override
public List<MappingIntent> writeExpressionAxioms() {
    List<MappingIntent> r = ClassExpression.Disjoint.super.writeExpressionAxioms();
    r.addAll( ClassExpression.Equivalent.super.writeExpressionAxioms());
    r.addAll( Definition.super.writeExpressionAxioms()); // call this before Sub or Super !!!
    r.addAll( ClassExpression.Sub.super.writeExpressionAxioms());
    r.addAll( ClassExpression.Super.super.writeExpressionAxioms());
    r.addAll( Instance.super.writeExpressionAxioms());
    return r;
}

// It returns the classRestrictions from the EntitySet (after being read from the ontology)
@Override
public DescriptorEntitySet.Restrictions getDefinitionClasses() {
    return classRestrictions;
}

// Is used by the descriptors's build() method. It's possible to change the return type based on need.
@Override
public FullClassDesc getDisjointClassDescriptor(OWLClass instance, OWLReferences ontology) {
    return new FullClassDesc( instance, ontology);
}
// It returns the disjointClasses from the EntitySet (after being read from the ontology)
@Override
public DescriptorEntitySet.Classes getDisjointClasses() {
    return disjointClasses;
}

// Is used by the descriptors's build() method. It's possible to change the return type based on need.
@Override
public FullClassDesc getEquivalentClassDescriptor(OWLClass instance, OWLReferences ontology) {
    return new FullClassDesc( instance, ontology);
}
// It returns the equivalentClasses from the EntitySet (after being read from the ontology)
@Override
public DescriptorEntitySet.Classes getEquivalentClasses() {
    return equivalentClasses;
}

// Is used by the descriptors's build() method. It's possible to change the return type based on need.
@Override
public FullClassDesc getSubClassDescriptor(OWLClass instance, OWLReferences ontology) {
    return new FullClassDesc( instance, ontology);
}
// It returns the subClasses from the EntitySet (after being read from the ontology)
@Override
public DescriptorEntitySet.Classes getSubClasses() {
    return subClasses;
}

// Is used by the descriptors's build() method. It's possible to change the return type based on need.
@Override
public FullClassDesc getSuperClassDescriptor(OWLClass instance, OWLReferences ontology) {
    return new FullClassDesc( instance, ontology);
}
// It returns the superClasses from the EntitySet (after being read from the ontology)
@Override
public DescriptorEntitySet.Classes getSuperClasses() {
    return superClasses;
}

// Is used by the descriptors's build() method. It's possible to change the return type based on need.
@Override
public LinkIndividualDesc getIndividualDescriptor(OWLNamedIndividual instance, OWLReferences ontology) {
    return new LinkIndividualDesc( instance, ontology);
}
// It returns the Individuals from the EntitySet (after being read from the ontology)
@Override
public DescriptorEntitySet.Individuals getIndividualInstances() {
    return classAssertedIndividuals;
}

Finally, we override the default method toString() in the class Object, to be able to print some interesting knowledge within the Descriptor when toString() is called.

// To show internal state of the Descriptor
@Override
public String toString() {
    return getClass().getSimpleName() + "{" + "\n" +
            "\n" +
            "\t" + getGround() + ":" + "\n" +
            "\n" +
            "\t\t≠ " + disjointClasses + "\n" +
            "\t\t≡ " + equivalentClasses + "\n" +
            "\t\t⇐ " + individuals + "\n" +
            "\t\t≐ " + classRestrictions + "\n" +
            "\t\t⊃ " + subClasses + "\n" +
            "\t\t⊂ " + superClasses + "\n" +
            "}" + "\n";
}

In a similar way

We can create simple (ObjectProperty, DataProperty and Individual) Descriptors, and furthermore, compound (ObjectProperty, DataProperty and Individual) Descriptors. Their examples can be found owloop/src/main/java/it/emarolab/owloop/descriptor/utility/classDescriptor/ and owloop/src/test/java/it/emarolab/owloopArticleExamples/exampleDescriptors/.