Synthia
Generic and flexible data structure generator
PrismPicker.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.vector;
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.petitpoucet.function.vector.NthElement;
27 import ca.uqac.lif.synthia.Picker;
29 
30 /**
31  * Generates a vector by independently picking a value for each of
32  * its coordinates. If each of the pickers define a possible range of
33  * values, this picker amounts to generating points inside a
34  * multi-dimensional "prism" --hence its name.
35  *
36  * @ingroup API
37  */
38 public class PrismPicker implements VectorPicker, ExplanationQueryable
39 {
40  /**
41  * The pickers for each dimension of the vector
42  */
44 
45  /**
46  * Creates a new prism picker.
47  * @param dimensions The pickers used to generate values for
48  * each of the dimensions of the vector. If given <i>n</i>
49  * arguments, the resulting picker will produce <i>n</i>-dimensional
50  * vectors.
51  */
52  @SuppressWarnings("unchecked")
53  public PrismPicker(Picker<? extends Number> ... dimensions)
54  {
55  super();
56  m_dimensions = dimensions;
57  }
58 
59  @Override
60  public void reset()
61  {
63  {
64  p.reset();
65  }
66  }
67 
68  @Override
69  public float[] pick()
70  {
71  float[] v = new float[m_dimensions.length];
72  for (int i = 0; i < m_dimensions.length; i++)
73  {
74  v[i] = m_dimensions[i].pick().floatValue();
75  }
76  return v;
77  }
78 
79  @SuppressWarnings("unchecked")
80  @Override
81  public PrismPicker duplicate(boolean with_state)
82  {
83  Picker<? extends Number>[] dimensions = new Picker[m_dimensions.length];
84  for (int i = 0; i < m_dimensions.length; i++)
85  {
86  dimensions[i] = m_dimensions[i].duplicate(with_state);
87  }
88  return new PrismPicker(dimensions);
89  }
90 
91  @Override
92  public int getDimension()
93  {
94  return m_dimensions.length;
95  }
96 
97  @Override
98  public PartNode getExplanation(Part p)
99  {
100  return getExplanation(p, NodeFactory.getFactory());
101  }
102 
103  @Override
104  public PartNode getExplanation(Part p, NodeFactory f)
105  {
106  int index = NthSuccessiveOutput.mentionedOutput(p), part_index = NthElement.mentionedElement(p);
107  PartNode root = f.getPartNode(p, this);
108  if (index < 0)
109  {
110  return null; // Invalid part
111  }
112  if (part_index < 0)
113  {
114  // p points at the whole vector
115  AndNode and = f.getAndNode();
116  NthSuccessiveOutput in_out = new NthSuccessiveOutput(index);
117  for (int i = 0; i < m_dimensions.length; i++)
118  {
119  PartNode pn = f.getPartNode(in_out, m_dimensions[i]);
120  and.addChild(pn);
121  }
122  root.addChild(and);
123  return root;
124  }
125  // p points at a number inside the vector
126  Part new_p = NthSuccessiveOutput.removeNthElement(p);
127  root.addChild(f.getPartNode(new_p, m_dimensions[part_index]));
128  return root;
129  }
130 
131  @Override
132  public String toString()
133  {
134  return "PrismPicker";
135  }
136 }
ca.uqac.lif.synthia.Picker
Picks an object.
Definition: Picker.java:36
ca.uqac.lif.synthia.vector.PrismPicker.pick
float[] pick()
Picks an object.
Definition: PrismPicker.java:69
ca.uqac.lif.synthia.vector.PrismPicker.reset
void reset()
Puts the picker back into its initial state.
Definition: PrismPicker.java:60
ca.uqac.lif.synthia.vector.PrismPicker.getExplanation
PartNode getExplanation(Part p, NodeFactory f)
Definition: PrismPicker.java:104
ca.uqac.lif.synthia.vector.PrismPicker.getExplanation
PartNode getExplanation(Part p)
Definition: PrismPicker.java:98
ca.uqac.lif.synthia.vector.PrismPicker
Generates a vector by independently picking a value for each of its coordinates.
Definition: PrismPicker.java:38
ca.uqac.lif.synthia.vector.PrismPicker.toString
String toString()
Definition: PrismPicker.java:132
ca.uqac
ca.uqac.lif.synthia
Definition: Bounded.java:19
ca.uqac.lif.synthia.vector.VectorPicker
Interface for pickers that generate arrays of floating-point numbers.
Definition: VectorPicker.java:29
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.vector.PrismPicker.PrismPicker
PrismPicker(Picker<? extends Number > ... dimensions)
Creates a new prism picker.
Definition: PrismPicker.java:53
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.vector.PrismPicker.duplicate
PrismPicker duplicate(boolean with_state)
Creates a copy of the picker.
Definition: PrismPicker.java:81
ca.uqac.lif.synthia.Picker.duplicate
Picker< T > duplicate(boolean with_state)
Creates a copy of the picker.
ca.uqac.lif.synthia.vector.PrismPicker.m_dimensions
Picker<? extends Number >[] m_dimensions
The pickers for each dimension of the vector.
Definition: PrismPicker.java:43
ca.uqac.lif.synthia.vector.PrismPicker.getDimension
int getDimension()
Returns the dimension of the picker, i.e.
Definition: PrismPicker.java:92
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