Skip to content
loopasam edited this page Mar 7, 2014 · 27 revisions

Table of content:


OWL classes handling

public static void main(String[] args) throws BrainException {

  //Creates a Brain object, containing an empty knowledge base.
  Brain brain = new Brain();
  
  //Add a new class called 'Nucleus' to the knowledge base.
  brain.addClass("Nucleus");
  
  //Retieves the class 'Nucleus'. The class is of type 'OWLClass'
  //as in the OWL-API
  OWLClass nucleus = brain.getOWLClass("Nucleus");
  
  //An exception will be thrown (BadNameException)
  //because the name is not encodable as a URI (blank spaces are not allowed).
  brain.addClass("Blood Coagulation");
  
  //Retrieves a class previously added to the knowledge base.
  OWLClass nucleus = brain.getOWLClass("Nucleus");
  
  //An error will be thrown (NonExistingClassException)
  //because the class in unknown in the knowledge base.
  brain.getOWLClass("Cell");
  
  //An exception (ExistingClassException) will be thrown because
  //the class already exists in the knowledge base.
  brain.addClass("Nucleus");
}

OWL object properties handling

public static void main(String[] args) throws BrainException {

  Brain brain = new Brain();  
  brain.addClass("Cellular-Component");
  
  //Add an object property
  brain.addObjectProperty("part-of");
  
  //Retrieves an object property as 'OWLObjectProperty'
  //as defined by the OWL-API
  OWLObjectProperty partOf = brain.getOWLObjectProperty("part-of");
  
  //Make an object property transitive
  brain.transitive("part-of");
  
  //Make an object property reflective
  brain.reflective("part-of");
  
  //Declare domain for the property
  brain.domain("part-of", "Cellular-Component");
  
  //Declare range for the property
  brain.range("part-of", "Cellular-Component");
  
  brain.addObjectProperty("regulates");
  brain.addObjectProperty("positively-regulates");
  
  //Declare chained properties. Here the chain expressed
  //is 'regulates o part-of' --> 'regulates'
  brain.chain("regulates o part-of", "regulates");  
}

OWL data properties handling

public static void main(String[] args) throws BrainException {

  Brain brain = new Brain();
  
  //Declare the OWL data property
  brain.addDataProperty("has-id");
  
  //Declare the property as functional 
  brain.functional("has-id");
  
  //Declare the range of the property using XML data type
  //here for instance: http://www.w3.org/2001/XMLSchema#integer
  brain.range("has-id", brain.INTEGER);
  
  //Declare the domain of the property
  brain.domain("has-id", "Thing");
}

OWL annotation properties handling

public static void main(String[] args) throws BrainException {

  Brain brain = new Brain();
  brain.addClass("BRAIN01");
  
  //Label the class using rdfs:label
  brain.label("BRAIN01", "This is the content of the label");
  
  //Retrieves the label of the class 'BRAIN01'
  String label = brain.getLabel("BRAIN01");
  
  //Label the class using rdfs:comment
  brain.comment("BRAIN01", "This is a useless comment");
  
  //Add a custom annotation property
  brain.addAnnotationProperty("creator");
    
  //Annotate the class with 
  brain.annotation("BRAIN01", "creator", "John Smith");
  
  //Retrieves the custom annotation property
  String creator = brain.getAnnotation("BRAIN01", "creator");

  //Sometimes multiple annotations are present for a given entity.
  //You can retrieve the list of values this way:
  String creators = brain.getAnnotations("BRAIN01", "creator");
}

OWL named individuals handling

public static void main(String[] args) throws BrainException {

  Brain brain = new Brain();
  
  //Add a new named individual to the ontology
  brain.addNamedIndividual("joe");
  brain.addNamedIndividual("mary");
  
  //Retrieves the individual
  OWLNamedIndividual joe = brain.getOWLNamedIndividual("joe");
  
  //Add a class
  brain.addClass("Human");

  //Type assertion for the individual
  brain.type("Human", "joe");
  
  //Add an object property to the ontology
  brain.addObjectProperty("knows");
  
  //Declare a triple
  brain.objectPropertyAssertion("joe", "knows", "mary")
  
  //Will return the individual joe (computed via reasoning)
  brain.getInstances("Human", false);
  brain.sleep();
}

File handling

public static void main(String[] args) throws BrainException {

  Brain brain = new Brain();
  brain.addClass("Nucleus");
  
  //Save the ontology at the specified path
  brain.save("your/path/knowledge-base.owl");
}

The default prefix for the classes is 'brain#'. The default ontology IRI is 'brain.owl'. The ontology is formated using Manchester OWL syntax.

Prefix: xsd: <http://www.w3.org/2001/XMLSchema#>
Prefix: owl: <http://www.w3.org/2002/07/owl#>
Prefix: : <brain#>
Prefix: xml: <http://www.w3.org/XML/1998/namespace>
Prefix: rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
Prefix: rdfs: <http://www.w3.org/2000/01/rdf-schema#>

Ontology: <brain.owl>

Class: Nucleus

It is possible to modify the default prefix by passing it to the constructor:

public static void main(String[] args) throws BrainException {

  //Specifiy a custom prefix (argument 1) and a custom IRI (argument 2) for
  //the ontology (where the ontology will be located on the web). 
  Brain brain = new Brain("http://www.example.org/", "http://www.example.org/demo.owl");
  brain.addClass("Nucleus");
  brain.save("your/path/knowledge-base.owl");
}

Notice that the default prefix changed in the file:

Prefix: : <http://www.example.org/>
Prefix: [...]

Ontology: <http://www.example.org/demo.owl>

Class: Nucleus

Axioms declaration (hierarchy)

public static void main(String[] args) throws BrainException {

  Brain brain = new Brain();
  brain.addClass("Nucleus");
  brain.addClass("Cell");
  brain.addObjectProperty("part-of");
  
  //Declare a subClassOf axiom. You can formulate class expressions too.
  brain.subClassOf("Nucleus", "part-of some Cell");
  
  brain.addClass("Biological-Cell");
  
  //Declare the two classes as equivalent
  brain.equivalentClasses("Biological-Cell", "Cell");
  
  brain.addObjectProperty("is-part-of");
  
  //Declare two properties as equivalent
  brain.equivalentProperties("part-of", "is-part-of");
  
  brain.addObjectProperty("positively-regulates");
  brain.addObjectProperty("regulates");
    
  //Declare a that the 'positively-regulates' object property
  //is a sub-property of the 'regulates' property
  brain.subPropertyOf("positively-regulates", "regulates");
  
}

References to external OWL entities

An important facet of OWL ontologies is the capacity to reference to a class from an external ontology. In Brain you have the possibility to do so. Just enter the valid IRI of the entity you want to refer too. You can also configure the prefix if you want to save a clean file later on.

public static void main(String[] args) throws BrainException {

  Brain brain = new Brain();
  brain.addClass("http://www.example.org/External-Class");
  brain.save("your/path/knowledge-base.owl");
}

The prefix of the class added will not be considered anymore, except when saving the file:

Prefix: : <brain#>
Prefix: : [...]

Ontology: <brain.owl>

Class: <http://www.example.org/External-Class>

It is possible to clean a bit the file by defining a prefix:

public static void main(String[] args) throws BrainException {

  Brain brain = new Brain();
  brain.addClass("http://www.example.org/External-Class");
  
  //Declare the prefix, first argument is the long form and the
  //second argument is the shortcut.
  brain.prefix("http://www.example.org/", "example");
  brain.save("your/path/knowledge-base.owl");
}

The file now:

Prefix: example: <http://www.example.org/>
Prefix: [...]

Ontology: <brain.owl>

Class: example:External-Class

Loading external knowledge-bases

public static void main(String[] args) throws BrainException {

  Brain brain = new Brain();
  
  //Load the ontology from the web
  brain.learn("https://raw.github.com/loopasam/Brain/master/src/test/resources/demo.owl");
  
  //Knowledge bases can also be learn from other brain objects
  Brain brain2 = new Brain();
  brain2.learn(brain);
  
  //The class is accessible and the content of the loaded ontology is accessible
  //standard operations can be performed, e.g.
  OWLClass cell = brain.getOWLClass("Cell");
  brain.addClass("MyNewClass");
  
  //Declare a prefix shortcut before saving in order to get a more human readable file
  brain.prefix("https://raw.github.com/loopasam/Brain/master/src/test/resources/", "demo");
  brain.save("your/path/knowledge-base.owl");
}

Querying the ontology with the reasoner

public static void main(String[] args) throws BrainException {
  
  Brain brain = new Brain();
  brain.learn("https://raw.github.com/loopasam/Brain/master/src/test/resources/demo.owl");
  
  //Retrieves the name of the subclasses of an expression (first argument).
  //The second argument direct whether only the direct subclasses should be
  //retrieved or not (true = subclasses; false = descendant classes).
  //This method is synchronized and should be thread safe.
  List<String> subClasses = brain.getSubClasses("part-of some Cell", false);
  
  //Iterates over the list and print the result.
  for (String subClass : subClasses) {
    System.out.println("Subclass of 'part-of some Cell': " + subClass);
  }

  brain.addClass("A");
  
  //Retrieves all the direct subclasses of 'Thing' (top level)
  //and prints the results
  List<String> subClasses1 = brain.getSubClasses("Thing", true);
  for (String subClass1 : subClasses1) {
    System.out.println("Subclass of 'Thing': " + subClass1);
  }
  
  //It is also possible to retrieve the super classes:
  brain.getSuperClasses("A", true);
  
  //as well as the equivalentClasses:
  brain.getEquivalentClasses("part-of some Cell");
  
  //Do not forget to put to sleep in order to free resources after all queries are done.
  brain.sleep();
}

Querying the ontology via the labels of entities

public static void main(String[] args) throws BrainException {
  
  Brain brain = new Brain();
  brain.addClass("ID01");
  brain.label("ID01", "Cell");
  
  //Retrieves the name of the subclasses of an expression
  //Formulated with entities labels (`rdfs:label`).
  //This method is synchronized and should be thread safe.
  List<String> subClasses = brain.getSubClassesFromLabel("Cell", false);
  //Note that labels should not contain spaces, or if they do the label
  //must be surrounded with single quotes (').
  
  //Do not forget to put to sleep in order to free resources after all queries are done.
  brain.sleep();
}

Logical test via the reasoner

public static void main(String[] args) throws BrainException {
  
  Brain brain = new Brain();
  brain.learn("https://raw.github.com/loopasam/Brain/master/src/test/resources/demo.owl");
  
  //Test whether 'Cell' is a direct subclass of 'Thing'
  boolean test = brain.isSubClass("Cell", "Thing", true);
  
  //Test for the super classes
  boolean test1 = brain.isSuperClass("part-of some Cell", "Nucleus", true);
  
  //Do not forget to put to sleep in order to free resources after all queries are done.
  brain.sleep();
}

Testing the profile of the ontology

Brain is focused on the handling of ontologies respecting the OWL 2 EL profile. However it doesn't strictly prevent you from using constructs not in the scope of this profile, as you still have access to the underlying OWL-API (facade pattern). If you have doubts about the profile of the ontology you are using, use this method to retrieve the violations.

public static void main(String[] args) throws BrainException {

  Brain brain = new Brain();
  brain.learn("https://raw.github.com/loopasam/Brain/master/src/test/resources/demo.owl");
  
  //Determine whether the ontology follows the EL profile or not
  boolean hasElProfile = brain.hasElProfile();
  
  //Print out the violations
  //A common violation not affecting the logic of the ontology
  //is 'Ontology IRI not absolute: <brain.owl>'. It is your call to look
  //at the violations and to correct them. Some of them are harmless,
  //but some of them might corrupt the results or throw errors later on
  //if not solved
  for (String violation : brain.getElProfileViolations()) {
    System.out.println(violation);
  }
}

Retrieving unsatisfiable classes

public static void main(String[] args) throws BrainException {

  Brain brain = new Brain();
  brain.addClass("Cell");
  
  //Asserts an inconsistent axiom
  brain.disjointClasses("Thing", "Cell");
  
  //Retrieves the unsatisfiable classes - "Cell" is in this list
  List<String> unsatisfiableClasses = brain.getUnsatisfiableClasses();
  
  //Do not forget to put to sleep in order to free resources after all queries are done.
  brain.sleep();
}

Access to OWL-API methods

Sometimes Brain methods do not provide enough control over the knowledge base. In such cases, the original OWL-API objects can be retrieved the following way:

public static void main(String[] args) throws BrainException {

  Brain brain = new Brain();
  
  //Retrieves the OWLOntology object from the OWL-API 
  brain.getOntology();
  
  //Retrieves the OWLOntology object from the OWL-API 
  //and applies a OWL-API method to count the axioms.
  brain.getOntology().getAxiomCount();
  
  //Retrieves the OWLDataFactory object from the OWL-API 
  brain.getFactory();
}