Synthia
Generic and flexible data structure generator
Freeze.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.NodeFactory;
22 import ca.uqac.lif.petitpoucet.Part;
23 import ca.uqac.lif.petitpoucet.PartNode;
24 import ca.uqac.lif.petitpoucet.function.ExplanationQueryable;
25 import ca.uqac.lif.synthia.Picker;
30 
31 /**
32  * Picker that returns the first object fetched from another picker and repeats
33  * it endlessly. For example, the following piece of code instantiates a
34  * {@link ca.uqac.lif.synthia.random.RandomFloat RandomFloat}
35  * <tt>r</tt>; the <tt>Freeze</tt> picker wraps around it, and keeps returning
36  * the first random float returned by <tt>r</tt>.
37  * <pre>
38  * RandomFloat r = new RandomFloat();
39  * Freeze&lt;Float&gt; f = new Freeze&lt;Float&gt;(r);
40  * float f1 = f.pick(); // 0.8104950, for example
41  * float f2 = f.pick(); // 0.8104950 again
42  * float f3 = f.pick(); // 0.8104950 again
43  * ...
44  * </pre>
45  * @param <T> The type of objects to pick
46  * @ingroup API
47  */
48 public class Freeze<T> implements Shrinkable<T>, ExplanationQueryable
49 {
50  /**
51  * The internal picker that is to be called
52  */
53  /*@ non_null @*/ protected Picker<T> m_innerPicker;
54 
55  /**
56  * The "frozen" value
57  */
58  protected T m_value = null;
59 
60  /**
61  * Creates a new freeze picker
62  * @param picker The underlying picker used to get the first
63  * value
64  */
65  public Freeze(/*@ non_null @*/ Picker<T> picker)
66  {
67  super();
68  m_innerPicker = picker;
69  }
70 
71 
72  /**
73  * Puts the freeze picker back into its initial state. This means that the
74  * sequence of calls to {@link #pick()} will produce the same values
75  * as when the object was instantiated.
76  */
77  @Override
78  public void reset()
79  {
80  m_value = null;
81  m_innerPicker.reset();
82  }
83 
84 
85  /**
86  * Picks a value or returns the same value if m_value is not null. Typically, this method is expected to return non-null
87  * objects; a <tt>null</tt> return value is used to signal that no more
88  * objects will be produced. That is, once this method returns
89  * <tt>null</tt>, it should normally return <tt>null</tt> on all subsequent
90  * calls.
91  * @return The frozen value.
92  */
93  @Override
94  public T pick()
95  {
96  if (m_value == null)
97  {
98  m_value = m_innerPicker.pick();
99  }
100  return m_value;
101  }
102 
103  @Override
104  /*@ non_null @*/ public Freeze<T> duplicate(boolean with_state)
105  {
106  Freeze<T> fp = new Freeze<>(m_innerPicker.duplicate(with_state));
107  if (with_state)
108  {
109  fp.m_value = m_value;
110  }
111  return fp;
112  }
113 
114  @Override
115  public Shrinkable<T> shrink(T o, Picker<Float> decision, float m)
116  {
117  return new PickSmallerComparable<T>(this, o);
118  }
119 
120  @Override
121  public PartNode getExplanation(Part p)
122  {
123  return getExplanation(p, NodeFactory.getFactory());
124  }
125 
126  @Override
127  public PartNode getExplanation(Part p, NodeFactory f)
128  {
129  PartNode root = f.getPartNode(p, this);
130  PartNode child = f.getPartNode(NthSuccessiveOutput.replaceOutIndexBy(p, 0), m_innerPicker);
131  root.addChild(child);
132  return root;
133  }
134 
135  @Override
136  public String toString()
137  {
138  return "Freeze(" + m_innerPicker + ")";
139  }
140 
141 
142  @Override
143  public Shrinkable<T> shrink(T o)
144  {
145  return shrink(o, RandomFloat.instance, 1);
146  }
147 }
ca.uqac.lif.synthia.Picker
Picks an object.
Definition: Picker.java:36
ca.uqac.lif.synthia.util.Freeze
Picker that returns the first object fetched from another picker and repeats it endlessly.
Definition: Freeze.java:48
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.Freeze.toString
String toString()
Definition: Freeze.java:136
ca.uqac.lif.synthia.util.Freeze.shrink
Shrinkable< T > shrink(T o, Picker< Float > decision, float m)
Shrinks a picker.
Definition: Freeze.java:115
ca.uqac.lif.synthia.util.Freeze.getExplanation
PartNode getExplanation(Part p, NodeFactory f)
Definition: Freeze.java:127
ca.uqac.lif.synthia.relative.PickSmallerComparable
A variant of PickIf that selects an element if it is smaller than a reference object.
Definition: PickSmallerComparable.java:37
ca.uqac.lif.synthia.util.Freeze.getExplanation
PartNode getExplanation(Part p)
Definition: Freeze.java:121
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
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.explanation
Objects related to the explanation of results produced by pickers.
Definition: Explanation.java:19
ca.uqac.lif
ca.uqac.lif.synthia.util.Freeze.shrink
Shrinkable< T > shrink(T o)
Shrinks a picker with default parameters.
Definition: Freeze.java:143
ca.uqac.lif.synthia.util.Freeze.duplicate
Freeze< T > duplicate(boolean with_state)
Creates a copy of the picker.
Definition: Freeze.java:104
ca.uqac.lif.synthia.util.Freeze.reset
void reset()
Puts the freeze picker back into its initial state.
Definition: Freeze.java:78
ca.uqac.lif.synthia.util.Freeze.Freeze
Freeze(Picker< T > picker)
Creates a new freeze picker.
Definition: Freeze.java:65
ca
ca.uqac.lif.synthia.relative
Pickers that produce a value in relation to another value.
Definition: package-info.java:24
ca.uqac.lif.synthia.util.Freeze.m_value
T m_value
The "frozen" value.
Definition: Freeze.java:58
ca.uqac.lif.synthia.util.Freeze.pick
T pick()
Picks a value or returns the same value if m_value is not null.
Definition: Freeze.java:94
ca.uqac.lif.synthia.random.RandomFloat
Picks a floating point number uniformly in an interval.
Definition: RandomFloat.java:30
ca.uqac.lif.synthia.util.Freeze.m_innerPicker
Picker< T > m_innerPicker
The internal picker that is to be called.
Definition: Freeze.java:53
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