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.File;
27import java.io.FileWriter;
28import java.io.IOException;
29import java.io.PrintWriter;
30import java.util.Map;
31
32public class TreeValues {
33	static ValueMap tryMap;
34	static ValueMap failMap;
35
36	public TreeValues(){
37	}
38
39	public static TreeValues graph(Node node) {
40		TreeValues treeValues = new TreeValues();
41		tryMap = new ValueMap();
42		failMap = new ValueMap();
43
44		treeValues.recordNodes(node);
45		return treeValues;
46	}
47
48	public void recordNodes(Node node) {
49		recordNode(node);
50		if(node.getChildren().size() > 0){
51			for(Node child : node.getChildren()){
52				recordNodes(child);
53			}
54		}
55
56	}
57
58	public void recordNode(Node node){
59		switch (node.getType()){
60		case 	ROOT:
61			break;
62		case TRY:
63		case TRYC:
64			tryMap.increment(node.getName(),node.getValue());
65			break;
66		case FAIL:
67		case FAILC:
68			failMap.increment(node.getName(),node.getValue());
69			break;
70		case SUCC:
71		case SUCCC:
72			break;
73		default:
74				System.out.println("Unexpected node type");
75		}
76
77	}
78	public void plot(String directory,String name){
79		try{
80			File f = new File(directory,name);
81			PrintWriter out = new PrintWriter(new FileWriter(f));
82			out.println("Fail");
83			out.println("INTEGER");
84			for(Map.Entry<ValueKey, Integer> pair : failMap.getMap().entrySet()){
85				out.println(pair.getValue()+"  "+pair.getKey().getName()+" "+
86						pair.getKey().getValue());
87//				System.out.println(pair.getKey().getName()+" "+
88//						pair.getKey().getValue()+" "+pair.getValue());
89			}
90			out.close();
91		} catch (IOException e) {
92			System.out.println("no luck");
93		}
94	}
95}
96