Synthia
Generic and flexible data structure generator
ComposeList.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.collection;
20 
21 import java.util.ArrayList;
22 import java.util.List;
23 
24 import ca.uqac.lif.petitpoucet.AndNode;
25 import ca.uqac.lif.petitpoucet.NodeFactory;
26 import ca.uqac.lif.petitpoucet.Part;
27 import ca.uqac.lif.petitpoucet.PartNode;
28 import ca.uqac.lif.petitpoucet.function.ExplanationQueryable;
29 import ca.uqac.lif.petitpoucet.function.vector.NthElement;
31 import ca.uqac.lif.synthia.Picker;
36 
37 /**
38  *
39  * @author Sylvain Hallé
40  *
41  * @param <T>
42  * @ingroup API
43  */
44 public class ComposeList<T> implements Picker<List<T>>, Shrinkable<List<T>>, ExplanationQueryable
45 {
46  /**
47  * The picker providing elements for the list.
48  */
49  /*@ non_null @*/ protected Picker<T> m_elements;
50 
51  /**
52  * The picker deciding on the length of the list.
53  */
54  /*@ non_null @*/ protected Picker<Integer> m_length;
55 
56  /**
57  * A list keeping track of the length of each list produced by the picker.
58  * This list is used to answer calls to
59  * {@link #getExplanation(Part, NodeFactory) getExplanation()}.
60  */
61  /*@ non_null @*/ protected List<Integer> m_lengths;
62 
63  /**
64  * Creates a new instance of the picker.
65  * @param elements The picker providing elements for the list
66  * @param length The picker deciding on the length of the list
67  */
68  public ComposeList(Picker<T> elements, Picker<Integer> length)
69  {
70  super();
71  m_elements = elements;
72  m_length = length;
73  m_lengths = new ArrayList<Integer>();
74  }
75 
76  /**
77  * Creates a new instance of the picker.
78  * @param elements The picker providing elements for the list
79  * @param length The fixed length of each list
80  */
81  public ComposeList(Picker<T> elements, int length)
82  {
83  this(elements, new Constant<Integer>(length));
84  }
85 
86  @Override
87  public void reset()
88  {
89  m_length.reset();
90  m_elements.reset();
91  }
92 
93  @Override
94  public List<T> pick()
95  {
96  int len = m_length.pick();
97  m_lengths.add(len);
98  List<T> list = new ComparableList<T>();
99  for (int i = 0; i < len; i++)
100  {
101  list.add(m_elements.pick());
102  }
103  return list;
104  }
105 
106  @Override
107  public ComposeList<T> duplicate(boolean with_state)
108  {
109  ComposeList<T> cl = new ComposeList<T>(m_elements.duplicate(with_state), m_length.duplicate(with_state));
110  if (with_state)
111  {
112  cl.m_lengths.addAll(m_lengths);
113  }
114  return cl;
115  }
116 
117  @Override
118  public PartNode getExplanation(Part p)
119  {
120  return getExplanation(p, NodeFactory.getFactory());
121  }
122 
123  @Override
124  public String toString()
125  {
126  return "ComposeList";
127  }
128 
129  @Override
130  public PartNode getExplanation(Part p, NodeFactory f)
131  {
132  PartNode root = f.getPartNode(p, this);
133  AndNode and = f.getAndNode();
134  root.addChild(and);
135  int index = NthSuccessiveOutput.mentionedOutput(p), part_index = NthElement.mentionedElement(p);
136  if (index < 0 || index >= m_lengths.size())
137  {
138  return null; // Invalid part
139  }
140  int offset = 0;
141  for (int i = 0; i < index; i++)
142  {
143  offset += m_lengths.size();
144  }
145  if (part_index < 0)
146  {
147  // p points at the whole list
148  for (int i = 0; i < m_lengths.get(index); i++)
149  {
150  Part new_p = NthSuccessiveOutput.replaceOutIndexBy(p, offset + i);
151  and.addChild(f.getPartNode(new_p, m_elements));
152  }
153  return root;
154  }
155  // p points at an element of the list
156  if (part_index >= m_lengths.get(index))
157  {
158  // Nonexistent element
159  return root;
160  }
161  Part new_p = NthSuccessiveOutput.removeNthElement(p);
162  new_p = NthSuccessiveOutput.replaceOutIndexBy(new_p, offset + part_index);
163  and.addChild(f.getPartNode(new_p, m_elements));
164  return root;
165  }
166 
167  @Override
168  public Shrinkable<List<T>> shrink(List<T> o, Picker<Float> decision, float magnitude)
169  {
170  if (!(m_elements instanceof Shrinkable) || !(m_length instanceof Shrinkable))
171  {
172  throw new CannotShrinkException("Inner pickers are not shrinkable");
173  }
175  }
176 
177  @Override
178  public Shrinkable<List<T>> shrink(List<T> o)
179  {
180  return shrink(o, RandomFloat.instance, 1);
181  }
182 }
ca.uqac.lif.synthia.Picker
Picks an object.
Definition: Picker.java:36
ca.uqac.lif.synthia.collection.ComposeList.pick
List< T > pick()
Picks an object.
Definition: ComposeList.java:94
ca.uqac.lif.synthia.Shrinkable
Interface signaling that a picker can be shrunk.
Definition: Shrinkable.java:36
ca.uqac.lif.synthia.explanation.NthSuccessiveOutput.replaceOutIndexBy
static Part replaceOutIndexBy(Part from, int index)
Given an arbitrary designator, replaces the first occurrence of NthOutput by an instance of NthInput ...
Definition: NthSuccessiveOutput.java:100
ca.uqac.lif.synthia.util
Miscellaneous pickers performing various functions.
Definition: ArrayPicker.java:19
ca.uqac.lif.synthia.collection.ComposeList.m_lengths
List< Integer > m_lengths
A list keeping track of the length of each list produced by the picker.
Definition: ComposeList.java:61
ca.uqac.lif.synthia.collection.ComparableList
Class that extends Java's ArrayList class to implements the Comparable interface.
Definition: ComparableList.java:31
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.collection.ComposeList.shrink
Shrinkable< List< T > > shrink(List< T > o, Picker< Float > decision, float magnitude)
Definition: ComposeList.java:168
ca.uqac.lif.synthia.random
Pickers that produce pseudo-random objects such as numbers.
Definition: AffineTransform.java:19
ca.uqac.lif.synthia.collection.ComposeList.reset
void reset()
Puts the picker back into its initial state.
Definition: ComposeList.java:87
ca.uqac.lif.synthia
Definition: Bounded.java:19
ca.uqac.lif.synthia.util.Constant
Picker that returns the same object every time.
Definition: Constant.java:37
ca.uqac.lif.synthia.collection.ComposeList.m_elements
Picker< T > m_elements
The picker providing elements for the list.
Definition: ComposeList.java:49
ca.uqac.lif.synthia.collection.ComposeList.m_length
Picker< Integer > m_length
The picker deciding on the length of the list.
Definition: ComposeList.java:54
ca.uqac.lif.synthia.collection.ComposeList.duplicate
ComposeList< T > duplicate(boolean with_state)
Creates a copy of the picker.
Definition: ComposeList.java:107
ca.uqac.lif.synthia.explanation
Objects related to the explanation of results produced by pickers.
Definition: Explanation.java:19
ca.uqac.lif
ca.uqac.lif.synthia.explanation.NthSuccessiveOutput.removeNthElement
static Part removeNthElement(Part from)
Removes the instance of NthElement that stands just before the first instance of NthSuccessiveOutput ...
Definition: NthSuccessiveOutput.java:171
ca
ca.uqac.lif.synthia.Picker.pick
T pick()
Picks an object.
ca.uqac.lif.synthia.collection.ComposeList.ComposeList
ComposeList(Picker< T > elements, int length)
Creates a new instance of the picker.
Definition: ComposeList.java:81
ca.uqac.lif.synthia.collection.ComposeList.getExplanation
PartNode getExplanation(Part p, NodeFactory f)
Definition: ComposeList.java:130
ca.uqac.lif.synthia.collection.ComposeList.toString
String toString()
Definition: ComposeList.java:124
ca.uqac.lif.synthia.random.RandomFloat
Picks a floating point number uniformly in an interval.
Definition: RandomFloat.java:30
ca.uqac.lif.synthia.explanation.NthSuccessiveOutput.mentionedOutput
static int mentionedOutput(Part d)
Retrieves the output sequence index mentioned in a designator.
Definition: NthSuccessiveOutput.java:144
ca.uqac.lif.synthia.collection.ComposeList
Definition: ComposeList.java:44
ca.uqac.lif.synthia.collection.ComposeList.getExplanation
PartNode getExplanation(Part p)
Definition: ComposeList.java:118
ca.uqac.lif.synthia.collection.ComposeList.shrink
Shrinkable< List< T > > shrink(List< T > o)
Definition: ComposeList.java:178
ca.uqac.lif.synthia.collection.ComposeList.ComposeList
ComposeList(Picker< T > elements, Picker< Integer > length)
Creates a new instance of the picker.
Definition: ComposeList.java:68
ca.uqac.lif.synthia.Picker.duplicate
Picker< T > duplicate(boolean with_state)
Creates a copy of the picker.
ca.uqac.lif.synthia.Picker.reset
void reset()
Puts the picker back into its initial state.
ca.uqac.lif.synthia.collection.ComposeShrunkList
Given a reference list, picks lists that are smaller.
Definition: ComposeShrunkList.java:43
ca.uqac.lif.synthia.CannotShrinkException
An exception to throw when a picker can't pick an other element.
Definition: CannotShrinkException.java:25
ca.uqac.lif.synthia.explanation.NthSuccessiveOutput
A Part pointing to the n-th output produced by a picker since its last call to reset().
Definition: NthSuccessiveOutput.java:36