Search in sources :

Example 1 with TransactionalCommandStack

use of org.eclipse.emf.transaction.TransactionalCommandStack in project AGREE by loonwerks.

the class EphemeralImplementationUtil method cleanup.

/**
 * Delete the accumulated ephemeral component implementations by deleting their containing {@link Resource}.
 * <p>
 * This method is intended to be called immediately prior to the instance of this utility going out of scope.
 * However, it may be called multiple times, deleting the ephemeral implementations and resource accumulated to
 * that point.
 */
public void cleanup() {
    final TransactionalEditingDomain domain = WorkspaceEditingDomainFactory.INSTANCE.createEditingDomain();
    // We execute this command on the command stack because otherwise, we will not
    // have write permissions on the editing domain.
    Command cmd = new RecordingCommand(domain) {

        @Override
        protected void doExecute() {
            try {
                Iterator<Resource> iter = ephemeralResources.iterator();
                while (iter.hasNext()) {
                    Resource res = iter.next();
                    res.delete(Collections.EMPTY_MAP);
                    iter.remove();
                }
            } catch (IOException e) {
                setErrorMessage(e.getMessage());
                e.printStackTrace();
            }
        }
    };
    try {
        ((TransactionalCommandStack) domain.getCommandStack()).execute(cmd, null);
    } catch (InterruptedException | RollbackException e) {
        setErrorMessage(e.getMessage());
        e.printStackTrace();
    }
}
Also used : TransactionalEditingDomain(org.eclipse.emf.transaction.TransactionalEditingDomain) RecordingCommand(org.eclipse.emf.transaction.RecordingCommand) TransactionalCommandStack(org.eclipse.emf.transaction.TransactionalCommandStack) RecordingCommand(org.eclipse.emf.transaction.RecordingCommand) Command(org.eclipse.emf.common.command.Command) Resource(org.eclipse.emf.ecore.resource.Resource) IOException(java.io.IOException) RollbackException(org.eclipse.emf.transaction.RollbackException)

Example 2 with TransactionalCommandStack

use of org.eclipse.emf.transaction.TransactionalCommandStack in project AGREE by loonwerks.

the class EphemeralImplementationUtil method generateEphemeralCompImplFromType.

/**
 * Generate an ephemeral {@link ComponentImplementation} matching the subtype of the given {@link ComponentType}.
 * <p>
 * Ephemerally generated component implementations are placed it in an ephemeral {@link Resource}.  The ephemeral
 * resources are intended to have short lifecycles and deleted by the {@link cleanup} method.
 *
 * @param ct The component type for which to create an ephemeral implementation.
 * @return A component implementation for the given component type.
 * @throws Exception
 */
@SuppressWarnings("unchecked")
public ComponentImplementation generateEphemeralCompImplFromType(ComponentType ct) throws Exception {
    // Resource aadlResource = OsateResourceUtil.getResource(getEphemeralImplURI(ct));
    Resource aadlResource = getResource(getEphemeralImplURI(ct));
    ephemeralResources.add(aadlResource);
    List<ComponentImplementation> resultList;
    ComponentImplementation result;
    final TransactionalEditingDomain domain = WorkspaceEditingDomainFactory.INSTANCE.createEditingDomain();
    // We execute this command on the command stack because otherwise, we will not
    // have write permissions on the editing domain.
    Command cmd = new RecordingCommand(domain) {

        public ComponentImplementation implementation;

        @Override
        protected void doExecute() {
            try {
                implementation = createComponentImplementationInternal(ct, aadlResource);
            } catch (InterruptedException e) {
            // Do nothing. Will be thrown after execute.
            }
        }

        @Override
        public List<ComponentImplementation> getResult() {
            return Collections.singletonList(implementation);
        }
    };
    ((TransactionalCommandStack) domain.getCommandStack()).execute(cmd, null);
    if (monitor.isCanceled()) {
        throw new InterruptedException();
    }
    try {
        // We're done: Save the model.
        // We don't respond to a cancel at this point
        monitor.subTask("Saving implementation model");
        aadlResource.save(null);
    } catch (IOException e) {
        e.printStackTrace();
        setErrorMessage(e.getMessage());
        return null;
    }
    resultList = (List<ComponentImplementation>) cmd.getResult();
    result = resultList.get(0);
    return result;
}
Also used : ComponentImplementation(org.osate.aadl2.ComponentImplementation) TransactionalEditingDomain(org.eclipse.emf.transaction.TransactionalEditingDomain) RecordingCommand(org.eclipse.emf.transaction.RecordingCommand) TransactionalCommandStack(org.eclipse.emf.transaction.TransactionalCommandStack) RecordingCommand(org.eclipse.emf.transaction.RecordingCommand) Command(org.eclipse.emf.common.command.Command) Resource(org.eclipse.emf.ecore.resource.Resource) IOException(java.io.IOException)

Example 3 with TransactionalCommandStack

use of org.eclipse.emf.transaction.TransactionalCommandStack in project AGREE by loonwerks.

the class EphemeralImplementationUtil method generateEphemeralCompInstanceFromImplementation.

/**
 * Generate an ephemeral {@link SystemInstance} matching the subtype of the given {@link ComponentType}.
 * <p>
 * Ephemerally generated system instances are placed it in an ephemeral {@link Resource}.  The ephemeral
 * resources are intended to have short lifecycles and deleted by the {@link cleanup} method.
 *
 * @param ct The component type for which to create an ephemeral implementation.
 * @return A system instance for the given component type.
 * @throws Exception
 * @since 2.8
 */
@SuppressWarnings("unchecked")
public SystemInstance generateEphemeralCompInstanceFromImplementation(ComponentImplementation ci) throws Exception {
    final TransactionalEditingDomain domain = WorkspaceEditingDomainFactory.INSTANCE.createEditingDomain();
    Resource instanceAadlResource = getResource(InstantiateModel.getInstanceModelURI(ci));
    ephemeralResources.add(instanceAadlResource);
    List<SystemInstance> instanceResultList;
    SystemInstance instanceResult;
    // We execute this command on the command stack because otherwise, we will not
    // have write permissions on the editing domain.
    Command instanceCmd = new RecordingCommand(domain) {

        public SystemInstance systemInstance;

        @Override
        protected void doExecute() {
            try {
                final InstantiateModel instantiateModel = new InstantiateModel(monitor, new AnalysisErrorReporterManager(new MarkerAnalysisErrorReporter.Factory(AadlConstants.INSTANTIATION_OBJECT_MARKER)));
                systemInstance = instantiateModel.createSystemInstance(ci, instanceAadlResource);
            } catch (InterruptedException e) {
            // Do nothing. Will be thrown after execute.
            } catch (Exception e) {
                e.printStackTrace();
                errorMessage = e.getMessage();
                e.getMessage();
            }
        }

        @Override
        public List<SystemInstance> getResult() {
            return Collections.singletonList(systemInstance);
        }
    };
    ((TransactionalCommandStack) domain.getCommandStack()).execute(instanceCmd, null);
    if (monitor.isCanceled()) {
        throw new InterruptedException();
    }
    try {
        // We're done: Save the model.
        monitor.subTask("Saving instance model");
        instanceAadlResource.save(null);
    } catch (IOException e) {
        e.printStackTrace();
        setErrorMessage(e.getMessage());
        return null;
    }
    instanceResultList = (List<SystemInstance>) instanceCmd.getResult();
    instanceResult = instanceResultList.get(0);
    return instanceResult;
}
Also used : InstantiateModel(org.osate.aadl2.instantiation.InstantiateModel) Resource(org.eclipse.emf.ecore.resource.Resource) Aadl2Factory(org.osate.aadl2.Aadl2Factory) WorkspaceEditingDomainFactory(org.eclipse.emf.workspace.WorkspaceEditingDomainFactory) IOException(java.io.IOException) AnalysisErrorReporterManager(org.osate.aadl2.modelsupport.errorreporting.AnalysisErrorReporterManager) RollbackException(org.eclipse.emf.transaction.RollbackException) IOException(java.io.IOException) TransactionalEditingDomain(org.eclipse.emf.transaction.TransactionalEditingDomain) RecordingCommand(org.eclipse.emf.transaction.RecordingCommand) TransactionalCommandStack(org.eclipse.emf.transaction.TransactionalCommandStack) RecordingCommand(org.eclipse.emf.transaction.RecordingCommand) Command(org.eclipse.emf.common.command.Command) SystemInstance(org.osate.aadl2.instance.SystemInstance)

Example 4 with TransactionalCommandStack

use of org.eclipse.emf.transaction.TransactionalCommandStack in project AGREE by loonwerks.

the class EphemeralImplementationUtil method generateEphemeralCompInstanceFromType.

/**
 * Generate an ephemeral {@link SystemInstance} matching the subtype of the given {@link ComponentType}.
 * <p>
 * Ephemerally generated system instances are placed it in an ephemeral {@link Resource}.  The ephemeral
 * resources are intended to have short lifecycles and deleted by the {@link cleanup} method.
 *
 * @param ct The component type for which to create an ephemeral implementation.
 * @return A system instance for the given component type.
 * @throws Exception
 * @since 2.8
 */
@SuppressWarnings("unchecked")
public SystemInstance generateEphemeralCompInstanceFromType(ComponentType ct) throws Exception {
    Resource implementationAadlResource = getResource(getEphemeralImplURI(ct));
    ephemeralResources.add(implementationAadlResource);
    List<ComponentImplementation> implementationResultList;
    ComponentImplementation implementationResult;
    final TransactionalEditingDomain domain = WorkspaceEditingDomainFactory.INSTANCE.createEditingDomain();
    Command implementationCmd = new RecordingCommand(domain) {

        public ComponentImplementation implementation;

        @Override
        protected void doExecute() {
            try {
                implementation = createComponentImplementationInternal(ct, implementationAadlResource);
            } catch (InterruptedException e) {
            // Do nothing. Will be thrown after execute.
            }
        }

        @Override
        public List<ComponentImplementation> getResult() {
            return Collections.singletonList(implementation);
        }
    };
    ((TransactionalCommandStack) domain.getCommandStack()).execute(implementationCmd, null);
    if (monitor.isCanceled()) {
        throw new InterruptedException();
    }
    try {
        // We're done: Save the model.
        // We don't respond to a cancel at this point
        monitor.subTask("Saving implementation model");
        implementationAadlResource.save(null);
    } catch (IOException e) {
        e.printStackTrace();
        setErrorMessage(e.getMessage());
        return null;
    }
    implementationResultList = (List<ComponentImplementation>) implementationCmd.getResult();
    implementationResult = implementationResultList.get(0);
    Resource instanceAadlResource = getResource(InstantiateModel.getInstanceModelURI(implementationResult));
    ephemeralResources.add(instanceAadlResource);
    List<SystemInstance> instanceResultList;
    SystemInstance instanceResult;
    // We execute this command on the command stack because otherwise, we will not
    // have write permissions on the editing domain.
    Command instanceCmd = new RecordingCommand(domain) {

        public SystemInstance systemInstance;

        @Override
        protected void doExecute() {
            try {
                final InstantiateModel instantiateModel = new InstantiateModel(monitor, new AnalysisErrorReporterManager(new MarkerAnalysisErrorReporter.Factory(AadlConstants.INSTANTIATION_OBJECT_MARKER)));
                systemInstance = instantiateModel.createSystemInstance(implementationResult, instanceAadlResource);
            } catch (InterruptedException e) {
            // Do nothing. Will be thrown after execute.
            } catch (Exception e) {
                e.printStackTrace();
                errorMessage = e.getMessage();
                e.getMessage();
            }
        }

        @Override
        public List<SystemInstance> getResult() {
            return Collections.singletonList(systemInstance);
        }
    };
    ((TransactionalCommandStack) domain.getCommandStack()).execute(instanceCmd, null);
    if (monitor.isCanceled()) {
        throw new InterruptedException();
    }
    try {
        // We're done: Save the model.
        monitor.subTask("Saving instance model");
        instanceAadlResource.save(null);
    } catch (IOException e) {
        e.printStackTrace();
        setErrorMessage(e.getMessage());
        return null;
    }
    instanceResultList = (List<SystemInstance>) instanceCmd.getResult();
    instanceResult = instanceResultList.get(0);
    return instanceResult;
}
Also used : ComponentImplementation(org.osate.aadl2.ComponentImplementation) InstantiateModel(org.osate.aadl2.instantiation.InstantiateModel) Resource(org.eclipse.emf.ecore.resource.Resource) Aadl2Factory(org.osate.aadl2.Aadl2Factory) WorkspaceEditingDomainFactory(org.eclipse.emf.workspace.WorkspaceEditingDomainFactory) IOException(java.io.IOException) AnalysisErrorReporterManager(org.osate.aadl2.modelsupport.errorreporting.AnalysisErrorReporterManager) RollbackException(org.eclipse.emf.transaction.RollbackException) IOException(java.io.IOException) TransactionalEditingDomain(org.eclipse.emf.transaction.TransactionalEditingDomain) RecordingCommand(org.eclipse.emf.transaction.RecordingCommand) TransactionalCommandStack(org.eclipse.emf.transaction.TransactionalCommandStack) RecordingCommand(org.eclipse.emf.transaction.RecordingCommand) Command(org.eclipse.emf.common.command.Command) SystemInstance(org.osate.aadl2.instance.SystemInstance)

Aggregations

IOException (java.io.IOException)4 Command (org.eclipse.emf.common.command.Command)4 Resource (org.eclipse.emf.ecore.resource.Resource)4 RecordingCommand (org.eclipse.emf.transaction.RecordingCommand)4 TransactionalCommandStack (org.eclipse.emf.transaction.TransactionalCommandStack)4 TransactionalEditingDomain (org.eclipse.emf.transaction.TransactionalEditingDomain)4 RollbackException (org.eclipse.emf.transaction.RollbackException)3 WorkspaceEditingDomainFactory (org.eclipse.emf.workspace.WorkspaceEditingDomainFactory)2 Aadl2Factory (org.osate.aadl2.Aadl2Factory)2 ComponentImplementation (org.osate.aadl2.ComponentImplementation)2 SystemInstance (org.osate.aadl2.instance.SystemInstance)2 InstantiateModel (org.osate.aadl2.instantiation.InstantiateModel)2 AnalysisErrorReporterManager (org.osate.aadl2.modelsupport.errorreporting.AnalysisErrorReporterManager)2