About Henriette Harmse

I am a software architect with 20 years experience as software developer, architect and consultant in a variety of industries (i.e. financial, healthcare, media, mining, etc). I have a PhD in Artificial Intelligence/Data Science. Currently I am working at EMBL-EBI where I am leading the development of their suite of Ontology Tools.

scala-logging with log4j2

In this brief post I provide a minimal complete example that uses scala-logging with log4j2. scala-logging is a Scala logging library wrapping SLF4J.

Scala code

Here is a minimal complete Scala code example using scala-logging. Note that we use a companion object to define an instance of Logger that will be shared across all instances of SimpleLoggingTest.

package org.henrietteharmse.tutorial

import com.typesafe.scalalogging.Logger

class SimpleLoggingTest {
  SimpleLoggingTest.logger.trace("Hello while instance
    of SimpleLoggingTest is created.")
}

object SimpleLoggingTest {
  private val logger = Logger[SimpleLoggingTest]

  def main(args: Array[String]):Unit = {
    logger.trace("Hello from SimpleLoggingTest 
      companion object")
    val simpleLoggingTest = new SimpleLoggingTest
    try {
      throw new RuntimeException("Some error")
    } catch {
      case t : Throwable => logger.error(s"Error: 
        ${t.getMessage}")
    }    
  }
}

Log4j configuration

In this section we provide a minimal log4j.xml configuration file. It defines a trace level logger for our application code with an error level logger for all other errors. Logging for our code will be written to a file in ./logs/App.log, and all errors, originating from other code than our own code, being logged to the console.

<?xml version="1.0" encoding="UTF-8"?>
  <Configuration status="WARN">
  <Appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%markerSimpleName %-5p 
        %C.%M():%L - %msg %ex{full}%n"/>
    </Console>
    <File name="Log" fileName="./logs/App.log">
      <PatternLayout>
        <Pattern>%markerSimpleName %-5p %C.%M():%L 
          - %msg %ex{full}%n</Pattern>
      </PatternLayout>
    </File>    
  </Appenders>
  <Loggers>
    <Logger name="org.henrietteharmse.tutorial" 
      level="trace" additivity="false">
      <AppenderRef ref="Log"/>
    </Logger>
    <Root level="error">
      <AppenderRef ref="Console"/>
    </Root>
  </Loggers>
</Configuration>

SBT configuration

The build.sbt file for building this application is given below. Most importantly it states the correct dependencies for using scala-logging with the libraryDependencies setting. To get debug information from log4j, we set the system property log4j2.debug to true.
ThisBuild / scalaVersion := "2.13.2"
ThisBuild / organization := 
  "org.henrietteharmse.tutorial"

val setLog4jDebug = sys.props("log4j2.debug") = "true"

lazy val root = (project in file("."))
  .settings(
  name := "scala-logging-with-log4j2",
  libraryDependencies ++= Seq(
    "com.typesafe.scala-logging" %% "scala-logging" % 
      "3.9.2",
    "org.slf4j" % "slf4j-api" % "1.7.30",
    "org.apache.logging.log4j" % "log4j-slf4j-impl" % 
      "2.13.3"
  ),
  scalacOptions ++= Seq("-deprecation")
)

Conclusion

This post gives a minimal complete example for using scala-logging with log4j using SBT. The complete code example can be found here on GitHub.

Setting up Virtuoso on Ubuntu 18.04

I recently needed to setup a local instance of Virtuoso on my Ubuntu 18.04 laptop. In particular I needed to be able to specify the directory that Virtuoso is installed. This meant that using a Linux package manager was not an option. I therefore opted to install Virtuoso from source.

  1. Here are the steps I followed to do this:
    We are using Virtuoso 7.2.2. in our production environment, hence I downloaded virtuoso-opensource-7.2.2.tar.gz from Virtuoso downloads.
  2. Then I followed the instructions to build Virtuoso from Building from Upstream Source.
    1. Check build environment by running the following commands:
      sudo apt-get install dpkg-dev build-essential 
      sudo apt-get install autoconf automake libtool flex bison gperf gawk m4 make odbcinst libxml2-dev libssl-dev libreadline-dev
      
    2. Extract the file:
      tar xvpfz virtuoso-opensource-7.2.2.tar.gz
    3. Now to configure Virtuoso to install to a directory of your choice, in the directory you extracted the .tar.gz, run the following:
      ./configure --prefix=directory_of_choice
      
    4. To build Virtuoso, run:
      make nice
      

      This step failed for me with an error message that looks something like the following

      make[3]: Entering directory '~/virtuoso-opensource/libsrc/Wi'
      /bin/bash ../../libtool  --tag=CC   --mode=compile gcc -DHAVE_CONFIG_H -I. -I../../libsrc/Dk    -fno-strict-aliasing -O2  -Wall  -DNDEBUG -DPOINTER_64   -I/home/klimek/virtuoso-opensource/libsrc/Xml.new  -DOPENSSL_NO_KRB5 -Dlinux -D_GNU_SOURCE -DFILE64 -D_LARGEFILE64_SOURCE   -I../../libsrc -I../../libsrc/Dk -I../../libsrc/zlib -I. -I../../libsrc/langfunc -I../../libsrc/plugin -I../../libsrc/Tidy -I../../libsrc/Xml.new -I../../libsrc/odbcsdk/include -DVAD -DDBP -DBIF_XPER -DOPSYS=\"Linux\" -DHOST=\"x86_64-unknown-linux-gnu\" -g -O2 -MT libwi_la-bif_crypto.lo -MD -MP -MF .deps/libwi_la-bif_crypto.Tpo -c -o libwi_la-bif_crypto.lo `test -f 'bif_crypto.c' || echo './'`bif_crypto.c
      libtool: compile:  gcc -DHAVE_CONFIG_H -I. -I../../libsrc/Dk -fno-strict-aliasing -O2 -Wall -DNDEBUG -DPOINTER_64 -I/home/klimek/virtuoso-opensource/libsrc/Xml.new -DOPENSSL_NO_KRB5 -Dlinux -D_GNU_SOURCE -DFILE64 -D_LARGEFILE64_SOURCE -I../../libsrc -I../../libsrc/Dk -I../../libsrc/zlib -I. -I../../libsrc/langfunc -I../../libsrc/plugin -I../../libsrc/Tidy -I../../libsrc/Xml.new -I../../libsrc/odbcsdk/include -DVAD -DDBP -DBIF_XPER -DOPSYS=\"Linux\" -DHOST=\"x86_64-unknown-linux-gnu\" -g -O2 -MT libwi_la-bif_crypto.lo -MD -MP -MF .deps/libwi_la-bif_crypto.Tpo -c bif_crypto.c  -fPIC -DPIC -o .libs/libwi_la-bif_crypto.o
      bif_crypto.c: In function ‘box_hmac’:
      bif_crypto.c:184:12: error: storage size of ‘ctx’ isn’t known
         HMAC_CTX ctx;
                  ^~~
      bif_crypto.c:190:3: warning: ‘HMAC_Init’ is deprecated [-Wdeprecated-declarations]
         HMAC_Init (&ctx, key, box_length (key) - DV_STRINGP (key) ? 1 : 0, md);
       ...

      which I fixed by installing libssl1.0-dev with

      sudo apt-get install libssl1.0-dev
      

      You can read more on this error here.
      After this re-run make nice.

    5. Lastly, I ran
      make install
      

      because I installed Virtuoso in my home directory. If you are not installing it in you home directory, you will need to run

      sudo make install
      
  3. To test your installation, go to localhost:8890.

 

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.