Creating Structured Data

This section and the next are advanced material, and can be skipped on a first reading of this chapter. The example in this section uses syntax from several previous sections (arithmetic, dot notation, non-frame predicates, etc); refer back as needed.

In other languages, such as OWL, we have to name all individuals. For example, we might have an ontology of quantities – values with an associated unit of measure. We may then have individuals like

SixInches : quantity#Quantity [
  quantity#magnitude -> 6,
  quantity#unit -> eng_val#Inch ].

In Flora, we can also represent such values using functional terms:

quantity(6,eng_val#Inch)

As we will see, this has some advantages.

First, let’s introduce a bit more ontology for this example. We can give the quantity#Quantity class a schema frame,

quantity#Quantity [|
  quantity#magnitude {1..1} => \double,
  quantity#unit {1..1} => quantity#Unit
|].

and for the quantity#Unit class, we have

quantity#Unit [|
  quantity#primaryUnit {1..1} => quantity#Unit,
  quantity#conversionFactorToPrimary {1..1} => \double
|].

An example of a unit definition would be

eng_val#Inch : quantity#Unit [|
  quantity#primaryUnit -> eng_val#Meter,
  quantity#conversionFactorToPrimary -> 0.0254
|].

So, the primary unit for inches is meters, and to convert an inch quantity to a meter quantity, it should be multiplied by 0.0254. We introduce a new property quantity#primaryMagnitude to represent the magnitude of a quantity in its primary unit (i.e. after the aforementioned multiplication). We can define this property using a rule:

?q [ quantity#primaryMagnitude -> ?pm ] :-
  ?q [ quantity#unit -> ? [ quantity#conversionFactorToPrimary -> ?cf ],
       quantity#magnitude -> ?m ],
  ?pm \is ?cf * ?m.

The ? is an anonymous variable without a name. In this example it stands for the unit.

So far, we have not used the functional representation. But let’s suppose that we want to perform calculations involving quantities, e.g., add them together. In OWL/SWRL, we cannot do this, because it would require us to create a new named individual for the result – something we cannot do.

However, in Flora we can achieve this due to the functional notation. For example, a rule to add quantities can be written as:

addQuantities(quantity(?m,?u), ?q1, ?q2) :-
  ?m \is ?q1.quantity#primaryMagnitude + ?q2.quantity#primaryMagnitude,
  ?u = ?q1.quantity#unit.quantity#primaryUnit,
  ?u = ?q2.quantity#unit.quantity#primaryUnit.

The first argument to the predicate addQuantities is the result of the addition: A functional term.

We can also write an axiom that declares the functional terms to be instances of the right class, with the same property values as the non-functional version:

quantity(?Magnitude, ?Unit) : quantity#Quantity [
  quantity#magnitude -> ?Magnitude,
  quantity#unit -> ?Unit ].

Technically, this is actually a fact with variables, but it works more like a rule. Recall that all variables in a statement are universally quantified. This axiom allows us to treat the functional terms the same way as the non-functional ones. For example, these queries return the same result:

?- SixInches [ quantity#magnitude -> ?x ].
?- quantity(6,eng_val#Inch) [ quantity#magnitude -> ?x ].

We can also write queries like:

?- addQuantities(?q, quantity(6,eng_val#Inch), quantity(2,eng_val#Centimeter)).

The query results are complex objects of the type we want:

?q = quantity(0.1724, eng_val#Meter)

Note that the functional terms should not be used in rule heads, except in the “result variable” position such as the first argument of addQuantities above. The reason is that doing so would preclude the rule from working with the corresponding non-functional (named) versions of the individuals.

A final note: The “created” quantities are not persisted, i.e., added to any flora file, or even to the KB. To add data to the KB from a rule, see Knowledge Base Updates. Adding content to a file cannot be easily done from a rule. Instead, this requires either manual editing, or using the Sunflower Foundation API.