import re, copy,sys
from collections import deque
ifile = open(sys.argv[1],"r")
target_event = int(sys.argv[3])
target_event_sup = 5
rules_list = []
i = 0
for line in ifile:
rule = deque([])
line_split = line.replace(" ","").strip().split("-1")
STORE = True
pre_day = -1
for token in line_split:
m = re.search('\<([0-9]+)\>([0-9]+)', token)
if(m is not None):
day = int(m.group(1))
if(pre_day != -1):
day = int(m.group(1))-pre_day
pre_day = int(m.group(1))
event = m.group(2)
rule.append((day,event))
STORE = (event == str(target_event))
m = re.search('\#SUP:([0-9]+)', token)
if(m is not None):
sup = m.group(1)
STORE = (STORE and sup >= target_event_sup)
if(STORE):
rules_list.append(rule)
i += 1
rules_list = sorted(rules_list, key=len)
#print rules_list
rules_dict = {}
for rule in rules_list:
first_event = rule[0][1]
if(first_event not in rules_dict.keys()):
rule.popleft()
rules_dict[first_event] = [rule]
else:
rule.popleft()
rules_dict[first_event].append(rule)
#print rules_dict.keys()
ifile.close()
active_rules = {}
day_cnt = 0
ifile = open(sys.argv[2],"r")
for line in ifile:
line = line.replace("-2","").split("-1")
for day in line:
events = re.findall("\ ([0-9]+)",day)
for event in events:
if(str(event) == str(target_event)):
print str(day_cnt) + "> Failure"
if(str(event) in rules_dict.keys()):
rules_to_activate = copy.deepcopy(rules_dict[str(event)]) #check which rules should be enabled
for rule in rules_to_activate:
act_rule = rule.popleft()
act_day = act_rule[0]
act_event = act_rule[1]
if(str(act_event) == str(target_event)):
print str(day_cnt)+"> Warning in " + str(act_day) + " days expect a fault!"
continue
if(day_cnt+act_day not in active_rules.keys()):
active_rules[day_cnt+act_day] = {act_event:[rule]}
else:
if(act_event not in active_rules[day_cnt+act_day].keys()):
active_rules[day_cnt+act_day][act_event] = [rule]
else:
active_rules[day_cnt+act_day][act_event].append(rule)
if(day_cnt in active_rules.keys()):
check_rules = active_rules[day_cnt]
for event in events:
if(event in check_rules.keys()):
for rule in check_rules[event]:
if(not rule):
del rule
else:
act_rule = rule.popleft()
act_day = act_rule[0]
act_event = act_rule[1]
if(len(rule) == 0):
print str(day_cnt)+"> Warning in " + str(act_day) + " days expect a fault!"
if(day_cnt+act_day not in active_rules.keys()):
active_rules[day_cnt+act_day] = {act_event:[rule]}
else:
if(act_event not in active_rules[day_cnt+act_day].keys()):
active_rules[day_cnt+act_day][act_event] = [rule]
else:
active_rules[day_cnt+act_day][act_event].append(rule)
#print active_rules
day_cnt += 1
if(day_cnt-1 in active_rules.keys()):
del active_rules[day_cnt-1]
ifile.close()