Synthia
Generic and flexible data structure generator
Tick.java
Go to the documentation of this file.
1 /*
2  Synthia, a data structure generator
3  Copyright (C) 2019-2020 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.util;
20 
21 import ca.uqac.lif.petitpoucet.AndNode;
22 import ca.uqac.lif.petitpoucet.NodeFactory;
23 import ca.uqac.lif.petitpoucet.Part;
24 import ca.uqac.lif.petitpoucet.PartNode;
25 import ca.uqac.lif.petitpoucet.function.ExplanationQueryable;
26 import ca.uqac.lif.synthia.Picker;
29 
30 /**
31  * Generates a sequence of monotonically increasing numerical values.
32  * The <tt>Tick</tt> picker requires two parameters:
33  * <ul>
34  * <li>A numerical picker indicating how the first value should be
35  * produced</li>
36  * <li>Another numerical picker indicating by how much to increment the
37  * current value to produce the next one</li>
38  * </ul>
39  * Consider the following piece of code:
40  * <pre>
41  * Tick t1 = new Tick(10, 0.5);
42  * Tick t2 = new Tick(new RandomInteger(8, 12), new RandomIntervalFloat(0.5, 1.5));
43  * </pre>
44  * The first instruction creates a <tt>Tick</tt> picker whose initial value is
45  * 10, and each subsequent value is incremented by exactly 0.5. The second
46  * instruction creates a <tt>Tick</tt> picker whose initial value is picked in
47  * the interval [8,12), and where each subsequent value is incremented by a
48  * number randomly chosen between 0.5 and 1.5.
49  * @ingroup API
50  */
51 public class Tick implements Picker<Number>, ExplanationQueryable
52 {
53  /**
54  * Picker that determines the start value
55  */
57 
58  /**
59  * Variable holding the current value of the tick
60  */
61  protected double m_currentValue;
62 
63  /**
64  * Picker that determines the increment for each
65  * subsequent value
66  */
68 
69  /**
70  * A flag indicating whether the tick has been asked for a value yet.
71  */
72  boolean m_first;
73 
74  /**
75  * Creates a new Tick picker.
76  * @param start A picker that determines the start value
77  * @param increment A picker that determines the increment for each
78  * subsequent value
79  */
81  {
82  super();
83  m_startValue = start;
84  m_increment = increment;
85  m_currentValue = m_startValue.pick().floatValue();
86  m_first = true;
87  }
88 
89  /**
90  * Creates a new Tick picker.
91  * @param start The start value
92  * @param increment The increment added to each subsequent value
93  */
94  public Tick(Number start, Number increment)
95  {
96  this(new Constant<>(start), new Constant<>(increment));
97  }
98 
99  /**
100  * Creates a new Tick picker.
101  * @param start The start value
102  * @param increment A picker that determines the increment for each
103  * subsequent value
104  */
105  public Tick(Number start, Picker<? extends Number> increment)
106  {
107  this(new Constant<>(start), increment);
108  }
109 
110  /**
111  * Creates a new Tick picker starting at value 0.
112  * @param increment A picker that determines the increment for each
113  * subsequent value
114  */
115  public Tick(Picker<? extends Number> increment)
116  {
117  this(new Constant<Number>(0), increment);
118  }
119 
120  /**
121  * Creates a new Tick picker starting at value 0, and where each subsequent
122  * value is incremented by a number uniformly chosen in the interval [0,1].
123  */
124  public Tick()
125  {
126  this(new Constant<Number>(0), new RandomFloat());
127  }
128 
129  /**
130  * Puts the tick back into its initial state. This means that the
131  * sequence of calls to {@link #pick()} will produce the same values
132  * as when the object was instantiated.
133  */
134  @Override
135  public void reset()
136  {
138  m_increment.reset();
139  m_currentValue = m_startValue.pick().floatValue();
140  m_first = true;
141  }
142 
143 
144  /**
145  * Picks a tick value. Typically, this method is expected to return non-null
146  * objects; a <tt>null</tt> return value is used to signal that no more
147  * objects will be produced. That is, once this method returns
148  * <tt>null</tt>, it should normally return <tt>null</tt> on all subsequent
149  * calls.
150  * @return The tick value.
151  */
152  @Override
153  public Number pick()
154  {
155  if (m_first)
156  {
157  m_first = false;
158  return m_currentValue;
159  }
160  m_currentValue += m_increment.pick().doubleValue();
161  return m_currentValue;
162  }
163 
164 
165  /**
166  * Creates a copy of the tick picker.
167  * @param with_state If set to <tt>false</tt>, the returned copy is set to
168  * the class' initial state (i.e. same thing as calling the picker's
169  * constructor). If set to <tt>true</tt>, the returned copy is put into the
170  * same internal state as the object it is copied from.
171  * @return The copy of the tick picker
172  */
173  @Override
174  public Tick duplicate(boolean with_state)
175  {
176  Tick tp = new Tick(m_startValue, m_increment);
177  if (with_state)
178  {
180  tp.m_first = m_first;
181  }
182  return tp;
183  }
184 
185  /*@ pure non_null @*/ public Tick duplicate(float start_value)
186  {
187  return new Tick(start_value, m_increment);
188  }
189 
190  /**
191  * Sets the current value of the tick picker
192  * @param value The new current value
193  * @return The actual instance of the tick picker
194  */
195  /*@ non_null @*/ public Tick setValue(float value)
196  {
197  m_currentValue = value;
198  m_startValue = new Constant<>(value);
199  return this;
200  }
201 
202  @Override
203  public String toString()
204  {
205  return "Tick(" + m_startValue + "," + m_increment + ")";
206  }
207 
208  @Override
209  public PartNode getExplanation(Part p)
210  {
211  return getExplanation(p, NodeFactory.getFactory());
212  }
213 
214  @Override
215  public PartNode getExplanation(Part p, NodeFactory f)
216  {
217  PartNode root = f.getPartNode(p, this);
218  int index = NthSuccessiveOutput.mentionedOutput(p);
219  if (index < 0)
220  {
221  return root;
222  }
223  if (index == 0)
224  {
225  Part new_p = NthSuccessiveOutput.replaceOutIndexBy(p, 0);
226  root.addChild(f.getPartNode(new_p, m_startValue));
227  return root;
228  }
229  AndNode and = f.getAndNode();
230  {
231  Part new_p = NthSuccessiveOutput.replaceOutIndexBy(p, index - 1);
232  and.addChild(f.getPartNode(new_p, this));
233  }
234  {
235  Part new_p = NthSuccessiveOutput.replaceOutIndexBy(p, index - 1);
236  and.addChild(f.getPartNode(new_p, m_increment));
237  }
238  root.addChild(and);
239  return root;
240  }
241 }
ca.uqac.lif.synthia.Picker
Picks an object.
Definition: Picker.java:36
ca.uqac.lif.synthia.util.Tick.duplicate
Tick duplicate(float start_value)
Definition: Tick.java:185
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.Tick.pick
Number pick()
Picks a tick value.
Definition: Tick.java:153
ca.uqac.lif.synthia.util.Tick.getExplanation
PartNode getExplanation(Part p, NodeFactory f)
Definition: Tick.java:215
ca.uqac.lif.synthia.util.Tick.Tick
Tick(Picker<? extends Number > increment)
Creates a new Tick picker starting at value 0.
Definition: Tick.java:115
ca.uqac
ca.uqac.lif.synthia.util.Tick.m_startValue
Picker<? extends Number > m_startValue
Picker that determines the start value.
Definition: Tick.java:56
ca.uqac.lif.synthia.random
Pickers that produce pseudo-random objects such as numbers.
Definition: AffineTransform.java:19
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.util.Tick.m_increment
Picker<? extends Number > m_increment
Picker that determines the increment for each subsequent value.
Definition: Tick.java:67
ca.uqac.lif.synthia.util.Tick.Tick
Tick()
Creates a new Tick picker starting at value 0, and where each subsequent value is incremented by a nu...
Definition: Tick.java:124
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.util.Tick
Generates a sequence of monotonically increasing numerical values.
Definition: Tick.java:51
ca
ca.uqac.lif.synthia.Picker.pick
T pick()
Picks an object.
ca.uqac.lif.synthia.util.Tick.toString
String toString()
Definition: Tick.java:203
ca.uqac.lif.synthia.util.Tick.Tick
Tick(Number start, Picker<? extends Number > increment)
Creates a new Tick picker.
Definition: Tick.java:105
ca.uqac.lif.synthia.util.Tick.reset
void reset()
Puts the tick back into its initial state.
Definition: Tick.java:135
ca.uqac.lif.synthia.util.Tick.Tick
Tick(Picker<? extends Number > start, Picker<? extends Number > increment)
Creates a new Tick picker.
Definition: Tick.java:80
ca.uqac.lif.synthia.util.Tick.Tick
Tick(Number start, Number increment)
Creates a new Tick picker.
Definition: Tick.java:94
ca.uqac.lif.synthia.util.Tick.m_currentValue
double m_currentValue
Variable holding the current value of the tick.
Definition: Tick.java:61
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.Picker.reset
void reset()
Puts the picker back into its initial state.
ca.uqac.lif.synthia.util.Tick.getExplanation
PartNode getExplanation(Part p)
Definition: Tick.java:209
ca.uqac.lif.synthia.util.Tick.duplicate
Tick duplicate(boolean with_state)
Creates a copy of the tick picker.
Definition: Tick.java:174
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
ca.uqac.lif.synthia.util.Tick.setValue
Tick setValue(float value)
Sets the current value of the tick picker.
Definition: Tick.java:195