ModelInstanceImpl modelInstance = referenceSourceParentElement.getModelInstance();
String identifier = referenceTargetAttribute.getValue(referenceTargetElement);
ModelElementInstance existingElement = modelInstance.getModelElementById(identifier);
if (existingElement == null || !existingElement.equals(referenceTargetElement)) {
throw new ModelReferenceException("Cannot create reference to model element " + referenceTargetElement
+": element is not part of model. Please connect element to the model first.");
}
else {
Source referenceSourceElement = modelInstance.newInstance(getReferenceSourceElementType());
setReferenceSource(referenceSourceParentElement, referenceSourceElement);