It is not a long time to contact the semantic web, so the content written in this regard is a note, and there may be many misunderstandings. Please point it out or discuss it :)
In general, if we build an ontology in an editor such as protege, we will wantProgramUse it, which requires some development interfaces. It is necessary to use a program to operate the ontology, because in many cases, it is unrealistic to manually create all the ontology through protege. Jena is a set of Apis developed by HP. It seems that HP has taken the lead in ontology. are other large companies still waiting to watch?
In this case, Jena is like protege to us. We use protege to operate the ontology, and the application uses Jena to do the same work, of course, these applications must be compiled by us. In fact, protege is also developed on the basis of Jena. if an exception is reported in the protege console, most of them will be related to Jena. Recently, a protege owl API is similar to packaging Jena. It is said that it is more convenient to use. This API is used by protege's owl plugin. I believe the author has developed the owl plugin, there is a basis for speaking these words.
The question is to use Jena to process owl. In fact, Jena certainly cannot only process owl, just as protege can process RDF (s) in addition to Owl. The most basic use of Jena is to process RDF (s), but after all, owl has become the W3C recommendation standard, so its support is also the trend of the times.
Now let's look at the actual situation. How can we use Jena to read the owl ontology we created using protege? Suppose you have an owl ontology file (. OWL), which defines the animal class (http://www.zoo.com/ont/animal, this is not an existing url, you do not need to try it out), and it has some instances, as shown belowCode:
Ontmodel m = Modelfactory. createontologymodel ();
File myfile = ...; M. Read ( New Fileinputstream (myfile ), "" );
Resiterator ITER = M. listsubjectswithproperty (RDF. type, M. getresource ( " Http://www.zoo.com/ont/Animal " ));
While (ITER. hasnext ()){
Resource animal = (Resource) ITER. Next ();
System. Out. println (animal. getlocalname ());
}
Unlike the operation of RDF (s), Com. HP. hpl. jena. ontology. ontmodel is specialized in processing ontology, Which is com. HP. hpl. jena. RDF. model. the subinterface of the model, which has all the functions of the model, and some functions not available to the model, such as listclasses (), listobjectproperties (), because the concept of "class" and "attribute" is only available in the ontology.
The code above is very simple. Create an ontmodel from modelfactory and read the model into memory from the specified file. The following code is an example to extract all the animal instances in the model (individual, also called individual) and print their names. You can also use the listindividuals () method to retrieve instances from the ontmodel, except that you have to judge whether they are animal instances in the obtained instances, I think it is better to use the preceding simple query method.
Jena has extended many iterator, such as resiterator, stmtiterator, and nodeiterator. At the beginning, it would be awkward to use them, as if they were better to use Java-standard iterator, I don't know how Jena's designers think about it. To be proficient, you must have a global understanding of the entire Jena API.
In the loop, every element we get is a resource, because everything in the ontology is a resource, whether you want to get subject, property or object, in Jena, all resources are actually obtained. In Jena, property is the sub-interface of resource, and Jena does not have the subject or object interface. (Note: In the owl ontology, subject-> property-> object makes up a triple, for example, Zhang Xiaogang-> father-> Zhang Dagang; or: sheep dolly-> RDF: type-> animal, RDF: type is a special attribute, indicating that the former is an instance of the latter)
Write this article for the moment. Next time, we will continue to introduce other ontology and use reasoning in the ontology.