Question

I made this code to create relation between objects.

OntClass Object = m.createClass(NS + Constants.Object);

ObjectProperty left = m.createObjectProperty(NS + Constants.left);
left.addDomain(Object);
left.addRange(Object);

ObjectProperty right = m.createObjectProperty(NS + Constants.right);
right.addDomain(Object);
right.addRange(Object);

As output, I got

<owl:ObjectProperty rdf:about="http://www.mydomain.com/ns1.owl#left">
  <rdfs:range rdf:resource="http://www.mydomain.com/ns1.owl#Object"/>
  <rdfs:domain rdf:resource="http://www.mydomain.com/ns1.owl#Object"/>
</owl:ObjectProperty>

<owl:ObjectProperty rdf:about="http://www.mydomain.com/ns1.owl#right">
  <rdfs:range rdf:resource="http://www.mydomain.com/ns1.owl#Object"/>
  <rdfs:domain rdf:resource="http://www.mydomain.com/ns1.owl#Object"/>
</owl:ObjectProperty>

I want to add restriction on these Object properties and make them Non-Transitive. For example, if I have three objects O1, O2 & O3, I can infer that:

O1 left O2, O2 right O1, O2 left O3, O3 right O2.

enter image description here

and do not permit O1 left O3 & O3 right O1.

enter image description here

I found there's the possibility to create transitive properties but for non-transitive ones I didn't know how to do.

Additionally, how can I add a cardinality restriction to represent one object by side.

Was it helpful?

Solution

FunctionalProperties and MinCardinality Restrictions

Based on you image, it looks like you're trying to represent something like a doubly-linked list. While you can, in OWL, make a property transitive, there's not a construction to say about a property p that:

(A)    p(a,b) ∧ p(b,c) → ¬p(a,c).

By simply not declaring the property transitive, you can have p(a,b) and p(b,c) and remain agnostic, so to speak, as to whether or not p(a,c) holds. However, in the case that it looks like you're describing, it sounds like you want your left and right properties to be functional. When a property p is functional, it means that:

(B)    p(a,b) ∧ p(a,c) → b = c

The logically equivalent contrapositive, of course, is that:

(C)    b ≠ c → ¬p(a,b) ∨¬p(a,c)

I think that's what you want here, because if you have:

  1. functional(left)
  2. left(o1,o2)
  3. left(o2,o3)
  4. o1 ≠ o2
  5. o1 ≠ o3
  6. o2 ≠ o3

Then from (C) and (6), we can infer that

¬ left(o1,o2) ∨ ¬ left(o1,o3)

but then, by (2), we have

¬ left(o1,o3)

which, I think, is the "non-transitivity" that you wanted. This also restricts each node to having at most one value for the left property. If you also want to ensure that each node has at least one value for the left property, you can use a minimum cardinality restriction:

Node ⊑ ≥1 left

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;

public class MinCardinalityExample {
    public static void main(String[] args) {
        String NS = "http://stackoverflow.com/q/22589068/1281433/";
        OntModel model = ModelFactory.createOntologyModel();
        model.setNsPrefix( "", NS );

        OntClass node = model.createClass( NS+"Node" );

        ObjectProperty left = model.createObjectProperty( NS+"left" )
                .convertToFunctionalProperty()
                .asObjectProperty();

        node.addSuperClass( model.createMinCardinalityRestriction( null, left, 1 ));

        model.write( System.out, "TTL" );
    }
}
@prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#> .
@prefix owl:   <http://www.w3.org/2002/07/owl#> .
@prefix xsd:   <http://www.w3.org/2001/XMLSchema#> .
@prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

:Node   a                owl:Class ;
        rdfs:subClassOf  [ a                   owl:Restriction ;
                           owl:minCardinality  "1"^^xsd:int ;
                           owl:onProperty      :left
                         ] .

:left   a       owl:FunctionalProperty , owl:ObjectProperty .

ExactCardinality Restrictions

Of course, note that a property being functional is really the same thing as a maximum cardinality restriction and a subclass axiom by which you say that everything is such that it has at most one value for the property:

⊤ ⊑ ≤ 1 p    is the same as    functional(p).

For your left property, you might take a simpler route and just assert that each Node has exactly one value for the left property:

Node ⊑ = 1 left

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;

public class ExactlyCardinalityExample {
    public static void main(String[] args) {
        String NS = "http://stackoverflow.com/q/22589068/1281433/";
        OntModel model = ModelFactory.createOntologyModel();
        model.setNsPrefix( "", NS );
        OntClass node = model.createClass( NS+"Node" );
        ObjectProperty left = model.createObjectProperty( NS+"left" );
        node.addSuperClass( model.createCardinalityRestriction( null, left, 1 ));
        model.write( System.out, "TTL" );
    }
}
@prefix :      <http://stackoverflow.com/q/22589068/1281433/> .
@prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#> .
@prefix owl:   <http://www.w3.org/2002/07/owl#> .
@prefix xsd:   <http://www.w3.org/2001/XMLSchema#> .
@prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

:Node   a                owl:Class ;
        rdfs:subClassOf  [ a                owl:Restriction ;
                           owl:cardinality  "1"^^xsd:int ;
                           owl:onProperty   :left
                         ] .

:left   a       owl:ObjectProperty .
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top