KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnode > Util > DFSRChecker > DFSR > DFSRDotVis


1 /*
2  * $Id: DFSRDotVis.java,v 1.4 2005/07/08 12:04:11 kofron Exp $
3  *
4  * Copyright 2004
5  * Distributed Systems Research Group
6  * Department of Software Engineering
7  * Faculty of Mathematics and Physics
8  * Charles University, Prague
9  *
10  * Copyright 2005
11  * Formal Methods In Software Engineering Group
12  * Institute of Computer Science
13  * Academy of Sciences of the Czech Republic
14  *
15  * This code was developed by Jan Kofron <kofron@nenya.ms.mff.cuni.cz>
16  */

17
18
19 package SOFA.SOFAnode.Util.DFSRChecker.DFSR;
20
21
22 import java.io.FileOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24
25
26 import SOFA.SOFAnode.Util.DFSRChecker.node.ActionRepository;
27 import SOFA.SOFAnode.Util.DFSRChecker.node.InvalidParameterException;
28 import SOFA.SOFAnode.Util.DFSRChecker.node.TreeNode;
29 import SOFA.SOFAnode.Util.DFSRChecker.state.State;
30 import SOFA.SOFAnode.Util.DFSRChecker.state.TransitionPair;
31
32 /**
33  * This class produces an output for dot visualization tool (part of GraphViz by Att).
34  * The visualizied subject is the transition graph represented by a TreeNode
35  *
36  */

37 public class DFSRDotVis implements IDFSRExtension {
38
39     /**
40      * Creates a new instance of DFSRDotVis.
41      *
42      * @param outputfile the name of the output file
43      * @param automaton automaton to be visualized
44      * @param repository an instance of ActionRepository for printing the action name.
45      */

46     public DFSRDotVis(FileOutputStream JavaDoc outputfile, TreeNode automaton, ActionRepository repository) {
47         this.outputfile = outputfile;
48         this.automaton = automaton;
49         this.repository = repository;
50     }
51
52     /**
53      * @see SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.DFSR.IDFSRExtension#action(SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.state.State)
54      */

55     public void action(State state) throws InvalidParameterException {
56     }
57
58     /**
59      * @see SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.DFSR.IDFSRExtension#actionNew(SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.state.State)
60      */

61     public boolean actionNew(State state) throws InvalidParameterException, CheckingException {
62         state.label = new String JavaDoc("S" + timestamp);
63
64         try {
65             // the state
66
boolean acc = automaton.isAccepting(state);
67             State initstate = automaton.getInitial();
68             boolean init = initstate.getSignature().equals(state.getSignature());
69             if (acc)
70                 outputfile.write((" \"" + state.getSignature() + "\" [label=\"S" + timestamp + "\", shape=doublecircle, style=filled, fillcolor=\"grey\"];\n").getBytes());
71             else
72                 outputfile.write((" \"" + state.getSignature() + "\" [label=\"S" + timestamp + "\"];\n").getBytes());
73
74             if (init)
75                 if (acc)
76                     outputfile.write((" \"" + state.getSignature() + "\" [shape=box, peripheries=2, style=filled, fillcolor=\"grey\"];\n").getBytes());
77                 else
78                     outputfile.write((" \"" + state.getSignature() + "\" [shape=box];\n").getBytes());
79
80             //... and its transitions
81
TransitionPair[] trans = automaton.getTransitions(state).transitions;
82             for (int i = 0; i < trans.length; ++i) {
83                 outputfile.write((" \"" + state.getSignature() + "\" -> \"" + trans[i].state.getSignature() + "\" [label=\"" + repository.getItemString(trans[i].eventIndex) + "\"];\n").getBytes());
84                 if (trans[i].state.label.equals("<no_label>"))
85                     outputfile.write((" \"" + trans[i].state.getSignature() + "\" [label=\".\"];\n").getBytes());
86             }
87
88         } catch (IOException JavaDoc e) {
89             throw new AcceptingStateException("Visualization backend: error while writing to the file.");
90         }
91
92         state.timestamp = timestamp++;
93
94         
95         return automaton.isAccepting(state);
96     }
97
98     /**
99      * @see SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.DFSR.IDFSRExtension#actionCycle(SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.state.State)
100      */

101     public void actionCycle(State state) throws InvalidParameterException {
102         if (state.getSignature().getCycleId() == Long.MAX_VALUE) {
103             state.cycleStart = true;
104             state.getSignature().setCycleId(state.timestamp);
105         }
106     }
107
108     /**
109      * @see SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.DFSR.IDFSRExtension#actionVisited(SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.state.State)
110      */

111     public void actionVisited(State state) throws InvalidParameterException {
112     }
113
114     /**
115      * @see SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.DFSR.IDFSRExtension#actionBack(SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.state.State)
116      */

117     public void actionBack(State state) throws InvalidParameterException {
118         try {
119             if (state.isAcceptingReachable()) {
120                 outputfile.write((" \"" + state.getSignature() + "\" [style=filled, fillcolor=\"grey\"];\n").getBytes());
121             }
122         } catch (IOException JavaDoc e) {
123         }
124
125     }
126     
127     /**
128      * @see SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.DFSR.IDFSRExtension#reset()
129      */

130     public void reset() {
131         timestamp = 0;
132     }
133
134     /**
135      * File where the description of the automata is stored
136      *
137      */

138     private FileOutputStream JavaDoc outputfile;
139
140     /**
141      * State index
142      */

143     private int timestamp = 0;
144
145     /**
146      * The root if the parse tree.
147      */

148     private TreeNode automaton;
149
150     /**
151      * Action repository.
152      */

153     private ActionRepository repository;
154
155 }
Popular Tags