Protege-owl API URLs are linked as follows (Http://protegewiki.stanford.edu/wiki/ProtegeOWL_API_Basics) one. Using the Owl model
Under the model package, there are some Java interfaces that make up the API. These interfaces allow access to the owl model and elements in the model: classes, attributes, instances, and so on. Program developers cannot directly access the specific implementations of these interfaces, only those interfaces can be manipulated. There is no need to worry about how protégé stores the internal details of the ontology, and all the methods in the interface are abstract, and you don't have to consider its implementation. The
most important model interface is Owlmodel, which provides a top-level container that can access resources in the ontology. You can use it to create, query, delete various types of resources, or you can use the objects returned by Owlmodel to do some sort of action. For example, the following code fragment creates the ontology model and class (represented in the owl language as: owl:class) and obtains the URI (Uniform Resource Identifier):
Owlmodel Owlmodel = Protegeowl.createjenaowlmodel ();
Owlnamedclass worldclass = Owlmodel.createowlnamedclass ("World");
System.out.println ("Class URI:" + Worldclass.geturi ());
Protegeowl provides two simple static methods that can be used to create Owlmodel, as well as direct import of existing owl files. For example, you can load an existing ontology according to the following methods:
String uri = "Http://www.co-ode.org/ontologies/pizza/2007/02/12/pizza.owl";
Owlmodel Owlmodel = Protegeowl.createjenaowlmodelfromuri (URI);
two. First name, namespace prefix and Uri
Owl and RDF are identified globally by their URIs (Uniform Resource identifiers), such as: http://www.owl-ontologies.com/travel.owl# Destination. Because these URIs are long and inconvenient to handle, the ontology resource is accessed and identified by name in the Protege-owl API. For example, we could use Owl:class instead of Http://www.w3.org/2002/07/owl#Class because "owl" is a namespace
The prefix of the http://www.w3.org/2002/07/owl#. Similarly, if you import the travel ontology, you can define a prefix "travel" to access all the imported classes and attributes, such as travel:destination. If our ontology is the default namespace, then its prefix is empty, and the resource name is only destination. Program developers can control namespace prefixes by using the NamespaceManager interface. To access the current Owlmodel NamespaceManager, you can use the Owlmodel.getnamespacemanager () method, assuming that we have loaded a travel ontology as a default namespace, We access the resources contained within the Owlmodel through the following ways:
Owlnamedclass Destinationclass =
owlmodel.getowlnamedclass ("destination");
Owlobjectproperty Hascontactproperty = owlmodel.getowlobjectproperty ("Hascontact");
Owldatatypeproperty Haszipcodeproperty = Owlmodel.getowldatatypeproperty ("Haszipcode");
Owlindividual Sydney = owlmodel.getowlindividual ("Sydney");
You can also query and manipulate the appropriate resources. For example, to extract a named object, you can use the
Rdfresource.geturi () method. three. Understanding Model Interface
The interfaces in the model package are arranged according to the inheritance hierarchy. Of all resources, the most basic interface is Rdfresource, and the other is derived from the subclass (Rdfsclass,rdfproperty,rdfindividual) of the class, attribute, and instance.
Rdfresource defines the basic operations of all resources, especially the get and set property values.
Rdfproperty is also the basic interface for Rdf:properties, as well as subtype Owl:datatypeproperty and Owl:objectproperty.
Hierarchies are complex for classes because there are many anonymous classes in owl, and the problem is dealt with later.
The following example creates a simple class, along with its instance and a pair of properties:
Owlmodel Owlmodel = Protegeowl.createjenaowlmodel ();
Owlnamedclass Personclass = owlmodel.createowlnamedclass ("person");
Owldatatypeproperty Ageproperty = Owlmodel.createowldatatypeproperty ("Age");
Ageproperty.setrange (Owlmodel.getxsdint ());
Ageproperty.setdomain (personclass);
Owlobjectproperty Childrenproperty = Owlmodel.createowlobjectproperty ("Children");
Childrenproperty.setrange (personclass);
Childrenproperty.setdomain (personclass);
Rdfindividual Darwin = Personclass.createrdfindividual ("Darwin");
Darwin.setpropertyvalue (Ageproperty, New Integer (0));
Rdfindividual Holgi = personclass.createrdfindividual ("Holger");
Holgi.setpropertyvalue (Childrenproperty, Darwin);
Holgi.setpropertyvalue (Ageproperty, New Integer (33));
Four. Creating named Classes and Instances
The Protégé-owl API has a clear distinction between named classes and anonymous classes. A named class is used to create an instance, and an anonymous class is used to specify the logical characteristics of the named class (the restriction condition). Anonymous classes are discussed later, so let's look at naming classes. In order to show Owl's normalization, there are two types of named classes: Rdfsnamedclass (Rdfs:class) and Owlnamedclass (Owl:class). Unless you are very explicit on RDF, you do it or create an owl class. When you create a good class, you can arrange them according to the relationships of the subclasses:
Owlnamedclass Personclass = owlmodel.createowlnamedclass ("person");
Create subclass (Complicated version)
Owlnamedclass brotherclass = Owlmodel.createowlnamedclass ("Brother");
Brotherclass.addsuperclass (personclass);
Brotherclass.removesuperclass (Owlmodel.getowlthingclass ());
In this example program, you first create a personclass and Brotherclass class, all of which are top-level classes. Classes created by the Owlmodel.createowlnamedclass (String name) method default to Owl:thing subclasses. Then the Personclass is set to the Brotherclass parent class, so Brotherclass has two parent classes, so there is a class behind it. A more convenient way to create a person subclass is as follows:
Owlnamedclass Sisterclass = Owlmodel.createowlnamedsubclass ("Sister", Personclass);
The final level of inheritance is as follows:
Person
Brother
sister
A simple recursive call might later be used to print an inheritance hierarchy with indentation.
Printclasstree (Personclass, "");
}
private static void Printclasstree (Rdfsclass cls, String indentation) {
System.out.println (indentation + Cls.getname ());
for (Iterator it = cls.getsubclasses (false). iterator (); It.hasnext ();) {
Rdfsclass subclass = (Rdfsclass) it.next ();
Printclasstree (Subclass, Indentation + " ");
}
In a recursive program, rdfsclass.getsubclasses () gets a Boolean parameter, if this argument is true, not only does it return a direct subclass of the class, it returns the subclass of the subclass, and so on. A named class can be used to produce an instance of which an instance of this class can be queried by the Rdfsclass.getinstances () method:
owlindividual individual = brotherclass.createowlindividual ("Hans");
Collection Brothers = Brotherclass.getinstances (false);
Assert (Brothers.contains (Hans));
ASSERT (brothers.size () = = 1);
There is a big difference between direct and indirect examples. The individual Hans is a direct example of brother, but an indirect example of person. Because brother is a subclass of person, each brother is a man. The programmer can decide whether to get a direct instance or an indirect instance by controlling the parameters, where true indicates that the returned instance includes an instance of the class subclass:
Assert (Personclass.getinstancecount (false) = 0);
ASSERT (Personclass.getinstancecount (true) = = 0);
ASSERT (Personclass.getinstances (True). Contains (Hans));
You can use the Rdfresource.getrdftype () method to get the class that it belongs to based on an instance. For example, Hans has a direct parent class brother and an indirect parent person:
ASSERT (Hans.getrdftype (). Equals (Brotherclass));
ASSERT (Hans.hasrdftype (Brotherclass));
Assert! (Hans.hasrdftype (Personclass, false));
ASSERT (Hans.hasrdftype (Personclass, true));
If the lifetime of a resource ends, it can be deleted through the Rdfresource.delete () method. The API uses the same conventions as other APIs, and when "delete" means complete destruction, and "remove" denotes the deletion of a reference to an object. This means that when you "delete" a resource, all its property values are simply "removed" and not "delete".
Hans.delete ();
Five. Using data type properties and data type values
To create a owl:datatypeproperty, you can use Owlmodel.createowldatatypeproperty (String name). By default, parameter values for data type properties can take any type, string, Integer, and so on. Owl defines several XML schemas to limit the scope of a property. The most commonly used XML Schemas are xsd:string, Xsd:int, Xsd:float, and Xsd:boolean. If you want to limit the attribute range to string, you can do this:
Owldatatypeproperty property = Owlmodel.createowldatatypeproperty ("name");
Name.setrange (Owlmodel.getxsdstring ());
Returns a reference to the getxsdstring () xsd:string when the getxsdstring () is invoked. Other default data types can be passed through similar owlmodel.getxsd ... method to access the. More complex data types can be visited through their URIs:
Rdfsdatatype Datetype = Owlmodel.getrdfsdatatypebyname ("Xsd:date");
For the default data type, the property value can be handled conveniently by the corresponding Java data type. For example, if you assign a property value to a string attribute, you can make a setpropertyvalue call to a string object. The corresponding mappings can also be used for other data types:
Individual.setpropertyvalue (Stringproperty, "MyString");
Individual.setpropertyvalue (Intproperty, new Integer);
Individual.setpropertyvalue (Floatproperty, New Float (4.2));
Individual.setpropertyvalue (Booleanproperty, boolean.true);
Conversely, there are simple ways to get data properties:
String stringvalue = (string) individual.getpropertyvalue (stringproperty);
Integer intvalue = (integer) individual.getpropertyvalue (intproperty);
float float = (float) individual.getpropertyvalue (floatproperty);
Boolean Boolean = (Boolean) individual.getpropertyvalue (Booleanproperty);
Values for all other data types are wrapped into objects of a rdfsliteral class. A value in a literal connects a data type. Values can be stored as string types and require user code solution encapsulation. The following example assigns a value to the XML Schema data type Xsd:data:
Rdfsdatatype xsddate = Owlmodel.getrdfsdatatypebyname ("Xsd:date");
Owldatatypeproperty Dateproperty = Owlmodel.createowldatatypeproperty ("Dateproperty", xsdDate);
Rdfsliteral dateliteral = owlmodel.createrdfsliteral ("1971-07-06", xsddate);
Individual.setpropertyvalue (Dateproperty, dateliteral);
Rdfsliteral mydate = (rdfsliteral) individual.getpropertyvalue (dateproperty);
System.out.println ("Date:" + mydate);
The code above will print out "date-1971-07-06".
The Rdfsliteral class is also used to bind a string type to a language label:
Rdfsliteral langliteral = owlmodel.createrdfsliteral ("Wert", "de");
Individual.setpropertyvalue (Stringproperty, langliteral);
Rdfsliteral result = (rdfsliteral) individual.getpropertyvalue (stringproperty);
ASSERT (Result.getlanguage (). Equals ("de"));
ASSERT (Result.getstring (). Equals ("Wert"));
In summary, the data type value being processed can be either a basic type Object (String,integer,float,boolean) or a rdfsliterals. If we have a default data type of literal, then it will be automatically simplified. In some cases, it is more convenient to use rdfsliterals, especially if the user's code is to execute arbitrary data types. In this case, the Protégé-owl API provides some convenient ways to return to RDFSLiterals:OWLModel.asRDFSLiteral () six. Using object types to establish relationships between resources
Owl:objectproperty is used to establish relationships between individuals. The following fragment creates an object attribute, "Children", which takes "persons" as a domain:
Owlnamedclass Personclass = owlmodel.createowlnamedclass ("person");
Owlobjectproperty Childrenproperty = Owlmodel.createowlobjectproperty ("Children");
Childrenproperty.setrange (Personclass);
Next, the API can also assign attribute values to individuals:
Rdfindividual Darwin = Personclass.createrdfindividual ("Darwin");
Rdfindividual Holgi = personclass.createrdfindividual ("Holger");
Holgi.setpropertyvalue (Childrenproperty, Darwin);
The API also provides methods to add and delete values:
Holgi.addpropertyvalue (Childrenproperty, other);
Holgi.removepropertyvalue (Childrenproperty, other);
Suppose you have a animal class now, and you want to specify the domain of the children attribute is animal and person. In owl, you need to create a owl:unionof class to declare a range, because declaring these two classes as rdfs:ranges will mean that an object must be both person and animal, which is a valid attribute. So, to make the right statement, just like the following:
<owl:class rdf:id= "Person"/> <owl:class rdf:id= "Animal"/> <owl:objectproperty rdf:id=
" Children ">
<rdfs:range>
<owl:Class>
<owl:unionof rdf:parsetype=" Collection ">
<owl:class rdf:about= "#Person"/>
<owl:class rdf:about= "#Animal"/>
</owl:unionOf>
</owl:Class>
</rdfs:range>
</owl:ObjectProperty>
Although the manual creation of unions properties is complex, the Protege-owl API provides a simple and dynamic way to create a domain:
Owlnamedclass Personclass = owlmodel.createowlnamedclass ("person");
Owlnamedclass Animalclass = Owlmodel.createowlnamedclass ("Animal");
Owlobjectproperty Childrenproperty = Owlmodel.createowlobjectproperty ("Children");
Childrenproperty.addunionrangeclass (personclass);
Childrenproperty.addunionrangeclass (Animalclass);
Object attributes can also be declared to have other characteristics, such as transitivity. In the following code fragment, the ancestor of the attribute is declared to be transitive, because if a is the ancestor of B, and B is the ancestor of C, then A is also the ancestor of C:
Owlobjectproperty Ancestorproperty = Owlmodel.createowlobjectproperty ("ancestor");
Ancestorproperty.setrange (personclass);
Ancestorproperty.settransitive (TRUE);
The same method applies to symmetric or feature properties. Seven. Referencing external/non-type resources
In many cases, resources in owl can refer to other non-owl resources. For example, you want to define a resource from an owl resource to a picture. OWL/RDF can contain connections to any URI, such as the following Hasimage properties:
<RDF:RDF
xmlns:rdf= "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:xsd= "http://www.w3.org/2001/ xmlschema# "
xmlns:rdfs=" http://www.w3.org/2000/01/rdf-schema# "
xmlns:owl=" http://www.w3.org/2002/07/ owl# "
xmlns=" http://www.owl-ontologies.com/unnamed.owl# "
xml:base=" http://www.owl-ontologies.com/ Unnamed.owl ">
<owl:ontology rdf:about=" "/>
<owl:class rdf:id=" person "/>
<RDF: Property rdf:id= "Hasimage"/>
<person rdf:id= "Darwin" >
The Protege-owl API uses the Rdfuntypedresource class to implement this connection. A resource of no type is an instance that has no rdf:type description. Because namespace prefixes are not applied to untyped resources, instances of the Rdfuntypedresource class are named with their full URI. The following program creates an ontology example to show the above:
Jenaowlmodel Owlmodel = Protegeowl.createjenaowlmodel ();
Owlnamedclass Personclass = owlmodel.createowlnamedclass ("person");
owlindividual individual = personclass.createowlindividual ("Darwin");
Rdfproperty Hasimageproperty = Owlmodel.createrdfproperty ("Hasimage");
String uri = "http://www.knublauch.com/darwin/Darwin-Feeding-Smiling.jpg";
Rdfuntypedresource image = Owlmodel.createrdfuntypedresource (URI);
Individual.addpropertyvalue (hasimageproperty, image);
JENA.DUMPRDF (Owlmodel.getontmodel ());
eight. Property fields
The property field specifies the type of resource through which the property value can be obtained. In some small instances, the attribute is not a domain, that is, in an owl file, it is not described by any rdfs:domain. Logically, this domain is composed only of owl:thing classes, because each class is derived from the Owl:thing class. In protégé, the domain default for a new property is null, meaning it is not typed,
Several methods are provided in the Rdfproperty class of the API to set up and query domain. The following code fragment creates a new property and sets the person class to its properties:
Owlnamedclass Personclass = owlmodel.createowlnamedclass ("person");
Owlobjectproperty Childrenproperty = Owlmodel.createowlobjectproperty ("Children");
Childrenproperty.setdomain (Personclass);
In principle, a property can have more than one definition field. Similar to the range description, having more than one domain means that attributes can be applied to the intersection of the domain class. This does not reflect the intent of the Model builder, and the more common example is declaring domain as a union of many classes, implemented by anonymous owl:unionof. In the Protégé-owl API, these union classes can be created automatically, referring to the following call:
Owlnamedclass Animalclass = Owlmodel.createowlnamedclass ("Animal");
Childrenproperty.adduniondomainclass (Animalclass);
The resulting results are like the following code:
<owl:class rdf:id= "Person"/> <owl:class rdf:id= "Animal"/> <owl:objectproperty rdf:id=
" Children ">
<rdfs:domain>
<owl:Class>
<owl:unionof rdf:parsetype=" Collection ">
<owl:class rdf:about= "#Person"/>
<owl:class rdf:about= "#Animal"/>
</owl:unionOf>
</owl:Class>
</rdfs:domain>
</owl:ObjectProperty>
Processing domain is a bit more complicated if you want to implement a child attribute hierarchy. If the child property does not have its own domain, it inherits the domain of the parent property. For example, if you have a son attribute that is a child of children and you do not specify son domain, then its domain will include person and animal:
Owlobjectproperty Sonsproperty = Owlmodel.createowlobjectproperty ("sons");
Sonsproperty.addsuperproperty (childrenproperty);
Assert (Sonsproperty.getdomain (false) = null);
ASSERT (Sonsproperty.getdomain (true) instanceof Owlunionclass);
In a program, the Rdfproperty.getdomain method can set a Boolean variable to differentiate between the immediate domain of a property and the domain it may inherit. In the example above, domain is composed of owlunionclass, and then a method rdfproperty.getuniondomain can get a simple collection of classes from the Union domains:
Collection Uniondomain = Sonsproperty.getuniondomain (true);
ASSERT (Uniondomain.contains (Personclass));
ASSERT (Uniondomain.contains (animalclass));
All above are my own according to the official Website API document translation, there are many deficiencies, only for reference. Look forward to the progress of the people who are studying the knowledge of ontology .... Thank you..