[8780cbec] | 1 | package com.nailabs.abac.test; |
---|
| 2 | |
---|
| 3 | import java.util.*; |
---|
| 4 | import java.io.*; |
---|
| 5 | |
---|
| 6 | import com.nailabs.abac.trust.*; |
---|
| 7 | import com.nailabs.abac.process.*; |
---|
| 8 | |
---|
| 9 | public class TextGraphObserver implements Observer { |
---|
| 10 | |
---|
| 11 | HashMap nodeMap = new HashMap(10); |
---|
| 12 | PrintStream out; |
---|
| 13 | int count = 0; |
---|
| 14 | |
---|
| 15 | public TextGraphObserver(String name) { |
---|
| 16 | try { |
---|
| 17 | out = new PrintStream(new FileOutputStream(name + ".xml")); |
---|
| 18 | } |
---|
| 19 | catch(Exception ex) { |
---|
| 20 | ex.printStackTrace(out); |
---|
| 21 | } |
---|
| 22 | out.println("<?xml version=\"1.0\"?>\n<Negotiation>"); |
---|
| 23 | } |
---|
| 24 | |
---|
| 25 | |
---|
| 26 | public void update(TTGNode node) { |
---|
| 27 | if(!nodeMap.containsKey(node.getGoal().toString())) { |
---|
| 28 | nodeMap.put(node.getGoal().toString(), node); |
---|
| 29 | node.addObserver(this); |
---|
| 30 | //out.print("<!--Now observing node for "); |
---|
| 31 | //out.print(node.getGoal().toString()); |
---|
| 32 | //out.println("-->"); |
---|
| 33 | } |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | public void update(Observable o, Object arg) { |
---|
| 37 | if(o instanceof TTG) { |
---|
| 38 | if(arg instanceof TTGNode) { |
---|
| 39 | update((TTGNode)arg); |
---|
| 40 | out.println("<!--updating from TTG-->"); |
---|
| 41 | return; |
---|
| 42 | } |
---|
| 43 | out.print("<Observation count=\""); |
---|
| 44 | out.print(++count); |
---|
| 45 | out.println("\">"); |
---|
| 46 | |
---|
| 47 | } else { |
---|
| 48 | if(o instanceof TTGNode && !(arg instanceof SatisfactionState)) { |
---|
| 49 | return; // skip over all but satisfaction states |
---|
| 50 | } |
---|
| 51 | out.print("<Observation count=\""); |
---|
| 52 | out.print(++count); |
---|
| 53 | out.println("\">"); |
---|
| 54 | if(o instanceof XMLizable) { |
---|
| 55 | out.print(((XMLizable)o).toXML()); |
---|
| 56 | } else { |
---|
| 57 | out.print(o); |
---|
| 58 | } |
---|
| 59 | } |
---|
| 60 | if(arg instanceof XMLizable) { |
---|
| 61 | out.print(((XMLizable)arg).toXML()); |
---|
| 62 | } else { |
---|
| 63 | out.print(arg); |
---|
| 64 | } |
---|
| 65 | out.println("</Observation>\n"); |
---|
| 66 | |
---|
| 67 | out.flush(); |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | public void close() { |
---|
| 71 | out.print("\n<!--"); |
---|
| 72 | out.print(count); |
---|
| 73 | out.println(" total observable events-->"); |
---|
| 74 | out.println("</Negotiation>"); |
---|
| 75 | out.flush(); |
---|
| 76 | out.close(); |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | } |
---|