Search code examples
javaarraysowlontologyowl-api

How to avoid representing instances in alphabetical order using OWLAPI


I have created Ontology using OWL-API. I have added Instances using an array.But Ontology represents it in alphabetical order not according to the order which I have included in the array. As a result of this the other instances're mismatched.

       String Item1_List[]={"PencilBox","Box"};        
       int Item1_QuntList[] = {5,4};
       Set<OWLAxiom> axioms = new HashSet<OWLAxiom>();
       for( int i=0 ; i<Item1_List.length ; i++){  
       axioms.add(df.getOWLDataPropertyAssertionAxiom(Item1_Name, Item1,Item1_List[i])); }

      for( int i=0 ; i<Item1_QuntList.length ; i++){
      axioms.add(df.getOWLDataPropertyAssertionAxiom(Item1_Quantity,Item1,Item1_QuntList[i])); }

      manager.addAxioms(ontology2, axioms);

And this is the output Ontology.

<!-- Item1 -->

<owl:NamedIndividual rdf:about="http://www.semanticweb.org/imesha/ontologies/2015/7/untitled-ontology-26#Item1">
    <rdf:type rdf:resource="http://www.semanticweb.org/imesha/ontologies/2015/7/untitled-ontology-26#Item"/>
    <Item1_Name rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Box</Item1_Name>
    <Item1_Name rdf:datatype="http://www.w3.org/2001/XMLSchema#string">PencilBox</Item1_Name>
    <Item1_Quantity rdf:datatype="http://www.w3.org/2001/XMLSchema#integer">4</Item1_Quantity>
    <Item1_Quantity rdf:datatype="http://www.w3.org/2001/XMLSchema#integer">5</Item1_Quantity>
    <has rdf:resource="http://www.semanticweb.org/imesha/ontologies/2015/7/untitled-ontology-26#Item2"/>
</owl:NamedIndividual>

Instance "Box" comes before the instance "PencilBox". How can I avoid this?


Solution

  • An ontology is defined as a set of axioms. The order is not specified and it is not semantically relevant. The owl api tries to keep it invariant when ontologies are edited, to reduce changes to files that might be held in source control systems - git repositories for example.

    Why do you need the instances to be represented in a certain order?

    Edit: adding an example to follow up on the comments.

    The Pencil and Box strings appear to represent individuals with attached properties, e.g., a quantity property. An alternative modelling that does not rely on order to attach quantity to individual can be obtained with this code:

    Code:

        OWLIndividual Item1 = df.getOWLNamedIndividual(IRI.create("http://test.com/test#Item1"));
        OWLObjectProperty Item1_Name = df.getOWLObjectProperty(IRI.create("http://test.com/test#Item"));
        OWLDataProperty Item1_Quantity = df.getOWLDataProperty(IRI.create("http://test.com/test#Quantity"));
        OWLIndividual Item1_List[] = { df.getOWLNamedIndividual(IRI.create("http://test.com/test#PencilBox")), df
            .getOWLNamedIndividual(IRI.create("http://test.com/test#Box")) };
        int Item1_QuntList[] = { 5, 4 };
        Set<OWLAxiom> axioms = new HashSet<OWLAxiom>();
        for (int i = 0; i < Item1_List.length; i++) {
            axioms.add(df.getOWLObjectPropertyAssertionAxiom(Item1_Name, Item1, Item1_List[i]));
            axioms.add(df.getOWLDataPropertyAssertionAxiom(Item1_Quantity, Item1_List[i], Item1_QuntList[i]));
        }
    

    Output:

    <?xml version="1.0"?>
    <rdf:RDF xmlns="owlapi:ontology#ont1#"
     xml:base="owlapi:ontology#ont1"
     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
     xmlns:test="http://test.com/test#"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
     xmlns:owl="http://www.w3.org/2002/07/owl#"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
     xmlns:xml="http://www.w3.org/XML/1998/namespace">
    <owl:Ontology rdf:about="owlapi:ontology#ont1"/>
    <owl:ObjectProperty rdf:about="http://test.com/test#Item"/>
    <owl:DatatypeProperty rdf:about="http://test.com/test#Quantity"/>
    <owl:NamedIndividual rdf:about="http://test.com/test#Box">
        <test:Quantity rdf:datatype="http://www.w3.org/2001/XMLSchema#integer">4</test:Quantity>
    </owl:NamedIndividual>
    <owl:NamedIndividual rdf:about="http://test.com/test#Item1">
        <test:Item rdf:resource="http://test.com/test#Box"/>
        <test:Item rdf:resource="http://test.com/test#PencilBox"/>
    </owl:NamedIndividual>
    <owl:NamedIndividual rdf:about="http://test.com/test#PencilBox">
        <test:Quantity rdf:datatype="http://www.w3.org/2001/XMLSchema#integer">5</test:Quantity>
    </owl:NamedIndividual>
    </rdf:RDF>