Aadl2SemanticSequencer.java

/**
 * Copyright (c) 2004-2025 Carnegie Mellon University and others. (see Contributors file).
 * All Rights Reserved.
 * 
 * NO WARRANTY. ALL MATERIAL IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY
 * KIND, EITHER EXPRESSED OR IMPLIED, AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR PURPOSE
 * OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT
 * MAKE ANY WARRANTY OF ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.
 * 
 * This program and the accompanying materials are made available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 * SPDX-License-Identifier: EPL-2.0
 * 
 * Created, in part, with funding and support from the United States Government. (see Acknowledgments file).
 * 
 * This program includes and/or can make use of certain third party source code, object code, documentation and other
 * files ("Third Party Software"). The Third Party Software that is used by this program is dependent upon your system
 * configuration. By using this program, You agree to comply with any and all relevant Third Party Software terms and
 * conditions contained in any such Third Party Software or separate license file distributed with such Third Party
 * Software. The parties who own the Third Party Software ("Third Party Licensors") are intended third party benefici-
 * aries to this license with respect to the terms applicable to their Third Party Software. Third Party Software li-
 * censes only apply to the Third Party Software and not any other portion of this program or this program as a whole.
 */
package org.osate.xtext.aadl2.serializer;

import com.google.common.base.Objects;
import com.google.inject.Inject;
import org.eclipse.emf.common.command.CommandStack;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.ParserRule;
import org.eclipse.xtext.serializer.ISerializationContext;
import org.eclipse.xtext.serializer.analysis.SerializationContext;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.osate.aadl2.AnnexLibrary;
import org.osate.aadl2.AnnexSubclause;
import org.osate.aadl2.DefaultAnnexLibrary;
import org.osate.aadl2.DefaultAnnexSubclause;
import org.osate.aadl2.FlowImplementation;
import org.osate.aadl2.FlowKind;
import org.osate.aadl2.FlowSpecification;
import org.osate.annexsupport.AnnexRegistry;
import org.osate.annexsupport.AnnexUnparser;
import org.osate.annexsupport.AnnexUnparserRegistry;
import org.osate.xtext.aadl2.services.Aadl2GrammarAccess;

@SuppressWarnings("all")
public class Aadl2SemanticSequencer extends AbstractAadl2SemanticSequencer {
  @Inject
  private Aadl2GrammarAccess grammarAccess;

  AnnexUnparserRegistry unparserRegistry;

  protected AnnexUnparserRegistry getAnnexUnparserRegistry() {
    AnnexUnparserRegistry _elvis = null;
    if (this.unparserRegistry != null) {
      _elvis = this.unparserRegistry;
    } else {
      AnnexRegistry _registry = AnnexRegistry.getRegistry(AnnexRegistry.ANNEX_UNPARSER_EXT_ID);
      _elvis = (this.unparserRegistry = ((AnnexUnparserRegistry) _registry));
    }
    return _elvis;
  }

  @Override
  public void createSequence(final ISerializationContext context, final EObject semanticObject) {
    boolean _matched = false;
    if (semanticObject instanceof DefaultAnnexLibrary) {
      if ((Objects.equal(context.getParserRule(), this.grammarAccess.getAnnexLibraryRule()) || Objects.equal(context.getParserRule(), this.grammarAccess.getDefaultAnnexLibraryRule()))) {
        _matched=true;
        final AnnexLibrary parsedLibrary = ((DefaultAnnexLibrary)semanticObject).getParsedAnnexLibrary();
        final AnnexUnparser annexUnparser = this.getAnnexUnparserRegistry().getAnnexUnparser(((DefaultAnnexLibrary)semanticObject).getName());
        if (((parsedLibrary != null) && (annexUnparser != null))) {
          try {
            this.performModification(semanticObject, new Runnable() {
              @Override
              public void run() {
                StringConcatenation _builder = new StringConcatenation();
                _builder.append("{**");
                String _unparseAnnexLibrary = annexUnparser.unparseAnnexLibrary(parsedLibrary, "  ");
                _builder.append(_unparseAnnexLibrary);
                _builder.append("**}");
                final String text = _builder.toString();
                ((DefaultAnnexLibrary)semanticObject).setSourceText(text);
              }
            });
          } catch (final Throwable _t) {
            if (_t instanceof Exception) {
              final Exception e = (Exception)_t;
              StringConcatenation _builder = new StringConcatenation();
              _builder.append("Error while serializing ");
              String _name = ((DefaultAnnexLibrary)semanticObject).getName();
              _builder.append(_name);
              _builder.append(" annex library");
              throw new RuntimeException(_builder.toString(), e);
            } else {
              throw Exceptions.sneakyThrow(_t);
            }
          }
        }
        this.sequence_DefaultAnnexLibrary(context, ((DefaultAnnexLibrary)semanticObject));
      }
    }
    if (!_matched) {
      if (semanticObject instanceof DefaultAnnexSubclause) {
        if ((Objects.equal(context.getParserRule(), this.grammarAccess.getAnnexSubclauseRule()) || Objects.equal(context.getParserRule(), this.grammarAccess.getDefaultAnnexSubclauseRule()))) {
          _matched=true;
          final AnnexSubclause parsedSubclause = ((DefaultAnnexSubclause)semanticObject).getParsedAnnexSubclause();
          final AnnexUnparser annexUnparser = this.getAnnexUnparserRegistry().getAnnexUnparser(((DefaultAnnexSubclause)semanticObject).getName());
          if (((parsedSubclause != null) && (annexUnparser != null))) {
            try {
              this.performModification(semanticObject, new Runnable() {
                @Override
                public void run() {
                  StringConcatenation _builder = new StringConcatenation();
                  _builder.append("{**");
                  String _unparseAnnexSubclause = annexUnparser.unparseAnnexSubclause(parsedSubclause, "  ");
                  _builder.append(_unparseAnnexSubclause);
                  _builder.append("**}");
                  final String text = _builder.toString();
                  ((DefaultAnnexSubclause)semanticObject).setSourceText(text);
                }
              });
            } catch (final Throwable _t) {
              if (_t instanceof Exception) {
                final Exception e = (Exception)_t;
                StringConcatenation _builder = new StringConcatenation();
                _builder.append("Error while serializing ");
                String _name = ((DefaultAnnexSubclause)semanticObject).getName();
                _builder.append(_name);
                _builder.append(" annex subclause");
                throw new RuntimeException(_builder.toString(), e);
              } else {
                throw Exceptions.sneakyThrow(_t);
              }
            }
          }
          this.sequence_DefaultAnnexSubclause(context, ((DefaultAnnexSubclause)semanticObject));
        }
      }
    }
    if (!_matched) {
      super.createSequence(context, semanticObject);
    }
  }

  protected TransactionalEditingDomain performModification(final EObject semanticObject, final Runnable runnable) {
    Resource _eResource = null;
    if (semanticObject!=null) {
      _eResource=semanticObject.eResource();
    }
    ResourceSet _resourceSet = null;
    if (_eResource!=null) {
      _resourceSet=_eResource.getResourceSet();
    }
    final ResourceSet resourceSet = _resourceSet;
    TransactionalEditingDomain domain = null;
    if ((resourceSet != null)) {
      domain = TransactionalEditingDomain.Factory.INSTANCE.getEditingDomain(resourceSet);
    }
    if ((domain == null)) {
      runnable.run();
    } else {
      CommandStack _commandStack = domain.getCommandStack();
      _commandStack.execute(new RecordingCommand(domain) {
        @Override
        protected void doExecute() {
          runnable.run();
        }
      });
    }
    return domain;
  }

  @Override
  protected void sequence_FlowPathSpec_FlowSinkSpec_FlowSourceSpec_FlowSpecRefinement(final ISerializationContext context, final FlowSpecification spec) {
    FlowSpecification _refined = spec.getRefined();
    boolean _tripleNotEquals = (_refined != null);
    if (_tripleNotEquals) {
      ParserRule _flowSpecRefinementRule = this.grammarAccess.getFlowSpecRefinementRule();
      SerializationContext.RuleContext _ruleContext = new SerializationContext.RuleContext(context, _flowSpecRefinementRule);
      this.sequence_FlowSpecRefinement(_ruleContext, spec);
    } else {
      FlowKind _kind = spec.getKind();
      if (_kind != null) {
        switch (_kind) {
          case SOURCE:
            ParserRule _flowSourceSpecRule = this.grammarAccess.getFlowSourceSpecRule();
            SerializationContext.RuleContext _ruleContext_1 = new SerializationContext.RuleContext(context, _flowSourceSpecRule);
            this.sequence_FlowSourceSpec(_ruleContext_1, spec);
            break;
          case PATH:
            ParserRule _flowPathSpecRule = this.grammarAccess.getFlowPathSpecRule();
            SerializationContext.RuleContext _ruleContext_2 = new SerializationContext.RuleContext(context, _flowPathSpecRule);
            this.sequence_FlowPathSpec(_ruleContext_2, spec);
            break;
          case SINK:
            ParserRule _flowSinkSpecRule = this.grammarAccess.getFlowSinkSpecRule();
            SerializationContext.RuleContext _ruleContext_3 = new SerializationContext.RuleContext(context, _flowSinkSpecRule);
            this.sequence_FlowSinkSpec(_ruleContext_3, spec);
            break;
          default:
            break;
        }
      }
    }
  }

  @Override
  protected void sequence_FlowPathImpl_FlowSinkImpl_FlowSourceImpl(final ISerializationContext context, final FlowImplementation impl) {
    FlowKind _kind = impl.getKind();
    if (_kind != null) {
      switch (_kind) {
        case SOURCE:
          ParserRule _flowSourceImplRule = this.grammarAccess.getFlowSourceImplRule();
          SerializationContext.RuleContext _ruleContext = new SerializationContext.RuleContext(context, _flowSourceImplRule);
          this.sequence_FlowSourceImpl(_ruleContext, impl);
          break;
        case PATH:
          ParserRule _flowPathImplRule = this.grammarAccess.getFlowPathImplRule();
          SerializationContext.RuleContext _ruleContext_1 = new SerializationContext.RuleContext(context, _flowPathImplRule);
          this.sequence_FlowPathImpl(_ruleContext_1, impl);
          break;
        case SINK:
          ParserRule _flowSinkImplRule = this.grammarAccess.getFlowSinkImplRule();
          SerializationContext.RuleContext _ruleContext_2 = new SerializationContext.RuleContext(context, _flowSinkImplRule);
          this.sequence_FlowSinkImpl(_ruleContext_2, impl);
          break;
        default:
          break;
      }
    }
  }
}