shrdlu-block-world 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (27) hide show
  1. shrdlu_block_world-0.1.0/LICENSE.md +21 -0
  2. shrdlu_block_world-0.1.0/PKG-INFO +133 -0
  3. shrdlu_block_world-0.1.0/README.md +94 -0
  4. shrdlu_block_world-0.1.0/pyproject.toml +36 -0
  5. shrdlu_block_world-0.1.0/setup.cfg +4 -0
  6. shrdlu_block_world-0.1.0/shrdlu_block_world.egg-info/PKG-INFO +133 -0
  7. shrdlu_block_world-0.1.0/shrdlu_block_world.egg-info/SOURCES.txt +25 -0
  8. shrdlu_block_world-0.1.0/shrdlu_block_world.egg-info/dependency_links.txt +1 -0
  9. shrdlu_block_world-0.1.0/shrdlu_block_world.egg-info/entry_points.txt +2 -0
  10. shrdlu_block_world-0.1.0/shrdlu_block_world.egg-info/top_level.txt +1 -0
  11. shrdlu_block_world-0.1.0/shrdlu_blocks/__init__.py +17 -0
  12. shrdlu_block_world-0.1.0/shrdlu_blocks/client.py +140 -0
  13. shrdlu_block_world-0.1.0/shrdlu_blocks/simulator/__init__.py +17 -0
  14. shrdlu_block_world-0.1.0/shrdlu_blocks/simulator/__main__.py +7 -0
  15. shrdlu_block_world-0.1.0/shrdlu_blocks/simulator/control.py +352 -0
  16. shrdlu_block_world-0.1.0/shrdlu_blocks/simulator/demo.py +54 -0
  17. shrdlu_block_world-0.1.0/shrdlu_blocks/simulator/env.py +247 -0
  18. shrdlu_block_world-0.1.0/shrdlu_blocks/simulator/geometry.py +451 -0
  19. shrdlu_block_world-0.1.0/shrdlu_blocks/simulator/scenes.py +229 -0
  20. shrdlu_block_world-0.1.0/shrdlu_blocks/simulator/server.py +261 -0
  21. shrdlu_block_world-0.1.0/shrdlu_blocks/simulator/state_pred.py +288 -0
  22. shrdlu_block_world-0.1.0/shrdlu_blocks/simulator/typedefs.py +19 -0
  23. shrdlu_block_world-0.1.0/shrdlu_blocks/viewer/__init__.py +30 -0
  24. shrdlu_block_world-0.1.0/shrdlu_blocks/viewer/web.py +203 -0
  25. shrdlu_block_world-0.1.0/shrdlu_blocks/viewer/web_static/index.html +78 -0
  26. shrdlu_block_world-0.1.0/shrdlu_blocks/viewer/web_static/web_viewer.css +303 -0
  27. shrdlu_block_world-0.1.0/shrdlu_blocks/viewer/web_static/web_viewer.js +505 -0
@@ -0,0 +1,21 @@
1
+ # MIT License
2
+
3
+ **Copyright (c) 2021 Aaron Hosford**
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ **THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.**
@@ -0,0 +1,133 @@
1
+ Metadata-Version: 2.4
2
+ Name: shrdlu-block-world
3
+ Version: 0.1.0
4
+ Summary: A small standalone SHRDLU blocks-world simulator.
5
+ Author-email: Aaron Hosford <hosford42@gmail.com>
6
+ License: # MIT License
7
+
8
+ **Copyright (c) 2021 Aaron Hosford**
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to whom the Software is
15
+ furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in all
18
+ copies or substantial portions of the Software.
19
+
20
+ **THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ SOFTWARE.**
27
+
28
+ Keywords: SHRDLU,blocks world,simulator
29
+ Classifier: License :: OSI Approved :: MIT License
30
+ Classifier: Operating System :: OS Independent
31
+ Classifier: Programming Language :: Python :: 3 :: Only
32
+ Classifier: Programming Language :: Python :: 3.8
33
+ Classifier: Topic :: Games/Entertainment :: Simulation
34
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
35
+ Requires-Python: >=3.8
36
+ Description-Content-Type: text/markdown
37
+ License-File: LICENSE.md
38
+ Dynamic: license-file
39
+
40
+ # SHRDLU Block World
41
+
42
+ Small standalone tabletop blocks-world simulator. It can run as a Python object,
43
+ a headless HTTP service, or a browser viewer for manual control.
44
+
45
+ The distribution name is `shrdlu-block-world`; the import package is
46
+ `shrdlu_blocks`. It has no third-party runtime dependencies.
47
+
48
+ ## Install
49
+
50
+ ```bash
51
+ cd ~/shrdlu-block-world
52
+ python3 -m pip install -e .
53
+ ```
54
+
55
+ ## Run
56
+
57
+ ```bash
58
+ # Browser viewer at http://127.0.0.1:8000/
59
+ shrdlu-block-world
60
+
61
+ # API only
62
+ shrdlu-block-world --headless
63
+ ```
64
+
65
+ Useful options:
66
+
67
+ - `--host HOST`
68
+ - `--port PORT`
69
+ - `--open-browser`
70
+
71
+ You can also run the module directly:
72
+
73
+ ```bash
74
+ python3 -m shrdlu_blocks.simulator
75
+ ```
76
+
77
+ ## Python API
78
+
79
+ ```python
80
+ from shrdlu_blocks import ShrdluBlocksEnv
81
+
82
+ env = ShrdluBlocksEnv()
83
+ env.execute_action({"name": "move_grasper", "args": {"x": -0.1, "y": 0.4}})
84
+ env.execute_action({"name": "lower_grasper", "args": {}})
85
+ print(env.snapshot_text())
86
+ ```
87
+
88
+ ## HTTP API
89
+
90
+ - `GET /api/state`
91
+ - `POST /api/action`
92
+ - `POST /api/reset`
93
+
94
+ `POST /api/action` expects an action object:
95
+
96
+ ```json
97
+ {
98
+ "action": {
99
+ "name": "move_grasper",
100
+ "args": {"x": -0.1, "y": 0.4}
101
+ }
102
+ }
103
+ ```
104
+
105
+ Supported action names:
106
+
107
+ - `move_grasper` with `x` and `y`
108
+ - `lower_grasper`
109
+ - `raise_grasper`
110
+ - `close_grasper`
111
+ - `open_grasper`
112
+ - `highlight_object`
113
+ - `unhighlight_object`
114
+
115
+ ## Configuration
116
+
117
+ ```bash
118
+ export SHRDLU_SIMULATOR_HOST=0.0.0.0
119
+ export SHRDLU_SIMULATOR_PORT=8000
120
+ export SHRDLU_WEB_OPEN_BROWSER=1
121
+ ```
122
+
123
+ For remote use, forward the viewer port and open `http://localhost:8000`:
124
+
125
+ ```bash
126
+ ssh -L 8000:localhost:8000 user@remote-host
127
+ ```
128
+
129
+ ## Layout
130
+
131
+ - `shrdlu_blocks/simulator/`: environment, controller, scene model, and HTTP server
132
+ - `shrdlu_blocks/viewer/`: browser viewer and static assets
133
+ - `shrdlu_blocks/client.py`: small HTTP client for a running service
@@ -0,0 +1,94 @@
1
+ # SHRDLU Block World
2
+
3
+ Small standalone tabletop blocks-world simulator. It can run as a Python object,
4
+ a headless HTTP service, or a browser viewer for manual control.
5
+
6
+ The distribution name is `shrdlu-block-world`; the import package is
7
+ `shrdlu_blocks`. It has no third-party runtime dependencies.
8
+
9
+ ## Install
10
+
11
+ ```bash
12
+ cd ~/shrdlu-block-world
13
+ python3 -m pip install -e .
14
+ ```
15
+
16
+ ## Run
17
+
18
+ ```bash
19
+ # Browser viewer at http://127.0.0.1:8000/
20
+ shrdlu-block-world
21
+
22
+ # API only
23
+ shrdlu-block-world --headless
24
+ ```
25
+
26
+ Useful options:
27
+
28
+ - `--host HOST`
29
+ - `--port PORT`
30
+ - `--open-browser`
31
+
32
+ You can also run the module directly:
33
+
34
+ ```bash
35
+ python3 -m shrdlu_blocks.simulator
36
+ ```
37
+
38
+ ## Python API
39
+
40
+ ```python
41
+ from shrdlu_blocks import ShrdluBlocksEnv
42
+
43
+ env = ShrdluBlocksEnv()
44
+ env.execute_action({"name": "move_grasper", "args": {"x": -0.1, "y": 0.4}})
45
+ env.execute_action({"name": "lower_grasper", "args": {}})
46
+ print(env.snapshot_text())
47
+ ```
48
+
49
+ ## HTTP API
50
+
51
+ - `GET /api/state`
52
+ - `POST /api/action`
53
+ - `POST /api/reset`
54
+
55
+ `POST /api/action` expects an action object:
56
+
57
+ ```json
58
+ {
59
+ "action": {
60
+ "name": "move_grasper",
61
+ "args": {"x": -0.1, "y": 0.4}
62
+ }
63
+ }
64
+ ```
65
+
66
+ Supported action names:
67
+
68
+ - `move_grasper` with `x` and `y`
69
+ - `lower_grasper`
70
+ - `raise_grasper`
71
+ - `close_grasper`
72
+ - `open_grasper`
73
+ - `highlight_object`
74
+ - `unhighlight_object`
75
+
76
+ ## Configuration
77
+
78
+ ```bash
79
+ export SHRDLU_SIMULATOR_HOST=0.0.0.0
80
+ export SHRDLU_SIMULATOR_PORT=8000
81
+ export SHRDLU_WEB_OPEN_BROWSER=1
82
+ ```
83
+
84
+ For remote use, forward the viewer port and open `http://localhost:8000`:
85
+
86
+ ```bash
87
+ ssh -L 8000:localhost:8000 user@remote-host
88
+ ```
89
+
90
+ ## Layout
91
+
92
+ - `shrdlu_blocks/simulator/`: environment, controller, scene model, and HTTP server
93
+ - `shrdlu_blocks/viewer/`: browser viewer and static assets
94
+ - `shrdlu_blocks/client.py`: small HTTP client for a running service
@@ -0,0 +1,36 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "shrdlu-block-world"
7
+ version = "0.1.0"
8
+ description = "A small standalone SHRDLU blocks-world simulator."
9
+ readme = "README.md"
10
+ requires-python = ">=3.8"
11
+ license = {file = "LICENSE.md"}
12
+ authors = [
13
+ {name = "Aaron Hosford", email = "hosford42@gmail.com"},
14
+ ]
15
+ keywords = ["SHRDLU", "blocks world", "simulator"]
16
+ classifiers = [
17
+ "License :: OSI Approved :: MIT License",
18
+ "Operating System :: OS Independent",
19
+ "Programming Language :: Python :: 3 :: Only",
20
+ "Programming Language :: Python :: 3.8",
21
+ "Topic :: Games/Entertainment :: Simulation",
22
+ "Topic :: Scientific/Engineering :: Artificial Intelligence",
23
+ ]
24
+
25
+ [project.scripts]
26
+ shrdlu-block-world = "shrdlu_blocks.simulator.demo:demo"
27
+
28
+ [tool.setuptools.packages.find]
29
+ include = ["shrdlu_blocks*"]
30
+
31
+ [tool.setuptools.package-data]
32
+ "shrdlu_blocks.viewer" = [
33
+ "web_static/*.css",
34
+ "web_static/*.html",
35
+ "web_static/*.js",
36
+ ]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,133 @@
1
+ Metadata-Version: 2.4
2
+ Name: shrdlu-block-world
3
+ Version: 0.1.0
4
+ Summary: A small standalone SHRDLU blocks-world simulator.
5
+ Author-email: Aaron Hosford <hosford42@gmail.com>
6
+ License: # MIT License
7
+
8
+ **Copyright (c) 2021 Aaron Hosford**
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to whom the Software is
15
+ furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in all
18
+ copies or substantial portions of the Software.
19
+
20
+ **THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ SOFTWARE.**
27
+
28
+ Keywords: SHRDLU,blocks world,simulator
29
+ Classifier: License :: OSI Approved :: MIT License
30
+ Classifier: Operating System :: OS Independent
31
+ Classifier: Programming Language :: Python :: 3 :: Only
32
+ Classifier: Programming Language :: Python :: 3.8
33
+ Classifier: Topic :: Games/Entertainment :: Simulation
34
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
35
+ Requires-Python: >=3.8
36
+ Description-Content-Type: text/markdown
37
+ License-File: LICENSE.md
38
+ Dynamic: license-file
39
+
40
+ # SHRDLU Block World
41
+
42
+ Small standalone tabletop blocks-world simulator. It can run as a Python object,
43
+ a headless HTTP service, or a browser viewer for manual control.
44
+
45
+ The distribution name is `shrdlu-block-world`; the import package is
46
+ `shrdlu_blocks`. It has no third-party runtime dependencies.
47
+
48
+ ## Install
49
+
50
+ ```bash
51
+ cd ~/shrdlu-block-world
52
+ python3 -m pip install -e .
53
+ ```
54
+
55
+ ## Run
56
+
57
+ ```bash
58
+ # Browser viewer at http://127.0.0.1:8000/
59
+ shrdlu-block-world
60
+
61
+ # API only
62
+ shrdlu-block-world --headless
63
+ ```
64
+
65
+ Useful options:
66
+
67
+ - `--host HOST`
68
+ - `--port PORT`
69
+ - `--open-browser`
70
+
71
+ You can also run the module directly:
72
+
73
+ ```bash
74
+ python3 -m shrdlu_blocks.simulator
75
+ ```
76
+
77
+ ## Python API
78
+
79
+ ```python
80
+ from shrdlu_blocks import ShrdluBlocksEnv
81
+
82
+ env = ShrdluBlocksEnv()
83
+ env.execute_action({"name": "move_grasper", "args": {"x": -0.1, "y": 0.4}})
84
+ env.execute_action({"name": "lower_grasper", "args": {}})
85
+ print(env.snapshot_text())
86
+ ```
87
+
88
+ ## HTTP API
89
+
90
+ - `GET /api/state`
91
+ - `POST /api/action`
92
+ - `POST /api/reset`
93
+
94
+ `POST /api/action` expects an action object:
95
+
96
+ ```json
97
+ {
98
+ "action": {
99
+ "name": "move_grasper",
100
+ "args": {"x": -0.1, "y": 0.4}
101
+ }
102
+ }
103
+ ```
104
+
105
+ Supported action names:
106
+
107
+ - `move_grasper` with `x` and `y`
108
+ - `lower_grasper`
109
+ - `raise_grasper`
110
+ - `close_grasper`
111
+ - `open_grasper`
112
+ - `highlight_object`
113
+ - `unhighlight_object`
114
+
115
+ ## Configuration
116
+
117
+ ```bash
118
+ export SHRDLU_SIMULATOR_HOST=0.0.0.0
119
+ export SHRDLU_SIMULATOR_PORT=8000
120
+ export SHRDLU_WEB_OPEN_BROWSER=1
121
+ ```
122
+
123
+ For remote use, forward the viewer port and open `http://localhost:8000`:
124
+
125
+ ```bash
126
+ ssh -L 8000:localhost:8000 user@remote-host
127
+ ```
128
+
129
+ ## Layout
130
+
131
+ - `shrdlu_blocks/simulator/`: environment, controller, scene model, and HTTP server
132
+ - `shrdlu_blocks/viewer/`: browser viewer and static assets
133
+ - `shrdlu_blocks/client.py`: small HTTP client for a running service
@@ -0,0 +1,25 @@
1
+ LICENSE.md
2
+ README.md
3
+ pyproject.toml
4
+ shrdlu_block_world.egg-info/PKG-INFO
5
+ shrdlu_block_world.egg-info/SOURCES.txt
6
+ shrdlu_block_world.egg-info/dependency_links.txt
7
+ shrdlu_block_world.egg-info/entry_points.txt
8
+ shrdlu_block_world.egg-info/top_level.txt
9
+ shrdlu_blocks/__init__.py
10
+ shrdlu_blocks/client.py
11
+ shrdlu_blocks/simulator/__init__.py
12
+ shrdlu_blocks/simulator/__main__.py
13
+ shrdlu_blocks/simulator/control.py
14
+ shrdlu_blocks/simulator/demo.py
15
+ shrdlu_blocks/simulator/env.py
16
+ shrdlu_blocks/simulator/geometry.py
17
+ shrdlu_blocks/simulator/scenes.py
18
+ shrdlu_blocks/simulator/server.py
19
+ shrdlu_blocks/simulator/state_pred.py
20
+ shrdlu_blocks/simulator/typedefs.py
21
+ shrdlu_blocks/viewer/__init__.py
22
+ shrdlu_blocks/viewer/web.py
23
+ shrdlu_blocks/viewer/web_static/index.html
24
+ shrdlu_blocks/viewer/web_static/web_viewer.css
25
+ shrdlu_blocks/viewer/web_static/web_viewer.js
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ shrdlu-block-world = shrdlu_blocks.simulator.demo:demo
@@ -0,0 +1,17 @@
1
+ """
2
+ SHRDLU: A simple environment for evaluating and testing natural language understanding systems
3
+ """
4
+
5
+ from shrdlu_blocks.simulator import ShrdluBlocksEnv, SimulatorServer, run_simulator_server
6
+ from shrdlu_blocks.client import DEFAULT_SIMULATOR_URL, ShrdluBlocksClient
7
+
8
+ __version__ = '0.1.0'
9
+ __author__ = 'Aaron Hosford'
10
+
11
+ __all__ = [
12
+ 'DEFAULT_SIMULATOR_URL',
13
+ 'ShrdluBlocksClient',
14
+ 'ShrdluBlocksEnv',
15
+ 'SimulatorServer',
16
+ 'run_simulator_server',
17
+ ]
@@ -0,0 +1,140 @@
1
+ """HTTP client for a running SHRDLU blocks simulator."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import json
6
+ from typing import Dict, List, Optional
7
+ from urllib import error, request
8
+
9
+ from shrdlu_blocks.simulator.env import ShrdluBlocksEnv
10
+
11
+ __all__ = ['DEFAULT_SIMULATOR_URL', 'ShrdluBlocksClient']
12
+
13
+
14
+ DEFAULT_SIMULATOR_URL = 'http://127.0.0.1:8000'
15
+
16
+
17
+ class ShrdluBlocksClient:
18
+ """Small client-side API for controlling a standalone simulator service."""
19
+
20
+ ACTION_SPECS = ShrdluBlocksEnv.ACTION_SPECS
21
+
22
+ def __init__(self, base_url: str = DEFAULT_SIMULATOR_URL, timeout: float = 30.0):
23
+ self._base_url = base_url.rstrip('/')
24
+ self._timeout = float(timeout)
25
+
26
+ @property
27
+ def base_url(self) -> str:
28
+ return self._base_url
29
+
30
+ def reset(self) -> Dict[str, object]:
31
+ payload = self._post('/api/reset', {})
32
+ return payload['snapshot']
33
+
34
+ def execute_action(self, action: Dict[str, object]) -> Optional[str]:
35
+ payload = self._post('/api/action', {'action': action})
36
+ if not payload.get('ok', False):
37
+ raise RuntimeError(str(payload.get('output', 'Simulator action failed.')))
38
+ return str(payload.get('output') or 'OK')
39
+
40
+ def snapshot(self) -> Dict[str, object]:
41
+ return self._state_payload()['snapshot']
42
+
43
+ def snapshot_text(self) -> str:
44
+ payload = self._state_payload()
45
+ text = payload.get('snapshot_text')
46
+ if isinstance(text, str):
47
+ return text
48
+ return self._snapshot_to_text(payload['snapshot'])
49
+
50
+ def action_help(self) -> str:
51
+ payload = self._state_payload()
52
+ text = payload.get('action_help')
53
+ if isinstance(text, str):
54
+ return text
55
+ return self._action_help_text()
56
+
57
+ def event_log(self, limit: int = 50) -> List[Dict[str, object]]:
58
+ payload = self._state_payload()
59
+ events = payload.get('event_log') or []
60
+ return list(events[-int(limit):])
61
+
62
+ def _state_payload(self) -> Dict[str, object]:
63
+ return self._get('/api/state')
64
+
65
+ def _get(self, path: str) -> Dict[str, object]:
66
+ try:
67
+ with request.urlopen(self._base_url + path, timeout=self._timeout) as response:
68
+ return self._decode_response(response.read())
69
+ except error.URLError as exc:
70
+ raise RuntimeError('Could not reach simulator at %s' % self._base_url) from exc
71
+
72
+ def _post(self, path: str, body: Dict[str, object]) -> Dict[str, object]:
73
+ data = json.dumps(body).encode('utf-8')
74
+ req = request.Request(
75
+ self._base_url + path,
76
+ data=data,
77
+ headers={'Content-Type': 'application/json'},
78
+ method='POST',
79
+ )
80
+ try:
81
+ with request.urlopen(req, timeout=self._timeout) as response:
82
+ return self._decode_response(response.read())
83
+ except error.HTTPError as exc:
84
+ details = exc.read().decode('utf-8', errors='replace')
85
+ raise RuntimeError('Simulator HTTP error %s: %s' % (exc.code, details)) from exc
86
+ except error.URLError as exc:
87
+ raise RuntimeError('Could not reach simulator at %s' % self._base_url) from exc
88
+
89
+ @staticmethod
90
+ def _decode_response(data: bytes) -> Dict[str, object]:
91
+ payload = json.loads(data.decode('utf-8'))
92
+ if not isinstance(payload, dict):
93
+ raise RuntimeError('Simulator returned a non-object JSON payload.')
94
+ return payload
95
+
96
+ @classmethod
97
+ def _action_help_text(cls) -> str:
98
+ lines = [
99
+ 'Allowed actions:',
100
+ 'Return JSON as {"response": "...", "action": {"name": "...", "args": {...}}}',
101
+ 'Use {"response": "...", "action": {"name": "finish", "args": {}}} when done.',
102
+ ]
103
+ for spec in cls.ACTION_SPECS:
104
+ lines.append(
105
+ ' {name} args={args} - {description}'.format(
106
+ name=spec['name'],
107
+ args=spec['args'],
108
+ description=spec['description'],
109
+ )
110
+ )
111
+ return '\n'.join(lines)
112
+
113
+ @staticmethod
114
+ def _snapshot_to_text(state: Dict[str, object]) -> str:
115
+ lines = [
116
+ 'World state:',
117
+ 'default_grasper=%r' % (state.get('default_grasper'),),
118
+ 'grasper_closed=%r' % (state.get('grasper_closed'),),
119
+ 'grasper_lowered=%r' % (state.get('grasper_lowered'),),
120
+ 'grasped_object=%r' % (state.get('grasped_object'),),
121
+ 'objects:',
122
+ ]
123
+ for obj in state.get('objects', []):
124
+ position = obj.get('position', {})
125
+ lines.append(
126
+ ' id={obj_id} kind={kind} color={color} graspable={graspable} support={support} '
127
+ 'resting_on={resting_on} grasped_by={grasped_by} pos=({x:.3f}, {y:.3f}, {z:.3f})'.format(
128
+ obj_id=obj.get('obj_id'),
129
+ kind=obj.get('kind'),
130
+ color=obj.get('color'),
131
+ graspable=obj.get('graspable'),
132
+ support=obj.get('can_support'),
133
+ resting_on=obj.get('resting_on'),
134
+ grasped_by=obj.get('grasped_by'),
135
+ x=float(position.get('x', 0.0)),
136
+ y=float(position.get('y', 0.0)),
137
+ z=float(position.get('z', 0.0)),
138
+ )
139
+ )
140
+ return '\n'.join(lines)
@@ -0,0 +1,17 @@
1
+ """Headless SHRDLU blocks-world simulator package."""
2
+
3
+ from shrdlu_blocks.simulator.env import ShrdluBlocksEnv
4
+ from shrdlu_blocks.simulator.server import (
5
+ DEFAULT_SIMULATOR_HOST,
6
+ DEFAULT_SIMULATOR_PORT,
7
+ SimulatorServer,
8
+ run_simulator_server,
9
+ )
10
+
11
+ __all__ = [
12
+ 'DEFAULT_SIMULATOR_HOST',
13
+ 'DEFAULT_SIMULATOR_PORT',
14
+ 'ShrdluBlocksEnv',
15
+ 'SimulatorServer',
16
+ 'run_simulator_server',
17
+ ]
@@ -0,0 +1,7 @@
1
+ """Run the standalone manually controlled simulator."""
2
+
3
+ from shrdlu_blocks.simulator.demo import demo
4
+
5
+
6
+ if __name__ == '__main__':
7
+ demo()