Skip to content

Commit

Permalink
impl, junit tests
Browse files Browse the repository at this point in the history
Issue #340
  • Loading branch information
rsoika committed Mar 2, 2024
1 parent cade742 commit 7c45a9c
Show file tree
Hide file tree
Showing 10 changed files with 382 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
import org.eclipse.glsp.server.features.validation.MarkerKind;
import org.eclipse.glsp.server.features.validation.MarkersReason;
import org.eclipse.glsp.server.features.validation.ModelValidator;
import org.openbpmn.bpmn.validation.BPMNValidationError;
import org.openbpmn.bpmn.BPMNModel;
import org.openbpmn.bpmn.validation.BPMNValidationHandler;
import org.openbpmn.bpmn.validation.BPMNValidationMarker;
import org.openbpmn.glsp.model.BPMNGModelState;

import com.google.inject.Inject;
Expand Down Expand Up @@ -52,15 +54,38 @@ public class BPMNGLSPValidator implements ModelValidator {

private List<Marker> bpmnMarkers = null;;

@Override
public List<Marker> validate(final List<GModelElement> elements, final String reason) {
public void beforeValidate(final List<GModelElement> elements, final String reason) {

logger.info("---==== Starte Validate new....");
bpmnMarkers = new ArrayList<>();

// init bpmn marker list....
if (MarkersReason.BATCH.equals(reason)) {
createBPMNMarkers();
createBPMNMarkers(modelState.getBpmnModel(), true);
}
if (MarkersReason.LIVE.equals(reason)) {
createBPMNMarkers(modelState.getBpmnModel(), false);
}
}

public void afterValidate(final List<GModelElement> elements, final String reason) {
// no op
}

@Override
public List<Marker> validate(final List<GModelElement> elements, final String reason) {
long l = System.currentTimeMillis();
beforeValidate(elements, reason);
List<Marker> markers = doValidate(elements, reason);
afterValidate(elements, reason);

logger.info("--------------------> validation took " + (System.currentTimeMillis() - l) + " ms");
return markers;
}

private List<Marker> doValidate(final List<GModelElement> elements, final String reason) {
List<Marker> markers = new ArrayList<>();

for (GModelElement element : elements) {
if (MarkersReason.LIVE.equals(reason)) {
markers.addAll(doLiveValidation(element));
Expand All @@ -70,17 +95,16 @@ public List<Marker> validate(final List<GModelElement> elements, final String re
markers.addAll(doValidationForCustomReason(element, reason));
}
if (!element.getChildren().isEmpty()) {
markers.addAll(validate(element.getChildren(), reason));
markers.addAll(doValidate(element.getChildren(), reason));
}
}

return markers;
}

@Override
public List<Marker> doBatchValidation(final GModelElement element) {

logger.info("...validate " + element.getId());
logger.info("...BatchValidate " + element.getId());

List<Marker> result = new ArrayList<>();
for (Marker marker : bpmnMarkers) {
Expand All @@ -92,16 +116,39 @@ public List<Marker> doBatchValidation(final GModelElement element) {
return result;
}

private void createBPMNMarkers() {
@Override
public List<Marker> doLiveValidation(GModelElement element) {

logger.info("...LiveValidate " + element.getId());

List<Marker> result = new ArrayList<>();
for (Marker marker : bpmnMarkers) {
if (marker.getElementId().equals(element.getId())) {
result.add(marker);
}
}

return result;
}

/**
* This method validates a given BPMNModel. It returns a list of
* validationErrors that contains a lable and a short description pointing to an
* element that causes a problem.
* <p>
*
* This method calls the Meta Model Validation method and converts the Meta
* Model Error markers into GLSP marker objects.
*/
private void createBPMNMarkers(BPMNModel model, boolean forceValidation) {
logger.fine("...starting validating model...");
bpmnMarkers = new ArrayList<>();

// Meta Model validation...
List<BPMNValidationError> errorList = modelState.getBpmnModel().validate();
List<BPMNValidationMarker> errorList = new BPMNValidationHandler().validate(model, forceValidation);

// Convert ErrorList into a GLSP Marker List
for (BPMNValidationError _error : errorList) {
if (BPMNValidationError.ErrorType.ERROR.equals(_error.getErrorType())) {
for (BPMNValidationMarker _error : errorList) {
if (BPMNValidationMarker.ErrorType.ERROR.equals(_error.getErrorType())) {
bpmnMarkers.add(new Marker(_error.getLabel(), _error.getDescription(), _error.getElementId(),
MarkerKind.ERROR));

Expand Down
18 changes: 0 additions & 18 deletions open-bpmn.metamodel/src/main/java/org/openbpmn/bpmn/BPMNModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@
import org.openbpmn.bpmn.exceptions.BPMNMissingElementException;
import org.openbpmn.bpmn.exceptions.BPMNModelException;
import org.openbpmn.bpmn.util.BPMNXMLUtil;
import org.openbpmn.bpmn.validation.BPMNValidationError;
import org.openbpmn.bpmn.validation.BPMNValidationHandler;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
Expand Down Expand Up @@ -1979,20 +1977,4 @@ private void writeXml(Document doc, OutputStream output) throws TransformerExcep
// === BUGFIX END ===
}

/**
* This method validates the current model. It returns a list of
* validationErrors that contains a lable and a short description pointing to an
* element that causes a problem.
* <p>
* If the list is empty, no errors where detected
*
* @return list of BPMNValidationError. Can be empty if no errors where
* detected.
*/
public List<BPMNValidationError> validate() {
BPMNValidationHandler validationHandler = new BPMNValidationHandler(this);
validationHandler.validate();
return validationHandler.getValidationErrors();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.openbpmn.bpmn.BPMNTypes;
import org.openbpmn.bpmn.elements.core.BPMNElementNode;
import org.openbpmn.bpmn.exceptions.BPMNModelException;
import org.openbpmn.bpmn.validation.BPMNValidationMarker;
import org.w3c.dom.Element;

/**
Expand Down Expand Up @@ -96,4 +97,21 @@ public List<Event> getAllBoundaryEvents() {
return result;
}

/**
* Validate Start, End, Catch and Throw event types
*/
@Override
public List<BPMNValidationMarker> validate() {
resetValidation();

if (this.getIngoingSequenceFlows().size() == 0
|| this.getOutgoingSequenceFlows().size() == 0) {
this.addValidationMarker(new BPMNValidationMarker("Task",
"A Task must have at least one ingoing and one outgoing Sequence Flow!", this.getId(),
BPMNValidationMarker.ErrorType.ERROR));
}

return this.getValidationMarkers();
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.openbpmn.bpmn.elements;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import org.openbpmn.bpmn.BPMNModel;
Expand All @@ -9,6 +10,7 @@
import org.openbpmn.bpmn.elements.core.BPMNElementNode;
import org.openbpmn.bpmn.exceptions.BPMNMissingElementException;
import org.openbpmn.bpmn.exceptions.BPMNModelException;
import org.openbpmn.bpmn.validation.BPMNValidationMarker;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
Expand Down Expand Up @@ -163,4 +165,64 @@ public Set<Element> getEventDefinitionsByType(String definitionType) {

}

/**
* Validate Start, End, Catch and Throw event types
*/
@Override
public List<BPMNValidationMarker> validate() {
resetValidation();
// Start event?
if (BPMNTypes.START_EVENT.equals(this.getType())) {

if (this.getIngoingSequenceFlows().size() > 0) {
this.addValidationMarker(new BPMNValidationMarker("Start Event",
"A Start Event may not have incoming Sequence Flows!", this.getId(),
BPMNValidationMarker.ErrorType.ERROR));

}
if (this.getOutgoingSequenceFlows().size() != 1) {
this.addValidationMarker(new BPMNValidationMarker("Start Event",
"A Start Event must have exactly one outgoing Sequence Flow!", this.getId(),
BPMNValidationMarker.ErrorType.ERROR));
}

}

// END event?
if (BPMNTypes.END_EVENT.equals(this.getType())) {

if (this.getIngoingSequenceFlows().size() == 0) {
this.addValidationMarker(new BPMNValidationMarker("End Event",
"An End Event must have at least one incoming Sequence Flow!", this.getId(),
BPMNValidationMarker.ErrorType.ERROR));
}
if (this.getOutgoingSequenceFlows().size() > 0) {
this.addValidationMarker(new BPMNValidationMarker("End Event",
"An End Event must NOT have any outgoing Sequence Flow!", this.getId(),
BPMNValidationMarker.ErrorType.ERROR));
}
}

// Catch Event?
if (BPMNTypes.CATCH_EVENT.equals(this.getType())) {
if (this.getOutgoingSequenceFlows().size() == 0) {
this.addValidationMarker(new BPMNValidationMarker("Catch Event",
"A Catch Event must have at least one outgoing Sequence Flow!", this.getId(),
BPMNValidationMarker.ErrorType.ERROR));
}
}

// Throw Event?
if (BPMNTypes.THROW_EVENT.equals(this.getType())) {
if (this.getIngoingSequenceFlows().size() == 0) {
this.addValidationMarker(new BPMNValidationMarker("Throw Event",
"A Throw Event must have at least one ingoing Sequence Flow!", this.getId(),
BPMNValidationMarker.ErrorType.ERROR));
}

}

return this.getValidationMarkers();
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package org.openbpmn.bpmn.elements;

import java.util.List;

import org.openbpmn.bpmn.BPMNModel;
import org.openbpmn.bpmn.elements.core.BPMNElementNode;
import org.openbpmn.bpmn.exceptions.BPMNModelException;
import org.openbpmn.bpmn.validation.BPMNValidationMarker;
import org.w3c.dom.Element;

public class Gateway extends BPMNElementNode {
Expand All @@ -24,4 +27,21 @@ public double getDefaultWidth() {
public double getDefaultHeight() {
return DEFAULT_HEIGHT;
}

/**
* Validate Start, End, Catch and Throw event types
*/
@Override
public List<BPMNValidationMarker> validate() {
resetValidation();

if (this.getIngoingSequenceFlows().size() == 0
|| this.getOutgoingSequenceFlows().size() == 0) {
this.addValidationMarker(new BPMNValidationMarker("Gateway",
"A Gateway must have at least one ingoing and one outgoing Sequence Flow!", this.getId(),
BPMNValidationMarker.ErrorType.ERROR));
}

return this.getValidationMarkers();
}
}
Loading

0 comments on commit 7c45a9c

Please sign in to comment.