phenoscape / scowl   1.4.1

MIT License GitHub

A Scala DSL for programming with the OWL API.

Scala versions: 3.x 2.13 2.12 2.11 2.10



Scowl provides a Scala DSL allowing a declarative approach to composing OWL expressions and axioms using the OWL API.


Since version 1.2.1, Scowl is available via Maven Central. Add the dependency to your build.sbt if you are using OWL API 4.x:

libraryDependencies += "org.phenoscape" %% "scowl" % "1.4.1"

For OWL API 5.x:

libraryDependencies += "org.phenoscape" %% "scowl-owlapi5" % "1.4.1"

Import org.phenoscape.scowl._, and Scowl implicit conversions will add pseudo Manchester syntax methods to native OWL API objects. Additionally, functional syntax-style constructors and extractors will be in scope.

Scowl 1.2+ is built with OWL API 4.x (and from 1.4.1, additionally OWL API 5.x). For OWL API 3.5, use Scowl 1.0.2. Scowl is cross-compiled to support Scala 2.13 and Scala 3.


The easiest way to get started is to see how the DSL can be used to implement all the examples from the OWL 2 Web Ontology Language Primer:

The examples below are also available in code.

Scowl expressions use and return native OWL API objects

import org.phenoscape.scowl._
// import org.phenoscape.scowl._

val hasParent = ObjectProperty("")
// hasParent: org.semanticweb.owlapi.model.OWLObjectProperty = <>

val isParentOf = ObjectProperty("")
// isParentOf: org.semanticweb.owlapi.model.OWLObjectProperty = <>

val isSiblingOf = ObjectProperty("")
// isSiblingOf: org.semanticweb.owlapi.model.OWLObjectProperty = <>

val Person = Class("")
// Person: org.semanticweb.owlapi.model.OWLClass = <>

val FirstCousin = Class("")
// FirstCousin: org.semanticweb.owlapi.model.OWLClass = <>

val axiom = FirstCousin EquivalentTo (Person and (hasParent some (Person and (isSiblingOf some (Person and (isParentOf some Person))))))
// axiom: org.semanticweb.owlapi.model.OWLEquivalentClassesAxiom = EquivalentClasses(<> ObjectIntersectionOf(<> ObjectSomeValuesFrom(<> ObjectIntersectionOf(<> ObjectSomeValuesFrom(<> ObjectIntersectionOf(<> ObjectSomeValuesFrom(<> <>)))))) )

Add some axioms and programmatically generated GCIs to an ontology

val manager = OWLManager.createOWLOntologyManager()
val ontology = manager.createOntology()
val PartOf = ObjectProperty("")
val HasPart = ObjectProperty("")
val DevelopsFrom = ObjectProperty("")
val Eye = Class("")
val Head = Class("")
val Tail = Class("")

manager.addAxiom(ontology, Eye SubClassOf (PartOf some Head))
manager.addAxiom(ontology, Eye SubClassOf (not(PartOf some Tail)))

val gcis = for {
  term <- ontology.getClassesInSignature(true)
} yield {
  (not(HasPart some term)) SubClassOf (not(HasPart some (DevelopsFrom some term)))
manager.addAxioms(ontology, gcis)

Using pattern matching extractors to implement negation normal form

def nnf(expression: OWLClassExpression): OWLClassExpression = expression match {
  case Class(_)                                                          => expression
  case ObjectComplementOf(Class(_))                                      => expression
  case ObjectComplementOf(ObjectComplementOf(expression))                => nnf(expression)
  case ObjectUnionOf(operands)                                           => ObjectUnionOf(
  case ObjectIntersectionOf(operands)                                    => ObjectIntersectionOf(
  case ObjectComplementOf(ObjectUnionOf(operands))                       => ObjectIntersectionOf( => nnf(ObjectComplementOf(c))))
  case ObjectComplementOf(ObjectIntersectionOf(operands))                => ObjectUnionOf( => nnf(ObjectComplementOf(c))))
  case ObjectAllValuesFrom(property, filler)                             => ObjectAllValuesFrom(property, nnf(filler))
  case ObjectSomeValuesFrom(property, filler)                            => ObjectSomeValuesFrom(property, nnf(filler))
  case ObjectMinCardinality(num, property, filler)                       => ObjectMinCardinality(num, property, nnf(filler))
  case ObjectMaxCardinality(num, property, filler)                       => ObjectMaxCardinality(num, property, nnf(filler))
  case ObjectExactCardinality(num, property, filler)                     => ObjectExactCardinality(num, property, nnf(filler))
  case ObjectComplementOf(ObjectAllValuesFrom(property, filler))         => ObjectSomeValuesFrom(property, nnf(ObjectComplementOf(filler)))
  case ObjectComplementOf(ObjectSomeValuesFrom(property, filler))        => ObjectAllValuesFrom(property, nnf(ObjectComplementOf(filler)))
  case ObjectComplementOf(ObjectMinCardinality(num, property, filler))   => ObjectMaxCardinality(math.max(num - 1, 0), property, nnf(filler))
  case ObjectComplementOf(ObjectMaxCardinality(num, property, filler))   => ObjectMinCardinality(num + 1, property, nnf(filler))
  case ObjectComplementOf(ObjectExactCardinality(num, property, filler)) => ObjectUnionOf(ObjectMinCardinality(num + 1, property, nnf(filler)), ObjectMaxCardinality(math.max(num - 1, 0), property, nnf(filler)))
  case _                                                                 => ???

Using pattern matching extractors in for comprehensions

// Print all properties and fillers used in existential restrictions in subclass axioms
for {
  SubClassOf(_, subclass, ObjectSomeValuesFrom(property, filler)) <- ontology.getAxioms
} yield {
  println(s"$property $filler")

// Make an index of language tags to label values
val langValuePairs = for {
  AnnotationAssertion(_, RDFSLabel, _, value @@ Some(lang)) <- ontology.getAxioms(Imports.INCLUDED)
} yield {
  lang -> value
val langToValues: Map[String, Set[String]] = langValuePairs.foldLeft(Map.empty[String, Set[String]]) {
  case (langIndex, (lang, value)) =>
    langIndex.updated(lang, langIndex.getOrElse(value, Set.empty) ++ Set(value))

Question or problem?

If you have questions about how to use Scowl, feel free to send an email to [email protected], or open an issue on the tracker. Contributions are welcome.


Development of Scowl has been supported by National Science Foundation grant DBI-1062404 to the University of North Carolina.


Scowl is open source under the MIT License. See LICENSE for more information.