Explore possible futures (python)

Commit and checkout

Move through the alternatives

Create a main file and paste the following inside it:

#!/usr/bin/env python

import rospy

from ontologenius import OntologiesManipulator

def main():
rospy.init_node('explore_futures')

onto = OntologiesManipulator()
onto.waitInit()

onto.add('bob')
onto.get('bob').close()

onto.copy('cpy', 'bob')

copied_onto = onto.get('cpy')
copied_onto.feeder.waitConnected();

copied_onto.feeder.addConcept('bob')
copied_onto.feeder.addInheritage('bob', 'human')
first_commit = copied_onto.feeder.commitAuto()

copied_onto.feeder.addObjectRelation('bob', 'eat', 'pasta')
copied_onto.feeder.addObjectRelation('pasta', 'isIn', 'bob')
copied_onto.feeder.commit('after_pasta')

copied_onto.feeder.checkout(first_commit)

copied_onto.feeder.addObjectRelation('bob', 'eat', 'burger')
copied_onto.feeder.addObjectRelation('burger', 'isIn', 'bob')
copied_onto.feeder.commit('after_burger')

copied_onto.feeder.checkout('after_pasta')
copied_onto.feeder.addDataRelation('bob', 'isHungry', 'bool', 'True')
hungry_commit = copied_onto.feeder.commitAuto()

copied_onto.feeder.checkout('after_pasta')
copied_onto.feeder.addDataRelation('bob', 'isHungry', 'bool', 'False')
copied_onto.feeder.commitAuto()

copied_onto.feeder.checkout(hungry_commit)
hungry_state = copied_onto.individuals.getOn('bob', 'isHungry')
print('-> test hungry on cpy')
for state in hungry_state:
print(state)

hungry_state = onto.get('bob').individuals.getOn('bob', 'isHungry')
print('-> test hungry on bob')
for state in hungry_state:
print(state)

onto.delete('cpy')

rospy.spin()

if __main__ == '__main__':
main()

Now, let's break the code down.

from ontologenius import OntologiesManipulator

Since we need to manipulate multiple instances, we have to import the OntologiesManipulator object.

onto.add('bob')
onto.get('bob').close()

Once we have an OntologiesManipulator object, we can use it to create an instance for our bob agent. This instance is the one that represents his knowledge base at present.

onto.copy('cpy', 'bob')

In order to create the instance that represents the possible futures, we use the copy function of the OntologiesManipulator object. This function creates a deep copy of an existing instance which becomes independent.

first_commit = copied_onto.feeder.commitAuto()

After making changes in our new instance, we can now commit these changes with the commitAuto function. It then returns the identifier of the commit that we must keep so that we can return to it later.

copied_onto.feeder.commit('after_pasta')

You can also use the commit function with a personalized commit identifier. The advantage is that you no longer need to store it or that you can use an identifier already existing in your program.

copied_onto.feeder.checkout(first_commit)

If we now want to return to the state of our first commit, nothing more simple, just do a checkout with the identifier of the first commit. After this line, it's as if Bob hadn't eaten the pasta.

In the following lines of the program, we simply go from one commit to another by making some modifications.

copied_onto.feeder.commitAuto()

copied_onto.feeder.checkout(hungry_commit)

In the lines above, we execute a commit in a state where bob is not hungry. We then checkout the commit where he is hungry.

hungry_state = copied_onto.individuals.getOn('bob', 'isHungry')
print('-> test hungry on cpy')
for state in hungry_state:
print(state)

hungry_state = onto.get('bob').individuals.getOn('bob', 'isHungry')
print('-> test hungry on bob')
for state in hungry_state:
print(state)

With these displays, we should see the fact that bob is hungry regarding the copied instance since we checked this state.

Regarding the bob instance, we should have no results for the getOn request since the copied instance is independent of the original instance.

onto.delete('cpy')

We can finally delete our copied instance because we no longer need it. By doing so, even if we had set an internal file to Ontologenius, this instance will not be saved.

Launch the program and verify if you have the expected results.