Importing Individuals from a text file into an OWL Ontology (Protege) - owl

I basically have a large file of a few thousand names each on a new line in .txt. I am using Protege to build my ontology and I want a quicker way to insert these names as Individuals into the concept 'Person' in my Ontology. is there anyway that this can be done using Protege or the OWL API as clicking the add button in protege and typing/copying each name then adding it to the 'Person' concept will take some time.
Thanks for any suggestions.

If using the OWL API, there is an example of how to do just this in the examples provided in the documentation:
public void shouldAddClassAssertion() throws OWLOntologyCreationException,
OWLOntologyStorageException {
// For more information on classes and instances see the OWL 2 Primer
// http://www.w3.org/TR/2009/REC-owl2-primer-20091027/#Classes_and_Instances
// In order to say that an individual is an instance of a class (in an
// ontology), we can add a ClassAssertion to the ontology. For example,
// suppose we wanted to specify that :Mary is an instance of the class
// :Person. First we need to obtain the individual :Mary and the class
// :Person Create an ontology manager to work with
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
OWLDataFactory dataFactory = manager.getOWLDataFactory();
// The IRIs used here are taken from the OWL 2 Primer
String base = "http://example.com/owl/families/";
PrefixManager pm = new DefaultPrefixManager(base);
// Get the reference to the :Person class (the full IRI will be
// <http://example.com/owl/families/Person>)
OWLClass person = dataFactory.getOWLClass(":Person", pm);
// Get the reference to the :Mary class (the full IRI will be
// <http://example.com/owl/families/Mary>)
OWLNamedIndividual mary = dataFactory
.getOWLNamedIndividual(":Mary", pm);
// Now create a ClassAssertion to specify that :Mary is an instance of
// :Person
OWLClassAssertionAxiom classAssertion = dataFactory
.getOWLClassAssertionAxiom(person, mary);
// We need to add the class assertion to the ontology that we want
// specify that :Mary is a :Person
OWLOntology ontology = manager.createOntology(IRI.create(base));
// Add the class assertion
manager.addAxiom(ontology, classAssertion);
// Dump the ontology to stdout
manager.saveOntology(ontology, new StreamDocumentTarget(
new ByteArrayOutputStream()));
}

Related

Check OWL Class

Suppose the range of an object property is defined through a class expression. I want to know whether a particular instance of the class can be used in the range of the object property. Is it possible to use the OWL API and check if a particular class is subsumed by this class expression ?
OWLAPI cannot provide a complete answer for this - an OWLReasoner implementation is necessary for complete results.
Given an implementation of OWLReasoner, to check entailment you can either list all the subclasses of a class expression and check if the class you're interested in appears in the response, or ask the reasoner if your class of interest is a subclass, i.e.,
OWLDataFactory df = ...
OWLClassExpression ce = ...
OWLClass c = ...
OWLReasoner r = ...
OWLAxiom ax = df.getOWLObjectSubClassOf(c, ce);
boolean cIsSubclassOfCE = r.isEntailed(ax);

How to analyze a complex class axiom using OWL API

I read the OWL API documentation, most of the examples are about create class axioms and add them to the ontology. Now, I need to retrieve the restriction of a class, and extract the elements in the restriction.
For example, in the pizza.owl, ChessePizza class is defined by the restriction: "Pizza and (hasTopping some CheeseTopping)". I can use the "getEquivalentClassesAxioms" function to get the whole axiom. But I want to know the details of this axiom, such as the object properties (hasTopping) and classes (CheeseTopping) used in this axiom. Is there any method to extract the elements of a axiom?
The best approach to, for example, extract the property for all existential restrictions, is to write an OWLObjectVisitor.
In a visitor, you implement a visit(OWL... o) for each class that the visitor knows about. For an axiom that defines A equivalentTo p some Q, the visitor would look something like:
OWLObjectVisitor v = new OWLObjectVisitor() {
public void visit(OWLEquivalentClassesAxiom ax) {
// this is an example of recursive visit
ax.classExpressions().forEach(c->c.accept(v));
}
public void visit(OWLObjectSomeValuesFrom ce) {
OWLObjectPropertyExpression p = ce.getProperty();
// here you can do what you need with the property.
}
};
axiom.accept(v);

Why OWLReasoner.getSubclasses(...) returns http://www.w3.org/2002/07/owl#Nothing?

Here is a piece of my ontology subclasses request, using JAVA-7 and owlapi library:
import org.semanticweb.owlapi.reasoner.OWLReasoner;
import org.semanticweb.owlapi.reasoner.OWLReasonerFactory;
import org.semanticweb.owlapi.reasoner.ConsoleProgressMonitor;
import org.semanticweb.owlapi.reasoner.OWLReasonerConfiguration;
...
...
OWLReasonerFactory reasonerFactory = new StructuralReasonerFactory();
ConsoleProgressMonitor progressMonitor = new ConsoleProgressMonitor();
OWLReasonerConfiguration config = new SimpleConfiguration(myconfiguration);
OWLReasoner reasoner = reasonerFactory.createReasoner(myontology, config);
Set<OWLClass> subclasses = reasoner.getSubClasses(myClazz, true).getFlattened();
Here is my question:
Why does the subclasses, what OWLReasoner.getSubClasses(...) method returns, contains all the subclasses of myClazz, but always also adds the OWLClass with URI http://www.w3.org/2002/07/owl#Nothing? I have defined nowhere this class.
Thanks in advance.
owl:Nothing is the class defined to be subclass of all classes in OWL, so it's included as subclass of all satisfiable classes (it is equivalent to all unsatisfiable classes).
To skip it during iterations, Node has a getEntitiesMinusBottom() method that will skip owl:Nothing.
At w3.org owl semantics, you can find more info about the owl:Nothing class: https://www.w3.org/TR/2004/REC-owl-semantics-20040210/#owl_Nothing

getting all types for instance using owl api 3.4.10 and jfact 1.2.1

I used Protege to make an ontology. It consists of two base classes. Each of them has three subsclasses. Subclasses are classes with specific object and data properties. When I make an instance I make it as class individual but with such data properties so that instance also belongs to some subclass. When I run Hermit in Protege, it is well done. It says that my instance belongs to class and subclass. But in Jfact in Java and Owlapi, when I try to print all instances from subclasses, my instance belongs only to superclass, not to subclass. Is it about JFact or something else?
for (OWLClass c : ont.getClassesInSignature()) {
if (c.getIRI().getFragment().equals("Baby")) {
NodeSet<OWLClass> subClasses = reasoner.getSubClasses(c, false);
for (OWLClass subClass : subClasses.getFlattened()) {
System.out.println(subClass.getIRI().getFragment());
NodeSet<OWLNamedIndividual> instances = reasoner.getInstances(subClass, false);
for (OWLNamedIndividual i : instances.getFlattened()) {
System.out.println(i.getIRI().getFragment());
s += i.getIRI().getFragment() + " ";
}
}
}
}
Looks like a bug in JFact. Can you add an issue on its tracker, with version and ontology?
Edit: JFact 1.2.1 is ancient and has known bugs. Update to at least 1.2.4 (and OWLAPI 3.5.5, the most recent release for the 3.x version).
If possible, I recommend updating to OWLAPI 4 (for Java 7) or 5 (for Java 8) and the same main version for JFact.

Find owlSubClasses of a class using the ROWLEX API?

I have a given ontology, on which I like to reason. I use the ROWLEX API for .NET.
How can I find all owlSubClasses of a class?
ROWLEX is not a reasoner. It has some functionality of a reasoner implemented - it is very necessary for the C# class generation - but its API is not designed for that. However, there is still hope, just not the most elegant solution.
I assume, that you have an ontology and you generated .NET classes from that using OwlGrinder.exe. Therefore by default, you have two associated .NET classes generated for each corresponding OWL class in the ontology: one light class and one full class. We are going to use the light classes only. We simply iterate through ALL .NET classes and filter out if is a subclass. That is it.
string baseClassUri = "http://myontology/2012/10#mybaseclass";
Assembly asm = GetMyAssemblyGeneratedByOwlGrinder();
Type[] subClasses = (from type in asm.GetTypes()
where type.IsSubclassOf(typeof(NC3A.SI.Rowlex.OwlThing))
// selecting subclasses only
let attributes = type.GetCustomAttributes(typeof(NC3A.SI.Rowlex.SubClassOfAttribute), false)
from attr in attributes
let subClassAttr = attr as NC3A.SI.Rowlex.SubClassOfAttribute
where subClassAttr.TypeUri == baseClassUri
// selecting light classes only
let lightAttributes = type.GetCustomAttributes(typeof(NC3A.SI.Rowlex.LightVersionAttribute), false)
from lightAttr in lightAttributes
let lightAttr_ = lightAttr as NC3A.SI.Rowlex.LightVersionAttribute
where lightAttr_.LightVersion == true
select type).ToArray();
I did not try the code, it may be buggy. But it does show the idea. Each generated class have a bunch of attributes added. These include their base classes in the SubClassOfAttribute and whether they are light classes or full classes using the LightVersionAttribute. You can filter out the classes you are interested in based on these attributes.

Resources