BrightSide Workbench Full Report + Source Code
FunnelGraphDOT.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.web.funnel.graph;
20 
21 import java.io.Writer;
22 import java.util.LinkedHashMap;
23 import java.util.Map;
24 import org.jgrapht.Graphs;
25 import org.jgrapht.graph.DefaultEdge;
26 import org.jgrapht.graph.SimpleDirectedGraph;
27 import org.jgrapht.nio.Attribute;
28 import org.jgrapht.nio.DefaultAttribute;
29 import org.turro.elephant.db.ElephantPU;
30 import org.turro.elephant.entities.web.WebGoal;
31 import org.turro.elephant.entities.web.WebItem;
32 import org.turro.graph.GraphUtil;
33 import org.turro.jpa.Dao;
34 import org.turro.sql.SqlClause;
35 import org.turro.string.Strings;
36 import org.turro.util.Cached;
37 
42 public class FunnelGraphDOT extends SimpleDirectedGraph<FunnelVertexDOT, DefaultEdge> {
43 
44  /* Initialization */
45 
46  private void addVertices() {
47  SqlClause.select("wi").from("WebItem wi")
48  .orderBy("ordering")
49  .dao(dao.get())
50  .resultList(WebItem.class)
51  .forEach(item -> {
52  addVertex(new FunnelVertexDOT(item));
53  });
54  SqlClause.select("wg").from("WebGoal wg")
55  .orderBy("ordering")
56  .dao(dao.get())
57  .resultList(WebGoal.class)
58  .forEach(item -> {
59  addVertex(new FunnelVertexDOT(item));
60  });
61  }
62 
63  private void addEdges() {
64  vertexSet().stream().sorted().forEach(vertex -> {
65  vertex.getNexts().forEach(item -> {
66  FunnelVertexDOT vitem = new FunnelVertexDOT(item);
67  if(containsVertex(vitem)) {
68  Graphs.addEdgeWithVertices(this, vertex, vitem);
69  }
70  });
71  });
72  }
73 
74  /* Utils */
75 
76  public static void DOT(Writer writer) {
77  GraphUtil.<FunnelVertexDOT, DefaultEdge>toDOT(writer, load(),
78  fi -> Strings.identifier(fi.getIdentifier()),
79  (fi) -> {
80  Map<String, Attribute> map = new LinkedHashMap<>();
81  map.put("color", DefaultAttribute.createAttribute(fi.getColor()));
82  map.put("fontcolor", DefaultAttribute.createAttribute("#FFFFFF"));
83  map.put("label", DefaultAttribute.createAttribute(fi.getCaption()));
84  return map;
85  });
86  }
87 
88  /* Factory */
89 
90  public static FunnelGraphDOT load() {
91  return new FunnelGraphDOT();
92  }
93 
94  private FunnelGraphDOT() {
95  super(DefaultEdge.class);
96  addVertices();
97  addEdges();
98  }
99 
100  /* Dao */
101 
102  protected final Cached<Dao> dao = Cached.instance(() -> new ElephantPU());
103 
104 }