1

I'm trying to learn how to use Jena. I have found this code on the net. The code runs and it creates an ontology but I have some questions about it. This is the code:

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

import com.hp.hpl.jena.ontology.AllValuesFromRestriction;
import com.hp.hpl.jena.ontology.DatatypeProperty;
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.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.RDFList;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.vocabulary.XSD;

public class people {

    public static void main(String[] args) {
        // Create an empty ontology model
        OntModel ontModel = ModelFactory.createOntologyModel();
        String ns = new String("http://www.example.com/onto1#");
        String baseURI = new String("http://www.example.com/onto1");

        // Create ‘Person’, ‘MalePerson’ and ‘FemalePerson’ classes
        OntClass person = ontModel.createClass(ns + "Person");
        OntClass malePerson = ontModel.createClass(ns + "MalePerson");
        OntClass femalePerson = ontModel.createClass(ns + "FemalePerson");

        // FemalePerson and MalePerson are subclasses of Person
        person.addSubClass(malePerson);
        person.addSubClass(femalePerson);

        // FemalePerson and MalePerson are disjoint
        malePerson.addDisjointWith(femalePerson);
        femalePerson.addDisjointWith(malePerson);

        // Create object property ‘hasSpouse’
        ObjectProperty hasSpouse = ontModel.createObjectProperty(ns + "hasSpouse");
        hasSpouse.setDomain(person);
        hasSpouse.setRange(person);

        // Create an AllValuesFromRestriction on hasSpouse:
        // MalePersons hasSpouse only FemalePerson
        AllValuesFromRestriction onlyFemalePerson = ontModel.createAllValuesFromRestriction(null, hasSpouse, femalePerson);

        // A MalePerson can have at most one spouse -> MaxCardinalityRestriction
        MaxCardinalityRestriction hasSpouseMaxCard = ontModel.createMaxCardinalityRestriction(null, hasSpouse, 1);

        // Constrain MalePerson with the two constraints defined above
        malePerson.addSuperClass(onlyFemalePerson);
        malePerson.addSuperClass(hasSpouseMaxCard);

        // Create class ‘MarriedPerson’
        OntClass marriedPerson = ontModel.createClass(ns + "MarriedPerson");
        MinCardinalityRestriction mincr = ontModel.createMinCardinalityRestriction(null, hasSpouse, 1);

        // A MarriedPerson A Person, AND with at least 1 spouse
        // A list must be created, that will hold the Person class
        // and the min cardinality restriction
        RDFNode[] constraintsArray = { person, mincr };
        RDFList constraints = ontModel.createList(constraintsArray);

        // The two classes are combined into one intersection class
        IntersectionClass ic = ontModel.createIntersectionClass(null, constraints);

        // ‘MarriedPerson’ is declared as an equivalent of the
        // intersection class defined above
        marriedPerson.setEquivalentClass(ic);

        ontModel.write(System.out, "RDF/XML");

    }
}

When I open it on protegé I see on "marriedPerson" : Person and (hasSpouse min 1 Thing).

The questions are :

  1. how can I set the marriedPerson section in order to have Person and (hasSpouse min 1 Person) ?
  2. At the moment, after running the code the ontology sets the marriedPerson section as Equivalent to Person and hasSpouse min 1 Thing... Is it better to have Person and hasSpouse min 1 Person or 1 Thing?
KeyPi
  • 516
  • 5
  • 20
  • 1
    possible duplicate of [How to add qualified cardinality in JENA](http://stackoverflow.com/questions/20562107/how-to-add-qualified-cardinality-in-jena) – Joshua Taylor Apr 23 '15 at 12:38

2 Answers2

2

A class expression like hasSpouse min 1 Person is a qualified cardinality restriction. These didn't exist in the original OWL, but were added in OWL2. Jena doesn't officially support OWL2, so there's no convenient way to add the qualified cardinality restriction.

That said, Jena is an RDF API, not an OWL API, and it is just providing a wrapper around the RDF serialization of OWL ontologies. You can access that serialization directly and create the triples that encode a qualified cardinality restriction.

See How to add qualified cardinality in JENA.

Community
  • 1
  • 1
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
  • Sorry, I started writing this up and remembered the other answer. This question is probably best considered a duplicate of that other question, and the answer there should work here. – Joshua Taylor Apr 23 '15 at 12:39
  • I'd still suggest leaving it because there's also a modelling question in there (part 2). – dhke Apr 23 '15 at 12:45
1

1. (hasSpouse min 1 Person)

This requires a qualified minimal cardinality restriction (i.e. Q instead of N). In Jena there are two different methods to create these.

Replace

ontModel.createMinCardinalityRestriction(null, hasSpouse, 1);

by

ontModel.createMinCardinalityQRestriction(null, hasSpouse, 1, person);

2. Is it better to have Person and (hasSpouse min 1 Person) or 1 Thing?

You already have

    hasSpouse.setDomain(person);

which globally asserts that everything that hasSpouse points to is a Person. Hence the qualification in the cardinality restriction is redundant, both versions are semantically equivalent.

The question to answer is: Is the qualification a property of the restriction or is it a property of the object property/role itself.

dhke
  • 15,008
  • 2
  • 39
  • 56
  • 1
    There's a good chance that the suggested method actually **won't work**. Note that in the question, [How to add qualified cardinality in JENA](http://stackoverflow.com/q/20562107/1281433) a user reported that **"ontModel.createMinCardinalityQRestriction"** will throw **"com.hp.hpl.jena.ontology.ProfileException: Attempted to use language construct CARDINALITY_Q that is not supported in the current language profile: OWL Full"**. You have to create the triples manually. – Joshua Taylor Apr 23 '15 at 12:40
  • Then it's even better that it would be redundant ;-) – dhke Apr 23 '15 at 12:45