Henriette's Notes

Home » Articles posted by Henriette Harmse

Author Archives: Henriette Harmse

Understanding OWL min vs max vs exactly Property Restrictions

The open world assumption trips people up in many ways. In this post we will be looking at how the open world assumption affects the semantics of the property restrictions min, max and exactly.

The example we will use throughout this post is that of a product that may have no price, 1 price, exactly 1 price, or many prices. We will firstly assume a product must have at least 1 price, then that it can have a maximum of 1 price, and finally we will assume that it must have exactly 1 price. We therefore assume that we have a hasPrice data property that is defined as follows:

DataProperty: hasPrice
  Range: 
    xsd:decimal

The example ontology can be found on GitHub. To be able to distinguish the different product types having different rules regarding how many prices they have, we will create 3 different classes called ProductWith_Min_1_Price, ProductWith_Max_1_Price and ProductWith_Exactly_1_Price respectively.

Before we look at examples and the semantics of the min, max, exactly property restrictions, let us briefly recall what is meant by the open world assumption.

Open World Assumption versus Closed World Assumption

OWL has been designed with the explicit intention to be able to deal with incomplete information. Consequently, OWL intentionally does not make any assumptions with regards to information that is not known. In particular, no assumption is made about the truth or falsehood of facts that cannot be deduced from the ontology. This approach is known as the open world assumption. This approach is in contrast with the closed world assumption typically used in information systems. With the closed world assumption facts that cannot be deduced from a knowledge base (i.e. database) are implicitly understood as being false [1, 2, 3].

As an example, in a database when a product does not have a price, the general assumption is that the product does not have price. Moreover, in a database if a product has 1 price, the assumption is that the product has only 1 price and no other prices. This in stark contrast to OWL. If an OWL ontology defines a product for which no explicit price is given, the assumption is not that the product has no price. Rather, no assumption is made as to whether the product has a price, has many prices or whether it has no price. Futhermore, if a product has a price, the assumption is not that this is necessarily the only price for that product. Rather, it allows for the possibility that no other price may exist, or that many other prices may exist, which is merely not known. The only information that holds in an ontology is information that is either explicitly stated, or that can be derived form explicit information.

The min Property Restriction

To define a product that must have at least 1 price we define it as follows:

Class: ProductWith_Min_1_Price
  SubClassOf: 
    hasPrice min 1 xsd:decimal
  
Individual: productWithoutPrice
  Types:  
    ProductWith_Min_1_Price

If we now create an individual of type ProductWith_Min_1_Price, say productWithoutPrice, which has no price information, we will find that the reasoner will not give an inconsistency. The reason for this is that the reasoner has no information with regards to whether productWithoutPrice has any price information. Hence, it is possible
that productWithoutPrice has a price (or prices) that is merely unknown. To make explicit that productWithoutPrice has no price information, we can define it as follows:

Individual: productWithoutPrice
  Types:  
    ProductWith_Min_1_Price,
    hasPrice max 0 xsd:decimal

This revised definition of productWithoutPrice will now result in the reasoner detecting an inconsistency. However, note that ProductWith_Min_1_Price allows for products
that have more than 1 price. Hence, the following will not result in an inconsistency.

Individual: productWithManyPrices
  Types:  
    ProductWith_Min_1_Price
  Facts:  
    hasPrice 2.5,
    hasPrice 3.25 

The max Property Restriction

To define a product that cannot have more than 1 price, we can define it as
follows:

Class: ProductWith_Max_1_Price
  SubClassOf: 
    hasPrice max 1 xsd:decimal 

If we now define an individual productWithMoreThan1Price with more than 1 price (as shown in the example below), the reasoner will detect an inconsistency.

Individual: productWithMoreThan1Price
  Types: 
    ProductWith_Max_1_Price
  Facts:  
    hasPrice 2.5,
    hasPrice 3.25

Note that individuals of type ProductWith_Max_1_Price can also have no price information without resulting in the reasoner giving an inconsistency. I.e., if we define the individual productWithoutPrice as

Individual: productWithoutPrice
  Types:  
    ProductWith_Max_1_Price,
    hasPrice max 0 xsd:decimal

it will not give an inconsistency.

The exactly Property Restriction

Let us now define ProductWith_Exactly_1_Price with the individual productWithExactly1Price as follows:

Class: ProductWith_Exactly_1_Price
  SubClassOf: 
    hasPrice exactly 1 xsd:decimal    
    
Individual: productWithExactly1Price
  Types: 
    ProductWith_Exactly_1_Price  
  Facts:
    hasPrice 7.1

The exactly property is essentially syntactical shorthand for specifying both the min and max restrictions using the same cardinality. Thus, we could just as well have defined ProductWith_Exactly_1_Price as:

Class: ProductWith_Exactly_1_Price
  SubClassOf: 
     hasPrice min 1 xsd:decimal,
     hasPrice max 1 xsd:decimal

or, given the classes we have already defined in the ontology, we can define it as:

Class: ProductWith_Exactly_1_Price
  SubClassOf: 
    ProductWith_Max_1_Price,
    ProductWith_Min_1_Price

Prefer exactly

Given that the exactly property restriction is syntactical sugar, should we prefer using the combination of min and max directly as shown above? My answer to this is no. My motivation for this is that the semantics of exactly is only equivalent to the intersection of min and max if the cardinalities are the same and the data/object types are the same. As such specifying

Class: ProductWith_Exactly_1_Price
  SubClassOf: 
    hasPrice exactly 1 xsd:decimal

has less opportunities for mistakes than specifying

Class: ProductWith_Exactly_1_Price
  SubClassOf: 
    hasPrice min 1 xsd:decimal,
    hasPrice max 1 xsd:decimal

Conclusion

In this post we looked at some of the ways in which the min, max and exactly property restrictions can trip people up due to the open world assumption. Please feel free to leave a comment if you have questions or suggestions about this post.

References

1. F. Baader and W. Nutt, Basic Description Logics, The Description Logic Handbook: Theory, Implementation and Applications (F. Baader, D. Calvanese, D. L. McGuinness, D. Nardi, and P. Patel-Schneider, eds.), Cambridge University Press, New York, USA, 2007, pp. 45–104.

2. M. Krötzsch, F. Simančı́k, and I. Horrocks, A Description Logic Primer, Computing Research Repository (CoRR) abs/1201.4089 (2012).

3. S. Rudolph, Foundations of Description Logics, Proceedings of the 7th International Conference on Reasoning Web: Semantic Technologies for the Web of Data (A. Polleres, C. d’Amato, M. Arenas, S. Handschuh, P. Kroner, S. Ossowski, and P. F. Patel-Schneider, eds.), Lecture Notes in Computer Science, vol. 6848, Springer, 2011, pp. 76–136.

Getting started with Ontotext GraphDB and Jena

In my previous post I explained how you can create an GraphDB repository and how you can update and query your repository using RDF4J. In this post I provide an example of how you can update and query a GraphDB repository using Jena. However, even though the code works, there are some pitfalls.

A Quick Example

First you need to add Jena as a Maven dependency:

<dependency>
 <groupId>org.apache.jena</groupId>
 <artifactId>apache-jena-libs</artifactId>
 <version>3.7.0</version>
 <type>pom</type>
</dependency>  

The Java code is straightforward:

package org.graphdb.jena.tutorial;

import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryExecutionFactory;
import org.apache.jena.query.QuerySolution;
import org.apache.jena.query.ResultSet;
import org.apache.jena.update.UpdateExecutionFactory;
import org.apache.jena.update.UpdateFactory;
import org.apache.jena.update.UpdateProcessor;
import org.apache.jena.update.UpdateRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;

public class SimpleInsertQueryExample {
  private static Logger logger = LoggerFactory.getLogger(SimpleInsertQueryExample.class);
  // Why This Failure marker
  private static final Marker WTF_MARKER = MarkerFactory.getMarker("WTF");
  
  // GraphDB 
  private static final String PERSONDATA_REPO_QUERY = 
      "http://localhost:7200/repositories/PersonData";
  private static final String PERSONDATA_REPO_UPDATE = 
      "http://localhost:7200/repositories/PersonData/statements";


  private static String strInsert;
  private static String strQuery;
  
  static {
    strInsert = 
        "INSERT DATA {"
         + "<http://dbpedia.org/resource/Grace_Hopper> "
         + "<http://dbpedia.org/ontology/birthDate>"
         + " \"1906-12-09\"^^<http://www.w3.org/2001/XMLSchema#date> ."
         + "<http://dbpedia.org/resource/Grace_Hopper> "
         + "<http://dbpedia.org/ontology/birthPlace> "
         + "<http://dbpedia.org/resource/New_York_City> ."
         + "<http://dbpedia.org/resource/Grace_Hopper> "
         + "<http://dbpedia.org/ontology/deathDate>"
         + " \"1992-01-01\"^^<http://www.w3.org/2001/XMLSchema#date> ."
         + "<http://dbpedia.org/resource/Grace_Hopper> "
         + "<http://dbpedia.org/ontology/deathPlace> "
         + "<http://dbpedia.org/resource/Arlington_County,_Virginia> ."
         + "<http://dbpedia.org/resource/Grace_Hopper> "
         + "<http://purl.org/dc/terms/description>"
         + " \"American computer scientist and United States Navy officer.\" ."
         + "<http://dbpedia.org/resource/Grace_Hopper> "
         + "<http://www.w3.org/1999/02/22-rdf-syntax-ns#type> "
         + "<http://dbpedia.org/ontology/Person> ."
         + "<http://dbpedia.org/resource/Grace_Hopper> "
         + "<http://xmlns.com/foaf/0.1/gender> \"female\" ."
         + "<http://dbpedia.org/resource/Grace_Hopper> "
         + "<http://xmlns.com/foaf/0.1/givenName> \"Grace\" ."
         + "<http://dbpedia.org/resource/Grace_Hopper> "
         + "<http://xmlns.com/foaf/0.1/name> \"Grace Hopper\" ."
         + "<http://dbpedia.org/resource/Grace_Hopper>"
         + " <http://xmlns.com/foaf/0.1/surname> \"Hopper\" ."        
         + "}";
    
    strQuery = 
        "SELECT ?name WHERE {?s <http://xmlns.com/foaf/0.1/name> ?name .}";
  }   
  
  private static void insert() {
    UpdateRequest updateRequest = UpdateFactory.create(strInsert);
    UpdateProcessor updateProcessor = UpdateExecutionFactory
        .createRemote(updateRequest, 
        PERSONDATA_REPO_UPDATE);
    updateProcessor.execute();
  }
  
  private static void query() {
    QueryExecution queryExecution = QueryExecutionFactory
        .sparqlService(PERSONDATA_REPO_QUERY, strQuery);
    for (ResultSet results = queryExecution.execSelect(); results.hasNext();) {
      QuerySolution qs = results.next();
      String strName = qs.get("?name").toString();
      logger.trace("name = " + strName);
    }    
    queryExecution.close();
  }

  
  public static void main(String[] args) {
    try {
      insert(); 
      query();      
    } catch (Throwable t) {
      logger.error(WTF_MARKER, t.getMessage(), t);
    }   
  }  
}

Some Pitfalls

The example I provided will insert RDF data into GraphDB and query it successfully. However, the data is inserted into the repository in the absence of a transaction. The transaction API of Jena is based on a Dataset. Historically Ontotext provided a Jena adapter with which a Jena Dataset could be created. However, based on my question on Stack Overflow in this regard, the Jena adapter is no longer supported by Ontotext. Hence, currently it is not clear to me how to enable transactions when using Jena to access GraphDB. So, if you know how to address this, please be so kind as to leave a comment with your insight!

Conclusion

In this post I provided a quick example of how you can access GraphDB using Jena. However, this example does not support transactions, and therefore you may want to look at rather using RDF4J with GraphDB. You can find this code at github.

Getting started with Ontotext GraphDB and RDF4J

In this post I will explain how to quickly get started with the free version of Ontotext GraphDB and RDF4J. Ontotext GraphDB is an RDF datastore and RDF4J is a Java framework for accessing RDF datastores (not just GraphDB). I will explain

  1. how to install and start GraphDB, as well as how to use the workbench to add a repository, and
  2. how to do SPARQL queries against GraphDB using RDF4J.

Install and start GraphDB and create a Repository

To gain access to the free version of GraphDB you have to email Ontotext. They will respond with an email with links to a desktop and stand-alone server version of GraphDB. You want to download the stand-alone server version. This is a graphdb-free-VERSION-dist.zip file, that you can extract somewhere on your filesystem, which I will refer to here as $GRAPHDB_ROOT. To start GraphDB, go to $GRAPHDB_ROOT/bin and run ./graphdb.

To access the workbench you can go to http://localhost:7200. To create a new repository, in the left-hand side menu navigate to Setup>Repositories. Click the Create new repository button. For our simple example we will use PersonData as an Repository ID. The rest of the settings we leave as-is. At the bottom of the page you can press the Create button.

Accessing a GraphDB Repository using RDF4J

To access our PersonData repository we will use RDF4J. Since GraphDB is based on the RDF4J libraries, we only need to include the GraphDB dependencies since these already include RDF4J. Thus, in our pom.xml file we only need to add the following:

 <dependency>
   <groupId>com.ontotext.graphdb</groupId>
   <artifactId>graphdb-free-runtime</artifactId>
   <version>8.5.0</version>
 </dependency>

In our example Java code we first insert some RDF data and then do a query based on the added data. For inserting data we start a transaction and commit it, or, if it fails we do a rollback. For querying the data we iterate through the TupleQueryResult, retrieving values for the binding variables we are interested in (i.e. name in this case). In line with the TupleQueryResult documentation, we close the TupleQueryResult once we are done.

package org.graphdb.rdf4j.tutorial;
package org.graphdb.rdf4j.tutorial;

import org.eclipse.rdf4j.model.impl.SimpleLiteral;
import org.eclipse.rdf4j.query.BindingSet;
import org.eclipse.rdf4j.query.QueryEvaluationException;
import org.eclipse.rdf4j.query.QueryLanguage;
import org.eclipse.rdf4j.query.TupleQuery;
import org.eclipse.rdf4j.query.TupleQueryResult;
import org.eclipse.rdf4j.query.Update;
import org.eclipse.rdf4j.repository.Repository;
import org.eclipse.rdf4j.repository.RepositoryConnection;
import org.eclipse.rdf4j.repository.http.HTTPRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;

public class SimpleInsertQueryExample {
  private static Logger logger = 
    LoggerFactory.getLogger(SimpleInsertQueryExample.class);
  // Why This Failure marker
  private static final Marker WTF_MARKER = 
    MarkerFactory.getMarker("WTF");
  
  // GraphDB 
  private static final String GRAPHDB_SERVER = 
    "http://localhost:7200/";
  private static final String REPOSITORY_ID = "PersonData";

  private static String strInsert;
  private static String strQuery;
  
  static {
    strInsert = 
        "INSERT DATA {"
         + "<http://dbpedia.org/resource/Grace_Hopper> <http://dbpedia.org/ontology/birthDate> \"1906-12-09\"^^<http://www.w3.org/2001/XMLSchema#date> ."
         + "<http://dbpedia.org/resource/Grace_Hopper> <http://dbpedia.org/ontology/birthPlace> <http://dbpedia.org/resource/New_York_City> ."
         + "<http://dbpedia.org/resource/Grace_Hopper> <http://dbpedia.org/ontology/deathDate> \"1992-01-01\"^^<http://www.w3.org/2001/XMLSchema#date> ."
         + "<http://dbpedia.org/resource/Grace_Hopper> <http://dbpedia.org/ontology/deathPlace> <http://dbpedia.org/resource/Arlington_County,_Virginia> ."
         + "<http://dbpedia.org/resource/Grace_Hopper> <http://purl.org/dc/terms/description> \"American computer scientist and United States Navy officer.\" ."
         + "<http://dbpedia.org/resource/Grace_Hopper> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/ontology/Person> ."
         + "<http://dbpedia.org/resource/Grace_Hopper> <http://xmlns.com/foaf/0.1/gender> \"female\" ."
         + "<http://dbpedia.org/resource/Grace_Hopper> <http://xmlns.com/foaf/0.1/givenName> \"Grace\" ."
         + "<http://dbpedia.org/resource/Grace_Hopper> <http://xmlns.com/foaf/0.1/name> \"Grace Hopper\" ."
         + "<http://dbpedia.org/resource/Grace_Hopper> <http://xmlns.com/foaf/0.1/surname> \"Hopper\" ."        
         + "}";
    
    strQuery = 
        "SELECT ?name FROM DEFAULT WHERE {" +
        "?s <http://xmlns.com/foaf/0.1/name> ?name .}";
  }  
  
  private static RepositoryConnection getRepositoryConnection() {
    Repository repository = new HTTPRepository(
      GRAPHDB_SERVER, REPOSITORY_ID);
    repository.initialize();
    RepositoryConnection repositoryConnection = 
      repository.getConnection();
    return repositoryConnection;
  }
  
  private static void insert(
    RepositoryConnection repositoryConnection) {
    
    repositoryConnection.begin();    
    Update updateOperation = repositoryConnection
      .prepareUpdate(QueryLanguage.SPARQL, strInsert);
    updateOperation.execute();
    
    try {
      repositoryConnection.commit();
    } catch (Exception e) {
      if (repositoryConnection.isActive())
        repositoryConnection.rollback();
    }
  }

  private static void query(
    RepositoryConnection repositoryConnection) {
    
    TupleQuery tupleQuery = repositoryConnection
      .prepareTupleQuery(QueryLanguage.SPARQL, strQuery);
    TupleQueryResult result = null;
    try {
      result = tupleQuery.evaluate();
      while (result.hasNext()) {
        BindingSet bindingSet = result.next();

        SimpleLiteral name = 
          (SimpleLiteral)bindingSet.getValue("name");
        logger.trace("name = " + name.stringValue());
      }
    }
    catch (QueryEvaluationException qee) {
      logger.error(WTF_MARKER, 
        qee.getStackTrace().toString(), qee);
    } finally {
      result.close();
    }    
  }  
  
  public static void main(String[] args) {
    RepositoryConnection repositoryConnection = null;
    try {   
      repositoryConnection = getRepositoryConnection();
      
      insert(repositoryConnection);
      query(repositoryConnection);      
      
    } catch (Throwable t) {
      logger.error(WTF_MARKER, t.getMessage(), t);
    } finally {
      repositoryConnection.close();
    }
  }  
}

Conclusion

In this brief post I gave a quick example of how you can setup a simple GraphDB repository and query it using SPARQL. You can find sample code on github.