BrightSide Workbench Full Report + Source Code
FunnelGraph.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.util.Set;
22 import java.util.TreeSet;
23 import java.util.function.Predicate;
24 import java.util.stream.Collectors;
25 import org.jgrapht.Graphs;
26 import org.jgrapht.graph.SimpleDirectedGraph;
27 import org.turro.elephant.db.ElephantPU;
28 import org.turro.elephant.entities.web.WebGoal;
29 import org.turro.elephant.entities.web.WebItem;
30 import org.turro.elephant.entities.web.WebItemType;
31 import org.turro.jpa.Dao;
32 import org.turro.sql.SqlClause;
33 import org.turro.util.Cached;
34 
39 public class FunnelGraph extends SimpleDirectedGraph<FunnelVertex, FunnelEdge> {
40 
42  FunnelVertex vertex = getVertex(item);
43  return vertex == null ? null : vertex.getActingType();
44  }
45 
46  public Set<FunnelVertex> getTargets() {
47  return getItems(vertex -> vertex.isTarget());
48  }
49 
50  public Set<FunnelVertex> getSolutions() {
51  return getItems(vertex -> vertex.isSolution());
52  }
53 
54  public Set<FunnelVertex> getServices() {
55  return getItems(vertex -> vertex.isService());
56  }
57 
58  public Set<FunnelVertex> getChains() {
59  return getItems(vertex -> vertex.isChain());
60  }
61 
62  public Set<FunnelVertex> getItems(Predicate<FunnelVertex> filter) {
63  return vertexSet().stream()
64  .filter(filter)
65  .collect(Collectors.toCollection(TreeSet::new));
66  }
67 
68  public FunnelVertex getVertex(String webTag) {
69  return vertexSet().stream()
70  .filter(vertex -> vertex.getWebTag().equals(webTag))
71  .findAny().orElse(null);
72  }
73 
75  return vertexSet().stream()
76  .filter(vertex -> vertex.getItem().equals(item))
77  .findAny().orElse(null);
78  }
79 
80  public WebGoal getGoal(String goalAction) {
81  return vertexSet().stream()
82  .flatMap(vertex -> vertex.getItem().getGoals().stream())
83  .filter(goal -> goal.getGoalAction().equals(goalAction))
84  .findAny().orElse(null);
85  }
86 
87  /* Initialization */
88 
89  private void addVertices() {
90  SqlClause.select("wi").from("WebItem wi")
91  .orderBy("ordering")
92  .dao(dao.get())
93  .resultList(WebItem.class)
94  .forEach(item -> {
95  addVertex(new FunnelVertex(this, item));
96  });
97  }
98 
99  private void addEdges() {
100  vertexSet().stream().sorted().forEach(vertex -> {
101  vertex.getItem().getNexts().forEach(item -> {
102  FunnelVertex vitem = new FunnelVertex(this, item);
103  if(containsVertex(vitem)) {
104  Graphs.addEdgeWithVertices(this, vertex, vitem);
105  }
106  });
107  });
108  }
109 
110  /* Utils */
111 
112  public static WebItemType behaveAs(WebItem item) {
113  return load().getBehaviour(item);
114  }
115 
116  /* Factory */
117 
118  public static FunnelGraph load() {
119  return new FunnelGraph();
120  }
121 
122  private FunnelGraph() {
123  super(FunnelEdge.class);
124  addVertices();
125  addEdges();
126  }
127 
128  /* Dao */
129 
130  protected final Cached<Dao> dao = Cached.instance(() -> new ElephantPU());
131 
132 }
FunnelVertex getVertex(WebItem item)
Set< FunnelVertex > getItems(Predicate< FunnelVertex > filter)
FunnelVertex getVertex(String webTag)
WebGoal getGoal(String goalAction)
WebItemType getBehaviour(WebItem item)
static WebItemType behaveAs(WebItem item)