• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /barrelfish-2018-10-04/usr/eclipseclp/Visualisation/src/com/parctechnologies/eclipse/visualisation/
1// BEGIN LICENSE BLOCK
2// Version: CMPL 1.1
3//
4// The contents of this file are subject to the Cisco-style Mozilla Public
5// License Version 1.1 (the "License"); you may not use this file except
6// in compliance with the License.  You may obtain a copy of the License
7// at www.eclipse-clp.org/license.
8//
9// Software distributed under the License is distributed on an "AS IS"
10// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See
11// the License for the specific language governing rights and limitations
12// under the License.
13//
14// The Original Code is  The ECLiPSe Constraint Logic Programming System.
15// The Initial Developer of the Original Code is  Cisco Systems, Inc.
16// Portions created by the Initial Developer are
17// Copyright (C) 2006 Cisco Systems, Inc.  All Rights Reserved.
18//
19// Contributor(s):
20//
21// END LICENSE BLOCK
22
23package com.parctechnologies.eclipse.visualisation;
24
25import java.util.Collection;
26import java.util.Iterator;
27import java.util.Map;
28import java.util.HashMap;
29import javax.swing.SwingUtilities;
30/**
31 * This class represents the current state of a scenario playback
32 *
33 *
34 */
35public class ScenarioIterator {
36
37    /** The model for this iterator */
38    private Scenario scenario;
39
40    /** The index of the next event to play in the scenario */
41    private int index;
42
43    /**
44     * Unification map, holding the mappings between scenario SymRefs and
45     * the SymRefs used in the current 'playing'
46     */
47    private Map map;
48
49    /**
50     * Create an iterator using the given Scenario as a model but with all
51     * symbolic references re-written to use
52     */
53    public ScenarioIterator(Scenario scenario) {
54	this.scenario = scenario ;
55	this.index = 0;
56	this.map = new HashMap();
57    }
58
59    /**
60     * Returns the current EventCommandList record
61     */
62    protected Scenario.EventCommandList getEventCommandList() {
63	return (Scenario.EventCommandList)scenario.getEventList().get(index);
64    }
65
66    /**
67     * Returns the commands for the current EventCommandList record
68     */
69    protected Collection getCommandList(int state) {
70	Scenario.EventCommandList eventCommandList = getEventCommandList();
71	return eventCommandList.getCommandList(state);
72    }
73
74    /**
75     * Does the given event match what happened next in the scenario
76     *
77     */
78    public boolean matches(VisEvent event) {
79	VisEvent peeked = peekEvent();
80	if (peeked == null) {
81	    return false;
82	}
83	if (event.unifies(peeked, map)) {
84	    return true;
85	}
86	return false;
87    }
88
89    /**
90     * Returns the next expected event.  This does NOT advance to the next one.
91     *
92     */
93    public VisEvent peekEvent() {
94	if (index >= scenario.getEventList().size()) {
95	    return null;
96	}
97	Scenario.EventCommandList eventCommandList =(Scenario.EventCommandList)
98	    scenario.getEventList().get(index);
99	return eventCommandList.getVisEvent();
100    }
101
102
103
104    /**
105     * Play the events associated with the current event, given the
106     * current state.
107     */
108    public void play(int state) {
109        if (DebuggingSupport.logMessages) {
110            DebuggingSupport.logMessage(this,
111                                        "\n\niterator state = " + state +
112                                        "scenario =" + this);
113        }
114	for(Iterator it = getCommandList(state).iterator(); it.hasNext(); ) {
115	    Command command = (Command)it.next();
116
117	    if (DebuggingSupport.logMessages) {
118		DebuggingSupport.logMessage(this,
119					    "iterator playing command="+
120					    command);
121	    }
122
123            if (command instanceof ImmediateCommand) {
124                // ImmediateCommands must not be exectuted on the
125                // current thread, not the Swing Thread
126                command.issue();
127            } else {
128                SwingUtilities.invokeLater(new IssueLater(command));
129            }
130	}
131
132	if (DebuggingSupport.logMessages) {
133	    DebuggingSupport.logMessage(this, "iterator play, state="+state);
134	}
135
136	if ( state == VisClientStateModel.NO_CURRENT_EVENT ) {
137
138	    if (DebuggingSupport.logMessages) {
139		DebuggingSupport.logMessage(this,
140					    "iterator play, event_is_finished");
141	    }
142
143	    // We have issued all the commands for this event
144	    index++;
145	}
146    }
147
148    /**
149     * Returns a string representing the contents of the scenario
150     * iterator.
151     */
152    public String toString() {
153        return
154            "ScenarioIterator index=" + index +
155            " scenario = " + scenario;
156    }
157
158    public static class IssueLater implements Runnable {
159	private Command command ;
160	public IssueLater(Command command) {
161	    this.command = command ;
162	}
163
164	public void run() {
165	    command.issue();
166	}
167    }
168
169}
170
171