-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReadFile.py
221 lines (171 loc) · 6.1 KB
/
ReadFile.py
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import Agent
import Location
import random
import re
import time
import streamlit as st
class ReadConfiguration():
def __init__(self):
self.worlds=None
self.time_steps=None
self.starting_exposed_percentage=None
self.agent_info_keys=None
self.interaction_info_keys=None
self.f = open('config.txt','r')
self.worlds=(int)(self.get_value())
self.time_steps=(int)(self.get_value())
self.agent_info_keys=self.get_value()
self.agents_filename=self.get_value()
self.interaction_info_keys=self.get_value()
self.interactions_files_list=self.get_value()
self.location_info_keys=self.get_value()
self.locations_filename=self.get_value()
self.event_info_keys=self.get_value()
self.events_files_list=self.get_value()
if 'Agent Index' not in self.agent_info_keys.split(':'):
print("Error! Agent file does not contain parameter \'Agent Index\'")
return None
if 'Agent Index' not in self.interaction_info_keys.split(':'):
print("Interaction definition does not contain parameter \'Agent Index\'")
if 'Interacting Agent Index' not in self.interaction_info_keys.split(':'):
print("Interaction definition does not contain parameter \'Interacting Agent Index\'")
if 'Location Index' not in self.location_info_keys.split(':'):
print('Location file does not contain parameter \'Location Index\'')
if 'Location Index' not in self.event_info_keys.split(':'):
print('Event definition does not contain parameter \'Location Index\'')
if 'Agents' not in self.event_info_keys.split(':'):
print('Event definition does not contain parameter \'Agents\'')
def get_value(self):
line=self.f.readline()
l = re.findall("\<.*?\>", line)
if len(l)!=1:
print("Error! Invalid entry in config.txt")
return None
value=(((l[0])[1:])[:-1])
return value
class ReadAgents():
def __init__(self,filename,config_obj):
f=open(filename,'r')
self.n=int(self.get_value(f.readline()))
agent_info_keys=self.get_value(f.readline())
if agent_info_keys != config_obj.agent_info_keys:
print("Error! Agent Information parameters donot match the config.txt file")
return None
self.parameter_keys=agent_info_keys.split(':')
self.agents={}
for i in range(self.n):
info_dict=self.create_info_dict(self.get_value(f.readline()).split(':'))
state=None#config_obj.default_state
agent=Agent.Agent(state,info_dict)
self.agents[agent.index]=agent
def create_info_dict(self,info_list):
info_dict={}
for i,key in enumerate(self.parameter_keys):
info_dict[key]=info_list[i]
return info_dict
def get_value(self,line):
if line.endswith('\n'):
line=line[:-1]
return line
class ReadFilesList():
def __init__(self,filename):
self.file_list=[]
f=open(filename,'r')
lines=f.readlines()
separator=' '
text=separator.join(lines)
l = re.findall("\<.*?\>", text)
for filename in l:
self.file_list.append(((filename)[1:])[:-1])
class ReadInteractions():
def __init__(self,filename,config_obj,agents_obj):
self.config_obj=config_obj
self.agents_obj=agents_obj
if filename=="" or filename==None:
return
f=open(filename,'r')
self.no_interactions=int(self.get_value(f.readline()))
interaction_info_keys=self.get_value(f.readline())
if interaction_info_keys != config_obj.interaction_info_keys:
print("Error! Interaction parameters donot match the config.txt file")
return None
self.parameter_keys=interaction_info_keys.split(':')
for i in range(self.no_interactions):
parameter_list=(self.get_value(f.readline())).split(':')
agent_index,info_dict=self.get_interaction(parameter_list)
agents_obj.agents[agent_index].add_contact(info_dict)
def get_interaction(self,parameter_list):
info_dict={}
agent_index=None
contact_agent_index=None
for i,key in enumerate(self.parameter_keys):
if key=='Agent Index':
agent_index=parameter_list[i]
info_dict[key]=parameter_list[i]
return agent_index,info_dict
def get_value(self,line):
if line.endswith('\n'):
line=line[:-1]
return line
class ReadLocations():
def __init__(self,filename,config_obj):
self.config_obj=config_obj
self.locations={}
if filename=="" or filename==None:
return
f=open(filename,'r')
self.no_locations=int(self.get_value(f.readline()))
location_info_keys=self.get_value(f.readline())
if location_info_keys != config_obj.location_info_keys:
print("Error! Location parameters donot match the config.txt file")
return None
self.parameter_keys=location_info_keys.split(':')
for i in range(self.no_locations):
info_dict=self.create_info_dict(self.get_value(f.readline()).split(':'))
location=Location.Location(info_dict)
self.locations[location.index]=location
def create_info_dict(self,info_list):
info_dict={}
for i,key in enumerate(self.parameter_keys):
info_dict[key]=info_list[i]
return info_dict
def get_value(self,line):
if line.endswith('\n'):
line=line[:-1]
return line
class ReadEvents():
def __init__(self,filename,config_obj,locations_obj):
self.config_obj=config_obj
self.locations_obj=locations_obj
if filename=="" or filename==None:
return
f=open(filename,'r')
self.no_events=int(self.get_value(f.readline()))
event_info_keys=self.get_value(f.readline())
if event_info_keys != config_obj.event_info_keys:
print("Error! Event parameters donot match the config.txt file")
return None
self.parameter_keys=event_info_keys.split(':')
for i in range(self.no_events):
parameter_list=(self.get_value(f.readline())).split(':')
location_index,info_dict=self.get_event(parameter_list)
self.locations_obj.locations[location_index].add_event(info_dict)
def get_event(self,parameter_list):
info_dict={}
location_index=None
for i,key in enumerate(self.parameter_keys):
if key=='Location Index':
location_index=parameter_list[i]
if key=='Agents':
info_dict[key]=list(set(parameter_list[i].split(',')))
if info_dict[key][-1]=='':
info_dict[key]=info_dict[:-1]
else:
info_dict[key]=parameter_list[i]
if location_index==None:
print("Error! No event to read")
return location_index,info_dict
def get_value(self,line):
if line.endswith('\n'):
line=line[:-1]
return line