Synthia
Generic and flexible data structure generator
Explanation.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.ArrayDeque;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Queue;
25 import java.util.Set;
26 
27 import ca.uqac.lif.dag.LabelledNode;
28 import ca.uqac.lif.dag.NestedNode.NestedNodeCrawler;
29 import ca.uqac.lif.dag.Node;
30 import ca.uqac.lif.dag.Pin;
31 import ca.uqac.lif.petitpoucet.NodeFactory;
32 import ca.uqac.lif.petitpoucet.Part;
33 import ca.uqac.lif.petitpoucet.PartNode;
34 import ca.uqac.lif.petitpoucet.function.ExplanationQueryable;
35 import ca.uqac.lif.synthia.Picker;
36 
37 /**
38  * Constructs an explanation graph for the output produced by a picker.
39  * @author Sylvain Hallé
40  * @ingroup API
41  */
42 public class Explanation
43 {
44  public static PartNode explain(Part part, Picker<?> picker)
45  {
46  NodeFactory factory = NodeFactory.getFactory().getFactory(part, picker);
47  Queue<PartNode> to_visit = new ArrayDeque<PartNode>();
48  PartNode root = factory.getPartNode(part, picker);
49  to_visit.add(root);
50  Set<PartNode> visited = new HashSet<PartNode>();
51  while (!to_visit.isEmpty())
52  {
53  LabelledNode current = to_visit.remove();
54  if (visited.contains(current) || !(current instanceof PartNode))
55  {
56  continue;
57  }
58  PartNode pn_current = (PartNode) current;
59  visited.add(pn_current);
60  if (!(pn_current.getSubject() instanceof ExplanationQueryable))
61  {
62  continue;
63  }
64  Part ppn_current = pn_current.getPart();
65  ExplanationQueryable eq = (ExplanationQueryable) pn_current.getSubject();
66  PartNode under = eq.getExplanation(ppn_current, factory);
67  if (under.equals(pn_current))
68  {
69  for (Pin<?> p : under.getOutputLinks(0))
70  {
71  Node n_child = (Node) p.getNode();
72  pn_current.addChild(n_child);
73  NestedNodeCrawler c = new NestedNodeCrawler(n_child);
74  c.crawl();
75  List<Node> leaves = c.getLeaves();
76  for (Node n : leaves)
77  {
78  if (n instanceof PartNode && !to_visit.contains(n) && !visited.contains(n))
79  {
80  to_visit.add((PartNode) n);
81  }
82  }
83  }
84  }
85  else
86  {
87  pn_current.addChild(under);
88  NestedNodeCrawler c = new NestedNodeCrawler(under);
89  List<Node> leaves = c.getLeaves();
90  for (Node n : leaves)
91  {
92  if (n instanceof PartNode && !to_visit.contains(n) && !visited.contains(n))
93  {
94  to_visit.add((PartNode) n);
95  }
96  }
97  }
98  }
99  return root;
100  }
101 }
ca.uqac.lif.synthia.Picker
Picks an object.
Definition: Picker.java:36
ca.uqac.lif.synthia.explanation.Explanation.explain
static PartNode explain(Part part, Picker<?> picker)
Definition: Explanation.java:44
ca.uqac
ca.uqac.lif.synthia
Definition: Bounded.java:19
ca.uqac.lif
ca
ca.uqac.lif.synthia.explanation.Explanation
Constructs an explanation graph for the output produced by a picker.
Definition: Explanation.java:42