Skip to content

MathematicalBuiltInLibrary

Martin O'Connor edited this page Aug 12, 2016 · 9 revisions

The SWRLAPI Mathematical Expressions Built-In Library is one of the SWRLAPI BuiltIn Libraries. It defines built-ins that be used to perform mathematical operations beyond the basic ones provided in the Core SWRL built-in library.

The built-ins in this library are defined by the SWRLTab Mathematical Ontology. The standard prefix is swrlm.

The following are the built-ins defined by this library:

  • 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)".
  • 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)
  • 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.

The swrlm:eval built-in uses the earlier free version (2.4.2) of the Java Math Expression Parser (JEP) for its expression parser. Detailed documentation on the types of expressions supported can be found here. 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. A list of the mathematical functions that JEP supports can be found here.

Examples

The following examples show how these built-in can be used in SQWRL queries. These queries can be executed using either the SQWRLQueryAPI or the SQWRLQueryTab.

The following query will display the area of a rectangle:

    Rectangle(?r) ^ hasWidth(?r, ?width) ^ hasHeight(?r, ?height) ^
    swrlm:eval(?area, "width * height", ?width, ?height)
    -> sqwrl:select(?area) 

The following query will display the radius and circumference of a circle:

    Circle(?c)  ^ hasRadius(?c, ?r) ^
    swrlm:eval(?circumference, "2 * pi * r", ?r)
    -> sqwrl:select(?r, ?circumference) 

Implicit multiplication is also supported, so the query can be rewritten as:

    Circle(?c) ^ hasRadius(?c, ?r) ^
    swrlm:eval(?circumference, "2 pi r", ?r)
    -> sqwrl:select(?r, ?circumference) 

The following query returns a random number between 0 and 1:

    swrlm:eval(?r, "rand()") -> sqwrl:select(?r) 

The following query returns the distance between two persons that have x and y location information:

    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)