1 | package com.algomagic.atn; |
---|
2 | |
---|
3 | import java.io.*; |
---|
4 | import java.util.*; |
---|
5 | |
---|
6 | public class PropertyLogParser |
---|
7 | implements LogParser, VisualizationConstants |
---|
8 | { |
---|
9 | |
---|
10 | private Properties _log; |
---|
11 | private List _updates; |
---|
12 | private Visualization _vis; |
---|
13 | |
---|
14 | |
---|
15 | public PropertyLogParser( ) { |
---|
16 | |
---|
17 | } |
---|
18 | |
---|
19 | public List parse( Visualization vis, String log1, String log2 ) { |
---|
20 | _vis = vis; |
---|
21 | List updates = null; |
---|
22 | List updates1 = parse( log1, LEFT_SIDE ); |
---|
23 | List updates2 = null; |
---|
24 | |
---|
25 | if( log2 != null ) { |
---|
26 | updates2 = parse( log2, RIGHT_SIDE ); |
---|
27 | return interleave( updates1, updates2 ); |
---|
28 | } |
---|
29 | |
---|
30 | return updates1; |
---|
31 | } |
---|
32 | |
---|
33 | |
---|
34 | private List interleave( List updates1, List updates2 ) { |
---|
35 | List updates = new ArrayList( ); |
---|
36 | Iterator first, second; |
---|
37 | |
---|
38 | |
---|
39 | ClusterUpdate u = (ClusterUpdate) updates1.get( 0 ); |
---|
40 | if( u.getLocal( ) ) { |
---|
41 | first = updates1.iterator( ); |
---|
42 | second = updates2.iterator( ); |
---|
43 | } else { |
---|
44 | first = updates2.iterator( ); |
---|
45 | second = updates1.iterator( ); |
---|
46 | } |
---|
47 | |
---|
48 | |
---|
49 | Object o; |
---|
50 | ClusterUpdate c; |
---|
51 | |
---|
52 | while( first.hasNext( ) ) { |
---|
53 | o = first.next( ); |
---|
54 | |
---|
55 | updates.add( o ); |
---|
56 | |
---|
57 | if( o instanceof ClusterUpdate ) { |
---|
58 | c = (ClusterUpdate)o; |
---|
59 | if( ! c.getStart( ) ) { |
---|
60 | break; |
---|
61 | } |
---|
62 | |
---|
63 | } |
---|
64 | } |
---|
65 | |
---|
66 | Iterator tmp = first; |
---|
67 | first = second; |
---|
68 | second = tmp; |
---|
69 | |
---|
70 | |
---|
71 | while( first.hasNext( ) && second.hasNext( ) ) { |
---|
72 | |
---|
73 | while( first.hasNext( ) ) { |
---|
74 | o = first.next( ); |
---|
75 | |
---|
76 | updates.add( o ); |
---|
77 | |
---|
78 | if( o instanceof ClusterUpdate ) { |
---|
79 | c = (ClusterUpdate)o; |
---|
80 | if( ! c.getStart( ) && c.getLocal( ) ) { |
---|
81 | break; |
---|
82 | } |
---|
83 | |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | while( second.hasNext( ) ) { |
---|
88 | o = second.next( ); |
---|
89 | |
---|
90 | updates.add( o ); |
---|
91 | |
---|
92 | if( o instanceof ClusterUpdate ) { |
---|
93 | c = (ClusterUpdate)o; |
---|
94 | if( ! c.getStart( ) && c.getLocal( ) ) { |
---|
95 | break; |
---|
96 | } |
---|
97 | |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | } |
---|
102 | |
---|
103 | if( first.hasNext( ) ) { |
---|
104 | while( first.hasNext( ) ) { |
---|
105 | updates.add( first.next( ) ); |
---|
106 | } |
---|
107 | } else if( second.hasNext( ) ) { |
---|
108 | while( second.hasNext( ) ) { |
---|
109 | updates.add( second.next( ) ); |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | for( int i = 0; i<(updates.size( )-1); i++ ) { |
---|
114 | Update u1 = (Update)updates.get( i ); |
---|
115 | Update u2 = (Update)updates.get( i+1 ); |
---|
116 | |
---|
117 | if( u1 instanceof ClusterUpdate && u2 instanceof ClusterUpdate ) { |
---|
118 | ClusterUpdate c1 = (ClusterUpdate)u1; |
---|
119 | ClusterUpdate c2 = (ClusterUpdate)u2; |
---|
120 | |
---|
121 | if( (!c1.getStart( ) && c1.getLocal( )) && (c2.getStart( ) && !c2.getLocal( )) ) { |
---|
122 | updates.set( i, c2 ); |
---|
123 | updates.set( i+1, c1 ); |
---|
124 | } |
---|
125 | } |
---|
126 | } |
---|
127 | |
---|
128 | |
---|
129 | |
---|
130 | return updates; |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | private List parse( String log, int side ) { |
---|
135 | _log = new Properties( ); |
---|
136 | _updates = new ArrayList( ); |
---|
137 | |
---|
138 | File f = new File( log ); |
---|
139 | if( f.exists( ) ) { |
---|
140 | try { |
---|
141 | _log.load( new FileInputStream( f ) ); |
---|
142 | } catch( Exception e ) { } |
---|
143 | } |
---|
144 | |
---|
145 | |
---|
146 | |
---|
147 | _vis.setName( _log.getProperty( "subject" ), side ); |
---|
148 | |
---|
149 | Collection c = new ArrayList( ); |
---|
150 | for( int i=1; _log.containsKey( "policy." + i ); i++ ) { |
---|
151 | c.add( _log.getProperty( "policy." + i ) ); |
---|
152 | } |
---|
153 | |
---|
154 | _vis.setPolicies( c, side ); |
---|
155 | |
---|
156 | |
---|
157 | boolean init = false; |
---|
158 | |
---|
159 | |
---|
160 | for( int i=1; _log.containsKey( getKey(i) ) || i==1; i++ ) { |
---|
161 | String location = _log.getProperty( getKey(i) ); |
---|
162 | boolean local = true; |
---|
163 | int index = i; |
---|
164 | |
---|
165 | |
---|
166 | |
---|
167 | if( !_log.containsKey( getKey( i, 1 ) ) ) { |
---|
168 | continue; |
---|
169 | } |
---|
170 | |
---|
171 | |
---|
172 | if( location.equals( "remote" ) ) { |
---|
173 | local = false; |
---|
174 | |
---|
175 | if( !init ) { |
---|
176 | |
---|
177 | _updates.add( new ClusterUpdate( i, |
---|
178 | true, |
---|
179 | local, |
---|
180 | side ) ); |
---|
181 | } |
---|
182 | } else if( location.equals( "local" ) ) { |
---|
183 | local = true; |
---|
184 | |
---|
185 | if( !init ) { |
---|
186 | _updates.add( new ClusterUpdate( i, |
---|
187 | true, |
---|
188 | local, |
---|
189 | side ) ); |
---|
190 | } |
---|
191 | } else if( location.equals( "init" ) ) { |
---|
192 | if( !_log.containsKey( getKey( 1, 1 ) ) ) { |
---|
193 | continue; |
---|
194 | } else { |
---|
195 | init = true; |
---|
196 | local = true; |
---|
197 | index = 2; |
---|
198 | _updates.add( new ClusterUpdate( index, |
---|
199 | true, |
---|
200 | local, |
---|
201 | side ) ); |
---|
202 | } |
---|
203 | } |
---|
204 | |
---|
205 | |
---|
206 | for( int j=1; _log.containsKey( getKey(i,j) ); j++ ) { |
---|
207 | String type = _log.getProperty( getKey(i,j) ); |
---|
208 | |
---|
209 | if( type.equals( "edge" ) ) { |
---|
210 | parseEdge( i, j, local, side, index ); |
---|
211 | } else if( type.equals( "node" ) ) { |
---|
212 | parseNode( i, j, local, side, index ); |
---|
213 | } |
---|
214 | } |
---|
215 | |
---|
216 | |
---|
217 | if( index==i) { |
---|
218 | |
---|
219 | if( location.equals( "remote" ) ) { |
---|
220 | _updates.add( new ClusterUpdate( i, |
---|
221 | false, |
---|
222 | local, |
---|
223 | side ) ); |
---|
224 | } else { |
---|
225 | _updates.add( new ClusterUpdate( i, |
---|
226 | false, |
---|
227 | local, |
---|
228 | side ) ); |
---|
229 | } |
---|
230 | |
---|
231 | |
---|
232 | } |
---|
233 | |
---|
234 | |
---|
235 | init = false; |
---|
236 | |
---|
237 | } |
---|
238 | |
---|
239 | |
---|
240 | return _updates; |
---|
241 | } |
---|
242 | |
---|
243 | private void parseNode( int i, int j, boolean local, int side, int c ) { |
---|
244 | String prefix = getPrefix(i,j); |
---|
245 | String name, type, cluster, satisfied, processed; |
---|
246 | name = _log.getProperty( prefix + "name" ); |
---|
247 | if( name == null ) { |
---|
248 | throw new RuntimeException( prefix ); |
---|
249 | } |
---|
250 | |
---|
251 | type = _log.getProperty( prefix + "nodeType" ); |
---|
252 | cluster = getCluster(c); |
---|
253 | satisfied = _log.getProperty( prefix + "satisfied" ); |
---|
254 | processed = _log.getProperty( prefix + "processing" ); |
---|
255 | |
---|
256 | // if( processed == null ) { |
---|
257 | // processed = NOT_PROC; |
---|
258 | // } else if( processed.equals( "verifier" ) ) { |
---|
259 | // processed = VERIFIER_PROC; |
---|
260 | // } else if( processed.equals( "opponent" ) ) { |
---|
261 | // processed = OPPONENT_PROC; |
---|
262 | // } |
---|
263 | |
---|
264 | // if( satisfied == null ) { |
---|
265 | // satisfied = UNKNOWN; |
---|
266 | // } |
---|
267 | |
---|
268 | |
---|
269 | if( processed == null ) { |
---|
270 | processed = NOT_PROC; |
---|
271 | } else if( processed.toLowerCase( ).equals( "verifier" ) ) { |
---|
272 | processed = VERIFIER_PROC; |
---|
273 | } else if( processed.toLowerCase( ).equals( "opponent" ) ) { |
---|
274 | processed = OPPONENT_PROC; |
---|
275 | } else if ( processed.toLowerCase( ).equals( "fully" ) ) { |
---|
276 | processed = FULLY_PROC; |
---|
277 | } |
---|
278 | |
---|
279 | if( satisfied == null ) { |
---|
280 | satisfied = UNKNOWN; |
---|
281 | } else if( satisfied.toLowerCase( ).equals( "satisfied" ) ) { |
---|
282 | satisfied = SATISFIED; |
---|
283 | } else if( satisfied.toLowerCase( ).equals( "unsatisfied" ) ) { |
---|
284 | satisfied = NOT_SATISFIED; |
---|
285 | } else if( satisfied.toLowerCase( ).equals( "unknown" ) ) { |
---|
286 | satisfied = UNKNOWN; |
---|
287 | } |
---|
288 | |
---|
289 | NodeUpdate nu = new NodeUpdate( name, type, cluster, satisfied, processed, side, local ); |
---|
290 | _updates.add( nu ); |
---|
291 | } |
---|
292 | |
---|
293 | |
---|
294 | private void parseEdge( int i, int j, boolean local, int side, int c ) { |
---|
295 | String prefix = getPrefix(i,j); |
---|
296 | String type, parentName, childName, childType, processed, satisfied; |
---|
297 | |
---|
298 | type = _log.getProperty( prefix + "edgeType" ); |
---|
299 | parentName = _log.getProperty( prefix + "parentName" ); |
---|
300 | childName = _log.getProperty( prefix + "childName" ); |
---|
301 | childType = _log.getProperty( prefix + "childType" ); |
---|
302 | processed = _log.getProperty( prefix + "childProcessing" ); |
---|
303 | satisfied = _log.getProperty( prefix + "childSatisfied" ); |
---|
304 | |
---|
305 | if( type == null || parentName == null || childName == null ) { |
---|
306 | throw new RuntimeException( prefix ); |
---|
307 | } |
---|
308 | |
---|
309 | |
---|
310 | if( childType == null ) { |
---|
311 | childType = "StandardTarget"; |
---|
312 | } |
---|
313 | |
---|
314 | |
---|
315 | if( processed == null ) { |
---|
316 | processed = NOT_PROC; |
---|
317 | } else if( processed.toLowerCase( ).equals( "verifier" ) ) { |
---|
318 | processed = VERIFIER_PROC; |
---|
319 | } else if( processed.toLowerCase( ).equals( "opponent" ) ) { |
---|
320 | processed = OPPONENT_PROC; |
---|
321 | } else if ( processed.toLowerCase( ).equals( "fully" ) ) { |
---|
322 | processed = FULLY_PROC; |
---|
323 | } |
---|
324 | |
---|
325 | if( satisfied == null ) { |
---|
326 | satisfied = UNKNOWN; |
---|
327 | } else if( satisfied.toLowerCase( ).equals( "satisfied" ) ) { |
---|
328 | satisfied = SATISFIED; |
---|
329 | } else if( satisfied.toLowerCase( ).equals( "failed" ) ) { |
---|
330 | satisfied = NOT_SATISFIED; |
---|
331 | } else if( satisfied.toLowerCase( ).equals( "unknown" ) ) { |
---|
332 | satisfied = UNKNOWN; |
---|
333 | } |
---|
334 | |
---|
335 | |
---|
336 | if( childType != null && childType.toLowerCase( ).equals( "trivialtarget" ) ) { |
---|
337 | satisfied = SATISFIED; |
---|
338 | } |
---|
339 | |
---|
340 | |
---|
341 | NodeUpdate nu = new NodeUpdate( childName, childType, getCluster(c), satisfied, processed, side, local ); |
---|
342 | _updates.add( new EdgeUpdate( parentName, nu, type, local, collectEvidence(i,j), side ) ); |
---|
343 | } |
---|
344 | |
---|
345 | |
---|
346 | private Collection collectEvidence( int i, int j ) { |
---|
347 | Collection c = new ArrayList( ); |
---|
348 | |
---|
349 | String key; |
---|
350 | for( int k=1; _log.containsKey( key = getPrefix(i,j) + "evidence." + Integer.toString(k) ); k++ ) { |
---|
351 | c.add( _log.getProperty( key ) ); |
---|
352 | } |
---|
353 | |
---|
354 | return c; |
---|
355 | } |
---|
356 | |
---|
357 | |
---|
358 | |
---|
359 | private String getKey( int i ) { |
---|
360 | return "update." + Integer.toString( i ) + ".location"; |
---|
361 | } |
---|
362 | |
---|
363 | private String getKey( int i, int j ) { |
---|
364 | return getPrefix( i, j ) + "type"; |
---|
365 | } |
---|
366 | |
---|
367 | private String getPrefix( int i, int j ) { |
---|
368 | return "update." + Integer.toString( i ) + "." + Integer.toString( j ) + "."; |
---|
369 | } |
---|
370 | |
---|
371 | private String getCluster( int i ) { |
---|
372 | return "cluster" + Integer.toString( i ); |
---|
373 | } |
---|
374 | |
---|
375 | |
---|
376 | } |
---|