The SWRLTab Mathematical Expressions Built-In Library is one of the SWRLTabBuiltInLibraries. It defines built-ins that be used to perform mathematical operations beyond the basic ones provided in the Core SWRL built-in library. (9Y3)
The built-ins in this library are defined by the SWRLTab Mathematical Ontology. The standard alias is swrlm. A copy of this file can be found the standard Protege-OWL repositories, and can be imported through the 'Import Ontology' option in the Metadata tab. (A4U)
The following are the built-ins defined by this library: (9Y6)
- sqrt Returns true if the first argument is equal to the square root of the second argument. If the first argument is unbound, bind it to the square root of the second argument. E.g., swrlm:sqrt(?r, 434) (A03)
- log Returns true if the first argument is equal to the natural logarithm (base e) of the second argument. If the first argument is unbound, bind it to the natural logarithm of the second argument. (9Y8)
- eval Returns true if the first argument is equals to the mathematical expression specified in the second argument, which may use the values of the variables in the the optional subsequent arguments. If the first argument is unbound, bind it to the result of the expression. E.g., swrlm:eval(?r, "(57 * x) / y)", ?x, ?y)". (A04)
The swrlm:eval built-in uses the earlier free version (2.4.0) of the Java Math Expression Parser for its expression parser. Detailed documentation on the types of expressions allowed can be found in the documentation on the JEP project website. The expression evaluator is configured to support standard constants, such as pi and e, and standard mathematical functions, such as ln and log. It also supports implicit multiplication. (A4V)
Examples (A4N)
The following examples show how these built-in can be used in SQWRL queries, though they can also be used in SWRL rules. These queries can be executed using either the SQWRLQueryTab or the SQWRLQueryAPI. (A4O)
The following query will display the area of a rectangle: (A4Y)
Rectangle(?r) ^ hasWidth(?r, ?width) ^ hasHeight(?r, ?height) ^ swrlm:eval(?area, "width * height", ?width, ?height) -> sqwrl:select(?area) (BIF)
The following query will display the radius and circumference of a circle: (A4P)
Circle(?c) ^ hasRadius(?c, ?r) ^ swrlm:eval(?circumference, "2 * pi * r", ?r) -> sqwrl:select(?r, ?circumference) (BIG)
Implicit multiplication is also supported, so the query can be rewritten as: (A4R)
Circle(?c) ^ hasRadius(?c, ?r) ^ swrlm:eval(?circumference, "2 pi r", ?r) -> sqwrl:select(?r, ?circumference) (BIH)
The following query returns a random number between 0 and 1: (A4S)
swrlm:eval(?r, "rand()") -> sqwrl:select(?r) (BII)
The following query returns the distance between two persons that have x and y location information: (AMP)
Person(?p1) ^ Person(?p2) ^ differentFrom(?p1,?p2) ^ hasX(?p1,?x1) ^ hasY(?p1,?y1) ^ hasX(?p2,?x2) ^ hasY(?p2,?y2) ^ swrlm:eval(?d, "sqrt(pow(x1-x2, 2) + pow(y1-y2, 2))", ?x1, ?y1, ?x2, ?y2) -> sqwrl:select(?p1, ?p2, ?d) (BIJ)