Blame view

spm_rules.py 2.89 KB
4eb80aaaa   Thanasis Naskos   init commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
  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()