001: /* Soot - a J*va Optimization Framework
002: * Copyright (C) 2004 Jennifer Lhotak
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the
016: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
017: * Boston, MA 02111-1307, USA.
018: */
019:
020: package ca.mcgill.sable.soot.interaction;
021:
022: import soot.toolkits.graph.interaction.*;
023: import java.util.*;
024:
025: public class DataKeeper {
026:
027: private List flowInfoList;
028: private FlowInfo current;
029: private int total;
030: private int repeat;
031: private InteractionController controller;
032:
033: public DataKeeper(InteractionController controller) {
034: setController(controller);
035: }
036:
037: public void stepBack() {
038: // this will be called from InteractionStepBack
039: // it will cause the most recent flow info data
040: // to be removed resulting in a blank if on iteration
041: // 0 or the results at this node from the
042: // previous iteration
043: // if it is the very fist node and
044: // very first iteration nothing will happen
045:
046: // no where to go back to
047: if (getCurrent() == null)
048: return;
049: int index = getFlowInfoList().indexOf(getCurrent());
050: FlowInfo previous;
051: FlowInfo clearTo;
052: // on first iter need to replace with empty
053: if (index > 0) {
054: previous = (FlowInfo) getFlowInfoList().get(index - 1);
055:
056: } else {
057: // at first node and want to go back
058: previous = null;
059: }
060: clearTo = findLast();
061:
062: setCurrent(previous);
063: getController().setEvent(
064: new InteractionEvent(IInteractionConstants.CLEARTO,
065: clearTo));
066: getController().handleEvent();
067: if (previous != null) {
068: getController().setEvent(
069: new InteractionEvent(IInteractionConstants.REPLACE,
070: previous));
071: getController().handleEvent();
072: }
073:
074: }
075:
076: private FlowInfo findLast() {
077: Iterator it = getFlowInfoList().iterator();
078: FlowInfo retInfo = new FlowInfo("", getCurrent().unit(),
079: getCurrent().isBefore());
080:
081: while (it.hasNext()) {
082: FlowInfo next = (FlowInfo) it.next();
083:
084: if (getCurrent().equals(next))
085: break;
086: if (getCurrent().unit().equals(next.unit())
087: && (getCurrent().isBefore() == next.isBefore())) {
088: retInfo = next;
089:
090: }
091: }
092: return retInfo;
093: }
094:
095: public void addFlowInfo(Object fi) {
096: if (getFlowInfoList() == null) {
097: setFlowInfoList(new ArrayList());
098: }
099: getFlowInfoList().add(fi);
100: setCurrent((FlowInfo) fi);
101: }
102:
103: public boolean inMiddle() {
104: if (getFlowInfoList() == null)
105: return false;
106: if (getFlowInfoList().indexOf(getCurrent()) == getFlowInfoList()
107: .size() - 1)
108: return false;
109: return true;
110: }
111:
112: public boolean canGoBack() {
113: if (getFlowInfoList() == null)
114: return false;
115:
116: if (getFlowInfoList().size() == 0)
117: return false;
118:
119: if (getCurrent().equals(getFlowInfoList().get(0)))
120: return false;
121:
122: return true;
123: }
124:
125: public void stepForward() {
126: int index = getFlowInfoList().indexOf(getCurrent());
127: FlowInfo next = (FlowInfo) getFlowInfoList().get(index + 1);
128: getController().setEvent(
129: new InteractionEvent(IInteractionConstants.REPLACE,
130: next));
131: getController().handleEvent();
132: setCurrent(next);
133:
134: }
135:
136: public void stepForwardAuto() {
137: int index = getFlowInfoList().indexOf(getCurrent());
138: for (int i = index + 1; i < getFlowInfoList().size(); i++) {
139: FlowInfo next = (FlowInfo) getFlowInfoList().get(i);
140: getController().setEvent(
141: new InteractionEvent(IInteractionConstants.REPLACE,
142: next));
143: getController().handleEvent();
144: setCurrent(next);
145: }
146: }
147:
148: /**
149: * @return
150: */
151: public FlowInfo getCurrent() {
152: return current;
153: }
154:
155: /**
156: * @return
157: */
158: public List getFlowInfoList() {
159: return flowInfoList;
160: }
161:
162: /**
163: * @return
164: */
165: public int getRepeat() {
166: return repeat;
167: }
168:
169: /**
170: * @return
171: */
172: public int getTotal() {
173: return total;
174: }
175:
176: /**
177: * @param info
178: */
179: public void setCurrent(FlowInfo info) {
180: current = info;
181: }
182:
183: /**
184: * @param list
185: */
186: public void setFlowInfoList(List list) {
187: flowInfoList = list;
188: }
189:
190: /**
191: * @param i
192: */
193: public void setRepeat(int i) {
194: repeat = i;
195: }
196:
197: /**
198: * @param i
199: */
200: public void setTotal(int i) {
201: total = i;
202: }
203:
204: /**
205: * @return
206: */
207: public InteractionController getController() {
208: return controller;
209: }
210:
211: /**
212: * @param controller
213: */
214: public void setController(InteractionController controller) {
215: this.controller = controller;
216: }
217:
218: }
|