Skip to content

Commit 42f8fa2

Browse files
committed
Builder support for process
Added builder support for processes, use "*"s for special compartments that you don't want shown by default in the state view.
1 parent 0ae862c commit 42f8fa2

File tree

4 files changed

+33
-23
lines changed

4 files changed

+33
-23
lines changed

ngcsimlib/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from . import controller
33
from . import commands
44

5-
65
import argparse, os, json
76
from types import SimpleNamespace
87
from importlib import import_module

ngcsimlib/compilers/process.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,25 @@ def execute(self, update_state=False, **kwargs):
4545
if self._method is None:
4646
warn("Attempting to execute a process with no transition steps")
4747
return
48-
state = self.pure(self.get_required_state(), **kwargs)
48+
state = self.pure(self.get_required_state(include_special_compartments=True), **kwargs)
4949
if update_state:
5050
self.updated_modified_state(state)
5151
return state
5252

5353
def as_obj(self):
5454
return {"name": self.name, "calls": self._calls}
5555

56-
def get_required_state(self):
56+
def get_required_state(self, include_special_compartments=False):
5757
compound_state = {}
5858
for context in self._needed_contexts:
59-
compound_state.update(context.get_current_state())
59+
compound_state.update(context.get_current_state(include_special_compartments))
6060
return compound_state
6161

6262
def updated_modified_state(self, state):
63-
Set_Compartment_Batch({key: value for key, value in state.items() if key in self.get_required_state()})
63+
Set_Compartment_Batch({key: value for key, value in state.items() if key in self.get_required_state(include_special_compartments=True)})
6464

6565

66-
def transition(output_compartments):
66+
def transition(output_compartments, builder=False):
6767
def _wrapper(f):
6868
@wraps(f)
6969
def inner(*args, **kwargs):
@@ -80,6 +80,7 @@ def inner(*args, **kwargs):
8080

8181
inner.class_name = class_name
8282
inner.resolver_key = resolver_key
83+
inner.builder = builder
8384

8485
add_component_transition(class_name, resolver_key,
8586
(f, output_compartments))

ngcsimlib/compilers/process_compiler/component_compiler.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,37 @@ def __make_get_param(p, component):
1111
def __make_get_comp(c, component):
1212
return lambda current_state, **kwargs: current_state.get(component.__dict__[c].path, None)
1313

14+
def builder(transition_method_to_build):
15+
component = transition_method_to_build.__self__
16+
builder_method = transition_method_to_build.f
17+
# method, output_compartments, args, params, input_compartments
18+
return builder_method(component)
19+
1420

1521
def compile(transition_method):
1622
composition = None
1723
component = transition_method.__self__
1824

19-
pure_fn = transition_method.f
20-
output = transition_method.output_compartments
25+
if transition_method.builder:
26+
pure_fn, output, args, parameters, compartments = builder(transition_method)
27+
else:
2128

22-
varnames = transition_method.fargs
29+
pure_fn = transition_method.f
30+
output = transition_method.output_compartments
2331

24-
args = []
25-
compartments = []
26-
parameters = []
32+
varnames = transition_method.fargs
2733

28-
for name in varnames:
29-
if name not in component.__dict__.keys():
30-
args.append(name)
31-
elif Compartment.is_compartment(component.__dict__[name]):
32-
compartments.append(name)
33-
else:
34-
parameters.append(name)
34+
args = []
35+
compartments = []
36+
parameters = []
37+
38+
for name in varnames:
39+
if name not in component.__dict__.keys():
40+
args.append(name)
41+
elif Compartment.is_compartment(component.__dict__[name]):
42+
compartments.append(name)
43+
else:
44+
parameters.append(name)
3545

3646
for conn in component.connections:
3747
composition = compose(composition, op_compile(conn))

ngcsimlib/context.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -651,15 +651,15 @@ def view_guide(self, guide, skip=None):
651651
guides += klass.guides.__dict__[guide.value]
652652
return guides
653653

654-
def _get_state_keys(self):
654+
def _get_state_keys(self, include_special_compartments=False):
655655
all_keys = []
656656
for comp_name in self.components.keys():
657657
all_keys.extend([key for key in Get_Compartment_Batch().keys()
658-
if self.path + "/" + comp_name in key])
658+
if (self.path + "/" + comp_name in key and (include_special_compartments or "*" not in key))])
659659
return all_keys
660660

661-
def get_current_state(self):
662-
return Get_Compartment_Batch(self._get_state_keys())
661+
def get_current_state(self, include_special_compartments=False):
662+
return Get_Compartment_Batch(self._get_state_keys(include_special_compartments))
663663

664664
def update_current_state(self, state):
665665
Set_Compartment_Batch({key: value for key, value in state.items() if key in self._get_state_keys()})

0 commit comments

Comments
 (0)