{"action":"created","issue":{"url":"https://api.github.com/repos/AlexandreDecan/sismic/issues/71","repository_url":"https://api.github.com/repos/AlexandreDecan/sismic","labels_url":"https://api.github.com/repos/AlexandreDecan/sismic/issues/71/labels{/name}","comments_url":"https://api.github.com/repos/AlexandreDecan/sismic/issues/71/comments","events_url":"https://api.github.com/repos/AlexandreDecan/sismic/issues/71/events","html_url":"https://github.com/AlexandreDecan/sismic/pull/71","id":335805393,"node_id":"MDExOlB1bGxSZXF1ZXN0MTk3NDExOTEx","number":71,"title":"A clock-based approach to deal with interpreter time","user":{"login":"AlexandreDecan","id":4992216,"node_id":"MDQ6VXNlcjQ5OTIyMTY=","avatar_url":"https://avatars1.githubusercontent.com/u/4992216?v=4","gravatar_id":"","url":"https://api.github.com/users/AlexandreDecan","html_url":"https://github.com/AlexandreDecan","followers_url":"https://api.github.com/users/AlexandreDecan/followers","following_url":"https://api.github.com/users/AlexandreDecan/following{/other_user}","gists_url":"https://api.github.com/users/AlexandreDecan/gists{/gist_id}","starred_url":"https://api.github.com/users/AlexandreDecan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AlexandreDecan/subscriptions","organizations_url":"https://api.github.com/users/AlexandreDecan/orgs","repos_url":"https://api.github.com/users/AlexandreDecan/repos","events_url":"https://api.github.com/users/AlexandreDecan/events{/privacy}","received_events_url":"https://api.github.com/users/AlexandreDecan/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":19,"created_at":"2018-06-26T12:53:49Z","updated_at":"2018-06-28T14:28:02Z","closed_at":null,"author_association":"OWNER","pull_request":{"url":"https://api.github.com/repos/AlexandreDecan/sismic/pulls/71","html_url":"https://github.com/AlexandreDecan/sismic/pull/71","diff_url":"https://github.com/AlexandreDecan/sismic/pull/71.diff","patch_url":"https://github.com/AlexandreDecan/sismic/pull/71.patch"},"body":"This PR deprecates `interpreter.time` and uses a clock-based approach to deal with time. \r\n\r\nA new class `Clock` is introduced. This class is used to create clocks that support both real and simulated time. By default, a clock relies on simulated time, and the time can be set by changing the value of `clock.time`. To rely on real time, one has to call `clock.start()`. Switching back to simulated time can be done with `clock.stop()`. Regardless of the notion of time that is currently used, the time value of a clock can be changed. Real time can be accelerated or slowed down by changing the `speed` attribute. \r\n\r\nThe current time of an interpreter can still be retrieved using `interpreter.time`, but this attribute is now deprecated (warns a `DeprecationWarning`). The new API is `interpreter.clock` to retrieve its clock, and `interpreter.clock.time` to get or set current time value. \r\n\r\nWhen property statecharts are bound to an interpreter, they share its clock. This shouldn't have any incidence on existing code. \r\n\r\nHowever, the `run_in_background` helper was modified: it does **NO** longer automatically set the `time` value of an interpreter. To simulate the previous behaviour, one has to call `interpreter.clock.start()` before (or right after) passing the interpreter to the helper.\r\n\r\nAll these changes will ease the execution of statecharts that relies on wall-time clock, as it is now sufficient to call  `interpreter.clock.start()` before the first call to `execute` or `execute_once`. Previously, the time had to be manually set before each call to `execute` or `execute_once`. "},"comment":{"url":"https://api.github.com/repos/AlexandreDecan/sismic/issues/comments/401054155","html_url":"https://github.com/AlexandreDecan/sismic/pull/71#issuecomment-401054155","issue_url":"https://api.github.com/repos/AlexandreDecan/sismic/issues/71","id":401054155,"node_id":"MDEyOklzc3VlQ29tbWVudDQwMTA1NDE1NQ==","user":{"login":"AlexandreDecan","id":4992216,"node_id":"MDQ6VXNlcjQ5OTIyMTY=","avatar_url":"https://avatars1.githubusercontent.com/u/4992216?v=4","gravatar_id":"","url":"https://api.github.com/users/AlexandreDecan","html_url":"https://github.com/AlexandreDecan","followers_url":"https://api.github.com/users/AlexandreDecan/followers","following_url":"https://api.github.com/users/AlexandreDecan/following{/other_user}","gists_url":"https://api.github.com/users/AlexandreDecan/gists{/gist_id}","starred_url":"https://api.github.com/users/AlexandreDecan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AlexandreDecan/subscriptions","organizations_url":"https://api.github.com/users/AlexandreDecan/orgs","repos_url":"https://api.github.com/users/AlexandreDecan/repos","events_url":"https://api.github.com/users/AlexandreDecan/events{/privacy}","received_events_url":"https://api.github.com/users/AlexandreDecan/received_events","type":"User","site_admin":false},"created_at":"2018-06-28T14:28:02Z","updated_at":"2018-06-28T14:28:02Z","author_association":"OWNER","body":"To avoid a Python `sleep`-based implementation, I suggest to rely on `threading.Timer` or even the low-level `signal` library (maybe not the best option, though). \r\n\r\n```python\r\ndef run(e, interpreter, delay):\r\n    interpreter.execute()  # Or execute_once if explicitly required!\r\n    if not e.is_set():\r\n        threading.Timer(delay, run, [e, interpreter, delay]).start()\r\n\r\ne = threading.Event()\r\ninterpreter = ...\r\ndelay = 0.05\r\nrun(e, interpreter, delay)\r\n``` \r\n\r\nA call to `e.set()` terminates the \"loop\". \r\n\r\nIf you want to explicitly deduce the processing time, I think this should do the trick:\r\n\r\n```python\r\ndef run(e, interpreter, delay):\r\n    start_time = time.time()\r\n    interpreter.execute()\r\n    elapsed = time.time() - start_time()\r\n    if not e.is_set():\r\n        threading.Timer(delay - elapsed, run, [e, interpreter, delay]).start()\r\n```\r\n\r\nYou can even `stop()` and `start()` the clock (`SimulatedClock`) before and after each call to `execute()`."}}
{"actor":{"display_login":"AlexandreDecan"}}