|
1 | 1 | from typing import Any |
2 | 2 |
|
3 | | -from process_bigraph_lang.compiler.pb_model import PBModel |
| 3 | +from process_bigraph_lang.compiler.pb_model import PBModel, PBCollectionType |
4 | 4 |
|
5 | 5 |
|
6 | 6 | def assemble_pb(pb_model: PBModel) -> dict[str, Any]: |
7 | 7 | doc: dict[str, Any] = dict(composition={}, state={}) |
8 | | - for store in pb_model.stores: |
9 | | - if store.value is not None: |
10 | | - set_value_at_path(doc["state"], store.full_path, value=store.value) |
11 | | - if store.data_type: |
12 | | - set_value_at_path(doc["composition"], store.full_path, value=store.data_type) |
13 | | - for step in pb_model.steps: |
14 | | - address = step.address |
| 8 | + for store_schema in pb_model.store_schemas: |
| 9 | + # TODO: handle collection_type properly |
| 10 | + if store_schema.default_value is not None or store_schema.collection_type is not None: |
| 11 | + if store_schema.collection_type is None: |
| 12 | + set_value_at_path( |
| 13 | + doc["composition"], store_schema.full_path + ["_default"], value=store_schema.default_value |
| 14 | + ) |
| 15 | + if store_schema.data_type: |
| 16 | + set_value_at_path( |
| 17 | + doc["composition"], store_schema.full_path + ["_type"], value=store_schema.data_type |
| 18 | + ) |
| 19 | + elif store_schema.collection_type == PBCollectionType(coll_type="map"): |
| 20 | + if store_schema.data_type: |
| 21 | + set_value_at_path(doc["composition"], store_schema.path + ["_type"], value="map") |
| 22 | + set_value_at_path(doc["composition"], store_schema.path + ["_value"], value=store_schema.data_type) |
| 23 | + else: |
| 24 | + raise ValueError( |
| 25 | + f"Unsupported collection type '{store_schema.collection_type}' for store schema '{store_schema.full_path}'" |
| 26 | + ) |
| 27 | + |
| 28 | + elif store_schema.data_type: |
| 29 | + set_value_at_path(doc["composition"], store_schema.full_path, value=store_schema.data_type) |
| 30 | + |
| 31 | + for store_state in pb_model.store_states: |
| 32 | + if store_state.value is not None: |
| 33 | + set_value_at_path(doc["state"], store_state.full_path, value=store_state.value) |
| 34 | + |
| 35 | + for step_schema in pb_model.step_schemas: |
| 36 | + # TODO: handle collection_type properly |
| 37 | + address = step_schema.address |
15 | 38 | if not address.startswith("local:"): |
16 | 39 | address = "local:!" + address |
17 | 40 | address_dict = dict(_type="quote", _default=address) |
18 | | - step_schema_dict: dict[str, Any] = dict(_type=step._type, address=address_dict) |
19 | | - if step.config_schema: |
20 | | - step_schema_dict["_config"] = step.config_schema |
21 | | - if step.input_schema: |
22 | | - step_schema_dict["_inputs"] = step.input_schema |
23 | | - if step.output_schema: |
24 | | - step_schema_dict["_outputs"] = step.output_schema |
25 | | - set_value_at_path(doc["composition"], step.full_path, value=step_schema_dict) |
| 41 | + step_schema_dict: dict[str, Any] = dict(_type=step_schema._type, address=address_dict) |
| 42 | + if step_schema.config_schema: |
| 43 | + step_schema_dict["_config"] = step_schema.config_schema |
| 44 | + if step_schema.input_schema: |
| 45 | + step_schema_dict["_inputs"] = step_schema.input_schema |
| 46 | + if step_schema.output_schema: |
| 47 | + step_schema_dict["_outputs"] = step_schema.output_schema |
| 48 | + if step_schema.default_config_state: |
| 49 | + step_schema_dict["config"] = step_schema.default_config_state |
| 50 | + if step_schema.default_input_state: |
| 51 | + step_schema_dict["inputs"] = step_schema.default_input_state |
| 52 | + if step_schema.default_output_state: |
| 53 | + step_schema_dict["outputs"] = step_schema.default_output_state |
| 54 | + if step_schema.collection_info is not None: |
| 55 | + if step_schema.collection_info.coll_type == "map": |
| 56 | + set_value_at_path(doc["composition"], step_schema.full_path + ["_type"], value="map") |
| 57 | + set_value_at_path(doc["composition"], step_schema.full_path + ["_value"], value=step_schema_dict) |
| 58 | + else: |
| 59 | + raise ValueError( |
| 60 | + f"Unsupported collection type '{step_schema.collection_info}' for step schema '{step_schema.full_path}'" |
| 61 | + ) |
| 62 | + else: |
| 63 | + set_value_at_path(doc["composition"], step_schema.full_path, value=step_schema_dict) |
26 | 64 |
|
27 | | - step_state_dict: dict[str, Any] = dict(_type=step._type) |
28 | | - if step.config_state: |
29 | | - step_state_dict["config"] = step.config_state |
30 | | - if step.input_state: |
31 | | - step_state_dict["inputs"] = step.input_state |
32 | | - if step.output_state: |
33 | | - step_state_dict["outputs"] = step.output_state |
34 | | - set_value_at_path(doc["state"], step.full_path, value=step_state_dict) |
| 65 | + for step_state in pb_model.step_states: |
| 66 | + step_state_dict: dict[str, Any] = dict(_type=step_state._type) |
| 67 | + if step_state.config_state: |
| 68 | + step_state_dict["config"] = step_state.config_state |
| 69 | + if step_state.input_state: |
| 70 | + step_state_dict["inputs"] = step_state.input_state |
| 71 | + if step_state.output_state: |
| 72 | + step_state_dict["outputs"] = step_state.output_state |
| 73 | + set_value_at_path(doc["state"], step_state.full_path, value=step_state_dict) |
35 | 74 |
|
36 | | - for process in pb_model.processes: |
37 | | - address = process.address |
| 75 | + for process_schema in pb_model.process_schemas: |
| 76 | + address = process_schema.address |
38 | 77 | if not address.startswith("local:"): |
39 | 78 | address = "local:!" + address |
40 | 79 | address_dict = dict(_type="quote", _default=address) |
41 | | - process_schema_dict: dict[str, Any] = dict(_type=process._type, address=address_dict) |
42 | | - if process.config_schema: |
43 | | - process_schema_dict["_config"] = process.config_schema |
44 | | - if process.input_schema: |
45 | | - process_schema_dict["_inputs"] = process.input_schema |
46 | | - if process.output_schema: |
47 | | - process_schema_dict["_outputs"] = process.output_schema |
48 | | - set_value_at_path(doc["composition"], process.full_path, value=process_schema_dict) |
| 80 | + process_schema_dict: dict[str, Any] = dict(_type=process_schema._type, address=address_dict) |
| 81 | + if process_schema.config_schema: |
| 82 | + process_schema_dict["_config"] = process_schema.config_schema |
| 83 | + if process_schema.input_schema: |
| 84 | + process_schema_dict["_inputs"] = process_schema.input_schema |
| 85 | + if process_schema.output_schema: |
| 86 | + process_schema_dict["_outputs"] = process_schema.output_schema |
| 87 | + if process_schema.default_config_state: |
| 88 | + process_schema_dict["config"] = process_schema.default_config_state |
| 89 | + if process_schema.default_input_state: |
| 90 | + process_schema_dict["inputs"] = process_schema.default_input_state |
| 91 | + if process_schema.default_output_state: |
| 92 | + process_schema_dict["outputs"] = process_schema.default_output_state |
| 93 | + # if process_schema.default_interval is not None: |
| 94 | + # process_schema_dict["_interval"] = process_schema.default_interval |
| 95 | + if process_schema.collection_info is not None: |
| 96 | + if process_schema.collection_info.coll_type == "map": |
| 97 | + set_value_at_path(doc["composition"], process_schema.full_path + ["_type"], value="map") |
| 98 | + set_value_at_path(doc["composition"], process_schema.full_path + ["_value"], value=process_schema_dict) |
| 99 | + else: |
| 100 | + raise ValueError( |
| 101 | + f"Unsupported collection type '{process_schema.collection_info}' for process schema '{process_schema.full_path}'" |
| 102 | + ) |
| 103 | + else: |
| 104 | + set_value_at_path(doc["composition"], process_schema.full_path, value=process_schema_dict) |
49 | 105 |
|
50 | | - address = process.address |
51 | | - if not address.startswith("local:"): |
52 | | - address = "local:!" + address |
53 | | - process_state_dict: dict[str, Any] = dict(_type=process._type) |
54 | | - if process.config_state: |
55 | | - process_state_dict["config"] = process.config_state |
56 | | - if process.input_state: |
57 | | - process_state_dict["inputs"] = process.input_state |
58 | | - if process.output_state: |
59 | | - process_state_dict["outputs"] = process.output_state |
60 | | - if process.interval is not None: |
61 | | - process_state_dict["interval"] = process.interval |
62 | | - set_value_at_path(doc["state"], process.full_path, value=process_state_dict) |
| 106 | + for process_state in pb_model.process_states: |
| 107 | + process_state_dict: dict[str, Any] = dict(_type=process_state._type) |
| 108 | + if process_state.config_state: |
| 109 | + process_state_dict["config"] = process_state.config_state |
| 110 | + if process_state.input_state: |
| 111 | + process_state_dict["inputs"] = process_state.input_state |
| 112 | + if process_state.output_state: |
| 113 | + process_state_dict["outputs"] = process_state.output_state |
| 114 | + if process_state.interval is not None: |
| 115 | + process_state_dict["interval"] = process_state.interval |
| 116 | + set_value_at_path(doc["state"], process_state.full_path, value=process_state_dict) |
63 | 117 |
|
64 | 118 | return doc |
65 | 119 |
|
|
0 commit comments