use of org.osate.ge.operations.StepResult in project osate2 by osate.
the class CreateVariablePaletteCommand method getOperation.
@Override
public Optional<Operation> getOperation(final GetTargetedOperationContext ctx) {
return ctx.getTarget().getBusinessObject(BehaviorAnnex.class).map(behaviorAnnex -> {
final PublicPackageSection section = getPackage(behaviorAnnex).map(AadlPackage::getPublicSection).orElse(null);
if (section == null) {
return null;
}
return Operation.createWithBuilder(builder -> {
final OperationBuilder<DataClassifier> prompt = builder.supply(() -> BehaviorAnnexUtil.promptForDataClassifier(behaviorAnnex.eResource()).filter(c -> BehaviorAnnexUtil.getPackage(c).isPresent()).map(StepResult::forValue).orElseGet(StepResult::abort));
final OperationBuilder<DataClassifier> addImportIfNeeded = prompt.modifyModel(null, (tag, prevResult) -> section, (tag, sectionToModify, dataClassifier) -> {
BehaviorAnnexUtil.getPackage(dataClassifier).ifPresent(classifierPkg -> AadlImportsUtil.addImportIfNeeded(sectionToModify, classifierPkg));
return StepResult.forValue(dataClassifier);
});
addImportIfNeeded.modifyModel(null, (tag, dataClassifier) -> behaviorAnnex, (tag, behaviorAnnexToModify, prevResult) -> {
final BehaviorVariable newVariable = (BehaviorVariable) EcoreUtil.create(AadlBaPackage.eINSTANCE.getBehaviorVariable());
final String newName = BehaviorAnnexNamingUtil.buildUniqueIdentifier(behaviorAnnexToModify, "new_behavior_variable");
newVariable.setName(newName);
newVariable.setDataClassifier(prevResult);
behaviorAnnexToModify.getVariables().add(newVariable);
return StepResultBuilder.create().showNewBusinessObject(ctx.getTarget(), newVariable).build();
});
});
});
}
use of org.osate.ge.operations.StepResult in project osate2 by osate.
the class ErrorModelGeUtil method createErrorModelSubclausePromptAndModifyOperation.
/**
* Creates an operation which allows prompt the user and then modifies the first error model subclause of a classifier or subcomponent.
* If the specified {@link BusinessObjectContext} doesn't contain an appropriate business object, an empty optional is returned.
* If a error model subclause doesn't exist in the classifier, it is created.
* @param <P> the result of prompting the user
* @param boc the business object being modified. Must be the business object context for a classifier or subcomponent.
* @param prompter is a function that will return results of prompting the user. If it returns an empty optional,
* the modification portion of the operation will not be executed.
* @param modifier a function that is executed to perform the model operation.
* @return an optional describing the operation. If the business object context does not have a classifier or subcomponent
* as a business object, an empty optional will be returned.
*/
public static <P> Optional<Operation> createErrorModelSubclausePromptAndModifyOperation(final BusinessObjectContext boc, final Supplier<Optional<P>> prompter, BiFunction<ErrorModelSubclause, P, StepResult<?>> modifier) {
final Object readonlyBo = boc.getBusinessObject();
final Classifier readonlyClassifier;
if (readonlyBo instanceof Classifier) {
readonlyClassifier = (Classifier) readonlyBo;
} else if (readonlyBo instanceof Subcomponent) {
readonlyClassifier = ((Subcomponent) readonlyBo).getAllClassifier();
} else {
readonlyClassifier = null;
}
if (readonlyClassifier == null) {
return Optional.empty();
}
return Optional.of(Operation.createWithBuilder(b -> b.supply(() -> prompter.get().map(v -> StepResult.forValue(v)).orElseGet(StepResult::abort)).modifyModel(null, (tag, promptResult) -> {
// Select the object to modify. If an error model subclause exists, modify it. Otherwise, modify the classifier.
final Optional<ErrorModelSubclause> readonlySubclause = getFirstErrorModelSubclause(readonlyClassifier);
if (readonlySubclause.isPresent()) {
return readonlySubclause.filter(subclause -> subclause.getContainingClassifier() != null).orElse(null);
} else {
return readonlyClassifier;
}
}, (tag, boToModify, promptResult) -> {
// Get or create the error model subclause as needed
final ErrorModelSubclause subclause;
if (boToModify instanceof Classifier) {
subclause = getOrCreateErrorModelSubclause((Classifier) boToModify);
} else {
subclause = (ErrorModelSubclause) boToModify;
}
// Perform the modification
return modifier.apply(subclause, promptResult);
})));
}
Aggregations