ReqSpecScopeProvider.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.reqspec.scoping;

import com.google.common.collect.Iterables;
import java.util.Collection;
import java.util.Collections;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.util.BasicInternalEList;
import org.eclipse.xtext.EcoreUtil2;
import org.eclipse.xtext.naming.QualifiedName;
import org.eclipse.xtext.resource.EObjectDescription;
import org.eclipse.xtext.resource.IEObjectDescription;
import org.eclipse.xtext.scoping.IScope;
import org.eclipse.xtext.scoping.Scopes;
import org.eclipse.xtext.scoping.impl.SimpleScope;
import org.eclipse.xtext.util.SimpleAttributeResolver;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.osate.aadl2.Classifier;
import org.osate.aadl2.ClassifierFeature;
import org.osate.aadl2.ComponentClassifier;
import org.osate.aadl2.ComponentImplementation;
import org.osate.aadl2.ComponentType;
import org.osate.aadl2.Connection;
import org.osate.aadl2.DirectionType;
import org.osate.aadl2.EndToEndFlow;
import org.osate.aadl2.Feature;
import org.osate.aadl2.FlowSpecification;
import org.osate.aadl2.Mode;
import org.osate.aadl2.NamedElement;
import org.osate.aadl2.StructuralFeature;
import org.osate.aadl2.Subcomponent;
import org.osate.aadl2.modelsupport.scoping.Aadl2GlobalScopeUtil;
import org.osate.alisa.common.common.AVariableReference;
import org.osate.alisa.common.scoping.CommonScopeProvider;
import org.osate.alisa.common.util.CommonUtilExtension;
import org.osate.reqspec.reqSpec.ContractualElement;
import org.osate.reqspec.reqSpec.Goal;
import org.osate.reqspec.reqSpec.ReqSpecPackage;
import org.osate.reqspec.reqSpec.Requirement;
import org.osate.reqspec.reqSpec.RequirementSet;
import org.osate.reqspec.reqSpec.StakeholderGoals;
import org.osate.reqspec.reqSpec.SystemRequirementSet;
import org.osate.reqspec.reqSpec.WhenCondition;
import org.osate.reqspec.util.ReqSpecUtilExtension;
import org.osate.xtext.aadl2.errormodel.errorModel.ErrorBehaviorState;
import org.osate.xtext.aadl2.errormodel.errorModel.ErrorFlow;
import org.osate.xtext.aadl2.errormodel.errorModel.ErrorPath;
import org.osate.xtext.aadl2.errormodel.errorModel.ErrorSource;
import org.osate.xtext.aadl2.errormodel.scoping.ErrorModelScopeProvider;
import org.osate.xtext.aadl2.errormodel.util.EMV2Util;

/**
 * This class contains custom scoping description.
 * 
 * see : http://www.eclipse.org/Xtext/documentation.html#scoping
 * on how and when to use it
 */
@SuppressWarnings("all")
public class ReqSpecScopeProvider extends CommonScopeProvider {
  public IScope scope_ContractualElement_targetElement(final ContractualElement context, final EReference reference) {
    final ComponentClassifier targetClassifier = ReqSpecUtilExtension.targetClassifier(context);
    if ((targetClassifier != null)) {
      if ((targetClassifier instanceof ComponentType)) {
        EList<Feature> _allFeatures = ((ComponentType)targetClassifier).getAllFeatures();
        EList<FlowSpecification> _allFlowSpecifications = ((ComponentType)targetClassifier).getAllFlowSpecifications();
        Iterable<StructuralFeature> _plus = Iterables.<StructuralFeature>concat(_allFeatures, _allFlowSpecifications);
        EList<Mode> _allModes = ((ComponentType)targetClassifier).getAllModes();
        Iterable<ClassifierFeature> _plus_1 = Iterables.<ClassifierFeature>concat(_plus, _allModes);
        Iterable<IEObjectDescription> _scopedElementsFor = Scopes.<EObject>scopedElementsFor(_plus_1, 
          QualifiedName.<EObject>wrapper(SimpleAttributeResolver.NAME_RESOLVER));
        return new SimpleScope(IScope.NULLSCOPE, _scopedElementsFor, true);
      }
      if ((targetClassifier instanceof ComponentImplementation)) {
        EList<Feature> _allFeatures_1 = ((ComponentImplementation)targetClassifier).getAllFeatures();
        EList<FlowSpecification> _allFlowSpecifications_1 = ((ComponentImplementation)targetClassifier).getType().getAllFlowSpecifications();
        Iterable<StructuralFeature> _plus_2 = Iterables.<StructuralFeature>concat(_allFeatures_1, _allFlowSpecifications_1);
        EList<Mode> _allModes_1 = ((ComponentImplementation)targetClassifier).getAllModes();
        Iterable<ClassifierFeature> _plus_3 = Iterables.<ClassifierFeature>concat(_plus_2, _allModes_1);
        EList<Subcomponent> _allSubcomponents = ((ComponentImplementation)targetClassifier).getAllSubcomponents();
        Iterable<ClassifierFeature> _plus_4 = Iterables.<ClassifierFeature>concat(_plus_3, _allSubcomponents);
        EList<EndToEndFlow> _allEndToEndFlows = ((ComponentImplementation)targetClassifier).getAllEndToEndFlows();
        Iterable<ClassifierFeature> _plus_5 = Iterables.<ClassifierFeature>concat(_plus_4, _allEndToEndFlows);
        EList<Connection> _allConnections = ((ComponentImplementation)targetClassifier).getAllConnections();
        Iterable<ClassifierFeature> _plus_6 = Iterables.<ClassifierFeature>concat(_plus_5, _allConnections);
        Iterable<IEObjectDescription> _scopedElementsFor_1 = Scopes.<EObject>scopedElementsFor(_plus_6, 
          QualifiedName.<EObject>wrapper(SimpleAttributeResolver.NAME_RESOLVER));
        return new SimpleScope(IScope.NULLSCOPE, _scopedElementsFor_1, true);
      }
    }
    return IScope.NULLSCOPE;
  }

  public IScope scope_Mode(final WhenCondition context, final EReference reference) {
    IScope _xblockexpression = null;
    {
      final ComponentClassifier targetClassifier = ReqSpecUtilExtension.targetClassifier(ReqSpecUtilExtension.containingRequirement(context));
      IScope _xifexpression = null;
      if ((targetClassifier instanceof ComponentType)) {
        Iterable<IEObjectDescription> _scopedElementsFor = Scopes.<EObject>scopedElementsFor(((ComponentType)targetClassifier).getAllModes(), 
          QualifiedName.<EObject>wrapper(SimpleAttributeResolver.NAME_RESOLVER));
        final SimpleScope thescope = new SimpleScope(IScope.NULLSCOPE, _scopedElementsFor, true);
        return thescope;
      } else {
        _xifexpression = IScope.NULLSCOPE;
      }
      _xblockexpression = _xifexpression;
    }
    return _xblockexpression;
  }

  public IScope scope_ErrorBehaviorState(final WhenCondition context, final EReference reference) {
    IScope _xblockexpression = null;
    {
      final ComponentClassifier targetClassifier = ReqSpecUtilExtension.targetClassifier(ReqSpecUtilExtension.containingRequirement(context));
      IScope _xifexpression = null;
      if ((targetClassifier != null)) {
        final Collection<ErrorBehaviorState> states = EMV2Util.getAllErrorBehaviorStates(targetClassifier);
        Iterable<IEObjectDescription> _scopedElementsFor = Scopes.<EObject>scopedElementsFor(states, QualifiedName.<EObject>wrapper(SimpleAttributeResolver.NAME_RESOLVER));
        final SimpleScope thescope = new SimpleScope(IScope.NULLSCOPE, _scopedElementsFor, true);
        return thescope;
      } else {
        _xifexpression = IScope.NULLSCOPE;
      }
      _xblockexpression = _xifexpression;
    }
    return _xblockexpression;
  }

  public IScope scope_AVariableDeclaration(final AVariableReference context, final EReference reference) {
    final IScope result = ReqSpecUtilExtension.scopeForGlobalVal(context, IScope.NULLSCOPE);
    final ContractualElement contract = ReqSpecUtilExtension.containingContractualElement(context);
    boolean _matched = false;
    if (contract instanceof Requirement) {
      _matched=true;
      return ReqSpecUtilExtension.scopeForValCompute(((Requirement)contract), result);
    }
    if (!_matched) {
      if (contract instanceof Goal) {
        _matched=true;
        return ReqSpecUtilExtension.scopeForVal(((Goal)contract), result);
      }
    }
    return null;
  }

  public IScope scope_AVariableDeclaration(final Requirement context, final EReference reference) {
    final IScope result = ReqSpecUtilExtension.scopeForGlobalVal(context, IScope.NULLSCOPE);
    return ReqSpecUtilExtension.scopeForValCompute(context, result);
  }

  public IScope scope_AVariableDeclaration(final Goal context, final EReference reference) {
    final IScope result = ReqSpecUtilExtension.scopeForGlobalVal(context, IScope.NULLSCOPE);
    return ReqSpecUtilExtension.scopeForVal(context, result);
  }

  public IScope scope_Requirement_refinesReference(final Requirement context, final EReference reference) {
    IScope _xblockexpression = null;
    {
      IScope result = this.delegateGetScope(context, reference);
      final RequirementSet reqs = ReqSpecUtilExtension.containingRequirementSet(context);
      if ((reqs instanceof SystemRequirementSet)) {
        final ComponentClassifier targetComponentClassifier = ((SystemRequirementSet)reqs).getTarget();
        final Function1<SystemRequirementSet, Boolean> _function = (SystemRequirementSet sysreqs) -> {
          return Boolean.valueOf(CommonUtilExtension.isSameorExtends(targetComponentClassifier, sysreqs.getTarget()));
        };
        final Iterable<SystemRequirementSet> listAccessibleSystemRequirements = IterableExtensions.<SystemRequirementSet>filter(Aadl2GlobalScopeUtil.<SystemRequirementSet>getAll(context, ReqSpecPackage.eINSTANCE.getSystemRequirementSet()), _function);
        for (final SystemRequirementSet sr : listAccessibleSystemRequirements) {
          boolean _isEmpty = sr.getRequirements().isEmpty();
          boolean _not = (!_isEmpty);
          if (_not) {
            Iterable<IEObjectDescription> _scopedElementsFor = Scopes.<EObject>scopedElementsFor(sr.getRequirements(), 
              QualifiedName.<EObject>wrapper(SimpleAttributeResolver.NAME_RESOLVER));
            SimpleScope _simpleScope = new SimpleScope(result, _scopedElementsFor, true);
            result = _simpleScope;
          }
        }
      }
      _xblockexpression = result;
    }
    return _xblockexpression;
  }

  public IScope scope_Requirement_decomposesReference(final Requirement context, final EReference reference) {
    return this.scope_Requirement_refinesReference(context, reference);
  }

  public IScope scope_Requirement_evolvesReference(final Requirement context, final EReference reference) {
    return this.scope_Requirement_refinesReference(context, reference);
  }

  public IScope scope_Requirement_inheritsReference(final Requirement context, final EReference reference) {
    return this.scope_Requirement_refinesReference(context, reference);
  }

  public IScope scope_Requirement_exception(final Requirement context, final EReference reference) {
    IScope _xblockexpression = null;
    {
      final ComponentClassifier targetClassifier = ReqSpecUtilExtension.targetClassifier(context);
      IScope _xifexpression = null;
      if ((targetClassifier != null)) {
        Collection<ErrorSource> _allErrorSources = EMV2Util.getAllErrorSources(targetClassifier);
        Collection<ErrorPath> _allErrorPaths = EMV2Util.getAllErrorPaths(targetClassifier);
        final Iterable<ErrorFlow> exceptionItems = Iterables.<ErrorFlow>concat(_allErrorSources, _allErrorPaths);
        final SimpleScope propscope = ErrorModelScopeProvider.scopeForErrorPropagation(targetClassifier, DirectionType.OUT);
        Iterable<IEObjectDescription> _scopedElementsFor = Scopes.<EObject>scopedElementsFor(exceptionItems, 
          QualifiedName.<EObject>wrapper(SimpleAttributeResolver.NAME_RESOLVER));
        final SimpleScope thescope = new SimpleScope(propscope, _scopedElementsFor, true);
        return thescope;
      } else {
        _xifexpression = IScope.NULLSCOPE;
      }
      _xblockexpression = _xifexpression;
    }
    return _xblockexpression;
  }

  @Override
  public IScope scope_AModelReference_modelElement(final EObject context, final EReference reference) {
    SimpleScope _xblockexpression = null;
    {
      final ContractualElement contractualElement = EcoreUtil2.<ContractualElement>getContainerOfType(context, ContractualElement.class);
      NamedElement _elvis = null;
      NamedElement _elvis_1 = null;
      NamedElement _elvis_2 = null;
      NamedElement _targetElement = null;
      if (contractualElement!=null) {
        _targetElement=contractualElement.getTargetElement();
      }
      if (_targetElement != null) {
        _elvis_2 = _targetElement;
      } else {
        ComponentClassifier _target = null;
        if (contractualElement!=null) {
          _target=contractualElement.getTarget();
        }
        _elvis_2 = _target;
      }
      if (_elvis_2 != null) {
        _elvis_1 = _elvis_2;
      } else {
        StakeholderGoals _containerOfType = EcoreUtil2.<StakeholderGoals>getContainerOfType(context, StakeholderGoals.class);
        ComponentClassifier _target_1 = null;
        if (_containerOfType!=null) {
          _target_1=_containerOfType.getTarget();
        }
        _elvis_1 = _target_1;
      }
      if (_elvis_1 != null) {
        _elvis = _elvis_1;
      } else {
        ComponentClassifier _target_2 = EcoreUtil2.<SystemRequirementSet>getContainerOfType(context, SystemRequirementSet.class).getTarget();
        _elvis = _target_2;
      }
      final NamedElement target = _elvis;
      IEObjectDescription _create = EObjectDescription.create("this", target);
      _xblockexpression = new SimpleScope(Collections.<IEObjectDescription>unmodifiableList(CollectionLiterals.<IEObjectDescription>newArrayList(_create)));
    }
    return _xblockexpression;
  }

  public EList<ComponentClassifier> getSelfPlusAncestors(final ComponentClassifier cl) {
    BasicInternalEList<ComponentClassifier> _xblockexpression = null;
    {
      final BasicInternalEList<ComponentClassifier> cls = new BasicInternalEList<ComponentClassifier>(ComponentClassifier.class);
      cls.add(cl);
      ComponentClassifier temp = cl;
      while ((temp.getExtended() != null)) {
        {
          boolean _contains = cls.contains(temp.getExtended());
          if (_contains) {
            return cls;
          }
          Classifier _extended = temp.getExtended();
          temp = ((ComponentClassifier) _extended);
          cls.add(temp);
        }
      }
      if ((cl instanceof ComponentImplementation)) {
        temp = ((ComponentImplementation)cl).getType();
        cls.add(temp);
        while ((temp.getExtended() != null)) {
          {
            boolean _contains = cls.contains(temp.getExtended());
            if (_contains) {
              return cls;
            }
            Classifier _extended = temp.getExtended();
            temp = ((ComponentClassifier) _extended);
            cls.add(temp);
          }
        }
      }
      _xblockexpression = cls;
    }
    return _xblockexpression;
  }
}