def start_n1_situation(self, predicate, subject_name): description = predicate+"("+subject_name+")" sit = Situation(desc=description) sit.starttime = time.time() self.current_situations_map[description] = sit self.ros_pub["situation_log"].publish("START " + description) self.target.timeline.update(sit) return sit.id
def test_situation_manipulations(self): s1 = Situation() s2 = Situation() self.assertEqual(len(self.timeline),0) self.timeline.append(s1) time.sleep(PROPAGATION_TIME) # wait for propagation self.assertEqual(len(self.timeline),1) self.assertIn(s1, self.timeline) self.assertEqual(self.timeline[0].endtime, 0) s1.endtime = 1 self.timeline.update(s1) time.sleep(PROPAGATION_TIME) # wait for propagation self.assertEqual(len(self.timeline),1) self.assertEqual(self.timeline[0].endtime, 1) self.timeline.update(s2) # alias for append time.sleep(PROPAGATION_TIME) # wait for propagation self.assertEqual(len(self.timeline),2) self.assertIn(s2, self.timeline) self.timeline.update(s2) # shouldn't do anything, as s2 is already present time.sleep(PROPAGATION_TIME) # wait for propagation self.assertEqual(len(self.timeline),2) s1.endtime = 2 s2.endtime = 3 self.timeline.update(s1) self.timeline.update(s2) time.sleep(PROPAGATION_TIME) # wait for propagation self.assertEqual(len(self.timeline),2) self.assertEqual(self.timeline[s1.id].endtime, 2) self.assertEqual(self.timeline[s2.id].endtime, 3) self.timeline.remove(s1) time.sleep(PROPAGATION_TIME) # wait for propagation self.assertEqual(len(self.timeline),1) self.assertEqual(self.timeline[0].id, s2.id) self.timeline.remove(s2) time.sleep(PROPAGATION_TIME) # wait for propagation self.assertEqual(len(self.timeline),0)
def start_predicate(self, timeline, predicate, subject_name, object_name=None, isevent=False): if object_name is None: description = predicate + "(" + subject_name + ")" else: description = predicate + "(" + subject_name + "," + object_name + ")" sit = Situation(desc=description) sit.starttime = time.time() if isevent: sit.endtime = sit.starttime self.current_situations_map[description] = sit self.log_pub[predicate].publish("START " + description) timeline.update(sit) return sit.id
def start(self): """ Starts (and return) a new situation This method sends the new situation to the remote. IT DOES NOT DIRECTLY modify the local copy of situations: the roundtrip is slower, but data consistency is easier to ensure. Note that the situation starting time is set by the client, at the time at which this method is called. """ situation = Situation() situation.starttime = time.time() self.update(situation) return situation
def _test_events_start_stop(self): t = self.timeline s = Situation() t.start(s) self.assertEquals(len(t.activesituations), 1) self.assertIn(s, t.activesituations) t.end(s) self.assertEquals(len(t.activesituations), 0) # can not call .event() with a situation with self.assertRaises(TypeError): t.event(s) e = Event() t.end(e) # should do nothing at all t.start(e) # events end immediately self.assertEquals(len(t.activesituations), 0) t.event(e) # for events, should be synonym with 't.start' self.assertEquals(len(t.activesituations), 0)
def test_events_base(self): s = Situation() self.timeline.start(s) time.sleep(PROPAGATION_TIME) # wait for propagation self.assertIn(s, self.timeline.situations) self.assertIn(s, self.timeline.activesituations) self.timeline.end(s) time.sleep(PROPAGATION_TIME) # wait for propagation self.assertIn(s, self.timeline.situations) self.assertNotIn(s, self.timeline.activesituations) s = createevent() self.timeline.start(s) time.sleep(PROPAGATION_TIME) # wait for propagation self.assertIn(s, self.timeline.situations) self.assertNotIn(s, self.timeline.activesituations) self.timeline.end(s) time.sleep(PROPAGATION_TIME) # wait for propagation self.assertIn(s, self.timeline.situations) self.assertNotIn(s, self.timeline.activesituations)
def test_situation_manipulations(self): s1 = Situation() s2 = Situation() self.assertEqual(len(self.timeline), 0) self.timeline.append(s1) time.sleep(PROPAGATION_TIME) # wait for propagation self.assertEqual(len(self.timeline), 1) self.assertIn(s1, self.timeline) self.assertEqual(self.timeline[0].endtime, 0) s1.endtime = 1 self.timeline.update(s1) time.sleep(PROPAGATION_TIME) # wait for propagation self.assertEqual(len(self.timeline), 1) self.assertEqual(self.timeline[0].endtime, 1) self.timeline.update(s2) # alias for append time.sleep(PROPAGATION_TIME) # wait for propagation self.assertEqual(len(self.timeline), 2) self.assertIn(s2, self.timeline) self.timeline.update( s2) # shouldn't do anything, as s2 is already present time.sleep(PROPAGATION_TIME) # wait for propagation self.assertEqual(len(self.timeline), 2) s1.endtime = 2 s2.endtime = 3 self.timeline.update(s1) self.timeline.update(s2) time.sleep(PROPAGATION_TIME) # wait for propagation self.assertEqual(len(self.timeline), 2) self.assertEqual(self.timeline[s1.id].endtime, 2) self.assertEqual(self.timeline[s2.id].endtime, 3) self.timeline.remove(s1) time.sleep(PROPAGATION_TIME) # wait for propagation self.assertEqual(len(self.timeline), 1) self.assertEqual(self.timeline[0].id, s2.id) self.timeline.remove(s2) time.sleep(PROPAGATION_TIME) # wait for propagation self.assertEqual(len(self.timeline), 0)
def _get_situation_from_remote(self, id): sitInCtxt = gRPC.SituationInContext(context=self._server_ctx, situation=gRPC.Situation(id=id)) try: gRPCSituation = self._ctx.rpc.getSituation(sitInCtxt, _TIMEOUT_SECONDS) except AbortionError as e: raise IndexError(e.details) # is it a new situation, or rather an update to an existing one? if id not in self._ids: self._ids.append(id) self._situations[id] = Situation.deserialize(gRPCSituation)
def start_hold_fact(self, node, gripper): sit = Situation(desc="hold(%s,%s)" % (gripper.name, node.name)).isevent() self.situations["hold(%s,%s)" % (gripper.name, node.name)] = sit self.target.timeline.update(sit)
def send_release_event(self, node, gripper): sit = Situation(desc="release(%s,%s)" % (gripper.name, node.name)).isevent() self.target.timeline.update(sit)
#! /usr/bin/env python import sys import time import underworlds from underworlds.types import Situation, createevent if len(sys.argv) != 2: print("Usage: %s <world>" % __file__) sys.exit(1) with underworlds.Context("provider") as ctx: world = ctx.worlds[sys.argv[1]] timeline = world.timeline s1 = Situation() s2 = Situation() s3 = Situation() timeline.start(s1) time.sleep(0.2) timeline.start(s2) timeline.event(s3) time.sleep(0.2) timeline.end(s2) time.sleep(0.5) timeline.end(s1)