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
28public class VisualizerCumulativeCost extends VisualizerLayout {
29
30	public VisualizerCumulativeCost(VisualContext context){
31		super(context);
32	}
33
34	@Override
35	void draw(PrintWriter out, VisualState visualState) {
36		Tuple[] areas = visualState.argumentTupleArray("areas");
37		Tuple[] task = visualState.argumentTupleArray("tasks");
38		FullDomain limit = visualState.argumentDomain("limit");
39		FullDomain end = visualState.argumentDomain("end");
40//		FullDomain cost = visualState.argumentDomain("cost");
41
42		int n = task.length;
43		int m = areas.length;
44		setHeight(limit.getMax());
45		int w =end.getMax();
46		for(int i=1;i<n;i++) {
47			int startMax = task[i].getField("start").getMax();
48			int durMax = task[i].getField("dur").getMax();
49			w = Math.max(w,startMax+durMax);
50		}
51		setWidth(w);
52		standardGrid(out);
53		int costMax =0;
54		for(int j=1;j<m;j++){
55			int x = areas[j].getField("x").getMax();
56			int y = areas[j].getField("y").getMax();
57			int width = areas[j].getField("width").getMax();
58			int height = areas[j].getField("height").getMax();
59			int cj = areas[j].getField("cost").getMax();
60			costMax = Math.max(cj, costMax);
61//			System.out.println("X "+x+" Y "+y+" W "+width+" H "+height+" Cj "+ cj);
62			roundedRectSVG(out,posX(x),posY(y),width,height,Colors.COMPARE2_COLOR);
63			textSVG(out,posX(x),posY(y+height/2),cj,Colors.ASSIGNED_TEXT_COLOR);
64		}
65
66		int[] profile = CumulativeProfile.computeProfile(task,width());
67		int[] outerProfile = CumulativeProfile.computeOuterProfile(task,width());
68		int oldOuterProfile =0;
69		for(int i=0;i <width();i++){
70//			openRectSVG(out,posX(i),posY(0),1,profile[i],Colors.UNASSIGNED_COLOR);
71			int current = Math.min(outerProfile[i], limit.getMax());
72			lineSVG(out,posX(i),posY(current),posX(i)+1,posY(current),
73					Colors.TOO_HIGH_COLOR);
74			lineSVG(out,posX(i),posY(oldOuterProfile),posX(i),posY(current),
75					Colors.TOO_HIGH_COLOR);
76			openRectSVG(out,posX(i),posY(0),1,profile[i],Colors.UNASSIGNED_COLOR);
77			oldOuterProfile = current;
78		}
79		int prev=0;
80		for(int j=1;j<m;j++){
81			int x = areas[j].getField("x").getMax();
82			int width = areas[j].getField("width").getMax();
83			int cj = areas[j].getField("cost").getMax();
84			int costLine = cj*limit.getMax()/costMax;
85//			openRectSVG(out,posX(x),posY(0),width,costLine,Colors.FOCUS_COLOR,0.3);
86			lineSVG(out,posX(x),posY(prev),posX(x),posY(costLine),
87					Colors.FOCUS_COLOR,0.4);
88			lineSVG(out,posX(x),posY(costLine),posX(x+width),posY(costLine),
89					Colors.FOCUS_COLOR,0.4);
90			prev= costLine;
91		}
92		drawLimit(out,limit);
93		drawEnd(out,end);
94	}
95
96	@Override
97	public InvariantType invariant(PrintWriter out, VisualState visualState) {
98		Tuple[] task = visualState.argumentTupleArray("tasks");
99		FullDomain limit = visualState.argumentDomain("limit");
100		FullDomain end = visualState.argumentDomain("end");
101
102		int n = task.length;
103		int w =0;
104		int volume = 0;
105		int minStart = task[1].getField("start").getMin();
106		for(int i=1;i<n;i++) {
107			int startMax = task[i].getField("start").getMax();
108			int startMin = task[i].getField("start").getMin();
109			int durMax = task[i].getField("dur").getMax();
110			int durMin = task[i].getField("dur").getMin();
111			int resMin = task[i].getField("res").getMin();
112			w = Math.max(w,startMax+durMax);
113			volume += durMin*resMin;
114			minStart = Math.min(minStart, startMin);
115		}
116		int[] profile = CumulativeProfile.computeProfile(task,w);
117		InvariantType overall = InvariantType.TRUE;
118		for(int i=0;i <w;i++){
119			if (profile[i] > limit.getMax()) {
120				overall = overall.update(InvariantType.INCONSISTENT);
121			}
122			if (profile[i] > limit.getMin()) {
123				overall= overall.update(InvariantType.MISSING_PROPAGATION);
124			}
125		}
126		if (end != null){
127			if (end.getMax() < minStart+Math.ceil(volume/limit.getMax())) {
128				overall = overall.update(InvariantType.INCONSISTENT);
129			}
130			if (end.getMin() < minStart+Math.ceil(volume/limit.getMax())) {
131				overall = overall.update(InvariantType.MISSING_PROPAGATION);
132			}
133		}
134		return overall;
135	}
136
137
138}
139