Skip to content

Commit

Permalink
updated condition workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
superstes committed Apr 1, 2021
1 parent f7fdb75 commit 1b1aa9b
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 111 deletions.
57 changes: 54 additions & 3 deletions code/core/device/output/condition/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

from core.config.object.setting.condition import GaConditionGroup, GaConditionLink
from core.device.output.condition.match import Go as ConditionResult
from core.device.output.condition.process.link import get_link_result
from core.device.log import device_logger

from collections import Counter

LINK_ORDER_ID_1 = 1
LINK_ORDER_ID_2 = 2


class Go:
def __init__(self, group: GaConditionGroup):
Expand Down Expand Up @@ -93,10 +95,9 @@ def _get_link_data_result(self, link: GaConditionLink, result_dict: dict) -> boo
self.logger.write(f"Getting result of condition-link \"{link.name}\"", level=9)

try:
result = get_link_result(
result = self._get_result(
link=link,
result_dict=result_dict,
device=self.group.name
)

self.logger.write(f"Result of condition-link \"{link.name}\": \"{result}\"", level=7)
Expand Down Expand Up @@ -221,5 +222,55 @@ def _get_member_list(link: GaConditionLink) -> list:
check_list.extend(list(link.condition_group_dict.values()))
return check_list

def _get_result(self, result_dict: dict, link: GaConditionLink) -> bool:
"""Calculate the link result from its member results.
Comparison is done using the link operator.
:param result_dict: Results of link members
:type result_dict: dict
:param link: Link that is currently processed
:type link: GaConditionLink
:return: Link result
:rtype: bool
"""
if len(result_dict) != 2 or LINK_ORDER_ID_1 not in result_dict or LINK_ORDER_ID_2 not in result_dict:
self.logger.write("Condition link \"%s\" (id \"%s\") has more or less than 2 results: \"%s\" => could be a configuration error"
% (link.name, link.object_id, result_dict), level=7)

# allowing a link with only one member
if len(result_dict) == 1:
return list(result_dict.values())[0]

raise ValueError(f'Got not acceptable results for members of link \"{link.name}\"')

op = link.operator
self.logger.write(f"Processing condition link \"{link.name}\", operator \"{op}\", result dict \"{result_dict}\"", level=6) # 7

if op == 'and':
result = all(result_dict.values())

elif op == 'nand':
result = not all(result_dict.values())

elif op == 'or':
result = any(result_dict.values())

elif op == 'nor':
result = not any(result_dict.values())

elif op == 'not':
result = True if result_dict[LINK_ORDER_ID_1] is True and result_dict[LINK_ORDER_ID_2] is False else False

elif op == 'xor':
result = result_dict[LINK_ORDER_ID_1] != result_dict[LINK_ORDER_ID_2]

elif op == 'xnor':
result = not (result_dict[LINK_ORDER_ID_1] != result_dict[LINK_ORDER_ID_2])

else:
self.logger.write("Condition link \"%s\" (id \"%s\") has an unsupported operator '%s" % (link.name, link.object_id, op), level=3)
raise KeyError(f"Unsupported operator for link \"{link.name}\"")

return result

def __del__(self):
self._reset_flags(self.group)
26 changes: 16 additions & 10 deletions code/core/device/output/condition/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def get(self) -> bool:
else:
data = self.data_list[0]

self.logger.write(f"Data of condition match \"{self.condition.name}\": \"{data}\"", level=6) # 7
result = self._compare_data(data=data)
self.logger.write(f"Result of condition match \"{self.condition.name}\": \"{result}\"", level=6)
return result
Expand All @@ -39,10 +40,15 @@ def get(self) -> bool:

def _compare_data(self, data) -> bool:
operator = self.condition.operator
value = self.data_type(self.condition.value)
self.logger.write("Condition item \"%s\" comparing data: \"%s %s %s\"" % (self.condition.name, data, operator, value), level=9)
result = False

if self.data_type == float:
value = round(self.data_type(self.condition.value), 2)
data = round(self.data_type(data), 2)

else:
value = self.data_type(self.condition.value)

if operator == '=':
if value == data:
result = True
Expand All @@ -60,17 +66,15 @@ def _compare_data(self, data) -> bool:
result = True

else:
self.logger.write("Condition item \"%s\" has an unsupported operator \"%s\" with value_type \"%s\""
% (self.condition.name, operator, self.data_type), level=4)
raise KeyError("Condition \"%s\" has an unsupported operator \"%s\"" % (self.condition.name, operator))
self.logger.write(f"Condition match \"{self.condition.name}\" has an unsupported operator \"{operator}\" "
f"with value_type \"{self.data_type}\"", level=4)
raise KeyError(f"Unsupported operator for condition \"{self.condition.name}\"")

self.logger.write("Condition item \"%s\" result for comparison \"%s %s %s\" => \"%s\"" % (self.condition.name, data, operator, value, result), level=7)
self.logger.write(f"Condition match \"{self.condition.name}\" result for comparison \"{value} {operator} {data}\" = {result}", level=6) # 7
return result

def _get_data(self) -> (float, int):
value_check = self.condition.check
self.logger.write("Getting data for condition item \"%s\" while using value_check \"%s\"" % (self.condition.name, value_check), level=9)

if value_check == 'min':
data = min(self.data_list)

Expand All @@ -81,7 +85,9 @@ def _get_data(self) -> (float, int):
data = (sum(self.data_list) / len(self.data_list))

else:
self.logger.write("Condition item \"%s\" has an unsupported value_check set: \"%s\"" % (self.condition.name, value_check), level=4)
raise KeyError("Condition \"%s\" has an unsupported value_check \"%s\"" % (self.condition.name, value_check))
self.logger.write(f"Condition match \"{self.condition.name}\" has an unsupported value_check set: \"{value_check}\"", level=4)
raise KeyError(f"Unsupported check type for condition \"{self.condition.name}\"")

self.logger.write(f"Data for condition match \"{self.condition.name}\" using value check \"{value_check}\": {data}", level=6) # 7

return data
77 changes: 0 additions & 77 deletions code/core/device/output/condition/process/link.py

This file was deleted.

10 changes: 4 additions & 6 deletions code/core/device/output/condition/process/match_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ def _get_data_list(self):

else:
self.logger.write("Condition item \"%s\" has an unsupported period_type \"%s\"" % (self.condition.name, period_type), level=4)
raise KeyError("Condition \"%s\" has an unsupported period_type \"%s\"" % (self.condition.name, period_type))
raise KeyError(f"Unsupported period type for condition match \"{self.condition.name}\"")

if data is None:
self.logger.write("No data received for condition item \"%s\"" % self.condition.name, level=5)
raise ValueError("No data received for condition item \"%s\" (id \"%s\")" % (self.condition.name, self.condition.object_id))
self.logger.write("No data received for condition item \"%s\" (id \"%s\")" % (self.condition.name, self.condition.object_id), level=5)
raise ValueError(f"Got no data for condition match \"{self.condition.name}\"")
# maybe we should let the user decide which data to use if none is found

self.data_type = self._get_data_type(data=data)
Expand All @@ -69,7 +69,7 @@ def _get_data_type(self, data: list) -> (bool, float, int, str):
else:
self.logger.write("Input device/model \"%s\" has an unsupported data data_type set \"%s\""
% (self.condition.check_instance.name, data_type), level=4)
raise KeyError("Input device/model \"%s\" has an unsupported data data_type set \"%s\"" % (self.condition.check_instance.name, data_type))
raise KeyError(f"Unsupported data type for input \"{self.condition.check_instance.name}\"")

self.logger.write("Condition item \"%s\", data \"%s\", type \"%s\"" % (self.condition.name, data, typ), level=9)

Expand All @@ -84,13 +84,11 @@ def _get_data_by_time(self) -> list:
stop_time = datetime.now().strftime(timestamp_format)

data_tuple_list = self.database.get(self.SQL_QUERY_TIME % (object_id, start_time, stop_time))

return data_tuple_list

def _get_data_by_range(self) -> list:
range_count = int(self.condition.period_data)
object_id = self.condition.check_instance.object_id

data_tuple_list = self.database.get(self.SQL_QUERY_RANGE % (object_id, range_count))

return data_tuple_list
4 changes: 2 additions & 2 deletions code/web/base/ga/submodels/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ class ObjectConditionLinkModel(BareModel):
('nand', 'NOT-AND (neither or one correct)'),
('or', 'OR (at least one correct)'),
('nor', 'NOT-OR (neither correct)'),
('xor', 'XOR (either none or both correct)'),
('xnor', 'NOT-XOR (one of two correct)'),
('xor', 'XOR (one of two correct)'),
('xnor', 'NOT-XOR (either none or both correct)'),
('not', 'NOT (first correct second incorrect)')
]

Expand Down
30 changes: 17 additions & 13 deletions code/web/base/ga/subviews/config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,24 +109,28 @@ def _get_switch(self):
params = '?action=Create'
_type_dict = sub_type_dict[self.type]

if switch_action in ['create', 'list']:
if switch_sub_type in _type_dict:
switch_type = _type_dict[switch_sub_type]['url']
try:
if switch_action in ['create', 'list']:
if switch_sub_type in _type_dict:
switch_type = _type_dict[switch_sub_type]['url']

else:
switch_sub_type = default_switch_sub_type
switch_type = _type_dict[default_switch_sub_type]['url']
else:
switch_sub_type = default_switch_sub_type
switch_type = _type_dict[default_switch_sub_type]['url']

if switch_action == 'add':
switch_action = 'create'

if switch_action == 'add':
switch_action = 'create'
if switch_sub_type in _type_dict:
if set_key(_type_dict[switch_sub_type], 'add_url'):
switch_type = _type_dict[switch_sub_type]['add_url']

if switch_sub_type in _type_dict:
if set_key(_type_dict[switch_sub_type], 'add_url'):
switch_type = _type_dict[switch_sub_type]['add_url']
params = f"?group={self.data['group']}&member_type={switch_sub_type}&group_type={self.type}&action=Add"

params = f"?group={self.data['group']}&member_type={switch_sub_type}&group_type={self.type}&action=Add"
return redirect(f"/{self.tmpl_root}/{switch_action}/{switch_type}/{params}")

return redirect(f"/{self.tmpl_root}/{switch_action}/{switch_type}/{params}")
except KeyError:
return redirect(self.post_redirect)

def _get_list(self):
dataset = self.model.objects.all()
Expand Down

0 comments on commit 1b1aa9b

Please sign in to comment.