SWRLRuleAndDataForTheMeasurementPattern - mdebellis/SemanticKG-Design GitHub Wiki

Exercise: Add a SWRL Rule and Data for the Measurement Pattern

The Measurement pattern is a useful way to see how SWRL built-ins can perform simple numerical conversions. SWRL includes predefined built-ins for math, dates, strings, and other common operations. In this exercise, you will inspect existing distance-conversion rules, add a new rule for miles, add the missing unit value for a 1 Mile measurement, and then run the reasoner to infer the canonical value and ordering relationships.


➤ Step 1: Open the SWRL Tab

Open the People Ontology in Protégé.

Go to:

Window > Tabs > SWRL Tab

This opens the Protégé interface for creating and editing SWRL rules.

You should see several existing rules, including:

Canonical_Distance_Centimeter_Conversion

These rules convert distance measurement values to the canonical unit for distance, which is meters.


➤ Step 2: Inspect the Centimeter Conversion Rule

Click the rule:

Canonical_Distance_Centimeter_Conversion

Then click:

Edit

The Edit button appears below the list of rules, toward the lower-right side of the SWRL Tab.

You should see the following rule:

Distance_Measurement(?m1) ^ has_unit(?m1, Centimeter) ^ value(?m1, ?v1) ^ swrlb:divide(?cv, ?v1, 100) -> canonical_value(?m1, ?cv)

This rule says:

  • Find a distance measurement ?m1.
  • Check that its unit is Centimeter.
  • Get its numeric value as ?v1.
  • Divide ?v1 by 100.
  • Bind the result to ?cv.
  • Assert ?cv as the canonical_value of the measurement.

Since the canonical unit is meters, a centimeter value must be divided by 100 to produce the corresponding value in meters.

The built-in:

swrlb:divide(?cv, ?v1, 100)

uses the second and third arguments as input values and binds the result to the first argument. In this case, it divides ?v1 by 100 and stores the result in ?cv.

Select:

Cancel

This closes the rule editor without changing the rule.


➤ Step 3: Inspect the Distance Ordering Rule

Now find the rule:

Distance_Ordering_Rule

Select it and click:

Edit

You are only viewing the rule, not changing it.

The rule should be:

Distance_Measurement(?m1) ^ Distance_Measurement(?m2) ^ canonical_value(?m1, ?v1) ^ canonical_value(?m2, ?v2) ^ swrlb:greaterThan(?v1, ?v2) -> greater_than(?m1, ?m2)

This rule compares the canonical values of distance measurements. If the canonical value of ?m1 is greater than the canonical value of ?m2, the rule asserts:

greater_than(?m1, ?m2)

This is why the Measurement pattern is useful. Once all measurements are converted to the same canonical unit, ordering comparisons become straightforward.

Select:

Cancel


➤ Step 4: Clone the Kilometer Conversion Rule

If you review the existing rules and distance units, you will see that the ontology includes the unit:

Mile

However, there is not yet a rule to convert miles to the canonical unit, meters. Let’s fix that.

You could create a new SWRL rule from scratch, but it is easier to clone an existing rule that already has the same structure.

Select the rule:

Canonical_Distance_Kilometer_Conversion

Do not click Edit. Instead, click:

Clone

Protégé creates a copy of the rule with the default name:

S1

Find the new rule S1, select it, and click:

Edit

The cloned rule should look like this:

Distance_Measurement(?m1) ^ has_unit(?m1, Kilometer) ^ value(?m1, ?v1) ^ swrlb:multiply(?cv, ?v1, 1000) -> canonical_value(?m1, ?cv)

This is a good starting point because kilometers and miles are both larger than meters, so both conversion rules use multiplication.


➤ Step 5: Change the Rule from Kilometers to Miles

Edit the cloned rule so that it uses Mile instead of Kilometer.

Also change the conversion factor from:

1000

to:

1609.34

The final rule should be:

Distance_Measurement(?m1) ^ has_unit(?m1, Mile) ^ value(?m1, ?v1) ^ swrlb:multiply(?cv, ?v1, 1609.34) -> canonical_value(?m1, ?cv)

Rename the rule from:

S1

to:

Canonical_Distance_Mile_Conversion

Then click:

Save

You have now added a SWRL rule that converts mile measurements into meters.


➤ Step 6: Open the Distance Measurement Individuals

The new rule is correct, but there is not yet a complete distance measurement that uses miles.

Go to the:

Individuals by class tab

If that tab is not already open, select:

Window > Tabs > Individuals by class

In the Class hierarchy view in the upper-left side of the tab, navigate to and select:

Distance_Measurement

In the Individuals view below the class hierarchy, you should see several existing distance measurements.

If you select each one, the Property assertions view in the lower-right corner should show that most of them already have a canonical_value and are ordered relative to the other measurements.

However, the individual labeled:

1 Mile

has a numeric value but no unit.

Select:

1 Mile

In the lower-right corner, the Property assertions view should now be labeled something like:

Property assertions: 1 Mile

At this point, the individual should only have one property assertion:

value 1.0

➤ Step 7: Add the Missing Unit

In the Property assertions view, click the + next to:

Object property assertions

This opens an editor with a field for the object property and a field for the value.

In the first field, type:

'has uni

Then press:

Ctrl+Space

Protégé should autocomplete the property to:

has unit

✅ You need to type enough of the label to distinguish has unit from has uncle. If you type only 'has u and press Ctrl+Space, Protégé should show both matching properties and let you choose the one you want.

In the second field, type:

Mil

Then press:

Ctrl+Space

Protégé should autocomplete the value to:

Mile

Note that when using autocomplete in Protégé, labels with spaces usually need to begin with a single quote. Since Mile does not contain a space, you can type it directly without the initial quote.


➤ Step 8: Synchronize the Reasoner

In the lower-right corner of Protégé, you should see a message like:

Reasoner state out of sync with active ontology

Make sure you are using the Pellet reasoner, since Pellet has strong support for SWRL rules.

Then select:

Reasoner > Synchronize reasoner

Also make sure the checkbox labeled:

Show inferences

is checked. This checkbox appears near the reasoner status message in the lower-right corner of Protégé.

✅ If you are not seeing expected inferences in Protégé, the Show inferences checkbox is one of the first things to check.

After synchronization, the 1 Mile individual should have an inferred canonical value of approximately:

1609.34

It should also now appear in the correct order relative to the other distance measurements. The greater_than, less_than, and equal_to values should be filled in automatically by the other SWRL rules.

That is the benefit of a good modeling pattern: once the pattern is defined, it is easy to extend, and the reasoner does most of the work.


Bottom Line

The Measurement pattern shows how SWRL built-ins can convert values into a canonical unit and then use those canonical values for comparison. By adding one conversion rule for Mile and one missing has unit assertion, the reasoner can infer the canonical meter value and automatically place the new measurement in the correct ordering relationships.