Search in sources :

Example 1 with StepResult

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();
            });
        });
    });
}
Also used : GetTargetedOperationContext(org.osate.ge.palette.GetTargetedOperationContext) BehaviorAnnexNamingUtil(org.osate.ge.ba.util.BehaviorAnnexNamingUtil) StepResult(org.osate.ge.operations.StepResult) Operation(org.osate.ge.operations.Operation) BehaviorVariable(org.osate.ba.aadlba.BehaviorVariable) EcoreUtil(org.eclipse.emf.ecore.util.EcoreUtil) AadlPackage(org.osate.aadl2.AadlPackage) AadlImportsUtil(org.osate.ge.aadl2.AadlImportsUtil) BehaviorAnnex(org.osate.ba.aadlba.BehaviorAnnex) BehaviorAnnexUtil.getPackage(org.osate.ge.ba.util.BehaviorAnnexUtil.getPackage) StepResultBuilder(org.osate.ge.operations.StepResultBuilder) PublicPackageSection(org.osate.aadl2.PublicPackageSection) BasePaletteCommand(org.osate.ge.palette.BasePaletteCommand) DataClassifier(org.osate.aadl2.DataClassifier) BehaviorAnnexUtil(org.osate.ge.ba.util.BehaviorAnnexUtil) Optional(java.util.Optional) OperationBuilder(org.osate.ge.operations.OperationBuilder) AadlBaPackage(org.osate.ba.aadlba.AadlBaPackage) TargetedPaletteCommand(org.osate.ge.palette.TargetedPaletteCommand) PublicPackageSection(org.osate.aadl2.PublicPackageSection) BehaviorVariable(org.osate.ba.aadlba.BehaviorVariable) BehaviorAnnex(org.osate.ba.aadlba.BehaviorAnnex) DataClassifier(org.osate.aadl2.DataClassifier) StepResult(org.osate.ge.operations.StepResult)

Example 2 with StepResult

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);
    })));
}
Also used : EMV2Util(org.osate.xtext.aadl2.errormodel.util.EMV2Util) URI(org.eclipse.emf.common.util.URI) StepResult(org.osate.ge.operations.StepResult) BiFunction(java.util.function.BiFunction) ComponentImplementation(org.osate.aadl2.ComponentImplementation) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ComponentClassifier(org.osate.aadl2.ComponentClassifier) Style(org.osate.ge.graphics.Style) BusinessObjectContext(org.osate.ge.BusinessObjectContext) Classifier(org.osate.aadl2.Classifier) Graphic(org.osate.ge.graphics.Graphic) ComponentType(org.osate.aadl2.ComponentType) GraphicalAnnexUtil(org.osate.ge.aadl2.GraphicalAnnexUtil) ConnectionBuilder(org.osate.ge.graphics.ConnectionBuilder) Subcomponent(org.osate.aadl2.Subcomponent) AadlGraphicalEditorException(org.osate.ge.aadl2.AadlGraphicalEditorException) Operation(org.osate.ge.operations.Operation) EcoreUtil(org.eclipse.emf.ecore.util.EcoreUtil) AadlPackage(org.osate.aadl2.AadlPackage) ArrowBuilder(org.osate.ge.graphics.ArrowBuilder) StyleBuilder(org.osate.ge.graphics.StyleBuilder) List(java.util.List) Stream(java.util.stream.Stream) ErrorModelSubclause(org.osate.xtext.aadl2.errormodel.errorModel.ErrorModelSubclause) Optional(java.util.Optional) ErrorModelLibrary(org.osate.xtext.aadl2.errormodel.errorModel.ErrorModelLibrary) ErrorModelPackage(org.osate.xtext.aadl2.errormodel.errorModel.ErrorModelPackage) NamedElement(org.osate.aadl2.NamedElement) ErrorModelSubclause(org.osate.xtext.aadl2.errormodel.errorModel.ErrorModelSubclause) Subcomponent(org.osate.aadl2.Subcomponent) ComponentClassifier(org.osate.aadl2.ComponentClassifier) Classifier(org.osate.aadl2.Classifier) StepResult(org.osate.ge.operations.StepResult)

Aggregations

Optional (java.util.Optional)2 EcoreUtil (org.eclipse.emf.ecore.util.EcoreUtil)2 AadlPackage (org.osate.aadl2.AadlPackage)2 Operation (org.osate.ge.operations.Operation)2 StepResult (org.osate.ge.operations.StepResult)2 List (java.util.List)1 BiFunction (java.util.function.BiFunction)1 Function (java.util.function.Function)1 Supplier (java.util.function.Supplier)1 Stream (java.util.stream.Stream)1 URI (org.eclipse.emf.common.util.URI)1 Classifier (org.osate.aadl2.Classifier)1 ComponentClassifier (org.osate.aadl2.ComponentClassifier)1 ComponentImplementation (org.osate.aadl2.ComponentImplementation)1 ComponentType (org.osate.aadl2.ComponentType)1 DataClassifier (org.osate.aadl2.DataClassifier)1 NamedElement (org.osate.aadl2.NamedElement)1 PublicPackageSection (org.osate.aadl2.PublicPackageSection)1 Subcomponent (org.osate.aadl2.Subcomponent)1 AadlBaPackage (org.osate.ba.aadlba.AadlBaPackage)1