Skip to content

Package: CommunicationManager

CommunicationManager

nameinstructionbranchcomplexitylinemethod
CommunicationManager(RouterConfiguration)
M: 0 C: 40
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
addDependsList(String, String)
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
addError(String)
M: 0 C: 17
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
addFatalError(String)
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
addProvidesList(String, String)
M: 0 C: 23
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
addValue(String, Integer, AbstractReturnValue)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
addValue(String, String, AbstractReturnValue)
M: 0 C: 84
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 14
100%
M: 0 C: 1
100%
addWarning(String)
M: 0 C: 17
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
checkProvidesAndDepends()
M: 1 C: 51
98%
M: 1 C: 9
90%
M: 1 C: 5
83%
M: 1 C: 11
92%
M: 0 C: 1
100%
clean()
M: 30 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
containsVariable(String)
M: 0 C: 21
100%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 0 C: 6
100%
M: 0 C: 1
100%
createLocalPackage()
M: 0 C: 41
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
findAssignsToDefAndContext(String, List, VariableContext)
M: 0 C: 40
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 11
100%
M: 0 C: 1
100%
finish()
M: 11 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
getErrors()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getFailed()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getFatalerrors()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getRouterConfiguration()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getValueByAssignment(VariableAssignment)
M: 6 C: 77
93%
M: 1 C: 11
92%
M: 1 C: 6
86%
M: 1 C: 17
94%
M: 0 C: 1
100%
getVariableAssignment(String)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getVariableAssignmentValue(String)
M: 0 C: 30
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
getVariableAssignmentsByDef(String)
M: 0 C: 31
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
getVariableTypeRegEx(String)
M: 13 C: 17
57%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 2 C: 4
67%
M: 0 C: 1
100%
getWarnings()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
interpret()
M: 0 C: 9
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
scanAndParse(Reader)
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%

Coverage

1: package pruefskript;
2:
3: import java.io.Reader;
4: import java.util.ArrayList;
5: import java.util.HashMap;
6: import java.util.Iterator;
7: import java.util.List;
8: import java.util.Locale;
9: import java.util.Map;
10: import java.util.TreeMap;
11:
12: import basic.PruefskriptConstants;
13: import model.Comment;
14: import model.ConfiguredPackage;
15: import model.Package;
16: import model.Position;
17: import model.RouterConfiguration;
18: import model.assignment.VariableAssignment;
19: import model.definition.AbstractVariableDefinition;
20: import model.definition.ConfigurationContext;
21: import model.definition.DefinitionFile;
22: import model.definition.ExtscriptContext;
23: import model.definition.HasNotDefault;
24: import model.definition.HasnotOpt;
25: import model.definition.Opt;
26: import model.definition.VariableContext;
27: import model.definition.VariableDefinitionSingle;
28: import model.type.AbstractVariableType;
29: import model.type.NamedVariableType;
30: import model.type.ReferenceIsUnresolvedException;
31: import model.type.TypeFile;
32: import pruefskript.parser.ExtParser;
33: import pruefskript.parser.exceptions.CheckScriptException;
34: import pruefskript.parser.exceptions.DependsException;
35: import pruefskript.parser.exceptions.FatalErrorException;
36: import pruefskript.parser.exceptions.ProvidesException;
37: import pruefskript.parser.exceptions.VariableDoesNotExistException;
38: import pruefskript.parser.exceptions.VariableTypeDoesNotExistException;
39: import pruefskript.parser.exceptions.WrongNodeException;
40: import pruefskript.parser.nodes.DependsProvidesElement;
41: import pruefskript.parser.nodes.IteratorHelper;
42: import pruefskript.parser.nodes.SkriptNode;
43: import pruefskript.parser.values.AbstractReturnValue;
44: import pruefskript.parser.values.BooleanValue;
45: import pruefskript.parser.values.NumericValue;
46: import pruefskript.parser.values.StringValue;
47: import pruefskript.parser.values.VersionValue;
48:
49: /**
50: * The CommunicationManager works as an Interface to communicate with scanner, parser and
51: * interpreter for ext-scripts.
52: *
53: * @author Phil
54: *
55: */
56: public final class CommunicationManager {
57:         /**
58:          * The {@link RouterConfiguration} to use.
59:          */
60:         private final RouterConfiguration rc;
61:         /**
62:          * failed is true, if a warning, an error or a fatal error occurred.
63:          */
64:         private Boolean failed;
65:
66:         /**
67:          * A list of errors. Could be empty but not null.
68:          */
69:         private List<String> errors;
70:         /**
71:          * A list of warnings. Could be empty but not null.
72:          */
73:         private List<String> warnings;
74:         /**
75:          * A list of fatalerrors. Could be empty but not null.
76:          */
77:         private List<String> fatalerrors;
78:
79:         /**
80:          * A list of provides. Could be empty but not null.
81:          */
82:         private List<DependsProvidesElement> provides;
83:
84:         /**
85:          * syntax tree of the given extskript.
86:          */
87:         private SkriptNode mySkript;
88:         /**
89:         *
90:         */
91:         private List<DependsProvidesElement> dependsList;
92:         /**
93:         *
94:         */
95:         private final Map<String, IteratorHelper> tempIterationVars =
96:                         new HashMap<String, IteratorHelper>();
97:
98:         /**
99:          * Initiates the CommunicationManager.
100:          *
101:          * @param rc
102:          * The {@link RouterConfiguration} to use.
103:          */
104:         public CommunicationManager(final RouterConfiguration rc) {
105:                 super();
106:                 this.rc = rc;
107:                 this.errors = new ArrayList<String>();
108:                 this.warnings = new ArrayList<String>();
109:                 this.fatalerrors = new ArrayList<String>();
110:                 this.provides = new ArrayList<DependsProvidesElement>();
111:                 this.dependsList = new ArrayList<DependsProvidesElement>();
112:                 this.failed = false;
113:         }
114:
115:         /**
116:          * @return the associated RouterConfiguration
117:          */
118:         public RouterConfiguration getRouterConfiguration() {
119:                 return this.rc;
120:         }
121:
122:         /**
123:          * Scanns and Parses the given extskript and setts the private field mySkript.
124:          *
125:          * @param extSkript
126:          * The script to scan and parse.
127:          */
128:         public void scanAndParse(final Reader extSkript) {
129:                 final ExtParser parser = new ExtParser(extSkript);
130:                 parser.yyparse();
131:                 this.mySkript = (SkriptNode) parser.getMyTree();
132:         }
133:
134:         /**
135:          * Interprets the Syntaxtree. Needs scan and parse before.
136:          *
137:          * @throws CheckScriptException
138:          * if there is any problem with the nodes.
139:          */
140:         public void interpret() throws CheckScriptException {
141:•                if (this.mySkript != null) {
142:                         this.mySkript.interpret(this);
143:                 }
144:         }
145:
146:         /**
147:          * Checks the Provides and Depends. Needs scan and parse and interpret before. Deletes all local
148:          * variables. Resets the communication manager.
149:          *
150:          * @throws CheckScriptException
151:          * if there is any problem with the nodes.
152:          */
153:         public void finish() throws CheckScriptException {
154:                 checkProvidesAndDepends();
155:                 this.rc.getPackages().remove(basic.PruefskriptConstants.LOCALPACKNAME);
156:                 this.clean();
157:         }
158:
159:         /**
160:          * .
161:          *
162:          * @throws DependsException
163:          * .
164:          */
165:         private void checkProvidesAndDepends() throws DependsException {
166:•                for (DependsProvidesElement actDepends : this.dependsList) {
167:                         boolean ok = false;
168:•                        for (DependsProvidesElement actProvides : this.provides) {
169:•                                if (actProvides.equals(actDepends) && actProvides.compareTo(actDepends) >= 0) {
170:                                         ok = true;
171:                                         break;
172:                                 }
173:                         }
174:•                        if (!ok) {
175:                                 this.fatalerrors.add(actDepends.toString());
176:                                 throw new DependsException(actDepends.toString());
177:                         }
178:                 }
179:         }
180:
181:         /**
182:          * @return the errors
183:          */
184:         public List<String> getErrors() {
185:                 return this.errors;
186:         }
187:
188:         /**
189:          * @param error
190:          * : The error to add. adds an element to the list of errors
191:          */
192:         public void addError(final String error) {
193:                 this.failed = true;
194:                 this.errors.add(PruefskriptConstants.ERRORTEXT + error);
195:         }
196:
197:         /**
198:          * @param warning
199:          * : The error to add. adds an element to the list of errors
200:          */
201:         public void addWarning(final String warning) {
202:                 this.failed = true;
203:                 this.warnings.add(PruefskriptConstants.WARNINGTEXT + warning);
204:         }
205:
206:         /**
207:          * @param fatalerror
208:          * : The error to add. adds an element to the list of errors
209:          * @throws FatalErrorException
210:          * : is thrown if something was added to the list
211:          */
212:         public void addFatalError(final String fatalerror) throws FatalErrorException {
213:                 this.failed = true;
214:                 this.fatalerrors.add(fatalerror);
215:                 throw new FatalErrorException(fatalerror);
216:         }
217:
218:         /**
219:          * @return the warnings
220:          */
221:         public List<String> getWarnings() {
222:                 return this.warnings;
223:         }
224:
225:         /**
226:          * @return the fatalerrors
227:          */
228:         public List<String> getFatalerrors() {
229:                 return this.fatalerrors;
230:         }
231:
232:         /**
233:          * @return the failed
234:          */
235:         public Boolean getFailed() {
236:                 return this.failed;
237:         }
238:
239:         /**
240:          * . .
241:          *
242:          * @param text
243:          * .
244:          * @param version
245:          * .
246:          * @throws ProvidesException
247:          * .
248:          */
249:         public void addProvidesList(final String text, final String version)
250:                         throws ProvidesException {
251:                 final DependsProvidesElement temp = new DependsProvidesElement(text, version);
252:•                if (this.provides.contains(temp)) {
253:                         throw new ProvidesException(temp.toString());
254:                 }
255:                 this.provides.add(temp);
256:         }
257:
258:         /**
259:          * @param version
260:          * .
261:          * @param string
262:          * .
263:          */
264:         public void addDependsList(final String string, final String version) {
265:                 this.dependsList.add(new DependsProvidesElement(string, version));
266:         }
267:
268:         /**
269:          * Returs all Values to an definition like test_% or test_%_% .
270:          *
271:          * @param value
272:          * .
273:          * @return .
274:          * @throws VariableDoesNotExistException
275:          * .
276:          */
277:         public List<AbstractReturnValue> getVariableAssignmentsByDef(final String value)
278:                         throws VariableDoesNotExistException {
279:                 final List<AbstractReturnValue> returnList = new ArrayList<AbstractReturnValue>();
280:                 AbstractVariableDefinition varDef;
281:
282:                 varDef = findAssignsToDefAndContext(value, returnList, new ConfigurationContext());
283:•                if (varDef == null) {
284:                         varDef = findAssignsToDefAndContext(value, returnList, new ExtscriptContext());
285:                 }
286:•                if (varDef == null) {
287:                         throw new VariableDoesNotExistException(value);
288:                 }
289:                 return returnList;
290:         }
291:
292:         /**
293:          * @param context
294:          * .
295:          * @param value
296:          * .
297:          * @param returnList
298:          * .
299:          * @return varDef.
300:          */
301:         private AbstractVariableDefinition findAssignsToDefAndContext(final String value,
302:                         final List<AbstractReturnValue> returnList, final VariableContext context) {
303:                 final AbstractVariableDefinition varDef =
304:                                 this.rc.getRouterSoftware().findDefinition(value, context);
305:
306:•                if (varDef != null) {
307:                         final Map<String, VariableAssignment> assigns =
308:                                         this.rc.findAssesByDefinition(varDef, context);
309:
310:                         final Map<String, VariableAssignment> assignsSorted =
311:                                         new TreeMap<String, VariableAssignment>();
312:                         assignsSorted.putAll(assigns);
313:
314:                         final Iterator<VariableAssignment> it = assignsSorted.values().iterator();
315:•                        while (it.hasNext()) {
316:                                 returnList.add(getValueByAssignment(it.next()));
317:                         }
318:                 }
319:                 return varDef;
320:         }
321:
322:         /**
323:          * @param name
324:          * .
325:          * @return Value to the Assignment of the parameter name.
326:          * @throws VariableDoesNotExistException
327:          * .
328:          */
329:         public AbstractReturnValue getVariableAssignmentValue(final String name)
330:                         throws VariableDoesNotExistException {
331:
332:                 VariableAssignment tempAssign = this.rc.findAssignmentByName(name);
333:•                if (tempAssign != null) {
334:                         return getValueByAssignment(tempAssign);
335:                 }
336:                 tempAssign = this.rc.findAssignmentByName(name, new ExtscriptContext());
337:•                if (tempAssign != null) {
338:                         return getValueByAssignment(tempAssign);
339:                 }
340:                 throw new VariableDoesNotExistException(name);
341:         }
342:
343:         /**
344:          *
345:          * @param tempassignmentlocal
346:          * .
347:          * @return value.
348:          */
349:         private AbstractReturnValue
350:                         getValueByAssignment(final VariableAssignment tempassignmentlocal) {
351:                 AbstractReturnValue value = null;
352:                 final AbstractVariableType type = tempassignmentlocal.getDefinition().getType();
353:•                if (type instanceof NamedVariableType) {
354:                         final String vartype = ((NamedVariableType) type).getName();
355:•                        if (vartype.equalsIgnoreCase(basic.PruefskriptConstants.MODELTYPENUMERIC)) {
356:                                 value = new NumericValue(Integer.valueOf(tempassignmentlocal.getValue()));
357:•                        } else if (vartype.equalsIgnoreCase(basic.PruefskriptConstants.MODELTYPEVERSION)) {
358:                                 value = new VersionValue(tempassignmentlocal.getValue());
359:•                        } else if (vartype.equalsIgnoreCase(basic.PruefskriptConstants.MODELTYPEBOOL)) {
360:•                                if (tempassignmentlocal.getValue().equals(PruefskriptConstants.TRUEVALUE)) {
361:                                         value = new BooleanValue(Boolean.TRUE);
362:                                 } else {
363:                                         value = new BooleanValue(Boolean.FALSE);
364:                                 }
365:                         } else {
366:                                 value = new StringValue(tempassignmentlocal.getValue());
367:                         }
368:                 } else {
369:                         value = new StringValue(tempassignmentlocal.getValue());
370:                 }
371:•                if (this.tempIterationVars.containsKey(tempassignmentlocal.getNameOfAssignment())) {
372:                         value.getIterationVars().putAll(this.tempIterationVars);
373:                 }
374:                 return value;
375:         }
376:
377:         /**
378:          * @param name
379:          * .
380:          * @return VariableAssignment matching the name parameter. If nothing matches result is null.
381:          */
382:         public VariableAssignment getVariableAssignment(final String name) {
383:                 return this.rc.findAssignmentByName(name);
384:         }
385:
386:         /**
387:          * @param name
388:          * .
389:          *
390:          * @return RegEx matching parameter name.
391:          * @throws VariableTypeDoesNotExistException
392:          * .
393:          */
394:         public String getVariableTypeRegEx(final String name)
395:                         throws VariableTypeDoesNotExistException {
396:                 try {
397:                         final NamedVariableType temptype = this.rc.getRouterSoftware().findTypeByName(name);
398:•                        if (temptype == null) {
399:                                 throw new VariableTypeDoesNotExistException(name);
400:                         }
401:
402:                         return temptype.getExpression().toStringResolved();
403:
404:                 } catch (final ReferenceIsUnresolvedException e) {
405:                         throw new VariableTypeDoesNotExistException(e + name);
406:                 }
407:         }
408:
409:         /**
410:          * @param defname
411:          * .
412:          * @param value
413:          * .
414:          * @param index
415:          * .
416:          * @throws WrongNodeException
417:          * .
418:          */
419:         public void addValue(final String defname, final Integer index,
420:                         final AbstractReturnValue value) throws WrongNodeException {
421:                 this.addValue(defname, defname.replace('%', index.toString().charAt(0)), value);
422:         }
423:
424:         /**
425:          * @param defname
426:          * Name of definition
427:          *
428:          * @param name
429:          * of Assignment
430:          *
431:          * @param value
432:          * of Definition
433:          *
434:          * @throws WrongNodeException
435:          * .
436:          */
437:         public void addValue(final String defname, final String name, final AbstractReturnValue value)
438:                         throws WrongNodeException {
439:
440:                 ConfiguredPackage pack = this.rc.getPackage(basic.PruefskriptConstants.LOCALPACKNAME);
441:•                if (pack == null) {
442:                         pack = createLocalPackage();
443:                 }
444:                 final AbstractVariableType type =
445:                                 this.rc.getRouterSoftware().findTypeByName(value.getModelType());
446:                 final VariableDefinitionSingle definition =
447:                                 new VariableDefinitionSingle(defname, type, false, false, new HasNotDefault(),
448:                                                 new HasnotOpt(), new Position(0, 0, ""), new ExtscriptContext());
449:                 pack.getPackage().getDefFile().getDefinitions().put(defname, definition);
450:                 pack.getAssignmentFile().getAssignments().put(name.toUpperCase(Locale.ENGLISH),
451:                                 new VariableAssignment(name.toUpperCase(Locale.ENGLISH), definition,
452:                                                 value.getStringRepresentation(), new Position(0, 0, "")));
453:•                if (value.existsIterationVars()) {
454:                         this.tempIterationVars.putAll(value.getIterationVars());
455:                 }
456:         }
457:
458:         /**
459:          * @return pack .
460:          */
461:         private ConfiguredPackage createLocalPackage() {
462:                 final Package pack =
463:                                 new Package(this.rc.getRouterSoftware(), basic.PruefskriptConstants.LOCALPACKNAME,
464:                                                 new TypeFile(new HashMap<String, NamedVariableType>(),
465:                                                                 new ArrayList<Comment>()),
466:                                                 new DefinitionFile(new HashMap<String, AbstractVariableDefinition>(),
467:                                                                 new ArrayList<Comment>()),
468:                                                 new HashMap<String, Opt>(), null);
469:                 this.rc.getRouterSoftware().add(pack);
470:                 return this.rc.add(pack);
471:         }
472:
473:         /**
474:          * ACHTUNG! Setzt den Manager auf den Startzustand zurueck. ALLE Warnungen UND PROVIDES und
475:          * DEPENDS gehen verloren. NUTZUNG WIRD NICHT EMPFOHLEN, NUR TESTZWECKE!
476:          */
477:         public void clean() {
478:                 this.errors = new ArrayList<String>();
479:                 this.warnings = new ArrayList<String>();
480:                 this.fatalerrors = new ArrayList<String>();
481:                 this.provides = new ArrayList<DependsProvidesElement>();
482:                 this.dependsList = new ArrayList<DependsProvidesElement>();
483:                 this.failed = false;
484:         }
485:
486:         /**
487:          * .
488:          *
489:          * @param name
490:          * . .
491:          * @return .
492:          */
493:         public boolean containsVariable(final String name) {
494:                 boolean contains = true;
495:•                if (this.rc.findAssignmentByName(name) == null) {
496:                         contains = false;
497:                 }
498:•                if (this.rc.findAssignmentByName(name, new ExtscriptContext()) != null) {
499:                         contains = true;
500:                 }
501:                 return contains;
502:         }
503:
504: }