Synthia
Generic and flexible data structure generator
TreePicker.java
Go to the documentation of this file.
1 /*
2  Synthia, a data structure generator
3  Copyright (C) 2019-2021 Laboratoire d'informatique formelle
4  Université du Québec à Chicoutimi, Canada
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published
8  by the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 package ca.uqac.lif.synthia.tree;
20 
21 import java.util.ArrayList;
22 import java.util.List;
23 
24 import ca.uqac.lif.synthia.Picker;
25 
26 /**
27  * Generates a tree of labeled nodes.
28  * @author Sylvain Hallé
29  *
30  * @param <T> The type of the node's labels
31  * @ingroup API
32  */
33 public class TreePicker<T> implements Picker<Node<T>>
34 {
36 
38 
39  protected Picker<Node<T>> m_node;
40 
42 
43  public TreePicker(Picker<Node<T>> node, Picker<Integer> total_height, Picker<Integer> degree, Picker<Float> child)
44  {
45  super();
46  m_totalHeight = total_height;
47  m_child = child;
48  m_node = node;
49  m_degree = degree;
50  }
51 
52  @Override
53  public void reset()
54  {
56  m_node.reset();
57  m_degree.reset();
58  }
59 
60  @Override
61  public Node<T> pick()
62  {
63  Node<T> root = m_node.pick();
64  int target_height = m_totalHeight.pick();
65  System.out.println("Height " + target_height);
66  List<Node<T>> list = new ArrayList<Node<T>>(1);
67  list.add(root);
68  populate(list, 0, target_height - 1, 1);
69  return root;
70  }
71 
72  protected void populate(List<Node<T>> list, int child_index, int target_height, int current_height)
73  {
74  if (list.isEmpty() || target_height == 0)
75  {
76  return;
77  }
78  for (int i = 0; i < list.size(); i++)
79  {
80  Node<T> node = list.get(i);
81  if (i == child_index)
82  {
83  int degree = 0;
84  do
85  {
86  degree = m_degree.pick();
87  } while (degree == 0);
88  int new_child_index = (int) Math.floor(m_child.pick() * (float) degree);
89  for (int j = 0; j < degree; j++)
90  {
91  node.m_children.add(m_node.pick());
92  }
93  populate(node.m_children, new_child_index, target_height - 1, current_height + 1);
94  }
95  else
96  {
97  int degree = m_degree.pick();
98  int new_child_index = (int) Math.floor(m_child.pick() * (float) degree);
99  int new_height = Math.min(target_height, Math.max(0, m_totalHeight.pick() - current_height));
100  for (int j = 0; j < degree; j++)
101  {
102  node.m_children.add(m_node.pick());
103  }
104  populate(node.m_children, new_child_index, new_height, current_height + 1);
105  }
106  }
107 
108  }
109 
110  @Override
111  public Picker<Node<T>> duplicate(boolean with_state)
112  {
113  // TODO Auto-generated method stub
114  return null;
115  }
116 
117 }
ca.uqac.lif.synthia.Picker
Picks an object.
Definition: Picker.java:36
ca.uqac.lif.synthia.tree.TreePicker.duplicate
Picker< Node< T > > duplicate(boolean with_state)
Creates a copy of the picker.
Definition: TreePicker.java:111
ca.uqac.lif.synthia.tree.TreePicker.m_child
Picker< Float > m_child
Definition: TreePicker.java:37
ca.uqac.lif.synthia.tree.TreePicker.pick
Node< T > pick()
Picks an object.
Definition: TreePicker.java:61
ca.uqac.lif.synthia.tree.TreePicker.m_node
Picker< Node< T > > m_node
Definition: TreePicker.java:39
ca.uqac.lif.synthia.tree.TreePicker.TreePicker
TreePicker(Picker< Node< T >> node, Picker< Integer > total_height, Picker< Integer > degree, Picker< Float > child)
Definition: TreePicker.java:43
ca.uqac
ca.uqac.lif.synthia
Definition: Bounded.java:19
ca.uqac.lif.synthia.tree.TreePicker.reset
void reset()
Puts the picker back into its initial state.
Definition: TreePicker.java:53
ca.uqac.lif.synthia.tree.TreePicker.m_totalHeight
Picker< Integer > m_totalHeight
Definition: TreePicker.java:35
ca.uqac.lif
ca.uqac.lif.synthia.tree.TreePicker.populate
void populate(List< Node< T >> list, int child_index, int target_height, int current_height)
Definition: TreePicker.java:72
ca
ca.uqac.lif.synthia.Picker.pick
T pick()
Picks an object.
ca.uqac.lif.synthia.tree.Node.m_children
List< Node< T > > m_children
The children of this node.
Definition: Node.java:41
ca.uqac.lif.synthia.tree.Node
Simple implementation of a labeled nodel.
Definition: Node.java:31
ca.uqac.lif.synthia.tree.TreePicker.m_degree
Picker< Integer > m_degree
Definition: TreePicker.java:41
ca.uqac.lif.synthia.Picker.reset
void reset()
Puts the picker back into its initial state.
ca.uqac.lif.synthia.tree.TreePicker
Generates a tree of labeled nodes.
Definition: TreePicker.java:33