Synthia
Generic and flexible data structure generator
NthSuccessiveOutput.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.explanation;
20 
21 import java.util.ArrayList;
22 import java.util.List;
23 
24 import ca.uqac.lif.petitpoucet.ComposedPart;
25 import ca.uqac.lif.petitpoucet.Part;
26 import ca.uqac.lif.petitpoucet.function.NthInput;
27 import ca.uqac.lif.petitpoucet.function.NthOutput;
28 import ca.uqac.lif.petitpoucet.function.vector.NthElement;
29 import ca.uqac.lif.synthia.Picker;
30 
31 /**
32  * A {@link Part} pointing to the n-th output produced by a picker since its
33  * last call to {@link Picker#reset() reset()}.
34  * @ingroup API
35  */
36 public class NthSuccessiveOutput implements Part
37 {
38  /**
39  * A static reference to an instance of the class with index = 0, thereby
40  * referring to the first output produced by a picker.
41  */
42  public static final NthSuccessiveOutput FIRST = new NthSuccessiveOutput(0);
43 
44  /**
45  * The index in the sequence of the output since the last reset.
46  */
47  public int m_index;
48 
49  /**
50  * Creates a new instance of the part.
51  * @param index The index in the sequence of the output since the last reset
52  */
53  public NthSuccessiveOutput(int index)
54  {
55  super();
56  m_index = index;
57  }
58 
59  /**
60  * Returns the index in the sequence of the output since the last reset.
61  * @return The index
62  */
63  public int getIndex()
64  {
65  return m_index;
66  }
67 
68  @Override
69  public String toString()
70  {
71  return "Output value " + m_index;
72  }
73 
74  @Override
75  public boolean appliesTo(Object o)
76  {
77  return o instanceof Picker;
78  }
79 
80  @Override
81  public Part head()
82  {
83  return this;
84  }
85 
86  @Override
87  public Part tail()
88  {
89  return null;
90  }
91 
92  /**
93  * Given an arbitrary designator, replaces the first occurrence of
94  * {@link NthOutput} by an instance of {@link NthInput} with given index.
95  * @param from The original part
96  * @param to The part to replace it with
97  * @return The new designator. The input object is not modified if it does
98  * not contain {@code d}
99  */
100  /*@ non_null @*/ public static Part replaceOutIndexBy(/*@ non_null @*/ Part from, int index)
101  {
102  Part to = FIRST;
103  if (index > 0)
104  {
105  to = new NthSuccessiveOutput(index);
106  }
107  if (from instanceof NthSuccessiveOutput)
108  {
109  return to;
110  }
111  if (from instanceof ComposedPart)
112  {
113  ComposedPart cd = (ComposedPart) from;
114  List<Part> desigs = new ArrayList<Part>();
115  boolean replaced = false;
116  for (int i = 0 ; i < cd.size(); i++)
117  {
118  Part in_d = cd.get(i);
119  if (in_d instanceof NthSuccessiveOutput && !replaced)
120  {
121  desigs.add(to);
122  replaced = true;
123  }
124  else
125  {
126  desigs.add(in_d);
127  }
128  }
129  if (!replaced)
130  {
131  // Return input object if no replacement was done
132  return from;
133  }
134  return new ComposedPart(desigs);
135  }
136  return from;
137  }
138 
139  /**
140  * Retrieves the output sequence index mentioned in a designator.
141  * @param d The designator
142  * @return The output sequence index, or -1 if no output index is mentioned
143  */
144  public static int mentionedOutput(Part d)
145  {
146  if (d instanceof NthSuccessiveOutput)
147  {
148  return ((NthSuccessiveOutput) d).getIndex();
149  }
150  if (d instanceof ComposedPart)
151  {
152  ComposedPart cd = (ComposedPart) d;
153  for (int i = 0; i < cd.size(); i++)
154  {
155  Part p = cd.get(i);
156  if (p instanceof NthSuccessiveOutput)
157  {
158  return ((NthSuccessiveOutput) p).getIndex();
159  }
160  }
161  }
162  return -1;
163  }
164 
165  /**
166  * Removes the instance of NthElement that stands just before the first
167  * instance of NthSuccessiveOutput in a composed designator.
168  * @param from The original composed designator
169  * @return The transformed designator
170  */
171  public static Part removeNthElement(Part from)
172  {
173  if (from instanceof NthElement || !(from instanceof ComposedPart))
174  {
175  return Part.nothing;
176  }
177  ComposedPart cd = (ComposedPart) from;
178  List<Part> desigs = new ArrayList<Part>();
179  boolean replaced = false;
180  for (int i = 0 ; i < cd.size(); i++)
181  {
182  Part in_d = cd.get(i);
183  if (in_d instanceof NthElement && !replaced && i < cd.size() - 1 && cd.get(i + 1) instanceof NthSuccessiveOutput)
184  {
185  // We skip this designator, which deletes in the output part
186  replaced = true;
187  }
188  else
189  {
190  desigs.add(in_d);
191  }
192  }
193  if (!replaced)
194  {
195  // Return input object if no replacement was done
196  return from;
197  }
198  return new ComposedPart(desigs);
199  }
200 }
ca.uqac.lif.synthia.Picker
Picks an object.
Definition: Picker.java:36
ca.uqac.lif.synthia.explanation.NthSuccessiveOutput.FIRST
static final NthSuccessiveOutput FIRST
A static reference to an instance of the class with index = 0, thereby referring to the first output ...
Definition: NthSuccessiveOutput.java:42
ca.uqac.lif.synthia.explanation.NthSuccessiveOutput.toString
String toString()
Definition: NthSuccessiveOutput.java:69
ca.uqac.lif.synthia.explanation.NthSuccessiveOutput.tail
Part tail()
Definition: NthSuccessiveOutput.java:87
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.explanation.NthSuccessiveOutput.head
Part head()
Definition: NthSuccessiveOutput.java:81
ca.uqac
ca.uqac.lif.synthia
Definition: Bounded.java:19
ca.uqac.lif.synthia.explanation.NthSuccessiveOutput.m_index
int m_index
The index in the sequence of the output since the last reset.
Definition: NthSuccessiveOutput.java:47
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.explanation.NthSuccessiveOutput.getIndex
int getIndex()
Returns the index in the sequence of the output since the last reset.
Definition: NthSuccessiveOutput.java:63
ca.uqac.lif.synthia.explanation.NthSuccessiveOutput.NthSuccessiveOutput
NthSuccessiveOutput(int index)
Creates a new instance of the part.
Definition: NthSuccessiveOutput.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.explanation.NthSuccessiveOutput.appliesTo
boolean appliesTo(Object o)
Definition: NthSuccessiveOutput.java:75
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