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  CPViz Constraint Visualization System
15// The Initial Developer of the Original Code is  Helmut Simonis
16// Portions created by the Initial Developer are
17// Copyright (C) 2009-2010 Helmut Simonis
18//
19// Contributor(s): 	Helmut Simonis, 4C, Univerity College Cork, Cork
20//
21//
22// END LICENSE BLOCK
23// ----------------------------------------------------------------------
24package ie.ucc.cccc.viz;
25import java.io.*;
26import java.util.Map;
27
28
29
30
31/**
32 * Holds the information about the state of a visualizer at a particular
33 * point of execution
34 * @author hsimonis
35 *
36 */
37public class VisualState extends VizParent {
38	private VisualState parent=null;
39	private Visualizer visualizer;
40	private State state;
41	private VizFocus focus;
42	private VizFailed failed;
43
44	public VisualState(Visualizer visualizer,State state){
45		assert visualizer != null;
46		assert state != null;
47		this.visualizer = visualizer;
48		this.state = state;
49		for(VisualState old : state.getParent().getVisualStates()){
50			// this is slow, and should be changed
51			if (old.getVisualizer() == visualizer){
52				parent = old;
53			}
54		}
55//		if (parent == null){
56//			System.out.println("No VisualState parent");
57//		}
58	}
59
60	public FullDomain getDomainAsList(){
61		return new FullDomain();
62	}
63
64	public Visualizer getVisualizer() {
65		return visualizer;
66	}
67
68	public State getState() {
69		return state;
70	}
71
72	public VisualState getParent() {
73		return parent;
74	}
75
76	/**
77	 * find the corresponding entries in the parent for a FullDomain vector
78	 * and extract which values have been removed
79	 * @param vars the array of new values
80	 * @return an array of removed values, which were in the parents entry,
81	 * but which are no longer there
82	 */
83	public FullDomain[] getRemovedValues(FullDomain[] vars){
84		FullDomain[] res = new FullDomain[vars.length];
85		if (getParent() == null){
86			for(int i=0;i<vars.length;i++){
87				res[i] = new FullDomain();
88			}
89		} else {
90			FullDomain[] old = getParent().getEntries().asDomainArray();
91			for(int i=0;i<vars.length;i++){
92				// this is hacked to handle different indexStart values
93				if (vars[i]==null){
94					res[i] = new FullDomain();
95				} else {
96					res[i] = old[i].getRemovedValues(vars[i]);
97				}
98			}
99		}
100		return res;
101	}
102
103	/**
104	 * find the corresponding entries in the parent for a FullDomain vector
105	 * and extract which values have been removed
106	 * @param argument String, the argument name
107	 * @param vars the array of current values in the domain
108	 * @return an array of domains which hold the removed values
109	 */
110	public FullDomain[] getRemovedValues(String argument,FullDomain[] vars){
111		FullDomain[] res = new FullDomain[vars.length];
112		if (getParent() == null){
113			// no parent, this happens in the root node
114			for(int i=0;i<vars.length;i++){
115				res[i] = new FullDomain();
116			}
117		} else {
118			FullDomain[] old = getParent().argumentDomainArray(argument);
119			for(int i=0;i<vars.length;i++){
120				// this is hacked to handle different indexStart values
121				if (vars[i]== null){
122					res[i] = new FullDomain();
123				} else {
124					res[i] = old[i].getRemovedValues(vars[i]);
125				}
126			}
127		}
128		return res;
129	}
130
131	public FullDomain getRemovedValues(String argument,FullDomain var){
132		FullDomain res;
133		if (getParent() == null){
134			// no parent, this happens in the root node
135				res = new FullDomain();
136		} else {
137			FullDomain old = getParent().argumentDomain(argument);
138			res = old.getRemovedValues(var);
139		}
140		return res;
141	}
142
143	public FullDomainMap getRemovedValues(FullDomainMap vars){
144		FullDomainMap res = new FullDomainMap();
145		if (getParent() == null){
146		} else {
147			FullDomainMap old = getParent().getEntries().asDomainMap();
148			for(Map.Entry<Pair, FullDomain> entry : old.entrySet()){
149//				System.out.println("Entry "+entry+ " old "+ old);
150				res.put(entry.getKey(),
151						entry.getValue().getRemovedValues(vars.get(entry.getKey())));
152			}
153		}
154		return res;
155	}
156
157	public FullDomainMap getRemovedValues(String attribute,FullDomainMap vars){
158		FullDomainMap res = new FullDomainMap();
159		if (getParent() == null){
160//			System.out.println("Null parent");
161		} else {
162			FullDomainMap old = getParent().argument(attribute).asDomainMap();
163			for(Map.Entry<Pair, FullDomain> entry : old.entrySet()){
164//				System.out.println("Entry "+entry+ " old "+ old);
165				res.put(entry.getKey(),
166						entry.getValue().getRemovedValues(vars.get(entry.getKey())));
167			}
168		}
169		return res;
170	}
171
172
173	public VizFocus getFocus() {
174		return focus;
175	}
176
177	public VizFailed getFailed() {
178		return failed;
179	}
180
181	public void focus(VizFocus entry){
182		focus = entry;
183	}
184	public void failed(VizFailed entry){
185		failed = entry;
186	}
187
188
189	public void draw(PrintWriter out) {
190		visualizer.draw(out,this);
191	}
192	public void drawBox(PrintWriter out,Colors color) {
193		visualizer.drawBox(out,this,color);
194	}
195	public InvariantType invariant(PrintWriter out) {
196		return visualizer.invariant(out,this);
197	}
198}
199