Hello Guys,

how do I extract (or know) the cardinality of a relationship property using Jena? I know when I haven't cardinality in my file owl, the cardinality is "many" to a particular class. Could anyone give me an example of using getCardinality. Is this the best way?

Thanks!

Debora

asked 19 Apr '11, 08:52

Deb's gravatar image

Deb
18125
accept rate: 0%


The getCardinality(rel) method will get you the cardinality of relation in the context of a particular resource. However, it sounds like you want to get the cardinality of a relation in the context of all individuals that participate in the relation, in which case you'd want to write something like this:

import java.util.HashSet;
import com.hp.hpl.jena.ontology.Individual;
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.Property;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;

public class Cardinality {
    public static void main(String args[]) {
        OntModel model= ModelFactory.createOntologyModel();
        OntClass foobar= model.createClass("http://example.org/foobar");
        Individual foo= foobar.createIndividual("http://example.org/foo");
        Individual bar= foobar.createIndividual("http://example.org/bar");
        Property rel= model.createProperty("http://example.org/rel");

        foo.addProperty(rel, "first");
        foo.addProperty(rel, "second");
        bar.addProperty(rel, "first");

        HashSet<Integer> cardinality= new HashSet<Integer>();
        ExtendedIterator<Individual> iter= model.listIndividuals();
        while (iter.hasNext()) {
            cardinality.add(iter.next().getCardinality(rel)); }
        System.out.println(cardinality);
    }
}
link

answered 19 Apr '11, 09:57

Ryan%20Kohl's gravatar image

Ryan Kohl
2.5k310
accept rate: 17%

In this example you created (class, individual, property and cardinality), then showed the cardinalities of each individual, right? So if I want to go looking for my ontology cardinality, can I only share the code below?

 HashSet<Integer> cardinality= new HashSet<Integer>();
    ExtendedIterator<Individual> iter= model.listIndividuals();
    while (iter.hasNext()) {
        cardinality.add(iter.next().getCardinality(rel)); }
    System.out.println(cardinality);
}
(19 Apr '11, 11:55) Deb Deb's gravatar image

Only through the property can I list their relationship cardinalities? I tried to extract cardinality through the class, but failed.

  ExtendedIterator classes = inf.listClasses();
  while (classes.hasNext()) {
    OntClass classe = (OntClass) classes.next();
    System.out.println("Classe: " + classe.getLocalName().toString());

    System.out.println("Cardinalidade: " + classe.getCardinality(null));

  }
(19 Apr '11, 11:59) Deb Deb's gravatar image

In regards to your first comment, yes, you can just share that code - the class, individuals, and property were just added to provide a working example.

You could in fact modify that code snippet to return the cardinality for a relation in the context of a class. In my example, you'd replace

ExtendedIterator<Individual> iter= model.listIndividuals();

with

ExtendedIterator<Individual> iter= model.listIndividuals(foobar);

which would restrict the individuals you inspect to just instances of the class (in this case, the 'foobar' class).

(19 Apr '11, 12:16) Ryan Kohl Ryan%20Kohl's gravatar image

That second code snippet of yours failed b/c of that null value on getCardinality. You might be thinking that classes are unary relations, and thus trying to get the number of instances by checking its cardinality. While this is in fact true in some logical systems, RDF goes down a different route - it's the rdf:type relation that you'd use with the class. The only things in RDF that can have cardinality are Properties (ie relations), which excludes Literals, Individuals, and Classes.

(19 Apr '11, 12:20) Ryan Kohl Ryan%20Kohl's gravatar image
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×599
×597
×175

Asked: 19 Apr '11, 08:52

Seen: 1,894 times

Last updated: 09 Nov '12, 04:22