Tutorial: Getting started with the knowledge base#

🏁 Goal of this tutorial

By the end of this tutorial, you will know how to make inferences based on facts and relationships. This is important when you want ARI to store and retrieve information but also to make high-level symbolic reasoning.

Pre-requisites#

  • This tutorial requires that you have already read and understood how the knowldge core API works

  • This tutorial requires that you already know how to add/remove/query the robot’s knowledge base.

Note

ARI already comes with the knowledge core installed and the reasonable OWL2 RL reasoner.

The code#

first_reasoning_task.py#
 1import rospy
 2import time
 3from knowledge_core.api import KB
 4
 5rospy.init_node("test_knowledge_base")
 6kb = KB()
 7
 8def on_robot_entering_alfred_property(evt):
 9    print(f'A robot entered Alfred\'s {evt[0]["place"]}: {evt[0]["robot"]}')
10
11# instanciate the kb with some facts
12kb += "ari rdf:type Robot"
13kb += "alfred rdf:type Human"
14kb += ["ari isIn kitchen"]
15kb += ["Human rdfs:subClassOf Animal"]
16
17# test existence with inference
18print("Is Alfred an animal?")
19if 'alfred rdf:type Animal' in kb:
20    print("yes, he is")
21time.sleep(0.1)
22
23
24# test complex event
25kb.subscribe(["?robot isIn ?place", "?place belongsTo alfred",
26            "?robot rdf:type Robot"], on_robot_entering_alfred_property)
27# add fact to the kb
28kb += "kitchen belongsTo alfred"
29
30#event should have been triggered
31time.sleep(0.1)
32
33rospy.spin()

The code explained#

Let’s first import the KB class from the knowledge_core.api module. This class offers a more ‘Pythonic’ wrapper around the knowledge base’s ROS API.

1import rospy
2import time
3from knowledge_core.api import KB

Next, we initialise a ROS node for the process and and create an object of the class KB.

5rospy.init_node("test_knowledge_base")
6kb = KB()

We define a callback which will trigger an event.In in this case it will print on the screen the string: "A robot entered Alfred's kitchen: ARI".

8  def on_robot_entering_alfred_property(evt):
9    print(f'A robot entered Alfred\'s {evt[0]["place"]}: {evt[0]["robot"]}')

Then, we can start populating the knowledge base, with some facts.

11# instanciate the kb with some facts
12kb += "ari rdf:type Robot"
13kb += "alfred rdf:type Human"
14kb += ["alfred looksAt ari", "ari isIn kitchen"]
15kb += ["Human rdfs:subClassOf Animal"]

Let’s know test the reasoner with some basic inference. We ask whether Alfred belongs to the class Animal.

17# test existence with inference
18print("Is Alfred an Animal?")
19if 'alfred rdf:type Animal' in kb:
20    print("yes, he is")
21time.sleep(0.1)

Finally, we try to make a more complex reasoning. We ask knowledge_core to trigger an event when someone enters in Alfred property. As soon as we add the fact that kitchen belongsTo alfred an event is triggered, printing the result of the callback on_robot_entering_alfred_property.

24# test complex event
25kb.subscribe(["?robot isIn ?place", "?place belongsTo alfred",
26            "?robot rdf:type Robot"], on_robot_entering_alfred_property)
27# add fact to the kb
28kb += "kitchen belongsTo alfred"
29
30#event should have been triggered
31time.sleep(0.1)
32
33rospy.spin()