001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.util;
028:
029: import java.util.*;
030:
031: import junit.framework.TestCase;
032: import junit.framework.*;
033:
034: public class TestStackMachine extends TestCase {
035: private class MySM extends StackMachine {
036: private String seq = null; // we'll accumulate the states here (yuck)
037:
038: String getSeq() {
039: return seq;
040: }
041:
042: protected void _set(State s) {
043: if (seq == null) {
044: seq = s.getKey();
045: } else {
046: seq = seq + " " + s.getKey();
047: }
048: super ._set(s);
049: }
050: }
051:
052: public void test_sm1() {
053: MySM sm = new MySM();
054:
055: sm.add(new StackMachine.SState("A") {
056: public void invoke() {
057: setVar("i", new Integer(1));
058: call("X", getVar("i"), "B");
059: }
060: });
061: sm.add(new StackMachine.SState("B") {
062: public void invoke() {
063: Integer i = (Integer) getVar("i");
064: //System.err.println(""+i+"*"+i+" = "+getResult());
065: setVar("i", new Integer(1 + i.intValue()));
066: call("X", getVar("i"), "C");
067: }
068: });
069: sm.add(new StackMachine.SState("C") {
070: public void invoke() {
071: Integer i = (Integer) getVar("i");
072: //System.err.println(""+i+"*"+i+" = "+getResult());
073: setVar("i", new Integer(1 + i.intValue()));
074: call("X", getVar("i"), "D");
075: }
076: });
077: sm.add(new StackMachine.SState("D") {
078: public void invoke() {
079: Integer i = (Integer) getVar("i");
080: //System.err.println(""+i+"*"+i+" = "+getResult());
081: setVar("i", new Integer(1 + i.intValue()));
082: call("X", getVar("i"), "E");
083: }
084: });
085: sm.add(new StackMachine.SState("E") {
086: public void invoke() {
087: Integer i = (Integer) getVar("i");
088: //System.err.println(""+i+"*"+i+" = "+getResult());
089: transit("DONE");
090: }
091: });
092:
093: sm.add(new StackMachine.SState("X") {
094: public void invoke() {
095: int arg = ((Integer) getArgument()).intValue();
096: //System.err.println("In X("+arg+")");
097: callReturn(new Integer(arg * arg));
098: }
099: });
100:
101: // initialize to A
102: sm.reset("A");
103: sm.go();
104:
105: assertEquals("A X POP B X POP C X POP D X POP E DONE", sm
106: .getSeq());
107: }
108:
109: public void test_sm2() {
110: MySM sm = new MySM();
111:
112: sm.add(new StackMachine.SState("T1") {
113: public void invoke() {
114: setVar("Collection", new ArrayList());
115: ArrayList stuff = new ArrayList();
116: stuff.add("A");
117: stuff.add("B");
118: stuff.add("C");
119: stuff.add("D");
120: stuff.add("E");
121: iterate(stuff, "Sub", "T2");
122: }
123: });
124: sm.add(new StackMachine.SState("T2") {
125: public void invoke() {
126: //System.out.println("I collected: ");
127: for (Iterator it = ((Collection) getVar("Collection"))
128: .iterator(); it.hasNext();) {
129: it.next();
130: //System.out.println("\t"+it.next());
131: }
132: transit("DONE");
133: }
134: });
135: sm.add(new StackMachine.SState("Sub") {
136: public void invoke() {
137: String s = (String) getArgument();
138: s = "[" + s + "]";
139: // get the "Collection" var from two frames above (iterator counts).
140: Collection c = (Collection) getVar("Collection");
141: c.add(s);
142: callReturn(null);
143: }
144: });
145: sm.set("T1");
146: //System.out.println("Testing iterate");
147: sm.go();
148:
149: assertEquals("T1 ITERATE ITERATE1 " + "Sub POP ITERATE1 "
150: + "Sub POP ITERATE1 " + "Sub POP ITERATE1 "
151: + "Sub POP ITERATE1 " + "Sub POP ITERATE1 "
152: + "POP T2 DONE", sm.getSeq());
153: }
154: }
|