use of org.osate.ge.aadl2.AadlGraphicalEditorException in project osate2 by osate.
the class CreateTransitionPaletteCommand method buildCreateOperation.
private Operation buildCreateOperation(final BusinessObjectContext srcBoc, final ErrorBehaviorState targetStateReadonly) {
final Object srcBo = srcBoc.getBusinessObject();
final URI targetStateUri = EcoreUtil.getURI(targetStateReadonly);
if (srcBo instanceof ErrorBehaviorState) {
final ErrorBehaviorState srcStateReadonly = (ErrorBehaviorState) srcBo;
final URI srcStateUri = EcoreUtil.getURI(srcStateReadonly);
return Operation.createWithBuilder(createOp -> createOp.supply(() -> StepResult.forValue(getStateMachine(srcStateReadonly))).modifyPreviousResult(stateMachine -> {
final ErrorBehaviorState targetState = (ErrorBehaviorState) stateMachine.eResource().getResourceSet().getEObject(targetStateUri, true);
final ErrorBehaviorState srcState = (ErrorBehaviorState) stateMachine.eResource().getResourceSet().getEObject(srcStateUri, true);
final NamedElementCollectionSingleSelectorModel<ErrorBehaviorEvent> model = new NamedElementCollectionSingleSelectorModel<>(stateMachine.getEvents());
if (!FilteringSelectorDialog.open(Display.getCurrent().getActiveShell(), "Select Event", new LabelFilteringListSelectorModel<>(model))) {
return null;
}
final ErrorBehaviorEvent event = model.getSelectedElement();
// Create the transition
final ErrorBehaviorTransition newTransition = ErrorModelFactory.eINSTANCE.createErrorBehaviorTransition();
newTransition.setSource(srcState);
// Set the target
if (srcState == targetState) {
newTransition.setSteadyState(true);
} else {
newTransition.setTarget(targetState);
}
newTransition.setName(ErrorModelNamingUtil.buildUniqueIdentifier(stateMachine, "new_transition"));
final ConditionElement conditionElement = ErrorModelFactory.eINSTANCE.createConditionElement();
newTransition.setCondition(conditionElement);
final EMV2PathElement conditionPathElement = ErrorModelFactory.eINSTANCE.createEMV2PathElement();
conditionPathElement.setNamedElement(event);
final QualifiedErrorEventOrPropagation errorEventOrPropogation = ErrorModelFactory.eINSTANCE.createQualifiedErrorEventOrPropagation();
errorEventOrPropogation.setEmv2Target(conditionPathElement);
conditionElement.setQualifiedErrorPropagationReference(errorEventOrPropogation);
stateMachine.getTransitions().add(newTransition);
return StepResultBuilder.create().showNewBusinessObject(srcBoc.getParent(), newTransition).build();
}));
} else if (srcBo instanceof ErrorBehaviorTransition || srcBo instanceof BehaviorTransitionTrunk || srcBo instanceof TransitionBranch) {
// Get the transition to modify
final ErrorBehaviorTransition transitionReadonly;
if (srcBo instanceof ErrorBehaviorTransition) {
transitionReadonly = (ErrorBehaviorTransition) srcBo;
} else if (srcBo instanceof BehaviorTransitionTrunk) {
transitionReadonly = ((BehaviorTransitionTrunk) srcBo).getTransition();
} else if (srcBo instanceof TransitionBranch) {
transitionReadonly = (ErrorBehaviorTransition) ((TransitionBranch) srcBo).eContainer();
} else {
throw new AadlGraphicalEditorException("Unexpected case: " + srcBo);
}
return Operation.createWithBuilder(createOp -> createOp.supply(() -> StepResult.forValue(transitionReadonly)).modifyPreviousResult(transition -> {
final ErrorBehaviorState targetState = (ErrorBehaviorState) transition.eResource().getResourceSet().getEObject(targetStateUri, true);
// Convert from using steady state and target field to using branches.
if (transition.getDestinationBranches().isEmpty()) {
final TransitionBranch firstBranch = ErrorModelFactory.eINSTANCE.createTransitionBranch();
final BranchValue firstBranchValue = ErrorModelFactory.eINSTANCE.createBranchValue();
firstBranchValue.setRealvalue("1.0");
if (transition.isSteadyState()) {
firstBranch.setSteadyState(true);
} else {
firstBranch.setTarget(transition.getTarget());
}
firstBranch.setValue(firstBranchValue);
transition.getDestinationBranches().add(firstBranch);
transition.eUnset(ErrorModelPackage.eINSTANCE.getErrorBehaviorTransition_Target());
transition.eUnset(ErrorModelPackage.eINSTANCE.getErrorBehaviorTransition_TargetToken());
transition.eUnset(ErrorModelPackage.eINSTANCE.getErrorBehaviorTransition_SteadyState());
}
// Create the new branch
final TransitionBranch newBranch = ErrorModelFactory.eINSTANCE.createTransitionBranch();
final BranchValue newBranchValue = ErrorModelFactory.eINSTANCE.createBranchValue();
newBranchValue.setRealvalue("1.0");
// Set the target
if (transition.getSource() == targetState) {
newBranch.setSteadyState(true);
} else {
newBranch.setTarget(targetState);
}
newBranch.setValue(newBranchValue);
transition.getDestinationBranches().add(newBranch);
return StepResultBuilder.create().build();
}));
} else {
throw new AadlGraphicalEditorException("Unsupported case: " + srcBo);
}
}
use of org.osate.ge.aadl2.AadlGraphicalEditorException in project osate2 by osate.
the class CreateErrorPropagationPaletteCommand method getOperation.
@Override
public Optional<Operation> getOperation(final GetTargetedOperationContext ctx) {
final Object bo = ctx.getTarget().getBusinessObject();
if (bo instanceof Feature) {
return createPropgationCreationOperation(ctx.getTarget(), (newPropagation, subclause) -> {
// Find the feature in the context of the EMV subclause. This is needed for reliable serialization.
final List<URI> path = ErrorModelGeUtil.createQualifiedPropagationPointPath(ctx.getTarget(), ErrorModelGeUtil.getClassifierSourceBoc(ctx.getTarget()).get(), new ArrayList<>());
newPropagation.setFeatureorPPRef(buildFeatureReference(subclause.eResource().getResourceSet(), path));
});
} else if (bo instanceof KeywordPropagationPoint) {
final KeywordPropagationPoint kw = (KeywordPropagationPoint) bo;
if (kw.getType() != KeywordPropagationPointType.ALL) {
return createPropgationCreationOperation(ctx.getTarget(), (newPropagation, subclause) -> newPropagation.setKind(kw.getType().getKind()));
}
} else if (bo instanceof PropagationPoint) {
return createPropgationCreationOperation(ctx.getTarget(), (newPropagation, subclause) -> {
// Find the propagation in the context of the EMV subclause.
// Check inherited subclauses as well. This is needed for reliable serialization.
final CombinedErrorModelSubclause combined = CombinedErrorModelSubclause.create(subclause.getContainingClassifier());
final String boName = ((PropagationPoint) bo).getName();
final PropagationPoint pp = combined.getPoints().filter(p -> Objects.equal(p.getName(), boName)).findAny().orElseThrow(() -> new AadlGraphicalEditorException("Unable to find propagation point"));
final FeatureorPPReference ppRef = ErrorModelFactory.eINSTANCE.createFeatureorPPReference();
ppRef.setFeatureorPP(pp);
newPropagation.setFeatureorPPRef(ppRef);
});
}
return Optional.empty();
}
use of org.osate.ge.aadl2.AadlGraphicalEditorException in project osate2 by osate.
the class CreateErrorPropagationPaletteCommand method createPropgationCreationOperation.
/**
* Creates an operation for creating a propagation for the target.
* @param target is the target of the propagation. It should be the context which will be the parent of the propagation
* @param init function called to finish initializing the propagation. It must set the kind or the feature or PP reference.
* @return the operation or an empty optional if a classifier could not be determined.
*/
private Optional<Operation> createPropgationCreationOperation(final BusinessObjectContext target, final BiConsumer<ErrorPropagation, ErrorModelSubclause> init) {
return ErrorModelGeUtil.getClassifierSourceBoc(target).flatMap(container -> {
final AadlPackage pkg = container.getBusinessObject(NamedElement.class).map(ne -> ne.getElementRoot()).map(root -> root instanceof AadlPackage ? ((AadlPackage) root) : null).orElseThrow(() -> new AadlGraphicalEditorException("Unable to find model"));
return ErrorModelGeUtil.createErrorModelSubclausePromptAndModifyOperation(container, () -> {
if (propagationAlreadyExists(target)) {
final String propagationOrContainmentLabel = (containment ? "containment" : "propagation");
final String inputOrOutputLabel = direction == DirectionType.IN ? "intput" : "output";
MessageDialog.openError(Display.getDefault().getActiveShell(), "Unable to create " + propagationOrContainmentLabel, "Propagation already exists. A propagation point may only have one " + inputOrOutputLabel + " error " + propagationOrContainmentLabel + " defined.");
return Optional.empty();
}
return ErrorModelUiUtil.promptForTypeSet(pkg);
}, (subclause, typeSet) -> {
final ErrorPropagation newPropagation = ErrorModelFactory.eINSTANCE.createErrorPropagation();
newPropagation.setTypeSet(typeSet);
newPropagation.setNot(containment);
newPropagation.setDirection(direction);
init.accept(newPropagation, subclause);
subclause.getPropagations().add(newPropagation);
return StepResultBuilder.create().showNewBusinessObject(target, newPropagation).build();
});
});
}
use of org.osate.ge.aadl2.AadlGraphicalEditorException in project osate2 by osate.
the class BehaviorActionBlockEmbeddedTextValue method create.
/**
* Creates an {@link EditableEmbeddedTextValue} that allows editing of the embedded AADL source for the {@link BehaviorActionBlock}
* @param behaviorTransition the Behavior Transition that owns the Behavior Action Block
* @return an {@link EditableEmbeddedTextValue} for the {@link BehaviorActionBlock}
*/
public static EditableEmbeddedTextValue create(final BehaviorTransition behaviorTransition) {
final String sourceText = BehaviorAnnexXtextUtil.getText(behaviorTransition);
final BehaviorActionBlock actionBlock = behaviorTransition.getActionBlock();
// Text before action block
final String prefix;
// Action block text
final String actionText;
// Text after action block
final String suffix;
if (actionBlock == null) {
// Transition offset
final int transitionOffset = behaviorTransition.getAadlBaLocationReference().getOffset();
final String transitionText = sourceText.substring(transitionOffset);
// Find transition terminating semicolon offset
final int terminationOffset = BehaviorAnnexXtextUtil.findUncommentedTerminationChar(transitionText, ';') + transitionOffset;
// Transition action prefix and add open bracket for action
prefix = sourceText.substring(0, terminationOffset) + "{";
// Empty action text
actionText = "";
// Add bracket to close action text
suffix = "}" + sourceText.substring(terminationOffset);
} else {
// Action offset
final int updateOffset = actionBlock.getAadlBaLocationReference().getOffset() + 1;
prefix = sourceText.substring(0, updateOffset);
// Note: Condition length only counts until the first space (assuming).
// For example, when dispatch condition is "on dispatch" length is 2.
// Find closing "]", to get condition text
final String afterTransitionText = sourceText.substring(updateOffset);
// Find action ending offset
final int terminationOffset = BehaviorAnnexXtextUtil.findUncommentedTerminationChar(afterTransitionText, '}') + updateOffset;
// Get formatted action block text
final AadlBaUnparser baUnparser = new AadlBaUnparser();
// Throw exception if first and last char is not a bracket
// to know when formatter has changed
final String formattedActionBlock = baUnparser.process(actionBlock);
final int lastIndex = formattedActionBlock.length() - 1;
if (!Objects.equals('{', formattedActionBlock.charAt(0)) || !Objects.equals('}', formattedActionBlock.charAt(lastIndex))) {
throw new AadlGraphicalEditorException("Unexpected action block format '" + formattedActionBlock + "'.");
}
// Split action at new line character and throw out action block brackets
final List<String> actionBlockText = getInnerActionBlockText(formattedActionBlock.split("\n"));
// Get whitespace to trim from each line after removing opening bracket
final int whitespace = getWhiteSpace(actionBlockText.get(0));
actionText = String.join("", actionBlockText.stream().map(ss -> ss.substring(whitespace)).toArray(String[]::new)).trim();
suffix = sourceText.substring(terminationOffset);
}
// Create the value
return new BehaviorActionBlockEmbeddedTextValue(behaviorTransition, sourceText.length(), prefix, actionText, suffix);
}
use of org.osate.ge.aadl2.AadlGraphicalEditorException in project osate2 by osate.
the class BehaviorAnnexXtextUtil method getText.
/**
* Returns the source of the resource which contains the specified element. If an Xtext document is open for the resource, then the contents
* of the document will be returned.
* @param element the element for which to return the resource source text
* @return the source text of the resource which contains the specified element.
*/
public static String getText(final Element element) {
final Resource resource = element.eResource();
if (!(resource instanceof XtextResource)) {
throw new AadlGraphicalEditorException("resource must be XtextResource");
}
final IXtextDocument xtextDocument = AgeXtextUtil.getDocumentByRootElement(element.getElementRoot());
return getText(xtextDocument, (XtextResource) resource);
}
Aggregations