From 2af2d1c328dd2ba7484e25e4c3f2aecab71c4c15 Mon Sep 17 00:00:00 2001 From: Eran Date: Thu, 4 Jan 2024 13:27:14 -0500 Subject: [PATCH] wip --- builder/builder_api.py | 27 +++++--- notebooks/demo.ipynb | 111 ++++++++++++++++++++----------- old/__init__.py | 0 old/builder1.py | 145 ----------------------------------------- 4 files changed, 93 insertions(+), 190 deletions(-) delete mode 100644 old/__init__.py delete mode 100644 old/builder1.py diff --git a/builder/builder_api.py b/builder/builder_api.py index 4f54018..12ce5b4 100644 --- a/builder/builder_api.py +++ b/builder/builder_api.py @@ -61,8 +61,16 @@ def fill_process_ports(tree, schema): if value.get('_type') in EDGE_KEYS: if '_ports' not in new_tree[key]: new_tree[key]['_ports'] = {} - new_tree[key]['_ports'].update(schema[key].get('_inputs', {})) - new_tree[key]['_ports'].update(schema[key].get('_outputs', {})) + input_ports = schema[key].get('_inputs', {}) + output_ports = schema[key].get('_outputs', {}) + + for port, v in input_ports.items(): + if port not in new_tree[key]['inputs']: + new_tree[key]['_ports'][port] = v + for port, v in output_ports.items(): + if port not in new_tree[key]['outputs']: + new_tree[key]['_ports'][port] = v + elif isinstance(value, dict) and key in schema: new_tree[key] = fill_process_ports(value, schema[key]) return new_tree @@ -165,8 +173,10 @@ def add_process( self.compiled_composite = None def connect(self, port=None, target=None): - assert self.schema.get('_type') in EDGE_KEYS, f"Invalid type for connect: {self.schema}, needs to be in {EDGE_KEYS}" + if not self.compiled_composite: + self.compile() + assert self.schema.get('_type') in EDGE_KEYS, f"Invalid type for connect: {self.schema}, needs to be in {EDGE_KEYS}" if port in self.schema['_inputs']: self.tree['inputs'][port] = target if port in self.schema['_outputs']: @@ -302,12 +312,13 @@ def update(self, state, interval): # b.tree ports = b['toy'].ports() print(ports) + # b.plot(filename='toy[1]') - b.compile() - ports = b['toy'].ports() - print(ports) - - b.plot() + b['toy'].connect(port='A', target='A_store') + b['A_store'] = 2.3 + b['toy'].connect(port='B', target='B_store') + b.plot(filename='toy[2]') + # b.write(filename='toy[2]', outdir='out') if __name__ == '__main__': diff --git a/notebooks/demo.ipynb b/notebooks/demo.ipynb index fb0007a..e16b149 100644 --- a/notebooks/demo.ipynb +++ b/notebooks/demo.ipynb @@ -6,8 +6,8 @@ "id": "b385dca6-942d-472c-9963-13b8cb33843c", "metadata": { "ExecuteTime": { - "end_time": "2024-01-04T17:23:41.275302Z", - "start_time": "2024-01-04T17:23:40.772396Z" + "end_time": "2024-01-04T18:21:10.713077Z", + "start_time": "2024-01-04T18:21:10.066013Z" } }, "outputs": [], @@ -22,8 +22,8 @@ "id": "30297af1-2d65-43d9-9ab7-0589d94a8cfe", "metadata": { "ExecuteTime": { - "end_time": "2024-01-04T17:23:41.279284Z", - "start_time": "2024-01-04T17:23:41.277066Z" + "end_time": "2024-01-04T18:21:10.716181Z", + "start_time": "2024-01-04T18:21:10.713591Z" } }, "outputs": [], @@ -37,7 +37,7 @@ "outputs": [ { "data": { - "text/plain": "['copasi',\n 'console-emitter',\n 'tellurium_process',\n 'cobra',\n 'ram-emitter',\n 'tellurium_step',\n 'database-emitter',\n 'smoldyn_process']" + "text/plain": "['smoldyn_process',\n 'console-emitter',\n 'tellurium_step',\n 'cobra',\n 'ram-emitter',\n 'copasi',\n 'tellurium_process',\n 'database-emitter']" }, "execution_count": 3, "metadata": {}, @@ -50,8 +50,8 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2024-01-04T17:23:41.285807Z", - "start_time": "2024-01-04T17:23:41.278653Z" + "end_time": "2024-01-04T18:21:10.720884Z", + "start_time": "2024-01-04T18:21:10.717768Z" } }, "id": "979e0bd85a31d4b0" @@ -66,8 +66,8 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2024-01-04T17:23:41.287886Z", - "start_time": "2024-01-04T17:23:41.285613Z" + "end_time": "2024-01-04T18:21:10.722818Z", + "start_time": "2024-01-04T18:21:10.720631Z" } }, "id": "a84d340622208914" @@ -106,8 +106,8 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2024-01-04T17:23:41.292263Z", - "start_time": "2024-01-04T17:23:41.290397Z" + "end_time": "2024-01-04T18:21:10.726752Z", + "start_time": "2024-01-04T18:21:10.725035Z" } }, "id": "1ee84904801a9ab" @@ -118,7 +118,7 @@ "outputs": [ { "data": { - "text/plain": "['copasi',\n 'console-emitter',\n 'tellurium_process',\n 'cobra',\n 'toy',\n 'ram-emitter',\n 'tellurium_step',\n 'database-emitter',\n 'smoldyn_process']" + "text/plain": "['smoldyn_process',\n 'console-emitter',\n 'tellurium_step',\n 'cobra',\n 'ram-emitter',\n 'copasi',\n 'tellurium_process',\n 'database-emitter',\n 'toy']" }, "execution_count": 6, "metadata": {}, @@ -131,8 +131,8 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2024-01-04T17:23:41.296560Z", - "start_time": "2024-01-04T17:23:41.293008Z" + "end_time": "2024-01-04T18:21:10.730640Z", + "start_time": "2024-01-04T18:21:10.727548Z" } }, "id": "dae8ca8db70c4845" @@ -147,8 +147,8 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2024-01-04T17:23:41.298756Z", - "start_time": "2024-01-04T17:23:41.296476Z" + "end_time": "2024-01-04T18:21:10.733312Z", + "start_time": "2024-01-04T18:21:10.729971Z" } }, "id": "8b1f49a78de17642" @@ -172,8 +172,8 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2024-01-04T17:23:41.319178Z", - "start_time": "2024-01-04T17:23:41.299134Z" + "end_time": "2024-01-04T18:21:10.753676Z", + "start_time": "2024-01-04T18:21:10.733042Z" } }, "id": "329e64816e8ff626" @@ -181,58 +181,95 @@ { "cell_type": "code", "execution_count": 9, - "outputs": [], + "outputs": [ + { + "data": { + "image/svg+xml": "\n\n\n\n\n\nbigraph\n\n\n\n('toy',)\n\ntoy\n\n\n\n\n('toy', 'A')->('toy',)\n\n\nA\n\n\n\n\n('toy', 'B')->('toy',)\n\n\nB\n\n\n\n\n('toy', 'C')->('toy',)\n\n\nC\n\n\n\n", + "text/plain": "" + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "b.compile()" + "b.plot()" ], "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2024-01-04T17:23:41.319385Z", - "start_time": "2024-01-04T17:23:41.303020Z" + "end_time": "2024-01-04T18:21:10.798231Z", + "start_time": "2024-01-04T18:21:10.737443Z" } }, - "id": "b217970ad04b9aeb" + "id": "3b1dd8569f3bb67a" }, { "cell_type": "code", "execution_count": 10, "outputs": [ { - "data": { - "image/svg+xml": "\n\n\n\n\n\nbigraph\n\n\n\n('toy',)\n\ntoy\n\n\n\n\n('toy', 'A')->('toy',)\n\n\nA\n\n\n\n\n('toy', 'B')->('toy',)\n\n\nB\n\n\n\n\n('toy', 'C')->('toy',)\n\n\nC\n\n\n\n", - "text/plain": "" - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" + "ename": "Exception", + "evalue": "cannot wire A as we are already at the top level {'A': 'float', 'B': 'float'}", + "output_type": "error", + "traceback": [ + "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m", + "\u001B[0;31mException\u001B[0m Traceback (most recent call last)", + "Cell \u001B[0;32mIn[10], line 2\u001B[0m\n\u001B[1;32m 1\u001B[0m b[\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mtoy\u001B[39m\u001B[38;5;124m'\u001B[39m]\u001B[38;5;241m.\u001B[39mconnect(port\u001B[38;5;241m=\u001B[39m\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mA\u001B[39m\u001B[38;5;124m'\u001B[39m, target\u001B[38;5;241m=\u001B[39m\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mA_store\u001B[39m\u001B[38;5;124m'\u001B[39m)\n\u001B[0;32m----> 2\u001B[0m \u001B[43mb\u001B[49m\u001B[43m[\u001B[49m\u001B[38;5;124;43m'\u001B[39;49m\u001B[38;5;124;43mtoy\u001B[39;49m\u001B[38;5;124;43m'\u001B[39;49m\u001B[43m]\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mconnect\u001B[49m\u001B[43m(\u001B[49m\u001B[43mport\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;124;43m'\u001B[39;49m\u001B[38;5;124;43mB\u001B[39;49m\u001B[38;5;124;43m'\u001B[39;49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mtarget\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;124;43m'\u001B[39;49m\u001B[38;5;124;43mB_store\u001B[39;49m\u001B[38;5;124;43m'\u001B[39;49m\u001B[43m)\u001B[49m\n\u001B[1;32m 3\u001B[0m \u001B[38;5;66;03m# b['toy'].connect(port='C', target='C_store')\u001B[39;00m\n", + "File \u001B[0;32m~/code/bigraph-builder/builder/builder_api.py:177\u001B[0m, in \u001B[0;36mBuilder.connect\u001B[0;34m(self, port, target)\u001B[0m\n\u001B[1;32m 175\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21mconnect\u001B[39m(\u001B[38;5;28mself\u001B[39m, port\u001B[38;5;241m=\u001B[39m\u001B[38;5;28;01mNone\u001B[39;00m, target\u001B[38;5;241m=\u001B[39m\u001B[38;5;28;01mNone\u001B[39;00m):\n\u001B[1;32m 176\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mcompiled_composite:\n\u001B[0;32m--> 177\u001B[0m \u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mcompile\u001B[49m\u001B[43m(\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m 179\u001B[0m \u001B[38;5;28;01massert\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mschema\u001B[38;5;241m.\u001B[39mget(\u001B[38;5;124m'\u001B[39m\u001B[38;5;124m_type\u001B[39m\u001B[38;5;124m'\u001B[39m) \u001B[38;5;129;01min\u001B[39;00m EDGE_KEYS, \u001B[38;5;124mf\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mInvalid type for connect: \u001B[39m\u001B[38;5;132;01m{\u001B[39;00m\u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mschema\u001B[38;5;132;01m}\u001B[39;00m\u001B[38;5;124m, needs to be in \u001B[39m\u001B[38;5;132;01m{\u001B[39;00mEDGE_KEYS\u001B[38;5;132;01m}\u001B[39;00m\u001B[38;5;124m\"\u001B[39m\n\u001B[1;32m 180\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m port \u001B[38;5;129;01min\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mschema[\u001B[38;5;124m'\u001B[39m\u001B[38;5;124m_inputs\u001B[39m\u001B[38;5;124m'\u001B[39m]:\n", + "File \u001B[0;32m~/code/bigraph-builder/builder/builder_api.py:208\u001B[0m, in \u001B[0;36mBuilder.compile\u001B[0;34m(self)\u001B[0m\n\u001B[1;32m 207\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21mcompile\u001B[39m(\u001B[38;5;28mself\u001B[39m):\n\u001B[0;32m--> 208\u001B[0m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mschema, tree \u001B[38;5;241m=\u001B[39m \u001B[43mtypes\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mcomplete\u001B[49m\u001B[43m(\u001B[49m\n\u001B[1;32m 209\u001B[0m \u001B[43m \u001B[49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mschema\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 210\u001B[0m \u001B[43m \u001B[49m\u001B[43mdict_from_builder_tree\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mtree\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m 211\u001B[0m \u001B[43m \u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m 212\u001B[0m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mtree \u001B[38;5;241m=\u001B[39m builder_tree_from_dict(tree)\n\u001B[1;32m 213\u001B[0m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mcompiled_composite \u001B[38;5;241m=\u001B[39m Composite({\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mstate\u001B[39m\u001B[38;5;124m'\u001B[39m: tree, \u001B[38;5;124m'\u001B[39m\u001B[38;5;124mcomposition\u001B[39m\u001B[38;5;124m'\u001B[39m: \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mschema})\n", + "File \u001B[0;32m~/code/bigraph-schema/bigraph_schema/type_system.py:967\u001B[0m, in \u001B[0;36mTypeSystem.complete\u001B[0;34m(self, initial_schema, initial_state)\u001B[0m\n\u001B[1;32m 963\u001B[0m full_schema \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39maccess(\n\u001B[1;32m 964\u001B[0m initial_schema)\n\u001B[1;32m 966\u001B[0m \u001B[38;5;66;03m# hydrate the state given the initial composition\u001B[39;00m\n\u001B[0;32m--> 967\u001B[0m state \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mhydrate\u001B[49m\u001B[43m(\u001B[49m\n\u001B[1;32m 968\u001B[0m \u001B[43m \u001B[49m\u001B[43mfull_schema\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 969\u001B[0m \u001B[43m \u001B[49m\u001B[43minitial_state\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m 971\u001B[0m \u001B[38;5;66;03m# fill in the parts of the composition schema\u001B[39;00m\n\u001B[1;32m 972\u001B[0m \u001B[38;5;66;03m# determined by the state\u001B[39;00m\n\u001B[1;32m 973\u001B[0m schema, state \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39minfer_schema(\n\u001B[1;32m 974\u001B[0m full_schema,\n\u001B[1;32m 975\u001B[0m state)\n", + "File \u001B[0;32m~/code/bigraph-schema/bigraph_schema/type_system.py:959\u001B[0m, in \u001B[0;36mTypeSystem.hydrate\u001B[0;34m(self, schema, state)\u001B[0m\n\u001B[1;32m 956\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21mhydrate\u001B[39m(\u001B[38;5;28mself\u001B[39m, schema, state):\n\u001B[1;32m 957\u001B[0m \u001B[38;5;66;03m# TODO: support partial hydration (!)\u001B[39;00m\n\u001B[1;32m 958\u001B[0m hydrated \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mhydrate_state(schema, state)\n\u001B[0;32m--> 959\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mfill\u001B[49m\u001B[43m(\u001B[49m\u001B[43mschema\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mhydrated\u001B[49m\u001B[43m)\u001B[49m\n", + "File \u001B[0;32m~/code/bigraph-schema/bigraph_schema/type_system.py:658\u001B[0m, in \u001B[0;36mTypeSystem.fill\u001B[0;34m(self, original_schema, state)\u001B[0m\n\u001B[1;32m 652\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21mfill\u001B[39m(\u001B[38;5;28mself\u001B[39m, original_schema, state\u001B[38;5;241m=\u001B[39m\u001B[38;5;28;01mNone\u001B[39;00m):\n\u001B[1;32m 653\u001B[0m \u001B[38;5;66;03m# # Removing deepcopy means the state may be updated\u001B[39;00m\n\u001B[1;32m 654\u001B[0m \u001B[38;5;66;03m# if state is not None:\u001B[39;00m\n\u001B[1;32m 655\u001B[0m \u001B[38;5;66;03m# state = copy.deepcopy(state)\u001B[39;00m\n\u001B[1;32m 656\u001B[0m schema \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39maccess(original_schema)\n\u001B[0;32m--> 658\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mfill_state\u001B[49m\u001B[43m(\u001B[49m\n\u001B[1;32m 659\u001B[0m \u001B[43m \u001B[49m\u001B[43mschema\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 660\u001B[0m \u001B[43m \u001B[49m\u001B[43mstate\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mstate\u001B[49m\u001B[43m)\u001B[49m\n", + "File \u001B[0;32m~/code/bigraph-schema/bigraph_schema/type_system.py:619\u001B[0m, in \u001B[0;36mTypeSystem.fill_state\u001B[0;34m(self, schema, state, top, path, type_key, context)\u001B[0m\n\u001B[1;32m 617\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;124m'\u001B[39m\u001B[38;5;124m_inputs\u001B[39m\u001B[38;5;124m'\u001B[39m \u001B[38;5;129;01min\u001B[39;00m schema:\n\u001B[1;32m 618\u001B[0m inputs \u001B[38;5;241m=\u001B[39m state\u001B[38;5;241m.\u001B[39mget(\u001B[38;5;124m'\u001B[39m\u001B[38;5;124minputs\u001B[39m\u001B[38;5;124m'\u001B[39m, {})\n\u001B[0;32m--> 619\u001B[0m state \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mfill_ports\u001B[49m\u001B[43m(\u001B[49m\n\u001B[1;32m 620\u001B[0m \u001B[43m \u001B[49m\u001B[43mschema\u001B[49m\u001B[43m[\u001B[49m\u001B[38;5;124;43m'\u001B[39;49m\u001B[38;5;124;43m_inputs\u001B[39;49m\u001B[38;5;124;43m'\u001B[39;49m\u001B[43m]\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 621\u001B[0m \u001B[43m \u001B[49m\u001B[43mwires\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43minputs\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 622\u001B[0m \u001B[43m \u001B[49m\u001B[43mstate\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mstate\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 623\u001B[0m \u001B[43m \u001B[49m\u001B[43mtop\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mtop\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 624\u001B[0m \u001B[43m \u001B[49m\u001B[43mpath\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mpath\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m 626\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;124m'\u001B[39m\u001B[38;5;124m_outputs\u001B[39m\u001B[38;5;124m'\u001B[39m \u001B[38;5;129;01min\u001B[39;00m schema:\n\u001B[1;32m 627\u001B[0m outputs \u001B[38;5;241m=\u001B[39m state\u001B[38;5;241m.\u001B[39mget(\u001B[38;5;124m'\u001B[39m\u001B[38;5;124moutputs\u001B[39m\u001B[38;5;124m'\u001B[39m, {})\n", + "File \u001B[0;32m~/code/bigraph-schema/bigraph_schema/type_system.py:571\u001B[0m, in \u001B[0;36mTypeSystem.fill_ports\u001B[0;34m(self, schema, wires, state, top, path)\u001B[0m\n\u001B[1;32m 568\u001B[0m subwires \u001B[38;5;241m=\u001B[39m (subwires,)\n\u001B[1;32m 570\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;28mlen\u001B[39m(path) \u001B[38;5;241m==\u001B[39m \u001B[38;5;241m0\u001B[39m:\n\u001B[0;32m--> 571\u001B[0m \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mException\u001B[39;00m(\n\u001B[1;32m 572\u001B[0m \u001B[38;5;124mf\u001B[39m\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mcannot wire \u001B[39m\u001B[38;5;132;01m{\u001B[39;00mport_key\u001B[38;5;132;01m}\u001B[39;00m\u001B[38;5;124m as we are already at the top level \u001B[39m\u001B[38;5;132;01m{\u001B[39;00mschema\u001B[38;5;132;01m}\u001B[39;00m\u001B[38;5;124m'\u001B[39m)\n\u001B[1;32m 574\u001B[0m peer \u001B[38;5;241m=\u001B[39m get_path(\n\u001B[1;32m 575\u001B[0m top,\n\u001B[1;32m 576\u001B[0m path[:\u001B[38;5;241m-\u001B[39m\u001B[38;5;241m1\u001B[39m])\n\u001B[1;32m 578\u001B[0m destination \u001B[38;5;241m=\u001B[39m establish_path(\n\u001B[1;32m 579\u001B[0m peer,\n\u001B[1;32m 580\u001B[0m subwires[:\u001B[38;5;241m-\u001B[39m\u001B[38;5;241m1\u001B[39m],\n\u001B[1;32m 581\u001B[0m top\u001B[38;5;241m=\u001B[39mtop,\n\u001B[1;32m 582\u001B[0m cursor\u001B[38;5;241m=\u001B[39mpath[:\u001B[38;5;241m-\u001B[39m\u001B[38;5;241m1\u001B[39m])\n", + "\u001B[0;31mException\u001B[0m: cannot wire A as we are already at the top level {'A': 'float', 'B': 'float'}" + ] } ], + "source": [ + "b['toy'].connect(port='A', target='A_store')\n", + "b['toy'].connect(port='B', target='B_store') # TODO -- compile is trying to connect A at the wrong level??\n", + "# b['toy'].connect(port='C', target='C_store')" + ], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2024-01-04T18:21:11.103806Z", + "start_time": "2024-01-04T18:21:10.798981Z" + } + }, + "id": "f02c15797bdb73b8" + }, + { + "cell_type": "code", + "execution_count": null, + "outputs": [], "source": [ "b.plot()" ], "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2024-01-04T17:23:41.374092Z", - "start_time": "2024-01-04T17:23:41.306551Z" + "end_time": "2024-01-04T18:21:11.103968Z", + "start_time": "2024-01-04T18:21:11.103916Z" } }, - "id": "3b1dd8569f3bb67a" + "id": "aeb344bcd6a120ae" }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, "outputs": [], "source": [], "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2024-01-04T17:23:41.380858Z", - "start_time": "2024-01-04T17:23:41.378630Z" + "end_time": "2024-01-04T18:21:11.105709Z", + "start_time": "2024-01-04T18:21:11.105131Z" } }, - "id": "f02c15797bdb73b8" + "id": "c975aab012b2706f" } ], "metadata": { diff --git a/old/__init__.py b/old/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/old/builder1.py b/old/builder1.py deleted file mode 100644 index 0277a26..0000000 --- a/old/builder1.py +++ /dev/null @@ -1,145 +0,0 @@ -from process_bigraph import Composite -from bigraph_schema.registry import type_schema_keys -from builder import pf -import json -import os -import uuid - - -def make_process_config( - type=None, - address=None, - config=None, - wires=None -): - return { - '_type': type, - 'address': address, - 'config': config, - 'wires': wires or {}, - } - - -class Node: - """ - Provides attribute-style access to the data within a tree - """ - - def __init__(self, data, schema_keys, path, builder): - self._data = data - self._schema_keys = schema_keys - self._path = path # the current path - self._builder = builder # the builder instance - - def __getattr__(self, item): - schema_key = f'_{item}' - if schema_key in self._schema_keys: - return self._data.get(schema_key) - raise AttributeError(f"{item} is not a valid attribute or schema key.") - - def __setattr__(self, key, value): - if key in ['_data', '_schema_keys', '_path', '_builder']: - super().__setattr__(key, value) - else: - schema_key = f'_{key}' - if schema_key in self._schema_keys: - self._data[schema_key] = value - else: - raise AttributeError(f"{key} is not a valid attribute or schema key.") - - def __repr__(self): - return f"Node({self._data})" - - def add_process( - self, - process_id, - type=None, - address=None, - config=None, - wires=None - ): - # Add the process with the given ID and attributes - self._data[process_id] = make_process_config(type, address, config, wires) - - # Update the builder's tree_dict - self._builder.update_tree(self._path, self._data) - - -class Builder: - schema_keys = {'_value'} - - def __init__(self, schema_keys=None, bigraph_dict=None): - if schema_keys: - self.schema_keys.update(f'_{key}' for key in schema_keys if not key.startswith('_')) - self._bigraph = bigraph_dict or {} - - def __repr__(self): - return f"{self._bigraph}" - - def __getitem__(self, keys): - if not isinstance(keys, tuple): - keys = (keys,) - - current_dict = self._bigraph - for key in keys: - current_dict = current_dict.setdefault(key, {}) - - return Node(current_dict, self.schema_keys, keys, self) - - def __setitem__(self, keys, value): - if not isinstance(keys, tuple): - keys = (keys,) - - current_dict = self._bigraph - for key in keys[:-1]: - current_dict = current_dict.setdefault(key, {}) - - # Assign the value to the '_value' key - current_dict[keys[-1]] = {'_value': value} - - def add_process( - self, - process_id, - type=None, - address=None, - config=None, - wires=None - ): - # Add the process with the given ID and attributes - self._bigraph[process_id] = make_process_config(type, address, config, wires) - - def update_tree(self, path, data): - # Navigate to the correct location in the tree and update the data - current_dict = self._bigraph - for key in path[:-1]: - current_dict = current_dict.setdefault(key, {}) - current_dict[path[-1]] = data - - - -def test_tree(): - - # Example usage: - tree = Builder(schema_keys=['apply', 'parameters']) - - # add a branch with a value - tree['a', 'b'] = 2.0 - tree['a', 'b2', 'c'] = 12.0 - - # access and print the 'apply' attribute - print(tree['a', 'b'].apply) # Output: None - tree['a', 'b'].apply = 'sum' - print(tree['a', 'b'].apply) # Output: 'sum' - print(tree['a', 'b'].value) # Output: 2.0 - - - # test with preloaded dict - tree2 = Builder(tree_dict={'path': {'to': {'leaf': {'_value': 1.0}}}}, schema_keys=['apply', 'parameters']) - - tree2 - - - - -if __name__ == '__main__': - test_tree()