BrightSide Workbench Full Report + Source Code
FunnelVertices.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.model;
20 
21 import java.util.Collections;
22 import java.util.ListIterator;
23 import java.util.Set;
24 import java.util.TreeSet;
25 import java.util.stream.Collectors;
26 import org.turro.elephant.context.IConstructor;
27 import org.turro.elephant.entities.web.WebGoal;
28 import org.turro.elephant.entities.web.WebItemType;
29 import org.turro.elephant.web.actions.WebLink;
30 import org.turro.elephant.web.tags.SessionTags;
31 import org.turro.graph.GraphUtil;
32 import org.turro.web.funnel.SessionGoals;
33 import org.turro.web.funnel.graph.FunnelGraph;
34 import org.turro.web.funnel.graph.FunnelVertex;
35 
40 public class FunnelVertices {
41 
42  public FunnelVertex current() {
43  return graph.getVertex(sessionTags.current());
44  }
45 
47  return graph.getVertex(sessionTags.previous());
48  }
49 
50  public Set<FunnelVertex> targets() {
51  if(useTags) {
52  return current().getTargets();
53  } else {
54  return graph.getTargets();
55  }
56  }
57 
59  return currentOf(WebItemType.TARGET);
60  }
61 
62  public Set<FunnelVertex> solutions() {
63  if(useTags) {
64  return current().getSolutions();
65  } else {
66  return graph.getSolutions();
67  }
68  }
69 
71  return currentOf(WebItemType.SOLUTION);
72  }
73 
74  public Set<FunnelVertex> services() {
75  if(useTags) {
76  return current().getServices();
77  } else {
78  return graph.getServices();
79  }
80  }
81 
83  return currentOf(WebItemType.SERVICE);
84  }
85 
86  public Set<FunnelVertex> nexts() {
87  FunnelVertex vertex = current();
88  if(vertex != null) {
89  return current().getNexts();
90  }
91  return null;
92  }
93 
94  public Set<WebGoal> goals() {
95  FunnelVertex vertex = current();
96  if(vertex != null) {
97  return current().getItem().getGoals();
98  }
99  return null;
100  }
101 
102  public Set<WebLink> externals() {
103  FunnelVertex vertex = current();
104  if(vertex != null) {
105  return current().getItem().getExternalLinks();
106  }
107  return null;
108  }
109 
110  public FunnelVertex getVertex(String webTag) {
111  return graph.getVertex(webTag);
112  }
113 
114  public WebGoal getGoal(String goalAction) {
115  return graph.getGoal(goalAction);
116  }
117 
118  public Set<FunnelVertex> siblings() {
119  final FunnelVertex vertex = current();
120  return switch(vertex.getActingType()) {
121  case TARGET -> targets().stream()
122  .filter(v -> !v.equals(vertex))
123  .collect(Collectors.toCollection(TreeSet::new));
124  case SOLUTION -> currentTarget().getSolutions().stream()
125  .filter(v -> !v.equals(vertex))
126  .collect(Collectors.toCollection(TreeSet::new));
127  case SERVICE -> currentSolution().getServices().stream()
128  .filter(v -> !v.equals(vertex))
129  .collect(Collectors.toCollection(TreeSet::new));
130  default -> Collections.EMPTY_SET;
131  };
132  }
133 
134  /* Behaviour */
135 
136  public FunnelVertices bySelection(boolean value) {
137  useTags = value;
138  return this;
139  }
140 
141  /* Utils */
142 
143  private FunnelVertex currentOf(WebItemType type) {
144  ListIterator<String> iterator = sessionTags.tags().listIterator(sessionTags.tags().size() - 1);
145  while(iterator.hasPrevious()) {
146  String webTag = iterator.previous();
147  FunnelVertex vertex = graph.getVertex(webTag);
148  if(vertex != null && type.equals(vertex.getItem().getType())) return vertex;
149  }
150  return null;
151  }
152 
153  /* Factory */
154 
155  public static FunnelVertices load(IConstructor constructor) {
156  return new FunnelVertices(constructor);
157  }
158 
159  private final SessionTags sessionTags;
160  private final SessionGoals sessionGoals;
161  private final FunnelGraph graph;
162 
163  private boolean useTags;
164 
165  private FunnelVertices(IConstructor constructor) {
166  sessionTags = SessionTags.get(constructor);
167  sessionGoals = SessionGoals.get(constructor);
168  graph = FunnelGraph.load();
169  if(!sessionGoals.goals().isEmpty()) {
170  GraphUtil.removeIfNoneOfSelfOrSuccessors(graph, v -> !Collections.disjoint(sessionGoals.goals(), v.getGoalActions()));
171  }
172  }
173 
174 }
FunnelVertex getVertex(String webTag)
WebGoal getGoal(String goalAction)
static FunnelVertices load(IConstructor constructor)
FunnelVertices bySelection(boolean value)