2014-07-16 14:15:23 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-02-02 13:46:21 +00:00
|
|
|
var filter = require('lodash/collection/filter'),
|
|
|
|
find = require('lodash/collection/find'),
|
|
|
|
forEach = require('lodash/collection/forEach');
|
2014-03-13 15:06:30 +00:00
|
|
|
|
2014-07-16 14:15:23 +00:00
|
|
|
var Refs = require('object-refs');
|
|
|
|
|
2014-10-31 14:05:16 +00:00
|
|
|
var elementToString = require('./Util').elementToString;
|
|
|
|
|
2014-07-16 14:15:23 +00:00
|
|
|
var diRefs = new Refs({ name: 'bpmnElement', enumerable: true }, { name: 'di' });
|
2014-03-13 15:06:30 +00:00
|
|
|
|
2015-02-17 09:55:28 +00:00
|
|
|
/**
|
|
|
|
* Returns true if an element has the given meta-model type
|
|
|
|
*
|
|
|
|
* @param {ModdleElement} element
|
|
|
|
* @param {String} type
|
|
|
|
*
|
|
|
|
* @return {Boolean}
|
|
|
|
*/
|
|
|
|
function is(element, type) {
|
|
|
|
return element.$instanceOf(type);
|
|
|
|
}
|
|
|
|
|
2014-10-28 14:09:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Find a suitable display candidate for definitions where the DI does not
|
|
|
|
* correctly specify one.
|
|
|
|
*/
|
|
|
|
function findDisplayCandidate(definitions) {
|
2015-02-02 13:46:21 +00:00
|
|
|
return find(definitions.rootElements, function(e) {
|
2015-02-17 09:55:28 +00:00
|
|
|
return is(e, 'bpmn:Process') || is(e, 'bpmn:Collaboration');
|
2014-10-28 14:09:17 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-07-16 14:15:23 +00:00
|
|
|
|
|
|
|
function BpmnTreeWalker(handler) {
|
2014-03-13 15:06:30 +00:00
|
|
|
|
2014-03-22 00:47:03 +00:00
|
|
|
// list of containers already walked
|
2015-08-25 13:45:44 +00:00
|
|
|
var handledElements = {};
|
2014-03-22 00:47:03 +00:00
|
|
|
|
2014-07-23 16:53:04 +00:00
|
|
|
// list of elements to handle deferred to ensure
|
|
|
|
// prerequisites are drawn
|
|
|
|
var deferred = [];
|
|
|
|
|
2014-03-13 15:06:30 +00:00
|
|
|
///// Helpers /////////////////////////////////
|
|
|
|
|
|
|
|
function contextual(fn, ctx) {
|
|
|
|
return function(e) {
|
|
|
|
fn(e, ctx);
|
|
|
|
};
|
|
|
|
}
|
2014-04-28 09:09:47 +00:00
|
|
|
|
2015-08-25 13:45:44 +00:00
|
|
|
function handled(element) {
|
|
|
|
handledElements[element.id] = element;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isHandled(element) {
|
|
|
|
return handledElements[element.id];
|
|
|
|
}
|
|
|
|
|
2014-07-16 14:15:23 +00:00
|
|
|
function visit(element, ctx) {
|
2014-03-13 15:06:30 +00:00
|
|
|
|
2014-07-16 14:15:23 +00:00
|
|
|
var gfx = element.gfx;
|
2014-03-22 00:47:03 +00:00
|
|
|
|
|
|
|
// avoid multiple rendering of elements
|
|
|
|
if (gfx) {
|
2014-10-28 14:09:17 +00:00
|
|
|
throw new Error('already rendered ' + elementToString(element));
|
2014-03-22 00:47:03 +00:00
|
|
|
}
|
|
|
|
|
2014-03-21 15:46:56 +00:00
|
|
|
// call handler
|
2014-07-16 14:15:23 +00:00
|
|
|
return handler.element(element, ctx);
|
2014-03-13 15:06:30 +00:00
|
|
|
}
|
|
|
|
|
2014-07-01 09:33:28 +00:00
|
|
|
function visitRoot(element, diagram) {
|
|
|
|
return handler.root(element, diagram);
|
|
|
|
}
|
|
|
|
|
2014-03-13 19:21:42 +00:00
|
|
|
function visitIfDi(element, ctx) {
|
2015-08-25 13:45:44 +00:00
|
|
|
|
2014-07-23 16:53:04 +00:00
|
|
|
try {
|
2015-08-25 13:45:44 +00:00
|
|
|
var gfx = element.di && visit(element, ctx);
|
|
|
|
|
|
|
|
handled(element);
|
|
|
|
|
|
|
|
return gfx;
|
2014-07-23 16:53:04 +00:00
|
|
|
} catch (e) {
|
|
|
|
logError(e.message, { element: element, error: e });
|
2014-12-09 17:32:34 +00:00
|
|
|
|
|
|
|
console.error('failed to import ' + elementToString(element));
|
|
|
|
console.error(e);
|
2014-03-13 19:21:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-21 15:46:56 +00:00
|
|
|
function logError(message, context) {
|
|
|
|
handler.error(message, context);
|
|
|
|
}
|
2014-03-13 19:21:42 +00:00
|
|
|
|
2014-03-13 15:06:30 +00:00
|
|
|
////// DI handling ////////////////////////////
|
|
|
|
|
2014-07-16 14:15:23 +00:00
|
|
|
function registerDi(di) {
|
|
|
|
var bpmnElement = di.bpmnElement;
|
2014-03-13 15:06:30 +00:00
|
|
|
|
2014-03-21 15:46:56 +00:00
|
|
|
if (bpmnElement) {
|
2014-11-26 19:54:52 +00:00
|
|
|
if (bpmnElement.di) {
|
|
|
|
logError('multiple DI elements defined for ' + elementToString(bpmnElement), { element: bpmnElement });
|
|
|
|
} else {
|
|
|
|
diRefs.bind(bpmnElement, 'di');
|
|
|
|
bpmnElement.di = di;
|
|
|
|
}
|
2014-03-21 15:46:56 +00:00
|
|
|
} else {
|
2014-10-28 14:09:17 +00:00
|
|
|
logError('no bpmnElement referenced in ' + elementToString(di), { element: di });
|
2014-03-21 15:46:56 +00:00
|
|
|
}
|
2014-03-13 15:06:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function handleDiagram(diagram) {
|
|
|
|
handlePlane(diagram.plane);
|
|
|
|
}
|
|
|
|
|
|
|
|
function handlePlane(plane) {
|
|
|
|
registerDi(plane);
|
|
|
|
|
2015-02-02 13:46:21 +00:00
|
|
|
forEach(plane.planeElement, handlePlaneElement);
|
2014-03-13 15:06:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function handlePlaneElement(planeElement) {
|
|
|
|
registerDi(planeElement);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////// Semantic handling //////////////////////
|
|
|
|
|
2015-11-24 11:53:18 +00:00
|
|
|
/**
|
|
|
|
* Handle definitions and return the rendered diagram (if any)
|
|
|
|
*
|
|
|
|
* @param {ModdleElement} definitions to walk and import
|
|
|
|
* @param {ModdleElement} [diagram] specific diagram to import and display
|
|
|
|
*
|
|
|
|
* @throws {Error} if no diagram to display could be found
|
|
|
|
*/
|
2014-03-13 15:06:30 +00:00
|
|
|
function handleDefinitions(definitions, diagram) {
|
|
|
|
// make sure we walk the correct bpmnElement
|
2014-04-28 09:09:47 +00:00
|
|
|
|
2014-03-13 15:06:30 +00:00
|
|
|
var diagrams = definitions.diagrams;
|
|
|
|
|
|
|
|
if (diagram && diagrams.indexOf(diagram) === -1) {
|
|
|
|
throw new Error('diagram not part of bpmn:Definitions');
|
|
|
|
}
|
|
|
|
|
2014-07-01 09:33:28 +00:00
|
|
|
if (!diagram && diagrams && diagrams.length) {
|
|
|
|
diagram = diagrams[0];
|
2014-03-17 10:11:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// no diagram -> nothing to import
|
|
|
|
if (!diagram) {
|
2015-11-24 11:53:18 +00:00
|
|
|
throw new Error('no diagram to display');
|
2014-03-13 15:06:30 +00:00
|
|
|
}
|
|
|
|
|
2014-03-22 00:47:03 +00:00
|
|
|
// load DI from selected diagram only
|
|
|
|
handleDiagram(diagram);
|
|
|
|
|
2014-03-13 15:06:30 +00:00
|
|
|
|
2014-10-28 14:09:17 +00:00
|
|
|
var plane = diagram.plane;
|
|
|
|
|
|
|
|
if (!plane) {
|
|
|
|
throw new Error('no plane for ' + elementToString(diagram));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var rootElement = plane.bpmnElement;
|
|
|
|
|
|
|
|
// ensure we default to a suitable display candidate (process or collaboration),
|
|
|
|
// even if non is specified in DI
|
2014-05-23 14:31:28 +00:00
|
|
|
if (!rootElement) {
|
2014-10-28 14:09:17 +00:00
|
|
|
rootElement = findDisplayCandidate(definitions);
|
|
|
|
|
|
|
|
if (!rootElement) {
|
2015-11-24 11:53:18 +00:00
|
|
|
throw new Error('no process or collaboration to display');
|
2014-10-28 14:09:17 +00:00
|
|
|
} else {
|
|
|
|
|
|
|
|
logError('correcting missing bpmnElement on ' + elementToString(plane) + ' to ' + elementToString(rootElement));
|
|
|
|
|
|
|
|
// correct DI on the fly
|
|
|
|
plane.bpmnElement = rootElement;
|
|
|
|
registerDi(plane);
|
|
|
|
}
|
2014-05-23 14:31:28 +00:00
|
|
|
}
|
|
|
|
|
2014-07-01 09:33:28 +00:00
|
|
|
|
|
|
|
var ctx = visitRoot(rootElement, plane);
|
|
|
|
|
2014-03-13 15:06:30 +00:00
|
|
|
if (is(rootElement, 'bpmn:Process')) {
|
2014-07-01 09:33:28 +00:00
|
|
|
handleProcess(rootElement, ctx);
|
2014-07-15 10:43:30 +00:00
|
|
|
} else if (is(rootElement, 'bpmn:Collaboration')) {
|
2014-07-01 09:33:28 +00:00
|
|
|
handleCollaboration(rootElement, ctx);
|
2014-03-22 00:47:03 +00:00
|
|
|
|
|
|
|
// force drawing of everything not yet drawn that is part of the target DI
|
2014-07-01 09:33:28 +00:00
|
|
|
handleUnhandledProcesses(definitions.rootElements, ctx);
|
2014-03-13 15:06:30 +00:00
|
|
|
} else {
|
2014-10-28 14:09:17 +00:00
|
|
|
throw new Error('unsupported bpmnElement for ' + elementToString(plane) + ' : ' + elementToString(rootElement));
|
2014-03-13 15:06:30 +00:00
|
|
|
}
|
2014-07-23 16:53:04 +00:00
|
|
|
|
|
|
|
// handle all deferred elements
|
|
|
|
handleDeferred(deferred);
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleDeferred(deferred) {
|
2015-02-02 13:46:21 +00:00
|
|
|
forEach(deferred, function(d) { d(); });
|
2014-03-13 15:06:30 +00:00
|
|
|
}
|
|
|
|
|
2014-07-15 10:43:30 +00:00
|
|
|
function handleProcess(process, context) {
|
|
|
|
handleFlowElementsContainer(process, context);
|
|
|
|
handleIoSpecification(process.ioSpecification, context);
|
|
|
|
|
|
|
|
handleArtifacts(process.artifacts, context);
|
|
|
|
|
|
|
|
// log process handled
|
2015-08-25 13:45:44 +00:00
|
|
|
handled(process);
|
2014-07-15 10:43:30 +00:00
|
|
|
}
|
|
|
|
|
2014-03-22 00:47:03 +00:00
|
|
|
function handleUnhandledProcesses(rootElements) {
|
|
|
|
|
|
|
|
// walk through all processes that have not yet been drawn and draw them
|
2014-07-01 09:33:28 +00:00
|
|
|
// if they contain lanes with DI information.
|
|
|
|
// we do this to pass the free-floating lane test cases in the MIWG test suite
|
2015-02-02 13:46:21 +00:00
|
|
|
var processes = filter(rootElements, function(e) {
|
2015-08-25 13:45:44 +00:00
|
|
|
return !isHandled(e) && is(e, 'bpmn:Process') && e.laneSets;
|
2014-03-22 00:47:03 +00:00
|
|
|
});
|
2014-07-01 09:33:28 +00:00
|
|
|
|
2014-03-22 00:47:03 +00:00
|
|
|
processes.forEach(contextual(handleProcess));
|
|
|
|
}
|
|
|
|
|
2014-07-15 10:43:30 +00:00
|
|
|
function handleMessageFlow(messageFlow, context) {
|
|
|
|
visitIfDi(messageFlow, context);
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleMessageFlows(messageFlows, context) {
|
2015-02-02 13:46:21 +00:00
|
|
|
forEach(messageFlows, contextual(handleMessageFlow, context));
|
2014-07-15 10:43:30 +00:00
|
|
|
}
|
|
|
|
|
2014-03-13 19:21:42 +00:00
|
|
|
function handleDataAssociation(association, context) {
|
|
|
|
visitIfDi(association, context);
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleDataInput(dataInput, context) {
|
|
|
|
visitIfDi(dataInput, context);
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleDataOutput(dataOutput, context) {
|
|
|
|
visitIfDi(dataOutput, context);
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleArtifact(artifact, context) {
|
|
|
|
|
|
|
|
// bpmn:TextAnnotation
|
|
|
|
// bpmn:Group
|
|
|
|
// bpmn:Association
|
2014-04-28 09:09:47 +00:00
|
|
|
|
2014-03-13 19:21:42 +00:00
|
|
|
visitIfDi(artifact, context);
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleArtifacts(artifacts, context) {
|
2014-10-31 14:05:16 +00:00
|
|
|
|
2015-02-02 13:46:21 +00:00
|
|
|
forEach(artifacts, function(e) {
|
2014-10-31 14:05:16 +00:00
|
|
|
if (is(e, 'bpmn:Association')) {
|
|
|
|
deferred.push(function() {
|
|
|
|
handleArtifact(e, context);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
handleArtifact(e, context);
|
|
|
|
}
|
|
|
|
});
|
2014-03-13 19:21:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function handleIoSpecification(ioSpecification, context) {
|
2014-04-28 09:09:47 +00:00
|
|
|
|
2014-03-13 19:21:42 +00:00
|
|
|
if (!ioSpecification) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-02 13:46:21 +00:00
|
|
|
forEach(ioSpecification.dataInputs, contextual(handleDataInput, context));
|
|
|
|
forEach(ioSpecification.dataOutputs, contextual(handleDataOutput, context));
|
2014-03-13 19:21:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function handleSubProcess(subProcess, context) {
|
|
|
|
handleFlowElementsContainer(subProcess, context);
|
|
|
|
handleArtifacts(subProcess.artifacts, context);
|
|
|
|
}
|
|
|
|
|
2014-03-13 15:06:30 +00:00
|
|
|
function handleFlowNode(flowNode, context) {
|
2014-03-13 19:21:42 +00:00
|
|
|
var childCtx = visitIfDi(flowNode, context);
|
2014-03-13 15:06:30 +00:00
|
|
|
|
2014-03-13 19:21:42 +00:00
|
|
|
if (is(flowNode, 'bpmn:SubProcess')) {
|
|
|
|
handleSubProcess(flowNode, childCtx || context);
|
|
|
|
}
|
2015-08-25 13:45:44 +00:00
|
|
|
|
|
|
|
if (is(flowNode, 'bpmn:Activity')) {
|
|
|
|
|
|
|
|
handleIoSpecification(flowNode.ioSpecification, context);
|
|
|
|
|
|
|
|
// defer handling of associations
|
|
|
|
deferred.push(function() {
|
|
|
|
forEach(flowNode.dataInputAssociations, contextual(handleDataAssociation, context));
|
|
|
|
forEach(flowNode.dataOutputAssociations, contextual(handleDataAssociation, context));
|
|
|
|
});
|
|
|
|
}
|
2014-03-13 15:06:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function handleSequenceFlow(sequenceFlow, context) {
|
2014-03-13 19:21:42 +00:00
|
|
|
visitIfDi(sequenceFlow, context);
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleDataElement(dataObject, context) {
|
|
|
|
visitIfDi(dataObject, context);
|
|
|
|
}
|
|
|
|
|
2014-07-23 16:53:04 +00:00
|
|
|
function handleBoundaryEvent(dataObject, context) {
|
2014-07-15 10:43:30 +00:00
|
|
|
visitIfDi(dataObject, context);
|
|
|
|
}
|
|
|
|
|
2014-03-13 19:21:42 +00:00
|
|
|
function handleLane(lane, context) {
|
|
|
|
var newContext = visitIfDi(lane, context);
|
|
|
|
|
|
|
|
if (lane.childLaneSet) {
|
|
|
|
handleLaneSet(lane.childLaneSet, newContext || context);
|
|
|
|
}
|
2015-10-15 21:49:52 +00:00
|
|
|
|
|
|
|
wireFlowNodeRefs(lane);
|
2014-03-13 19:21:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function handleLaneSet(laneSet, context) {
|
2015-02-02 13:46:21 +00:00
|
|
|
forEach(laneSet.lanes, contextual(handleLane, context));
|
2014-03-13 19:21:42 +00:00
|
|
|
}
|
2014-03-13 15:06:30 +00:00
|
|
|
|
2014-03-13 19:21:42 +00:00
|
|
|
function handleLaneSets(laneSets, context) {
|
2015-02-02 13:46:21 +00:00
|
|
|
forEach(laneSets, contextual(handleLaneSet, context));
|
2014-03-13 15:06:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function handleFlowElementsContainer(container, context) {
|
2014-03-13 19:21:42 +00:00
|
|
|
if (container.laneSets) {
|
|
|
|
handleLaneSets(container.laneSets, context);
|
|
|
|
}
|
2015-08-25 13:45:44 +00:00
|
|
|
|
2015-10-09 23:33:21 +00:00
|
|
|
handleFlowElements(container.flowElements, context);
|
2014-03-13 19:21:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function handleFlowElements(flowElements, context) {
|
2015-02-02 13:46:21 +00:00
|
|
|
forEach(flowElements, function(e) {
|
2014-03-13 15:06:30 +00:00
|
|
|
if (is(e, 'bpmn:SequenceFlow')) {
|
2014-07-23 16:53:04 +00:00
|
|
|
deferred.push(function() {
|
|
|
|
handleSequenceFlow(e, context);
|
|
|
|
});
|
2014-07-15 10:43:30 +00:00
|
|
|
} else if (is(e, 'bpmn:BoundaryEvent')) {
|
2014-07-23 16:53:04 +00:00
|
|
|
deferred.unshift(function() {
|
|
|
|
handleBoundaryEvent(e, context);
|
|
|
|
});
|
2014-07-15 10:43:30 +00:00
|
|
|
} else if (is(e, 'bpmn:FlowNode')) {
|
2014-03-13 15:06:30 +00:00
|
|
|
handleFlowNode(e, context);
|
2014-07-15 10:43:30 +00:00
|
|
|
} else if (is(e, 'bpmn:DataObject')) {
|
2014-03-13 19:21:42 +00:00
|
|
|
// SKIP (assume correct referencing via DataObjectReference)
|
2014-07-15 10:43:30 +00:00
|
|
|
} else if (is(e, 'bpmn:DataStoreReference')) {
|
2014-03-13 19:21:42 +00:00
|
|
|
handleDataElement(e, context);
|
2014-07-15 10:43:30 +00:00
|
|
|
} else if (is(e, 'bpmn:DataObjectReference')) {
|
2014-03-13 19:21:42 +00:00
|
|
|
handleDataElement(e, context);
|
2014-03-13 15:06:30 +00:00
|
|
|
} else {
|
2014-06-23 12:44:03 +00:00
|
|
|
logError(
|
2014-10-28 14:09:17 +00:00
|
|
|
'unrecognized flowElement ' + elementToString(e) + ' in context ' +
|
2014-10-31 14:05:16 +00:00
|
|
|
(context ? elementToString(context.businessObject) : null),
|
2014-06-23 12:44:03 +00:00
|
|
|
{ element: e, context: context });
|
2014-03-13 15:06:30 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleParticipant(participant, context) {
|
2014-03-13 19:21:42 +00:00
|
|
|
var newCtx = visitIfDi(participant, context);
|
2014-03-13 15:06:30 +00:00
|
|
|
|
|
|
|
var process = participant.processRef;
|
|
|
|
if (process) {
|
2014-03-13 19:21:42 +00:00
|
|
|
handleProcess(process, newCtx || context);
|
2014-03-13 15:06:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleCollaboration(collaboration) {
|
|
|
|
|
2015-02-02 13:46:21 +00:00
|
|
|
forEach(collaboration.participants, contextual(handleParticipant));
|
2014-03-13 15:06:30 +00:00
|
|
|
|
2014-03-13 19:21:42 +00:00
|
|
|
handleArtifacts(collaboration.artifacts);
|
|
|
|
|
2015-01-02 11:56:32 +00:00
|
|
|
// handle message flows latest in the process
|
|
|
|
deferred.push(function() {
|
|
|
|
handleMessageFlows(collaboration.messageFlows);
|
|
|
|
});
|
2014-03-13 15:06:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-15 21:49:52 +00:00
|
|
|
function wireFlowNodeRefs(lane) {
|
|
|
|
// wire the virtual flowNodeRefs <-> relationship
|
|
|
|
forEach(lane.flowNodeRef, function(flowNode) {
|
|
|
|
var lanes = flowNode.get('lanes');
|
|
|
|
|
|
|
|
if (lanes) {
|
|
|
|
lanes.push(lane);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-03-13 15:06:30 +00:00
|
|
|
///// API ////////////////////////////////
|
2014-04-28 09:09:47 +00:00
|
|
|
|
2014-03-13 15:06:30 +00:00
|
|
|
return {
|
|
|
|
handleDefinitions: handleDefinitions
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-06-23 12:44:03 +00:00
|
|
|
module.exports = BpmnTreeWalker;
|