implemented the rule engine

master
Malar Kannan 2017-06-08 14:33:52 +05:30
commit d0d17a7fb6
7 changed files with 263 additions and 0 deletions

104
.gitignore vendored Normal file
View File

@ -0,0 +1,104 @@
# Created by https://www.gitignore.io/api/python
### Python ###
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover
.hypothesis/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# pyenv
.python-version
# celery beat schedule file
celerybeat-schedule
# SageMath parsed files
*.sage.py
# dotenv
.env
# virtualenv
.venv
venv/
ENV/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# End of https://www.gitignore.io/api/python

Binary file not shown.

111
quartic_rule.py Normal file
View File

@ -0,0 +1,111 @@
import json
import datetime
class Rule(object):
"""docstring for Rule."""
def __init__(self, rule_line,lineno):
super(Rule, self).__init__()
self.pattern,constraint = [x.strip() for x in rule_line.split("->")]
space_sep = constraint.split()
(self.value_type,op),const = space_sep[:2]," ".join(space_sep[2:])
self.const = get_const_for_type(self.value_type,const)
self.neg = True if op[0] == "!" else False
self.op = op[1:] if self.neg else op
self.lineno = lineno
def __repr__(self):
neg_val = "not" if self.neg else ""
rule_val = [self.pattern,self.value_type,neg_val,self.op,self.const]
return "{}:{} {} {} {}".format(*rule_val)
def apply_neg(self,x):
return not x if self.neg else x
def apply(self,signal):
if signal.signal == self.pattern:
if signal.value_type != self.value_type:
return True
if self.op == ">":
return self.apply_neg(signal.value > self.const)
if self.op == "<":
return self.apply_neg(signal.value < self.const)
if self.op == "=":
return self.apply_neg(signal.value == self.const)
return True
@classmethod
def rule_engine_from(cls,rule_str):
return Rule.rule_engine_gen([cls(rule_str,1)])
@staticmethod
def rule_engine_gen(rule_list):
def check_rules(signal):
for rule in rule_list:
if not rule.apply(signal):
return (False,rule)
return (True,None)
return check_rules
@classmethod
def read_rules(cls,filename):
rule_list = []
with open(filename,"r") as rule_file:
lineno = 1
for line in rule_file.readlines():
rulestr = line.strip()
if rulestr != "" and not rulestr.startswith("#"):
rule_list.append(cls(rulestr,lineno))
lineno+=1
return Rule.rule_engine_gen(rule_list)
date_parse = lambda x : datetime.datetime.strptime(x, "%Y-%m-%d %H:%M:%S")
def get_const_for_type(value_type,value_data):
if value_type == "Integer":
value = int(float(value_data))
elif value_type == "String":
value = str(value_data)
elif value_type == "Datetime":
if value_data == "now":
value = datetime.datetime.now()
else:
value = date_parse(value_data)
else:
value = "invalid"
return value
class SignalItem(object):
"""docstring for SignalItem."""
def __init__(self, sig_dict):
super(SignalItem, self).__init__()
self.signal = sig_dict["signal"]
self.value_type = sig_dict["value_type"]
self.value = get_const_for_type(self.value_type,sig_dict["value"])
def __repr__(self):
return self.signal+":"+str(self.value)
def load_sigs(filename):
with open(filename,"r") as sig_file:
return json.load(sig_file)
def validate_sigs(signals,rule_engine):
invalids = []
index = 0
for sig in signals:
sig_obj = SignalItem(sig)
index+=1
status,rule = rule_engine(sig_obj)
if not status:
invalids.append((sig_obj,rule,index))
return invalids
def main():
check_rules = Rule.read_rules("./rules.txt")
signals = load_sigs("./raw_data.json")
invalids = validate_sigs(signals,check_rules)
invalid_idx = [i[2] for i in invalids]
print invalid_idx
for sig_obj,rule,index in invalids:
print index,sig_obj," Cause Rule ",rule.lineno
if __name__ == '__main__':
main()

41
quartic_rule_test.py Normal file
View File

@ -0,0 +1,41 @@
import unittest
import quartic_rule
class QuarticTest(unittest.TestCase):
"""docstring for QuarticTest."""
def setUp(self):
self.signals = quartic_rule.load_sigs("./raw_data.json")
pass
def test_rules(self):
check_rules = quartic_rule.Rule.read_rules("./rules.txt")
invalids = quartic_rule.validate_sigs(self.signals,check_rules)
invalid_idx = [i[2] for i in invalids]
expected = [16 ,26 ,37 ,77 ,88 ,96 ,155,172,190]
self.assertEqual(invalid_idx ,expected)
def test_rules_test(self):
check_rules = quartic_rule.Rule.read_rules("./rules_test.txt")
invalids = quartic_rule.validate_sigs(self.signals,check_rules)
invalid_idx = [i[2] for i in invalids]
expected = [1, 9, 45, 53, 85, 109, 110, 119, 122, 142, 152, 199]
self.assertEqual(invalid_idx ,expected)
def test_rule1(self):
check_rules = quartic_rule.Rule.rule_engine_from("ATL3 -> Datetime !> now")
invalids = quartic_rule.validate_sigs(self.signals,check_rules)
invalid_idx = [i[2] for i in invalids]
expected = [26, 37, 96, 172, 190]
self.assertEqual(invalid_idx ,expected)
def test_rule2(self):
check_rules = quartic_rule.Rule.rule_engine_from("ATL2 -> Datetime != 2017-05-13 06:22:35")
invalids = quartic_rule.validate_sigs(self.signals,check_rules)
invalid_idx = [i[2] for i in invalids]
expected = [9]
self.assertEqual(invalid_idx ,expected)
if __name__ == '__main__':
unittest.main()

1
raw_data.json Normal file

File diff suppressed because one or more lines are too long

3
rules.txt Normal file
View File

@ -0,0 +1,3 @@
ATL1 -> Integer !> 240
ATL2 -> String != LOW
ATL3 -> Datetime !> now

3
rules_test.txt Normal file
View File

@ -0,0 +1,3 @@
ATL1 -> Integer !> 12
ATL2 -> String != HIGH
ATL2 -> Datetime !> 2017-05-13 06:22:34