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;
25
26import java.io.PrintWriter;
27
28/**
29 * Visualizer to show an element constraint
30 * @author hsimonis
31 *
32 */
33public class VisualizerElement extends VisualizerDrawer {
34
35	public VisualizerElement(VisualContext context){
36		super(context);
37	}
38
39	@Override
40	void draw(PrintWriter out, VisualState visualState) {
41		FullDomain var = visualState.argumentDomain("1");
42		int[] vars2 = visualState.argumentIntArray("2");
43		FullDomain cost = visualState.argumentDomain("3");
44		int n = vars2.length;
45		setMin(min(vars2));
46		setMax(max(vars2));
47		setWidth(n-1);
48		setHeight(max()-min()+1);
49
50		standardGrid(out);
51		// grid for X
52		gridSVG(out,leftX(),top2Y(),width(),1);
53		// grid for C
54		gridSVG(out,left2X(),topY(),1,height());
55		// labels marking X and C variables
56		textSVG(out,labelX(),top2Y(),
57				"X",Colors.LABEL_TEXT_COLOR);
58		textSVG(out,left2X(),labelY(),
59				"C",Colors.LABEL_TEXT_COLOR);
60		// draw cost list entries
61		for(int i=1;i<n;i++){
62			unitSquareSVG(out,posX(i),posY(vars2[i]),Colors.CONSTANT_COLOR);
63		}
64		// draw X domain
65		for(int value : var){
66			unitSquareSVG(out,posX(value),top2Y(),domainBasedColor(var));
67		}
68		// draw cost domain
69		for(int value : cost){
70			unitSquareSVG(out,left2X(),posY(value),domainBasedColor(cost));
71		}
72	}
73
74	private int min(int[] x){
75		int res=x[1];
76		for(int i=2;i<x.length;i++) {
77			res = Math.min(res,x[i]);
78		}
79		return res;
80	}
81
82	private int max(int[] x){
83		int res=x[1];
84		for(int i=2;i<x.length;i++) {
85			res = Math.max(res,x[i]);
86		}
87		return res;
88	}
89}
90
91