Skip to content

Commit

Permalink
some refactoring of MojoValidator.java
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasgeiger committed Oct 31, 2017
1 parent 02c9275 commit 4b7fb80
Showing 1 changed file with 88 additions and 52 deletions.
140 changes: 88 additions & 52 deletions src/main/java/de/uniba/dsg/bpmnspector/MojoValidator.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
package de.uniba.dsg.bpmnspector;

import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.util.List;

import api.Location;
import api.LocationCoordinate;
import api.ValidationException;
import api.ValidationResult;
import api.Violation;
import api.Warning;
import api.*;
import de.jena.uni.mojo.Mojo;
import de.jena.uni.mojo.analysis.information.AnalysisInformation;
import de.jena.uni.mojo.error.AbundanceAnnotation;
Expand All @@ -29,10 +20,16 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.util.List;

public class MojoValidator implements BpmnProcessValidator {

private static final Logger LOGGER = (Logger) LoggerFactory.getLogger(MojoValidator.class.getSimpleName());

private final String MOJO_MESSAGE_PREFIX = "mojo";

private final Mojo mojo;
private final XMLOutputter xmlOutputter = new XMLOutputter();

Expand All @@ -47,58 +44,96 @@ public void validate(BPMNProcess process, ValidationResult result) throws Valida
// FIXME Use processAsString information as soon as Mojo is capable to do this
List<Annotation> mojoResult = mojo.verify(process.getBaseURI(), processAsString, "bpmn.xml", analysisInformation, StandardCharsets.UTF_8);
//List<Annotation> mojoResult = mojo.verify(new File(process.getBaseURI()), analysisInformation);
addMojoResultToValidationResult(mojoResult, result, process);
if (!mojoResult.isEmpty()) {
addMojoResultToValidationResult(mojoResult, result, process);
}

}

private void addMojoResultToValidationResult(List<Annotation> mojoResult, ValidationResult validationResult, BPMNProcess baseProcess)
throws ValidationException {
if (!mojoResult.isEmpty()) {
for (Annotation annotation : mojoResult) {
for (Annotation annotation : mojoResult) {

LOGGER.debug("Found an mojo annotation: Class: " + annotation.getClass() + " AlarmCategory: " + annotation.getAlarmCategory());
if (LOGGER.isDebugEnabled()) {
annotation.printInformation(null);
System.out.println("Annotation class: " + annotation.getClass() + " AlarmCategory: " + annotation.getAlarmCategory());
if (annotation instanceof DeadlockAnnotation) {
String message = "Mojo: Process contains a deadlock.\nPaths to deadlock:\n";
List<List<AbstractEdge>> listOfPaths = ((DeadlockAnnotation) annotation).getListOfFailurePaths();
for (List<AbstractEdge> path : listOfPaths) {
message += "Path: ";
for (int i = 0; i < path.size(); i++) {
AbstractEdge edge = path.get(i);
if (edge.source instanceof BaseElement) {
message += edge.source.getClass().getSimpleName() + "[" + ((BaseElement) edge.source).getId() + "] \u2192 ";
}
}
Object lastTarget = path.get(path.size() - 1).target;
message += lastTarget.getClass().getSimpleName() + "[" + ((BaseElement) lastTarget).getId() + "])";
message += "\n";
}
String bpmnId = ((BaseElement) annotation.getInterpretedPrintableNodes().get(0)).getId();
Location locationOfViolation = searchForViolationFile(bpmnId, baseProcess);
Violation violation = new Violation(locationOfViolation, message, "Deadlock");
validationResult.addViolation(violation);
} else if (annotation instanceof AbundanceAnnotation) {
String message = "Mojo: Process contains a lack of synchronization.";
String bpmnId = ((BaseElement) annotation.getInterpretedPrintableNodes().get(0)).getId();
Location locationOfViolation = searchForViolationFile(bpmnId, baseProcess);
Violation violation = new Violation(locationOfViolation, message, "LackOfSync");
validationResult.addViolation(violation);
} else if (EAlarmCategory.ERROR.equals(annotation.getAlarmCategory())) {
throw new ValidationException("Unknown error during mojo execution.");
} else if (EAlarmCategory.WARNING.equals(annotation.getAlarmCategory())) {
Location locationOfWarning;
if (annotation.getInterpretedPrintableNodes().isEmpty()) {
locationOfWarning = new Location(Paths.get(baseProcess.getBaseURI()).toAbsolutePath(), LocationCoordinate.EMPTY);
} else {
String bpmnId = ((BaseElement) annotation.getInterpretedPrintableNodes().get(0)).getId();
locationOfWarning = searchForViolationFile(bpmnId, baseProcess);
}
Warning warning = new Warning(annotation.getDescription(), locationOfWarning);
validationResult.addWarning(warning);
}

if (annotation instanceof DeadlockAnnotation) {
handleDeadlockAnnotation(validationResult, baseProcess, annotation);
} else if (annotation instanceof AbundanceAnnotation) {
handleAbundanceAnnotation(validationResult, baseProcess, annotation);
} else if (EAlarmCategory.WARNING.equals(annotation.getAlarmCategory())) {
handleWarningAnnotation(validationResult, baseProcess, annotation);
} else if (EAlarmCategory.ERROR.equals(annotation.getAlarmCategory())) {
throw new ValidationException("Unknown error during mojo execution.");
}
}
}

private void handleDeadlockAnnotation(ValidationResult validationResult, BPMNProcess baseProcess, Annotation annotation) throws ValidationException {
StringBuilder builder = new StringBuilder(MOJO_MESSAGE_PREFIX);
builder.append(": Process contains a deadlock.\nPaths to deadlock:\n");
List<List<AbstractEdge>> listOfPaths = ((DeadlockAnnotation) annotation).getListOfFailurePaths();
for (List<AbstractEdge> path : listOfPaths) {
builder.append("Path: ");
for (int i = 0; i < path.size(); i++) {
AbstractEdge edge = path.get(i);
if (edge.source instanceof BaseElement) {
String edgeString = String.format("%s[%s] \u2192", edge.source.getClass().getSimpleName(), ((BaseElement) edge.source).getId());
builder.append(edgeString);
}
}
Object lastTarget = path.get(path.size() - 1).target;
String lastTargetString = String.format("%s[%s])%n",
lastTarget.getClass().getSimpleName(),
((BaseElement) lastTarget).getId());
builder.append(lastTargetString);
}
String bpmnId = ((BaseElement) annotation.getInterpretedPrintableNodes().get(0)).getId();
Location locationOfViolation = determineLocationByXPath(bpmnId, baseProcess);
Violation violation = new Violation(locationOfViolation, builder.toString(), "Deadlock");
validationResult.addViolation(violation);
}

private void handleAbundanceAnnotation(ValidationResult validationResult, BPMNProcess baseProcess, Annotation annotation) throws ValidationException {
String message = MOJO_MESSAGE_PREFIX+"Process contains a lack of synchronization.";
String bpmnId = ((BaseElement) annotation.getInterpretedPrintableNodes().get(0)).getId();
Location locationOfViolation = determineLocationByXPath(bpmnId, baseProcess);
Violation violation = new Violation(locationOfViolation, message, "LackOfSync");
validationResult.addViolation(violation);
}

private void handleWarningAnnotation(ValidationResult validationResult, BPMNProcess baseProcess, Annotation annotation) throws ValidationException {
if (annotation.getInterpretedPrintableNodes().isEmpty()) {
validationResult.addWarning(createMojoWarning(annotation.getDescription(), baseProcess));
} else {
BaseElement problematicElement = ((BaseElement) annotation.getInterpretedPrintableNodes().get(0));
validationResult.addWarning(createMojoWarning(annotation.getDescription(), baseProcess, problematicElement));
}
}

private Warning createMojoWarning(String message, BPMNProcess affectedProcess, BaseElement affectedElement) throws ValidationException {
String fullMessage;
Location locationOfWarning;

if (affectedElement == null) {
locationOfWarning = new Location(Paths.get(affectedProcess.getBaseURI()), LocationCoordinate.EMPTY);
fullMessage = String.format("%s: %s", MOJO_MESSAGE_PREFIX, message);
} else {
String bpmnId = affectedElement.getId();
locationOfWarning = determineLocationByXPath(bpmnId, affectedProcess);
fullMessage = String.format("%s: %s: %s", MOJO_MESSAGE_PREFIX, affectedElement.getClass().getSimpleName(), message);
}

return new Warning(fullMessage, locationOfWarning);
}

private Warning createMojoWarning(String message, BPMNProcess affectedProcess) throws ValidationException {
return createMojoWarning(message, affectedProcess, null);
}

private Location searchForViolationFile(String xpathExpression, BPMNProcess baseProcess)
private Location determineLocationByXPath(String xpathExpression, BPMNProcess baseProcess)
throws ValidationException {

LOGGER.debug("Found ID:" + xpathExpression);
Expand Down Expand Up @@ -138,4 +173,5 @@ private Location searchForViolationFile(String xpathExpression, BPMNProcess base
private static String createIdBpmnExpression(String id) {
return String.format("//bpmn:*[@id = '%s']", id);
}

}

0 comments on commit 4b7fb80

Please sign in to comment.