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.util.*;
26
27/**
28 * Data structure to hold the parsed tree of search tree nodes.
29 * Allow for mapping from integer ids to the nodes
30 * @author hsimonis
31 *
32 */
33public class Tree {
34	private Map<Integer,Node> map;
35	private int nrNodes;
36
37	public Tree() {
38		map = new HashMap<Integer,Node>();
39		nrNodes = 0;
40	}
41
42	public Map<Integer,Node> getMap() {
43		return map;
44	}
45
46	public int getNrNodes() {
47		return nrNodes;
48	}
49
50	public Node getRootNode() {
51		return getMap().get(0);
52	}
53
54	public void addNode(NodeType type,int id,int parent,String name,int size,int value) {
55		Node node = new Node(type,id,parent,name,size,value);
56//		System.out.println("addNode "+node);
57		map.put(id,node);
58		nrNodes ++;
59
60	}
61
62	public void addNode(NodeType type,int id,int parent,String name,int size,String choice) {
63		Node node = new Node(type,id,parent,name,size,choice);
64//		System.out.println("addNode "+node);
65		map.put(id,node);
66		nrNodes ++;
67
68	}
69
70	public void addNode(NodeType type,int id) {
71		Node node = new Node(type,id);
72//		System.out.println("addNode "+node);
73		map.put(id,node);
74		nrNodes++;
75
76	}
77
78	public void markSucc(int id) {
79//		System.out.println("addNode "+type + " id " + id);
80		Node node = map.get(id);
81		if (node.getType()== NodeType.TRY) {
82			node.setType(NodeType.SUCC);
83		} else if (node.getType()== NodeType.TRYC) {
84			node.setType(NodeType.SUCCC);
85		} else {
86			System.out.println("Wrong node type, TRY or TRY expected, not "+node.getType());
87		}
88
89	}
90
91	/**
92	 * when all nodes have been added, create the children list in all nodes
93	 * process the nodes in the original order, so that the children list is
94	 * sorted the right way
95	 */
96	public void buildParentLists() {
97		// note the strict < in test
98		for(int i=0; i<nrNodes;i++) {
99			addToParent(i);
100		}
101		// we can now count decendents
102		map.get(0).count();
103	}
104
105	/**
106	 * utility method for integer keys instead of nodes
107	 * @param i
108	 */
109	public void addToParent(int i){
110		addToParent(map.get(i));
111
112	}
113
114	/**
115	 * add a node to the children list of the parent
116	 * @param node Node
117	 */
118	public void addToParent(Node node) {
119		if (node.getParent() >= 0) {
120//			System.out.println("Node "+node.getId()+" Parent "+node.getParent());
121			map.get(node.getParent()).addChild(node);
122		}
123	}
124
125	/**
126	 * find the node number of the parent of a node
127	 * @param nr integer node number
128	 * @return int node number of parent
129	 */
130	public int findParent(int nr){
131		if (nr == -1) {
132			return -1;
133		} else {
134			Node current = map.get(nr);
135			return current.getParent();
136		}
137	}
138
139	/**
140	 * check is tree with the given number is not a fail node (FAIL or FAILC)
141	 * @param nr int
142	 * @return boolean True if node is not a fail node
143	 */
144	public boolean isNotFailState(int nr) {
145		return (map.get(nr).getType() != NodeType.FAIL) && (map.get(nr).getType() != NodeType.FAILC);
146	}
147}
148
149
150
151