As we saw in the previous chapter, the default inference capabilities are quite modest. From a practical point of view, it is much more interesting to be able to define your own rules, according to which logical conclusions should be obtained. The user-defined inference rule writing syntaxes for this purpose are SWRL, SPIN, and SHACL. SWRL rules historically arose first and are practically not used in industrial applications. Then the SPIN notation appeared, but currently the SHACL specification, which defines ways to write conditions for checking the consistency of ontologies, and its extension SHACL Rules, designed to write arbitrary rules for obtaining new facts based on already known ones, are relevant. Despite the significantly different syntax, ideologically the principle of writing rules in all notations is very similar. Here we will look at a simple example of writing rules in SWRL notation, because it is the most visual, and you can create and debug such rules in the Protege editor.
The syntax for SWRL rules is quite simple. Each rule consists of two parts - a condition and a conclusion, which is generated if the condition is met. Both the condition and the conclusion can consist of several atoms - elementary logical expressions. The condition is satisfied if all its constituent atoms are true; if the condition is met, then all the atoms that make up the output will “fire.” Attention - negation and disjunction are not supported in atoms.
Each atom represents a predicate - a statement about some objects of the ontology. The syntax for writing predicates corresponds to that accepted in logic. For example, if it is known that Peter is the son of Ivan, this can be written as a predicate: isSon (Peter, Ivan). What is indicated in parentheses are the arguments of the predicate. When composing SWRL rules, variables or concrete objects can be used as arguments.
Predicates can be more than just relational statements; Let's look at all the options for predicates that can be used in SWRL rules.
1. Class membership statements. It is written in the following form: Man (Peter). The argument is an individual object. A minimal SWRL rule might look like this:
Man (?x) -> Man (?x)
This rule states that any instance of the class Man is also an instance of the class Person. However, such a simple output can be obtained using OWL, without using SWRL. As we can see, variables in SWRL, like in SPARQL, are preceded by a question mark; The -> sign is used to separate conditions and output.
2. Statements about the existence of a connection. An example is the already given expression isSon (Peter, ?x) - note that it will have practical meaning in the rule only if at least one argument is a variable.
3. Statements about the meaning of a literal property. Using a method similar to that given in the previous paragraph, you can get the values of literal properties into variables, and then compare them with various expressions using built-in functions. Example: hasAge ( ?x , ?y ) ∧ swrlb:greaterThanOrEqual ( ?y, 16 ). This condition checks that the object ?x has an age greater than or equal to 16. As we can see from this example, the sign ∧ is used to list several conditions - this and some other notation corresponds to the generally accepted notation for writing Boolean expressions. Note also that to check if a property value is equal to any value, you can use a shorter expression: hasName (?x, “Ivan”), or isStudent (?x, true).
4. Condition for the separation of two individual objects: differentFrom ( ?x, ?y )
5. Condition for the coincidence of two objects: sameAs (?x, ?y). The condition will be true if the variables ?x and ?y are equal to the same object.
6. The condition that the value of a variable belongs to a specific data type. For example: xsd:int ( ?x )
7. The so-called built-ins, one of which we have already seen in paragraph 3: swrlb:greaterThanOrEqual. There is an extensive set of such functions, as well as the ability to define your own.
The result is formulated using atoms in the same form as the condition.
Let's look at an example of creating and executing a SWRL rule in the Protégé editor. To do this, we will use its old version, Protégé 3, which had a visual interface for building rules. The rule entry window looks like this:
Rice. 26. Creating a SWRL rule in the Protégé editor.This rule states that if ?x works for company ?y, and company ?y has a director ?z, then ?x has a boss ?z. The editor provides hints as you enter rules and highlights incorrectly entered rules.
Let's return to the ontology example from the last chapter. Having created the person Jackson in the ontology, the connection hasBoss (Alpha, Jackson), and applying our rule, we will see in the properties of the user Smith that his boss is Jackson.
The mechanism for applying rules in Protégé 3 is quite simple, since it involves exporting rules and the entire ontology content to the Drools business rules execution component, applying the rules, and then importing them back. Protégé 4 and 5 use the Pellet inference engine to execute SWRL rules, working directly with the content of ontologies, but there is no visual interface for creating rules.
As we can see, the SWRL rules allow you to create very flexible conditions for obtaining new knowledge. The disadvantage of this technology is the need for special training of the user to construct rules, the presence of deep knowledge of the ontology, as well as weak software implementations.
Next section: Overview of OWL 1 and OWL 2 features