Synthia
Generic and flexible data structure generator
RandomSubList.java
Go to the documentation of this file.
1 package ca.uqac.lif.synthia.random;
2 
3 import ca.uqac.lif.synthia.Picker;
7 
8 import java.util.ArrayList;
9 import java.util.List;
10 
11 /**
12  * An equivalent of {@link RandomSubString} but for lists.
13  *
14  * @param <T> The type of the list used by the {@link Picker}.
15  * @ingroup API
16  */
17 public class RandomSubList<T> implements Shrinkable<List<T>>
18 {
19 
20  /**
21  * {@link RandomBoolean} used to determine if a element of the list will be added in the sublist
22  * or not.
23  */
24  protected RandomBoolean m_pick;
25 
26  /**
27  * If the element will be added in the sublist, this {@link RandomBoolean} is used to determine if
28  * the element will be added as is or be first passed to the {@link Shrinkable}
29  * {@link #m_listReducer}.
30  */
31  protected RandomBoolean m_asIs;
32 
33  /**
34  * {@link Shrinkable} to reduce elements when {@link #m_asIs} decides that the curent element
35  * will be reduced. If it's the case, this picker uses the {@link #shrink(List)} method,
36  * passes the element as parameter and uses the {@link Picker#pick()} method of the created
37  * picker to generated the element to add in the sublist.
38  */
40 
41  /**
42  * The list of elements used to generate sublists.
43  */
44  protected List<T> m_elements;
45 
46  /**
47  * Private constructor used in the {@link #duplicate(boolean)} method.
48  *
49  * @param pick The {@link #m_pick} attribute.
50  * @param as_is The {@link #m_asIs} attribute.
51  * @param elements The {@link #m_elements} attribute.
52  * @param list_reducer The {@link #m_listReducer} attribute.
53  */
54  private RandomSubList(RandomBoolean pick, RandomBoolean as_is, List<T> elements,
55  Shrinkable<T> list_reducer)
56  {
57  m_pick = pick;
58  m_asIs = as_is;
59  m_elements = elements;
60  m_listReducer = list_reducer;
61  }
62 
63  /**
64  * Public method to create a new instance of the class.
65  *
66  * @param elements The list of elements used to generate sublists.
67  * @param list_reducer The {@link Shrinkable} used to reduce elements of the original list.
68  */
69  public RandomSubList(List<T> elements, Shrinkable<T> list_reducer)
70  {
71  m_pick = new RandomBoolean();
72  m_asIs = new RandomBoolean();
73  m_elements = elements;
74  m_listReducer = list_reducer;
75  }
76 
77  @Override
78  public void reset()
79  {
80  m_pick.reset();
81  m_asIs.reset();
82  m_listReducer.reset();
83  }
84 
85  @Override
86  public List<T> pick()
87  {
88  List<T> output_list = new ArrayList<>();
89  for (T m_element : m_elements)
90  {
91  if (m_pick.pick())
92  {
93  if (m_asIs.pick())
94  {
95  output_list.add(m_element);
96  }
97  else
98  {
99  output_list.add(m_listReducer.shrink(m_element, new RandomFloat(), 1).pick());
100  }
101  }
102  }
103 
104  return output_list;
105  }
106 
107  @Override
108  public Picker<List<T>> duplicate(boolean with_state)
109  {
110  return new RandomSubList<T>(m_pick.duplicate(with_state), m_asIs.duplicate(with_state),
111  new ArrayList<T>(m_elements), (Shrinkable<T>) m_listReducer.duplicate(with_state));
112 
113  }
114 
115  /**
116  * Create a new {@link RandomSubList} picker based on a given list. If this list is
117  * empty, the method will return a {@link NothingPicker}. The new instance will also have the same
118  * internal states for the {@link #m_listReducer}, {@link #m_pick} and {@link #m_asIs} attributes
119  * as the original one.
120  *
121  * @param element The list used by the new {@link RandomSubList} picker to produce sublists.
122  * @return The new instance of the class or a {@link NothingPicker}.
123  * @warning Depending of the content of the list, it is possible that the returned object is a
124  * {@link NothingPicker}. If a {@link RandomSubList} is returned, it is also possible that some of
125  * the sublists generated by the {@link #pick()} method of this instance could generate
126  * {@link ca.uqac.lif.synthia.NoMoreElementException}'s.
127  */
128  @Override
129  public Shrinkable<List<T>> shrink(List<T> element, Picker<Float> decision, float magnitude)
130  {
131  if (!element.getClass().getSimpleName().equals("ArrayList"))
132  {
133  return new NothingPicker<List<T>>();
134  }
135  else
136  {
137  List<T> objects = (List<T>) element;
138 
139  if (objects.isEmpty())
140  {
141  return new NothingPicker<List<T>>();
142  }
143 
144  return new RandomSubList<T>(m_pick, m_asIs.duplicate(true), objects,
145  (Shrinkable<T>) m_listReducer.duplicate(true));
146  }
147  }
148 
149  @Override
150  public Shrinkable<List<T>> shrink(List<T> o)
151  {
152  return shrink(o, RandomFloat.instance, 1);
153  }
154 }
ca.uqac.lif.synthia.Picker
Picks an object.
Definition: Picker.java:36
ca.uqac.lif.synthia.string.RandomSubString
RandomPicker that produces random substrings from an original one.
Definition: RandomSubString.java:16
ca.uqac.lif.synthia.Shrinkable
Interface signaling that a picker can be shrunk.
Definition: Shrinkable.java:36
ca.uqac.lif.synthia.random.RandomSubList.m_pick
RandomBoolean m_pick
RandomBoolean used to determine if a element of the list will be added in the sublist or not.
Definition: RandomSubList.java:24
ca.uqac.lif.synthia.util
Miscellaneous pickers performing various functions.
Definition: ArrayPicker.java:19
ca.uqac.lif.synthia.random.RandomSubList.RandomSubList
RandomSubList(List< T > elements, Shrinkable< T > list_reducer)
Public method to create a new instance of the class.
Definition: RandomSubList.java:69
ca.uqac.lif.synthia.random.RandomSubList.m_listReducer
Shrinkable< T > m_listReducer
Shrinkable to reduce elements when m_asIs decides that the curent element will be reduced.
Definition: RandomSubList.java:39
ca.uqac.lif.synthia.random.RandomFloat.instance
static final transient RandomFloat instance
A public static instance of RandomFloat.
Definition: RandomFloat.java:45
ca.uqac
ca.uqac.lif.synthia.random.RandomSubList.reset
void reset()
Puts the picker back into its initial state.
Definition: RandomSubList.java:78
ca.uqac.lif.synthia.random.RandomSubList.m_elements
List< T > m_elements
The list of elements used to generate sublists.
Definition: RandomSubList.java:44
ca.uqac.lif.synthia
Definition: Bounded.java:19
ca.uqac.lif.synthia.string
Pickers producing and manipulating character strings.
Definition: AsString.java:19
ca.uqac.lif.synthia.random.RandomSubList.shrink
Shrinkable< List< T > > shrink(List< T > o)
Definition: RandomSubList.java:150
ca.uqac.lif.synthia.random.RandomBoolean.pick
Boolean pick()
Picks a random boolean.
Definition: RandomBoolean.java:93
ca.uqac.lif.synthia.random.RandomSubList.duplicate
Picker< List< T > > duplicate(boolean with_state)
Creates a copy of the picker.
Definition: RandomSubList.java:108
ca.uqac.lif
ca.uqac.lif.synthia.random.RandomSubList.m_asIs
RandomBoolean m_asIs
If the element will be added in the sublist, this RandomBoolean is used to determine if the element w...
Definition: RandomSubList.java:31
ca.uqac.lif.synthia.util.NothingPicker
A RelativePicker that only throws a NoMoreElementException when the pick() method is called.
Definition: NothingPicker.java:36
ca.uqac.lif.synthia.random.RandomPicker.reset
void reset()
Puts the picker back into its initial state.
Definition: RandomPicker.java:68
ca
ca.uqac.lif.synthia.random.RandomBoolean.duplicate
RandomBoolean duplicate(boolean with_state)
Creates a copy of the RandomBoolean picker.
Definition: RandomBoolean.java:108
ca.uqac.lif.synthia.random.RandomSubList.shrink
Shrinkable< List< T > > shrink(List< T > element, Picker< Float > decision, float magnitude)
Create a new RandomSubList picker based on a given list.
Definition: RandomSubList.java:129
ca.uqac.lif.synthia.random.RandomSubList.pick
List< T > pick()
Picks an object.
Definition: RandomSubList.java:86
ca.uqac.lif.synthia.random.RandomFloat
Picks a floating point number uniformly in an interval.
Definition: RandomFloat.java:30
ca.uqac.lif.synthia.random.RandomSubList
An equivalent of RandomSubString but for lists.
Definition: RandomSubList.java:17
ca.uqac.lif.synthia.random.RandomBoolean
Picks a Boolean value.
Definition: RandomBoolean.java:34