Synthia
Generic and flexible data structure generator
ExplanationList1.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 examples.provenance;
20 
21 import java.util.List;
22 
23 import ca.uqac.lif.petitpoucet.ComposedPart;
24 import ca.uqac.lif.petitpoucet.PartNode;
25 import ca.uqac.lif.petitpoucet.function.LineageDotRenderer;
26 import ca.uqac.lif.petitpoucet.function.vector.NthElement;
34 import ca.uqac.lif.synthia.util.Freeze;
35 import ca.uqac.lif.synthia.util.Tick;
37 import examples.util.Utilities;
38 
39 /**
40  * Demonstration of Synthia's explainability features on a wiring of
41  * pickers producing a list of points. The code in this example corresponds to
42  * the following diagram:
43  * <p>
44  * <img src="./doc-files/provenance/List.png" alt="Wiring of pickers" />
45  * <p>
46  * A chain of pickers is instructed to generate a list of four
47  * two-dimensional points using a {@link PrismPicker}, with constraints on their
48  * possible values. The x coordinate starts at $0$ and increments by a fixed
49  * value that is initially chosen to be either 1 or 2; the y coordinate
50  * repeatedly iterates through the values 0, 1 and -1; a {@link PickIf} removes
51  * any point lying on the line x=y. A possible first output of this chain is
52  * the list [(1,-1),(2,0),(3,1),(4,-1)].
53  * <p>
54  * One can then ask for a first list out of this chain, and then ask for an
55  * explanation of the first point inside the list by passing to the static
56  * method {@link Explanation#explain(ca.uqac.lif.petitpoucet.Part, ca.uqac.lif.synthia.Picker) Explanation.explain()}
57  * a the appropriate <em>designator</em>. This has for effect of linking this
58  * value to the output produced by other pickers, ultimately producing a graph
59  * such as the one shown below.
60  * <p>
61  * <img src="./doc-files/provenance/Explanation.png" alt="Explanation graph" />
62  * <p>
63  * In the source code, this graph is merely printed to the console in the DOT
64  * format of the <a href="https://graphviz.org">Graphviz</a> graph rendering
65  * tool. The picture above is a beautified version of that graph using
66  * pictograms instead of text, and simplified by removing "and" nodes.
67  * <p>
68  * This graph is not a static template obtained from the wiring of pickers; to
69  * illustrate this fact, the graph below shows an alternate explanation, which
70  * is what one obtains when {@link RandomInteger} picks the value 2 instead of
71  * 1; in that case the output list becomes [(2,1),(4,-1),(6,0),(8,1)].
72  * <p>
73  * <img src="./doc-files/provenance/Explanation_alt.png"
74  * alt="Other explanation graph" />
75  * <p>
76  * In the source code, one can change the value of the seed given to the
77  * {@link RandomInteger} <tt>r_int</tt> until this alternate graph is obtained
78  * instead of the first one.
79  *
80  * @ingroup Examples
81  */
82 public class ExplanationList1
83 {
84  @SuppressWarnings("unchecked")
85  public static void main(String[] args)
86  {
87  /* Step 1: create the pickers and connect them. These lines of code create
88  * exactly the pipeline shown in the picture above. */
89  RandomInteger r_int = new RandomInteger(1, 3);
90  r_int.setSeed(40002); // Modify this seed to make r_int pick another value
91  Freeze<Integer> fr_int = new Freeze<Integer>(r_int);
93  Tick t = new Tick(zero, fr_int);
94  Playback<Integer> pb = new Playback<Integer>(0, new Integer[] {0, -1, 1}).setLoop(true);
95  PrismPicker hp = new PrismPicker(t, pb);
96  PickIf<float[]> filter = new PickIf<float[]>(hp) {
97  protected boolean select(float[] p) { return p[0] != p[1]; }
98  };
100  ComposeList<float[]> list = new ComposeList<float[]>(filter, four);
101 
102  /* Step 2: ask for a first list out of ComposeList and display it */
103  List<float[]> l = list.pick();
104  list.pick();
105  list.pick();
106  Utilities.println(System.out, l);
107 
108  /* Step 3: request the explanation graph for the first element of
109  * the first list produced by ComposeList. */
110  PartNode tree = Explanation.explain(ComposedPart.compose(new NthElement(0), NthSuccessiveOutput.FIRST), list);
111  LineageDotRenderer renderer = new LineageDotRenderer(tree);
112  renderer.render(System.out);
113  }
114 }
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.util.Freeze
Picker that returns the first object fetched from another picker and repeats it endlessly.
Definition: Freeze.java:48
ca.uqac.lif.synthia.collection.ComposeList.pick
List< T > pick()
Picks an object.
Definition: ComposeList.java:94
ca.uqac.lif.synthia.collection
Pickers generating and manipulating collections, such as lists and sets.
Definition: ComparableList.java:19
ca.uqac.lif.synthia.util
Miscellaneous pickers performing various functions.
Definition: ArrayPicker.java:19
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.explanation.Explanation.explain
static PartNode explain(Part part, Picker<?> picker)
Definition: Explanation.java:44
examples.provenance.ExplanationList1
Demonstration of Synthia's explainability features on a wiring of pickers producing a list of points.
Definition: ExplanationList1.java:82
ca.uqac.lif.synthia.vector
Pickers generating multi-dimensional numerical vectors.
Definition: HyperspherePicker.java:19
ca.uqac.lif.synthia.sequence.Playback
Picker that returns values taken from a list.
Definition: Playback.java:61
ca.uqac
examples.util
Definition: package-info.java:1
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
examples.provenance.ExplanationList1.main
static void main(String[] args)
Definition: ExplanationList1.java:85
examples.util.Utilities.println
static void println(PrintStream ps, Object o)
Calls print(PrintStream, Object) and appends a new line.
Definition: Utilities.java:111
examples.util.Utilities
Object providing a few utility methods to simplify the examples in this project.
Definition: Utilities.java:30
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.random.RandomInteger.setSeed
RandomInteger setSeed(int seed)
Definition: RandomInteger.java:102
ca.uqac.lif.synthia.random.RandomInteger
Picks an integer uniformly in an interval.
Definition: RandomInteger.java:31
ca.uqac.lif.synthia.relative.PickIf
Returns object from a picker satisfying a condition.
Definition: PickIf.java:39
ca.uqac.lif.synthia.util.Tick
Generates a sequence of monotonically increasing numerical values.
Definition: Tick.java:51
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.explanation.Explanation
Constructs an explanation graph for the output produced by a picker.
Definition: Explanation.java:42
examples
ca.uqac.lif.synthia.collection.ComposeList
Definition: ComposeList.java:44
ca.uqac.lif.synthia.sequence
Pickers related to the generation of a sequence of values.
Definition: BehaviorTree.java:19
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