BrightSide Workbench Full Report + Source Code
FunnelVertex.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.Collections;
22 import java.util.List;
23 import java.util.Objects;
24 import java.util.Set;
25 import java.util.TreeSet;
26 import java.util.stream.Collectors;
27 import org.jgrapht.Graphs;
28 import org.turro.collections.CollectionUtil;
29 import org.turro.elephant.context.IConstructor;
30 import org.turro.elephant.entities.web.WebItem;
31 import org.turro.elephant.web.ElContext;
32 import org.turro.elephant.web.ElContextMap;
33 import org.turro.elephant.entities.web.WebItemType;
34 import org.turro.elephant.web.tags.TagsServlet;
35 import org.turro.graph.GraphUtil;
36 import org.turro.util.Comparison;
37 
42 public class FunnelVertex implements Comparable<FunnelVertex> {
43 
44  private final FunnelGraph graph;
45  private final WebItem item;
46 
47  public FunnelVertex(FunnelGraph graph, WebItem item) {
48  this.graph = graph;
49  this.item = item;
50  }
51 
52  public boolean isActor() {
53  return !(isChain() || isWrong());
54  }
55 
56  public boolean isTarget() {
57  return WebItemType.TARGET.equals(item.getType());
58  }
59 
60  public boolean isSolution() {
61  return WebItemType.SOLUTION.equals(item.getType());
62  }
63 
64  public boolean isService() {
65  return WebItemType.SERVICE.equals(item.getType());
66  }
67 
68  public boolean isChain() {
69  return WebItemType.CHAIN.equals(item.getType());
70  }
71 
72  public boolean isWrong() {
73  return item.getType() == null;
74  }
75 
76  public String getWebTag() {
77  return item.getWebTag();
78  }
79 
81  if(isActor()) return item.getType();
82  Set<WebItemType> types = GraphUtil.deepPredecessorListOf(graph, this, (v) -> v.isActor(), (v) -> v.isActor())
83  .stream().map(v -> v.getItem().getType())
84  .collect(Collectors.toSet());
85  if(types.isEmpty() || types.size() > 1) return null;
86  WebItemType type = CollectionUtil.from(types).first();
87  if(WebItemType.CHAIN.equals(type)) return null;
88  return type;
89  }
90 
91  public Set<FunnelVertex> getNexts() {
92  return Graphs.successorListOf(graph, this).stream().collect(Collectors.toCollection(TreeSet::new));
93  }
94 
95  public Set<FunnelVertex> getTargets() {
96  return switch(getActingType()) {
97  case SOLUTION, SERVICE -> GraphUtil.deepPredecessorListOf(graph, this, (v) -> v.isTarget());
98  default -> Collections.EMPTY_SET;
99  };
100  }
101 
102  public Set<FunnelVertex> getSolutions() {
103  return switch(getActingType()) {
104  case TARGET -> GraphUtil.deepSuccessorListOf(graph, this, (v) -> v.isSolution());
105  case SERVICE -> GraphUtil.deepPredecessorListOf(graph, this, (v) -> v.isSolution());
106  default -> Collections.EMPTY_SET;
107  };
108  }
109 
110  public Set<FunnelVertex> getServices() {
111  return switch(getActingType()) {
112  case TARGET, SOLUTION -> GraphUtil.deepSuccessorListOf(graph, this, (v) -> v.isService());
113  default -> Collections.EMPTY_SET;
114  };
115  }
116 
117  public Set<FunnelVertex> getNeighborOf() {
118  return Graphs.neighborSetOf(graph, this).stream()
119  .filter(vertex -> !vertex.equals(this))
120  .collect(Collectors.toSet());
121  }
122 
123  public Set<FunnelVertex> getSiblings() {
124  return switch(getActingType()) {
125  case TARGET -> graph.getTargets().stream()
126  .filter(v -> !v.equals(FunnelVertex.this))
127  .collect(Collectors.toCollection(TreeSet::new));
128  case SOLUTION -> getTargets().stream()
129  .flatMap(v -> v.getSolutions().stream())
130  .filter(v -> !v.equals(FunnelVertex.this))
131  .collect(Collectors.toCollection(TreeSet::new));
132  case SERVICE -> getSolutions().stream()
133  .flatMap(v -> v.getServices().stream())
134  .filter(v -> !v.equals(FunnelVertex.this))
135  .collect(Collectors.toCollection(TreeSet::new));
136  default -> Collections.EMPTY_SET;
137  };
138  }
139 
140  public WebItem getItem() {
141  return item;
142  }
143 
144  public List<String> getGoalActions() {
145  return item.getGoals().stream().map(g -> g.getGoalAction()).toList();
146  }
147 
148  public String getWebPath(IConstructor constructor) {
150  if(context != null) {
151  return context.getFullPath();
152  } else {
153  return TagsServlet.navigateAndSet(item.getWebTag(), constructor.getCurrentContext().getPath());
154  }
155  }
156 
157  /* Utils */
158 
159  @Override
160  public int hashCode() {
161  int hash = 7;
162  hash = 97 * hash + Objects.hashCode(this.item);
163  return hash;
164  }
165 
166  @Override
167  public boolean equals(Object obj) {
168  if (this == obj) {
169  return true;
170  }
171  if (obj == null) {
172  return false;
173  }
174  if (getClass() != obj.getClass()) {
175  return false;
176  }
177  final FunnelVertex other = (FunnelVertex) obj;
178  return Objects.equals(this.item, other.item);
179  }
180 
181  /* Comparable */
182 
183  @Override
184  public int compareTo(FunnelVertex o) {
185  return Comparison.ascendant()
186  .compare(item.getOrdering(), o.getItem().getOrdering())
187  .compare(item.getWebTag(), o.getItem().getWebTag())
188  .get();
189  }
190 
191 }
static ElContext getContextFromWebTag(String webTag)
static String navigateAndSet(String fromTag, String path)
FunnelVertex(FunnelGraph graph, WebItem item)
String getWebPath(IConstructor constructor)