org.camunda.bpm.model.xml.instance.ModelElementInstance Java Examples

The following examples show how to use org.camunda.bpm.model.xml.instance.ModelElementInstance. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PathCoverageExecutionListener.java    From camunda-bpm-process-test-coverage with Apache License 2.0 6 votes vote down vote up
/**
 * Events aren't reported like SequenceFlows and Activities, so we need
 * special handling. If a sequence flow has an event as the source or the
 * target, we add it to the coverage. It's pretty straight forward if a
 * sequence flow is active, then it's source has been covered anyway and it
 * will most definitely arrive at its target.
 * 
 * @param transitionId
 * @param processDefinition
 * @param repositoryService
 */
private void handleEvent(String transitionId, ProcessDefinition processDefinition,
        RepositoryService repositoryService) {

    final BpmnModelInstance modelInstance = repositoryService.getBpmnModelInstance(processDefinition.getId());

    final ModelElementInstance modelElement = modelInstance.getModelElementById(transitionId);
    if (modelElement.getElementType().getInstanceType() == SequenceFlow.class) {

        final SequenceFlow sequenceFlow = (SequenceFlow) modelElement;

        // If there is an event at the sequence flow source add it to the
        // coverage
        final FlowNode source = sequenceFlow.getSource();
        addEventToCoverage(processDefinition, source);

        // If there is an event at the sequence flow target add it to the
        // coverage
        final FlowNode target = sequenceFlow.getTarget();
        addEventToCoverage(processDefinition, target);

    }

}
 
Example #2
Source File: FlowNodeImpl.java    From camunda-bpmn-model with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("rawtypes")
public void updateAfterReplacement() {
  super.updateAfterReplacement();
  Collection<Reference> incomingReferences = getIncomingReferencesByType(SequenceFlow.class);
  for (Reference<?> reference : incomingReferences) {
    for (ModelElementInstance sourceElement : reference.findReferenceSourceElements(this)) {
      String referenceIdentifier = reference.getReferenceIdentifier(sourceElement);

      if (referenceIdentifier != null && referenceIdentifier.equals(getId()) && reference instanceof AttributeReference) {
        String attributeName = ((AttributeReference) reference).getReferenceSourceAttribute().getAttributeName();
        if (attributeName.equals(BPMN_ATTRIBUTE_SOURCE_REF)) {
          getOutgoing().add((SequenceFlow) sourceElement);
        }
        else if (attributeName.equals(BPMN_ATTRIBUTE_TARGET_REF)) {
          getIncoming().add((SequenceFlow) sourceElement);
        }
      }
    }

  }
}
 
Example #3
Source File: IdsElementReferenceCollectionImpl.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
@Override
public void referencedElementRemoved(ModelElementInstance referenceTargetElement, Object referenceIdentifier) {
  for (ModelElementInstance referenceSourceElement : findReferenceSourceElements(referenceTargetElement)) {
    List<String> referenceIdentifiers = getReferenceIdentifiers(referenceSourceElement);
    if (referenceIdentifiers.contains(referenceIdentifier)) {
      if (referenceIdentifiers.size() == 1) {
        // remove whole element
        removeReference(referenceSourceElement, referenceTargetElement);
      }
      else {
        // remove only single identifier
        referenceIdentifiers.remove(referenceIdentifier);
        setReferenceIdentifiers(referenceSourceElement, referenceIdentifiers);
      }
    }
  }
}
 
Example #4
Source File: ReferenceImpl.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
/**
 * Get the reference target model element instance
 *
 * @param referenceSourceElement the reference source model element instance
 * @return the reference target model element instance or null if not set
 */
@SuppressWarnings("unchecked")
public T getReferenceTargetElement(ModelElementInstance referenceSourceElement) {
  String identifier = getReferenceIdentifier(referenceSourceElement);
  ModelElementInstance referenceTargetElement = referenceSourceElement.getModelInstance().getModelElementById(identifier);
  if (referenceTargetElement != null) {
    try {
      return (T) referenceTargetElement;

    } catch(ClassCastException e) {
      throw new ModelReferenceException("Element " + referenceSourceElement + " references element " + referenceTargetElement + " of wrong type. "
        + "Expecting " + referenceTargetAttribute.getOwningElementType() + " got " + referenceTargetElement.getElementType());
    }
  }
  else {
    return null;
  }
}
 
Example #5
Source File: ExecutionListenerBpmnModelExecutionContextTest.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
private void assertFlowElementIs(Class<? extends FlowElement> elementClass) {
  BpmnModelInstance modelInstance = ModelExecutionContextExecutionListener.modelInstance;
  assertNotNull(modelInstance);

  Model model = modelInstance.getModel();
  Collection<ModelElementInstance> events = modelInstance.getModelElementsByType(model.getType(Event.class));
  assertEquals(3, events.size());
  Collection<ModelElementInstance> gateways = modelInstance.getModelElementsByType(model.getType(Gateway.class));
  assertEquals(1, gateways.size());
  Collection<ModelElementInstance> tasks = modelInstance.getModelElementsByType(model.getType(Task.class));
  assertEquals(1, tasks.size());

  FlowElement flowElement = ModelExecutionContextExecutionListener.flowElement;
  assertNotNull(flowElement);
  assertTrue(elementClass.isAssignableFrom(flowElement.getClass()));
}
 
Example #6
Source File: UriElementReferenceImpl.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
@Override
public String getReferenceIdentifier(ModelElementInstance referenceSourceElement) {
  // TODO: implement something more robust (CAM-4028)
  String identifier = referenceSourceElement.getAttributeValue("href");
  if (identifier != null) {
    String[] parts = identifier.split("#");
    if (parts.length > 1) {
      return parts[parts.length - 1];
    }
    else {
      return parts[0];
    }
  }
  else {
    return null;
  }
}
 
Example #7
Source File: UriElementReferenceCollectionImpl.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
@Override
public String getReferenceIdentifier(ModelElementInstance referenceSourceElement) {
  // TODO: implement something more robust (CAM-4028)
  String identifier = referenceSourceElement.getAttributeValue("href");
  if (identifier != null) {
    String[] parts = identifier.split("#");
    if (parts.length > 1) {
      return parts[parts.length - 1];
    }
    else {
      return parts[0];
    }
  }
  else {
    return null;
  }
}
 
Example #8
Source File: ModelElementInstanceImpl.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private void updateIncomingReferences(ModelElementInstance oldInstance, ModelElementInstance newInstance) {
  String oldId = oldInstance.getAttributeValue("id");
  String newId = newInstance.getAttributeValue("id");

  if (oldId == null || newId == null) {
    return;
  }

  Collection<Attribute<?>> attributes = ((ModelElementTypeImpl) oldInstance.getElementType()).getAllAttributes();
  for (Attribute<?> attribute : attributes) {
    if (attribute.isIdAttribute()) {
      for (Reference<?> incomingReference : attribute.getIncomingReferences()) {
        ((ReferenceImpl<ModelElementInstance>) incomingReference).referencedElementUpdated(newInstance, oldId, newId);
      }
    }
  }

}
 
Example #9
Source File: ModelInstanceValidator.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
public ValidationResults validate() {

  ValidationResultsCollectorImpl resultsCollector = new ValidationResultsCollectorImpl();

  for (ModelElementValidator validator : validators) {

    Class<? extends ModelElementInstance> elementType = validator.getElementType();
    Collection<? extends ModelElementInstance> modelElementsByType = modelInstanceImpl.getModelElementsByType(elementType);

    for (ModelElementInstance element : modelElementsByType) {

      resultsCollector.setCurrentElement(element);

      try {
        validator.validate(element, resultsCollector);
      }
      catch(RuntimeException e) {
        throw new RuntimeException("Validator " + validator + " threw an exception while validating "+element, e);
      }
    }

  }

  return resultsCollector.getResults();
}
 
Example #10
Source File: ModelElementInstanceImpl.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
public void replaceChildElement(ModelElementInstance existingChild, ModelElementInstance newChild) {
  DomElement existingChildDomElement = existingChild.getDomElement();
  DomElement newChildDomElement = newChild.getDomElement();

  // unlink (remove all references) of child elements
  ((ModelElementInstanceImpl) existingChild).unlinkAllChildReferences();

  // update incoming references from old to new child element
  updateIncomingReferences(existingChild, newChild);

  // replace the existing child with the new child in the DOM
  domElement.replaceChild(newChildDomElement, existingChildDomElement);

  // execute after replacement updates
  newChild.updateAfterReplacement();
}
 
Example #11
Source File: AttributeImpl.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public void removeAttribute(ModelElementInstance modelElement) {
  if (namespaceUri == null) {
    modelElement.removeAttribute(attributeName);
  }
  else {
    modelElement.removeAttributeNs(namespaceUri, attributeName);
  }
}
 
Example #12
Source File: AlternativeNsTest.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
@Test
public void getChildElementsByTypeForAlternativeNs() {
  ModelElementInstance birdo = modelInstance.getModelElementById("birdo");
  assertThat(birdo, is(notNullValue()));
  Collection<Wings> elements = birdo.getChildElementsByType(Wings.class);
  assertThat(elements.size(), is(1));
  assertThat(elements.iterator().next().getTextContent(), is("zisch"));
}
 
Example #13
Source File: ModelElementTypeImpl.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
protected ModelElementInstance createModelElementInstance(ModelTypeInstanceContext instanceContext) {
  if (isAbstract) {
    throw new ModelTypeException("Model element type " + getTypeName() + " is abstract and no instances can be created.");
  }
  else {
    return instanceProvider.newInstance(instanceContext);
  }
}
 
Example #14
Source File: QueryImpl.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public <V extends ModelElementInstance> Query<V> filterByType(Class<V> elementClass) {
  List<V> filtered = new ArrayList<V>();
  for (T instance : collection) {
    if (elementClass.isAssignableFrom(instance.getClass())) {
      filtered.add((V) instance);
    }
  }
  return new QueryImpl<V>(filtered);
}
 
Example #15
Source File: ModelUtil.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the {@link ModelElementInstanceImpl ModelElement} for a DOM element.
 * If the model element does not yet exist, it is created and linked to the DOM.
 *
 * @param domElement the child element to create a new {@link ModelElementInstanceImpl ModelElement} for
 * @param modelInstance the {@link ModelInstanceImpl ModelInstance} for which the new {@link ModelElementInstanceImpl ModelElement} is created
 * @param modelType the {@link ModelElementTypeImpl ModelElementType} to create a new {@link ModelElementInstanceImpl ModelElement} for
 * @return the child model element
 */
public static ModelElementInstance getModelElement(DomElement domElement, ModelInstanceImpl modelInstance, ModelElementTypeImpl modelType) {
  ModelElementInstance modelElement = domElement.getModelElementInstance();

  if(modelElement == null) {
    modelElement = modelType.newInstance(modelInstance, domElement);
    domElement.setModelElementInstance(modelElement);
  }
  return modelElement;
}
 
Example #16
Source File: ModelElementTypeImpl.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public Collection<ModelElementInstance> getInstances(ModelInstance modelInstance) {
  ModelInstanceImpl modelInstanceImpl = (ModelInstanceImpl) modelInstance;
  DomDocument document = modelInstanceImpl.getDocument();

  List<DomElement> elements = getElementsByNameNs(document, typeNamespace);

  List<ModelElementInstance> resultList = new ArrayList<ModelElementInstance>();
  for (DomElement element : elements) {
    resultList.add(ModelUtil.getModelElement(element, modelInstanceImpl, this));
  }
  return resultList;
}
 
Example #17
Source File: ModelInstanceImpl.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public ModelElementInstance getDocumentElement() {
  DomElement rootElement = document.getRootElement();
  if(rootElement != null) {
    return ModelUtil.getModelElement(rootElement, this);
  } else {
    return null;
  }
}
 
Example #18
Source File: BaseElementImpl.java    From camunda-bpmn-model with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
public DiagramElement getDiagramElement() {
  Collection<Reference> incomingReferences = getIncomingReferencesByType(DiagramElement.class);
  for (Reference<?> reference : incomingReferences) {
    for (ModelElementInstance sourceElement : reference.findReferenceSourceElements(this)) {
      String referenceIdentifier = reference.getReferenceIdentifier(sourceElement);
      if (referenceIdentifier != null && referenceIdentifier.equals(getId())) {
        return (DiagramElement) sourceElement;
      }
    }
  }
  return null;
}
 
Example #19
Source File: GetCmmnModelElementTypeRule.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected void starting(Description description) {
  String className = description.getClassName();
  className =  className.replaceAll("Test", "");
  Class<? extends ModelElementInstance> instanceClass = null;
  try {
    instanceClass = (Class<? extends ModelElementInstance>) Class.forName(className);
  } catch (ClassNotFoundException e) {
    throw new RuntimeException(e);
  }
  modelInstance = Cmmn.createEmptyModel();
  model = modelInstance.getModel();
  modelElementType = model.getType(instanceClass);
}
 
Example #20
Source File: ModelElementInstanceImpl.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public Collection<ModelElementInstance> getChildElementsByType(ModelElementType childElementType) {
  List<ModelElementInstance> instances = new ArrayList<ModelElementInstance>();
  for (ModelElementType extendingType : childElementType.getExtendingTypes()) {
    instances.addAll(getChildElementsByType(extendingType));
  }
  Model model = modelInstance.getModel();
  Set<String> alternativeNamespaces = model.getAlternativeNamespaces(childElementType.getTypeNamespace());
  List<DomElement> elements = domElement.getChildElementsByNameNs(asSet(childElementType.getTypeNamespace(), alternativeNamespaces), childElementType.getTypeName());
  instances.addAll(ModelUtil.getModelElementCollection(elements, modelInstance));
  return instances;
}
 
Example #21
Source File: IdsElementReferenceCollectionImpl.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
@Override
protected void updateReference(ModelElementInstance referenceSourceElement, String oldIdentifier, String newIdentifier) {
  List<String> referenceIdentifiers = getReferenceIdentifiers(referenceSourceElement);
  if (referenceIdentifiers.contains(oldIdentifier)) {
    int index = referenceIdentifiers.indexOf(oldIdentifier);
    referenceIdentifiers.remove(oldIdentifier);
    referenceIdentifiers.add(index, newIdentifier);
    setReferenceIdentifiers(referenceSourceElement, referenceIdentifiers);
  }
}
 
Example #22
Source File: AttributeReferenceCollection.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
@Override
protected void setReferenceIdentifier(ModelElementInstance referenceSourceElement, String referenceIdentifier) {
  if (referenceIdentifier != null && !referenceIdentifier.isEmpty()) {
    super.setReferenceIdentifier(referenceSourceElement, referenceIdentifier);
  } else {
    referenceSourceAttribute.removeAttribute(referenceSourceElement);
  }
}
 
Example #23
Source File: ModelInstanceImpl.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public <T extends ModelElementInstance> T newInstance(ModelElementType type, String id) {
  ModelElementInstance modelElementInstance = type.newInstance(this);
  if (id != null && !id.isEmpty()) {
    ModelUtil.setNewIdentifier(type, modelElementInstance, id, false);
  } else {
    ModelUtil.setGeneratedUniqueIdentifier(type, modelElementInstance, false);
  }
  return (T) modelElementInstance;
}
 
Example #24
Source File: QNameElementReferenceCollectionImpl.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
@Override
public String getReferenceIdentifier(ModelElementInstance referenceSourceElement) {
  String identifier = super.getReferenceIdentifier(referenceSourceElement);
  if (identifier != null) {
    QName qName = QName.parseQName(identifier);
    return qName.getLocalName();
  }
  else {
    return null;
  }
}
 
Example #25
Source File: AttributeAssert.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public AttributeAssert hasValue(ModelElementInstance modelElementInstance, Object value) {
  isNotNull();

  Object actualValue = actual.getValue(modelElementInstance);

  if (!value.equals(actualValue)) {
    failWithMessage("Expected attribute <%s> to have value <%s> but was <%s>", attributeName, value, actualValue);
  }

  return this;
}
 
Example #26
Source File: AttributeAssert.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public AttributeAssert hasValue(ModelElementInstance modelElementInstance) {
  isNotNull();

  Object actualValue = actual.getValue(modelElementInstance);

  if (actualValue == null) {
    failWithMessage("Expected attribute <%s> to have a value but has not", attributeName);
  }

  return this;
}
 
Example #27
Source File: ModelElementInstanceImpl.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
/**
 * Removes every reference to children of this.
 */
private void unlinkAllChildReferences() {
  List<ModelElementType> childElementTypes = elementType.getAllChildElementTypes();
  for (ModelElementType type : childElementTypes) {
    Collection<ModelElementInstance> childElementsForType = getChildElementsByType(type);
    for (ModelElementInstance childElement : childElementsForType) {
      ((ModelElementInstanceImpl) childElement).unlinkAllReferences();
    }
  }
}
 
Example #28
Source File: UserTaskBpmnModelExecutionContextTest.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
private void assertModelInstance() {
  BpmnModelInstance modelInstance = ModelExecutionContextTaskListener.modelInstance;
  assertNotNull(modelInstance);

  Collection<ModelElementInstance> events = modelInstance.getModelElementsByType(modelInstance.getModel().getType(Event.class));
  assertEquals(2, events.size());

  Collection<ModelElementInstance> tasks = modelInstance.getModelElementsByType(modelInstance.getModel().getType(Task.class));
  assertEquals(1, tasks.size());

  Process process = (Process) modelInstance.getDefinitions().getRootElements().iterator().next();
  assertEquals(PROCESS_ID, process.getId());
  assertTrue(process.isExecutable());
}
 
Example #29
Source File: AttributeReferenceCollection.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
@Override
protected void updateReference(ModelElementInstance referenceSourceElement, String oldIdentifier, String newIdentifier) {
  String referencingIdentifier = getReferenceIdentifier(referenceSourceElement);
  List<String> references = StringUtil.splitListBySeparator(referencingIdentifier, separator);
  if(oldIdentifier != null && references.contains(oldIdentifier)) {
    referencingIdentifier = referencingIdentifier.replace(oldIdentifier, newIdentifier);
    setReferenceIdentifier(referenceSourceElement, newIdentifier);
  }
}
 
Example #30
Source File: ModelElementInstanceImpl.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public void setUniqueChildElementByNameNs(ModelElementInstance newChild) {
  ModelUtil.ensureInstanceOf(newChild, ModelElementInstanceImpl.class);
  ModelElementInstanceImpl newChildElement = (ModelElementInstanceImpl) newChild;

  DomElement childElement = newChildElement.getDomElement();
  ModelElementInstance existingChild = getUniqueChildElementByNameNs(childElement.getNamespaceURI(), childElement.getLocalName());
  if(existingChild == null) {
    addChildElement(newChild);
  } else {
    replaceChildElement(existingChild, newChildElement);
  }
}