BrightSide Workbench Full Report + Source Code
WebParser.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.parser;
20 
21 import java.util.Collections;
22 import java.util.Set;
23 import org.turro.annotation.ExternalParser;
24 import org.turro.collections.CollectionUtil;
25 import org.turro.elephant.entities.web.WebGoal;
26 import org.turro.elephant.web.actions.WebLink;
27 import org.turro.elephant.web.tags.TagsServlet;
28 import org.turro.marker.ElephantMarker;
29 import org.turro.web.funnel.graph.FunnelGraphDOT;
30 import org.turro.web.funnel.graph.FunnelVertex;
31 import org.turro.web.funnel.model.FunnelVertices;
32 
37 @ExternalParser
38 public class WebParser extends AbstractParser {
39 
40  @Override
41  public String getParserName() {
42  return "Elephant Web";
43  }
44 
45  @Override
46  protected boolean doExecute() {
47  boolean done = false;
48  if("wtargets".equals(getMacroName())) {
50  fv.bySelection("true".equals(getToken(2)));
51  Set<FunnelVertex> vertices = switch(getToken(1)) {
52  case "all" -> fv.targets();
53  case "current" -> CollectionUtil.setOf(fv.currentTarget());
54  default -> Collections.EMPTY_SET;
55  };
56  vertexMarker(vertices, getToken(3));
57  done = true;
58  } else if("wsolutions".equals(getMacroName())) {
60  fv.bySelection("true".equals(getToken(2)));
61  Set<FunnelVertex> vertices = switch(getToken(1)) {
62  case "all" -> fv.solutions();
63  case "current" -> CollectionUtil.setOf(fv.currentSolution());
64  default -> Collections.EMPTY_SET;
65  };
66  vertexMarker(vertices, getToken(3));
67  done = true;
68  } else if("wservices".equals(getMacroName())) {
70  fv.bySelection("true".equals(getToken(2)));
71  Set<FunnelVertex> vertices = switch(getToken(1)) {
72  case "all" -> fv.services();
73  case "current" -> CollectionUtil.setOf(fv.currentService());
74  default -> Collections.EMPTY_SET;
75  };
76  vertexMarker(vertices, getToken(3));
77  done = true;
78  } else if("wnexts".equals(getMacroName())) {
80  Set<FunnelVertex> vertices = fv.nexts();
81  vertexMarker(vertices, getToken(1));
82  done = true;
83  } else if("wsiblings".equals(getMacroName())) {
85  Set<FunnelVertex> vertices = fv.siblings();
86  vertexMarker(vertices, getToken(1));
87  done = true;
88  } else if("wgoals".equals(getMacroName())) {
90  Set<WebGoal> goals = fv.goals();
91  goalMarker(goals, getToken(1));
92  done = true;
93  } else if("witem".equals(getMacroName())) {
95  vertexMarker(CollectionUtil.setOf(fv.getVertex(getToken(1))), getToken(2));
96  done = true;
97  } else if("wgoal".equals(getMacroName())) {
99  goalMarker(CollectionUtil.setOf(fv.getGoal(getToken(1))), getToken(2));
100  done = true;
101  } else if("wexternals".equals(getMacroName())) {
103  linkMarker(fv.externals(), getToken(1));
104  done = true;
105  } else if("funneling-graph".equals(getMacroName())) {
107  done = true;
108  }
109  return done;
110  }
111 
112  @Override
113  protected void explainMacros(ParserMacroSet macros) {
114  macros.addMacro("wtargets")
115  .addParameter("mode", false, "all")
116  .addPossibleValue("all")
117  .addPossibleValue("current")
118  .addParameter("selection", true, "true")
119  .addPossibleValue("true")
120  .addPossibleValue("false")
121  .addParameter("template", true, "summary")
122  .addPossibleValue("summary")
123  .addPossibleValue("vlist")
124  .addPossibleValue("hlist");
125  macros.addMacro("wsolutions")
126  .copyParametersFrom("wtargets", macros);
127  macros.addMacro("wservices")
128  .copyParametersFrom("wtargets", macros);
129  macros.addMacro("wnexts")
130  .addParameter("template", true, "summary")
131  .addPossibleValue("summary")
132  .addPossibleValue("vlist")
133  .addPossibleValue("hlist");
134  macros.addMacro("wsiblings")
135  .copyParametersFrom("wnexts", macros);
136  macros.addMacro("wgoals")
137  .addParameter("template", true, "gsummary");
138  macros.addMacro("wcurrent")
139  .addParameter("template", true, "full");
140  macros.addMacro("witem")
141  .addParameter("wtag", false)
142  .addParameter("template", true, "full");
143  macros.addMacro("wgoal")
144  .addParameter("waction", false)
145  .addParameter("template", true, "gfull");
146  macros.addMacro("wexternals")
147  .addParameter("template", true, "webLinks");
148  }
149 
150  private void vertexMarker(Set<FunnelVertex> vertices, String template) {
151  if(CollectionUtil.from(vertices).isEmpty()) return;
153  marker.put("vertices", vertices);
154  marker.put("backLink", TagsServlet.navigateBack(constructor, null));
155  out.write(marker.parse("web/funnel", template));
156  }
157 
158  private void goalMarker(Set<WebGoal> goals, String template) {
159  if(CollectionUtil.from(goals).isEmpty()) return;
160  ElephantMarker marker = new ElephantMarker(constructor);
161  marker.put("goals", goals);
162  out.write(marker.parse("web/funnel", template));
163  }
164 
165  private void linkMarker(Set<WebLink> links, String template) {
166  ElephantMarker marker = new ElephantMarker(constructor);
167  marker.put("links", links);
168  out.write(marker.parse("web/funnel", template));
169  }
170 
171 }
static String navigateBack(IConstructor constructor, String path)
String parse(String rootTmpl, String tmpl)
Object put(Object key, Object value)
ParserMacro addMacro(String macroName)
ParserMacro addPossibleValue(String value)
void copyParametersFrom(String macroName, ParserMacroSet macros)
ParserMacro addParameter(String name, boolean optional)
void explainMacros(ParserMacroSet macros)
Definition: WebParser.java:113
static FunnelVertices load(IConstructor constructor)
FunnelVertices bySelection(boolean value)