Search in sources :

Example 1 with EcoreMetamodelDescriptor

use of org.eclipse.sirius.ecore.extender.business.api.accessor.EcoreMetamodelDescriptor in project sirius-components by eclipse-sirius.

the class CreateInstanceOperationHandler method handle.

@Override
public IStatus handle(Map<String, Object> variables) {
    String typeName = this.createInstance.getTypeName();
    String referenceName = this.createInstance.getReferenceName();
    String variableName = this.createInstance.getVariableName();
    Map<String, Object> childVariables = new HashMap<>(variables);
    if (typeName != null && !typeName.isBlank() && referenceName != null && !referenceName.isBlank()) {
        // @formatter:off
        Optional<EObject> optionalOwnerEObject = Optional.ofNullable(variables.get(VariableManager.SELF)).filter(EObject.class::isInstance).map(EObject.class::cast);
        Optional<EditingDomain> optionalEditingDomain = Optional.of(variables.get(IEditingContext.EDITING_CONTEXT)).filter(EditingContext.class::isInstance).map(EditingContext.class::cast).map(EditingContext::getDomain);
        // @formatter:on
        Matcher matcher = SEPARATOR.matcher(typeName);
        if (optionalEditingDomain.isPresent() && optionalOwnerEObject.isPresent() && matcher.find()) {
            EditingDomain editingDomain = optionalEditingDomain.get();
            EObject ownerEObject = optionalOwnerEObject.get();
            String packageName = typeName.substring(0, matcher.start());
            String className = typeName.substring(matcher.end());
            Optional<EPackage> optionalEPackage = this.ePackageService.findEPackage(editingDomain.getResourceSet().getPackageRegistry(), packageName);
            EcoreIntrinsicExtender ecoreIntrinsicExtender = new EcoreIntrinsicExtender();
            if (optionalEPackage.isPresent()) {
                EPackage ePackage = optionalEPackage.get();
                ecoreIntrinsicExtender.updateMetamodels(Collections.singleton(new EcoreMetamodelDescriptor(ePackage)));
            }
            EObject createdInstance = ecoreIntrinsicExtender.createInstance(className);
            if (createdInstance == null) {
                // $NON-NLS-1$
                this.logger.warn("The creation of an instance of Type {} failed.", typeName);
            } else {
                Object addedObject = ecoreIntrinsicExtender.eAdd(ownerEObject, referenceName, createdInstance);
                if (addedObject == null) {
                    // $NON-NLS-1$
                    this.logger.warn("The feature {} does not exist on {}.", referenceName, typeName);
                } else if (variableName != null && !variableName.isBlank()) {
                    childVariables.put(variableName, createdInstance);
                }
                // Sub-operations are executed in the context of the new element
                childVariables.put(VariableManager.SELF, createdInstance);
            }
        }
    }
    List<ModelOperation> subModelOperations = this.createInstance.getSubModelOperations();
    return this.childModelOperationHandler.handle(this.objectService, this.representationMetadataSearchService, this.identifierProvider, this.interpreter, childVariables, subModelOperations);
}
Also used : HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) EcoreMetamodelDescriptor(org.eclipse.sirius.ecore.extender.business.api.accessor.EcoreMetamodelDescriptor) EditingDomain(org.eclipse.emf.edit.domain.EditingDomain) EPackage(org.eclipse.emf.ecore.EPackage) ModelOperation(org.eclipse.sirius.viewpoint.description.tool.ModelOperation) IEditingContext(org.eclipse.sirius.components.core.api.IEditingContext) EditingContext(org.eclipse.sirius.components.emf.services.EditingContext) EObject(org.eclipse.emf.ecore.EObject) EcoreIntrinsicExtender(org.eclipse.sirius.ecore.extender.business.internal.accessor.ecore.EcoreIntrinsicExtender) EObject(org.eclipse.emf.ecore.EObject)

Example 2 with EcoreMetamodelDescriptor

use of org.eclipse.sirius.ecore.extender.business.api.accessor.EcoreMetamodelDescriptor in project sirius-components by eclipse-sirius.

the class ToolImageProvider method getInstance.

private Optional<EObject> getInstance(String domainClass) {
    Matcher matcher = SEPARATOR.matcher(domainClass);
    if (matcher.find()) {
        String packageName = domainClass.substring(0, matcher.start());
        String className = domainClass.substring(matcher.end());
        var optionalEPackage = new EPackageService().findEPackage(this.ePackageRegistry, packageName);
        if (optionalEPackage.isPresent()) {
            EPackage ePackage = optionalEPackage.get();
            EcoreIntrinsicExtender ecoreIntrinsicExtender = new EcoreIntrinsicExtender();
            ecoreIntrinsicExtender.updateMetamodels(List.of(new EcoreMetamodelDescriptor(ePackage)));
            EObject instance = ecoreIntrinsicExtender.createInstance(className);
            return Optional.ofNullable(instance);
        }
    }
    return Optional.empty();
}
Also used : Matcher(java.util.regex.Matcher) EPackageService(org.eclipse.sirius.components.emf.compatibility.EPackageService) EObject(org.eclipse.emf.ecore.EObject) EcoreIntrinsicExtender(org.eclipse.sirius.ecore.extender.business.internal.accessor.ecore.EcoreIntrinsicExtender) EcoreMetamodelDescriptor(org.eclipse.sirius.ecore.extender.business.api.accessor.EcoreMetamodelDescriptor) EPackage(org.eclipse.emf.ecore.EPackage)

Aggregations

Matcher (java.util.regex.Matcher)2 EObject (org.eclipse.emf.ecore.EObject)2 EPackage (org.eclipse.emf.ecore.EPackage)2 EcoreMetamodelDescriptor (org.eclipse.sirius.ecore.extender.business.api.accessor.EcoreMetamodelDescriptor)2 EcoreIntrinsicExtender (org.eclipse.sirius.ecore.extender.business.internal.accessor.ecore.EcoreIntrinsicExtender)2 HashMap (java.util.HashMap)1 EditingDomain (org.eclipse.emf.edit.domain.EditingDomain)1 IEditingContext (org.eclipse.sirius.components.core.api.IEditingContext)1 EPackageService (org.eclipse.sirius.components.emf.compatibility.EPackageService)1 EditingContext (org.eclipse.sirius.components.emf.services.EditingContext)1 ModelOperation (org.eclipse.sirius.viewpoint.description.tool.ModelOperation)1