Skip to content

Commit

Permalink
Fix typos
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielguarisa committed Oct 25, 2023
1 parent 39b1fdd commit 9eee09d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,16 @@ import retrack
parser = retrack.Parser(rule)

# Create a runner
runner = retrack.Runner(parser)
runner = retrack.Runner(parser, name="your-rule")

# Run the rule/model passing the data
runner.execute(data)
```

The `Parser` class parses the rule/model and creates a graph of nodes. The `Runner` class runs the rule/model using the data passed to the runner. The `data` is a dictionary or a list of dictionaries containing the data that will be used to evaluate the conditions and execute the actions. To see wich data is required for the given rule/model, check the `runner.request_model` property that is a pydantic model used to validate the data.

Optionally you can name the rule by passing the `name` field to the `retrack.Runner` constructor. This is useful to identify the rule when exceptions are raised.

### Creating a rule/model

A rule is a set of conditions and actions that are executed when the conditions are met. The conditions are evaluated using the data passed to the runner. The actions are executed when the conditions are met.
Expand Down
20 changes: 14 additions & 6 deletions retrack/engine/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@


class Runner:
def __init__(self, parser: Parser):
def __init__(self, parser: Parser, name: str = None):
self._parser = parser
self._name = name
self._internal_runners = {}
self.reset()
self._set_constants()
Expand All @@ -23,19 +24,25 @@ def __init__(self, parser: Parser):
self._set_internal_runners()

@classmethod
def from_json(cls, data: typing.Union[str, dict], **kwargs):
def from_json(cls, data: typing.Union[str, dict], name: str = None, **kwargs):
if isinstance(data, str) and data.endswith(".json"):
if name is None:
name = data
data = json.loads(open(data).read())
elif not isinstance(data, dict):
raise ValueError("data must be a dict or a json file path")

parser = Parser(data, **kwargs)
return cls(parser)
return cls(parser, name=name)

@property
def parser(self) -> Parser:
return self._parser

@property
def name(self) -> str:
return self._name

@property
def request_manager(self) -> RequestManager:
return self._request_manager
Expand Down Expand Up @@ -68,8 +75,9 @@ def _set_internal_runners(self):
constants.FLOW_NODE_NAME, []
):
try:
node_data = self.parser.get_by_id(node_id).data
self._internal_runners[node_id] = Runner.from_json(
self.parser.get_by_id(node_id).data.parsed_value()
node_data.parsed_value(), name=node_data.name
)
except Exception as e:
raise Exception(
Expand Down Expand Up @@ -211,8 +219,8 @@ def execute(
try:
self.__run_node(node_id)
except Exception as e:
print(f"Error running node {node_id}")
raise e
raise Exception(f"Error running node {node_id} in {self.name}") from e

if self.states[constants.OUTPUT_REFERENCE_COLUMN].isna().sum() == 0:
break

Expand Down
1 change: 1 addition & 0 deletions retrack/nodes/dynamic/flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

class FlowV0MetadataModel(pydantic.BaseModel):
value: str
name: typing.Optional[str] = None
default: typing.Optional[str] = None

def parsed_value(self) -> typing.Dict[str, typing.Any]:
Expand Down
10 changes: 6 additions & 4 deletions tests/resources/rule-of-rules.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@
"2": {
"id": 2,
"data": {
"value": "{\n\t\"id\": \"demo@0.1.0\",\n\t\"nodes\": {\n\t\t\"0\": {\n\t\t\t\"id\": 0,\n\t\t\t\"data\": {},\n\t\t\t\"inputs\": {},\n\t\t\t\"outputs\": {\n\t\t\t\t\"output_up_void\": {\n\t\t\t\t\t\"connections\": [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\"node\": 2,\n\t\t\t\t\t\t\t\"input\": \"input_void\",\n\t\t\t\t\t\t\t\"data\": {}\n\t\t\t\t\t\t}\n\t\t\t\t\t]\n\t\t\t\t},\n\t\t\t\t\"output_down_void\": {\n\t\t\t\t\t\"connections\": [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\"node\": 3,\n\t\t\t\t\t\t\t\"input\": \"input_void\",\n\t\t\t\t\t\t\t\"data\": {}\n\t\t\t\t\t\t}\n\t\t\t\t\t]\n\t\t\t\t}\n\t\t\t},\n\t\t\t\"position\": [\n\t\t\t\t0,\n\t\t\t\t0\n\t\t\t],\n\t\t\t\"name\": \"Start\"\n\t\t},\n\t\t\"2\": {\n\t\t\t\"id\": 2,\n\t\t\t\"data\": {\n\t\t\t\t\"name\": \"var_a\",\n\t\t\t\t\"default\": null\n\t\t\t},\n\t\t\t\"inputs\": {\n\t\t\t\t\"input_void\": {\n\t\t\t\t\t\"connections\": [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\"node\": 0,\n\t\t\t\t\t\t\t\"output\": \"output_up_void\",\n\t\t\t\t\t\t\t\"data\": {}\n\t\t\t\t\t\t}\n\t\t\t\t\t]\n\t\t\t\t}\n\t\t\t},\n\t\t\t\"outputs\": {\n\t\t\t\t\"output_value\": {\n\t\t\t\t\t\"connections\": [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\"node\": 4,\n\t\t\t\t\t\t\t\"input\": \"input_value_0\",\n\t\t\t\t\t\t\t\"data\": {}\n\t\t\t\t\t\t}\n\t\t\t\t\t]\n\t\t\t\t}\n\t\t\t},\n\t\t\t\"position\": [\n\t\t\t\t364.0234375,\n\t\t\t\t-195.8359375\n\t\t\t],\n\t\t\t\"name\": \"Input\"\n\t\t},\n\t\t\"3\": {\n\t\t\t\"id\": 3,\n\t\t\t\"data\": {\n\t\t\t\t\"name\": \"var_b\",\n\t\t\t\t\"default\": null\n\t\t\t},\n\t\t\t\"inputs\": {\n\t\t\t\t\"input_void\": {\n\t\t\t\t\t\"connections\": [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\"node\": 0,\n\t\t\t\t\t\t\t\"output\": \"output_down_void\",\n\t\t\t\t\t\t\t\"data\": {}\n\t\t\t\t\t\t}\n\t\t\t\t\t]\n\t\t\t\t}\n\t\t\t},\n\t\t\t\"outputs\": {\n\t\t\t\t\"output_value\": {\n\t\t\t\t\t\"connections\": [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\"node\": 4,\n\t\t\t\t\t\t\t\"input\": \"input_value_1\",\n\t\t\t\t\t\t\t\"data\": {}\n\t\t\t\t\t\t}\n\t\t\t\t\t]\n\t\t\t\t}\n\t\t\t},\n\t\t\t\"position\": [\n\t\t\t\t363.8376785973601,\n\t\t\t\t108.76168656142642\n\t\t\t],\n\t\t\t\"name\": \"Input\"\n\t\t},\n\t\t\"4\": {\n\t\t\t\"id\": 4,\n\t\t\t\"data\": {\n\t\t\t\t\"operator\": \"*\"\n\t\t\t},\n\t\t\t\"inputs\": {\n\t\t\t\t\"input_value_0\": {\n\t\t\t\t\t\"connections\": [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\"node\": 2,\n\t\t\t\t\t\t\t\"output\": \"output_value\",\n\t\t\t\t\t\t\t\"data\": {}\n\t\t\t\t\t\t}\n\t\t\t\t\t]\n\t\t\t\t},\n\t\t\t\t\"input_value_1\": {\n\t\t\t\t\t\"connections\": [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\"node\": 3,\n\t\t\t\t\t\t\t\"output\": \"output_value\",\n\t\t\t\t\t\t\t\"data\": {}\n\t\t\t\t\t\t}\n\t\t\t\t\t]\n\t\t\t\t}\n\t\t\t},\n\t\t\t\"outputs\": {\n\t\t\t\t\"output_value\": {\n\t\t\t\t\t\"connections\": [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\"node\": 6,\n\t\t\t\t\t\t\t\"input\": \"input_value\",\n\t\t\t\t\t\t\t\"data\": {}\n\t\t\t\t\t\t}\n\t\t\t\t\t]\n\t\t\t\t}\n\t\t\t},\n\t\t\t\"position\": [\n\t\t\t\t717.8109142505134,\n\t\t\t\t-74.6040121470534\n\t\t\t],\n\t\t\t\"name\": \"Math\"\n\t\t},\n\t\t\"6\": {\n\t\t\t\"id\": 6,\n\t\t\t\"data\": {},\n\t\t\t\"inputs\": {\n\t\t\t\t\"input_value\": {\n\t\t\t\t\t\"connections\": [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\"node\": 4,\n\t\t\t\t\t\t\t\"output\": \"output_value\",\n\t\t\t\t\t\t\t\"data\": {}\n\t\t\t\t\t\t}\n\t\t\t\t\t]\n\t\t\t\t}\n\t\t\t},\n\t\t\t\"outputs\": {\n\t\t\t\t\"output_value\": {\n\t\t\t\t\t\"connections\": [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\"node\": 7,\n\t\t\t\t\t\t\t\"input\": \"input_value\",\n\t\t\t\t\t\t\t\"data\": {}\n\t\t\t\t\t\t}\n\t\t\t\t\t]\n\t\t\t\t}\n\t\t\t},\n\t\t\t\"position\": [\n\t\t\t\t1005.7455832447746,\n\t\t\t\t-52.25458299824986\n\t\t\t],\n\t\t\t\"name\": \"Round\"\n\t\t},\n\t\t\"7\": {\n\t\t\t\"id\": 7,\n\t\t\t\"data\": {\n\t\t\t\t\"message\": null\n\t\t\t},\n\t\t\t\"inputs\": {\n\t\t\t\t\"input_value\": {\n\t\t\t\t\t\"connections\": [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\"node\": 6,\n\t\t\t\t\t\t\t\"output\": \"output_value\",\n\t\t\t\t\t\t\t\"data\": {}\n\t\t\t\t\t\t}\n\t\t\t\t\t]\n\t\t\t\t}\n\t\t\t},\n\t\t\t\"outputs\": {},\n\t\t\t\"position\": [\n\t\t\t\t1279.293661227661,\n\t\t\t\t-54.172931549720175\n\t\t\t],\n\t\t\t\"name\": \"Output\"\n\t\t}\n\t}\n}",
"default": null
"value": "{\"id\":\"demo@0.1.0\",\"nodes\":{\"0\":{\"id\":0,\"data\":{},\"inputs\":{},\"outputs\":{\"output_up_void\":{\"connections\":[{\"node\":2,\"input\":\"input_void\",\"data\":{}}]},\"output_down_void\":{\"connections\":[{\"node\":3,\"input\":\"input_void\",\"data\":{}}]}},\"position\":[0,0],\"name\":\"Start\"},\"2\":{\"id\":2,\"data\":{\"name\":\"var_a\",\"default\":null},\"inputs\":{\"input_void\":{\"connections\":[{\"node\":0,\"output\":\"output_up_void\",\"data\":{}}]}},\"outputs\":{\"output_value\":{\"connections\":[{\"node\":4,\"input\":\"input_value_0\",\"data\":{}}]}},\"position\":[364.0234375,-195.8359375],\"name\":\"Input\"},\"3\":{\"id\":3,\"data\":{\"name\":\"var_b\",\"default\":null},\"inputs\":{\"input_void\":{\"connections\":[{\"node\":0,\"output\":\"output_down_void\",\"data\":{}}]}},\"outputs\":{\"output_value\":{\"connections\":[{\"node\":4,\"input\":\"input_value_1\",\"data\":{}}]}},\"position\":[363.8376785973601,108.76168656142642],\"name\":\"Input\"},\"4\":{\"id\":4,\"data\":{\"operator\":\"*\"},\"inputs\":{\"input_value_0\":{\"connections\":[{\"node\":2,\"output\":\"output_value\",\"data\":{}}]},\"input_value_1\":{\"connections\":[{\"node\":3,\"output\":\"output_value\",\"data\":{}}]}},\"outputs\":{\"output_value\":{\"connections\":[{\"node\":6,\"input\":\"input_value\",\"data\":{}}]}},\"position\":[717.8109142505134,-74.6040121470534],\"name\":\"Math\"},\"6\":{\"id\":6,\"data\":{},\"inputs\":{\"input_value\":{\"connections\":[{\"node\":4,\"output\":\"output_value\",\"data\":{}}]}},\"outputs\":{\"output_value\":{\"connections\":[{\"node\":7,\"input\":\"input_value\",\"data\":{}}]}},\"position\":[1005.7455832447746,-52.25458299824986],\"name\":\"Round\"},\"7\":{\"id\":7,\"data\":{\"message\":null},\"inputs\":{\"input_value\":{\"connections\":[{\"node\":6,\"output\":\"output_value\",\"data\":{}}]}},\"outputs\":{},\"position\":[1279.293661227661,-54.172931549720175],\"name\":\"Output\"}}}",
"default": null,
"name": "example"
},
"inputs": {
"input_var_a": {
Expand Down Expand Up @@ -70,7 +71,7 @@
},
"position": [
588.546875,
-135.41796875
-135.7265625
],
"name": "FlowV0"
},
Expand Down Expand Up @@ -165,5 +166,6 @@
],
"name": "Output"
}
}
},
"version": "8a3b4cc507.2023-10-25"
}

0 comments on commit 9eee09d

Please sign in to comment.