19 package ca.uqac.lif.synthia.sequence;
21 import java.util.ArrayList;
22 import java.util.List;
62 protected List<BehaviorTree<T>> m_children;
69 protected int m_index;
77 m_children =
new ArrayList<BehaviorTree<T>>();
85 @SuppressWarnings(
"unchecked")
89 m_children =
new ArrayList<BehaviorTree<T>>(nodes.length);
102 @SuppressWarnings(
"unchecked")
136 while (t ==
null && m_index < m_children.size())
157 public Sequence<T>
duplicate(
boolean with_state)
159 Sequence<T> tn =
new Sequence<T>();
162 tn.m_children.add(child.duplicate(with_state));
166 tn.m_index = m_index;
178 protected List<ProbabilityChoice<T>> m_choices;
186 protected int m_chosenIndex;
201 m_choices =
new ArrayList<ProbabilityChoice<T>>();
203 m_floatPicker = float_picker;
216 for (ProbabilityChoice<T> pc : m_choices)
234 if (m_chosenIndex < 0)
236 float[] probs =
new float[m_choices.size()];
237 float total_prob = 0f;
238 for (
int i = 0; i < probs.length; i++)
240 total_prob += m_choices.get(i).getProbability();
241 probs[i] = total_prob;
243 float f = m_floatPicker.
pick();
245 while (index < probs.length)
247 if (f <= probs[index])
249 m_chosenIndex = index;
255 if (m_chosenIndex >= m_choices.size())
259 return m_choices.get(m_chosenIndex).getObject();
271 public Selector<T>
duplicate(
boolean with_state)
273 Selector<T> ch =
new Selector<T>(m_floatPicker.
duplicate(with_state));
274 for (ProbabilityChoice<T> pc : m_choices)
276 ch.m_choices.add(pc.duplicate(with_state));
280 ch.m_chosenIndex = m_chosenIndex;
295 m_choices.add(
new ProbabilityChoice<T>(node, probability));
305 public String toString()
307 StringBuilder out =
new StringBuilder();
308 int size = m_choices.size();
309 String beg =
"", end =
"";
316 for (
int i = 0; i < m_choices.size(); i++)
322 out.append(m_choices.get(i).toString());
325 return out.toString();
386 return m_picker.pick();
399 public Leaf<T>
duplicate(
boolean with_state)
401 return new Leaf<T>(m_picker.duplicate(with_state));
405 public String toString()
407 return m_picker.toString();