ReqSpecUtilExtension.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.util;

import com.google.common.collect.Iterables;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.EcoreUtil2;
import org.eclipse.xtext.naming.QualifiedName;
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.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.osate.aadl2.ComponentCategory;
import org.osate.aadl2.ComponentClassifier;
import org.osate.aadl2.NamedElement;
import org.osate.alisa.common.common.AVariableDeclaration;
import org.osate.alisa.common.common.Description;
import org.osate.alisa.common.common.ValDeclaration;
import org.osate.alisa.common.util.CommonUtilExtension;
import org.osate.reqspec.reqSpec.ContractualElement;
import org.osate.reqspec.reqSpec.GlobalConstants;
import org.osate.reqspec.reqSpec.Goal;
import org.osate.reqspec.reqSpec.Requirement;
import org.osate.reqspec.reqSpec.RequirementSet;
import org.osate.reqspec.reqSpec.StakeholderGoals;
import org.osate.reqspec.reqSpec.SystemRequirementSet;

@SuppressWarnings("all")
public class ReqSpecUtilExtension {
  /**
   * return the classifier of the target.
   * The target could be an element inside a classifier. Then return its classifier.
   * If the target is a classifier return it.
   */
  public static ComponentClassifier targetClassifier(final ContractualElement req) {
    ComponentClassifier _target = req.getTarget();
    boolean _tripleNotEquals = (_target != null);
    if (_tripleNotEquals) {
      return req.getTarget();
    }
    EObject container = req;
    while ((container.eContainer() != null)) {
      {
        container = container.eContainer();
        if ((container instanceof SystemRequirementSet)) {
          final SystemRequirementSet rs = ((SystemRequirementSet)container);
          ComponentClassifier _target_1 = rs.getTarget();
          boolean _tripleNotEquals_1 = (_target_1 != null);
          if (_tripleNotEquals_1) {
            return rs.getTarget();
          }
        } else {
          if ((container instanceof StakeholderGoals)) {
            final StakeholderGoals rs_1 = ((StakeholderGoals)container);
            ComponentClassifier _target_2 = rs_1.getTarget();
            boolean _tripleNotEquals_2 = (_target_2 != null);
            if (_tripleNotEquals_2) {
              return rs_1.getTarget();
            }
          }
        }
      }
    }
    return null;
  }

  public static ContractualElement containingContractualElement(final EObject sh) {
    return EcoreUtil2.<ContractualElement>getContainerOfType(sh, ContractualElement.class);
  }

  public static Requirement containingRequirement(final EObject sh) {
    return EcoreUtil2.<Requirement>getContainerOfType(sh, Requirement.class);
  }

  public static RequirementSet containingRequirementSet(final EObject sh) {
    return EcoreUtil2.<RequirementSet>getContainerOfType(sh, RequirementSet.class);
  }

  public static StakeholderGoals containingStakeholderGoals(final EObject sh) {
    return EcoreUtil2.<StakeholderGoals>getContainerOfType(sh, StakeholderGoals.class);
  }

  public static IScope scopeForGlobalVal(final EObject context, final IScope parentScope) {
    IScope result = parentScope;
    final EList<GlobalConstants> projectconstants = ReqSpecUtilExtension.getImportedGlobals(context);
    Iterable<AVariableDeclaration> constants = new BasicEList<AVariableDeclaration>();
    for (final GlobalConstants pc : projectconstants) {
      EList<ValDeclaration> _constants = pc.getConstants();
      Iterable<AVariableDeclaration> _plus = Iterables.<AVariableDeclaration>concat(constants, _constants);
      constants = _plus;
    }
    boolean _isEmpty = IterableExtensions.isEmpty(constants);
    boolean _not = (!_isEmpty);
    if (_not) {
      Iterable<IEObjectDescription> _scopedElementsFor = Scopes.<EObject>scopedElementsFor(constants, QualifiedName.<EObject>wrapper(SimpleAttributeResolver.NAME_RESOLVER));
      SimpleScope _simpleScope = new SimpleScope(result, _scopedElementsFor, 
        true);
      result = _simpleScope;
    }
    return result;
  }

  public static EList<GlobalConstants> getImportedGlobals(final EObject context) {
    EList<GlobalConstants> _xblockexpression = null;
    {
      final RequirementSet sr = ReqSpecUtilExtension.containingRequirementSet(context);
      final StakeholderGoals sg = ReqSpecUtilExtension.containingStakeholderGoals(context);
      EList<GlobalConstants> _elvis = null;
      EList<GlobalConstants> _importConstants = null;
      if (sr!=null) {
        _importConstants=sr.getImportConstants();
      }
      if (_importConstants != null) {
        _elvis = _importConstants;
      } else {
        EList<GlobalConstants> _importConstants_1 = null;
        if (sg!=null) {
          _importConstants_1=sg.getImportConstants();
        }
        _elvis = _importConstants_1;
      }
      final EList<GlobalConstants> res = _elvis;
      _xblockexpression = res;
    }
    return _xblockexpression;
  }

  public static IScope scopeForValCompute(final Requirement req, final IScope parentscope) {
    IScope result = ReqSpecUtilExtension.scopeForValGoal(req, parentscope);
    return ReqSpecUtilExtension.scopeForValComputeReq(req, result);
  }

  public static IScope scopeForValComputeReq(final Requirement req, final IScope parentscope) {
    IScope result = parentscope;
    EList<Requirement> _decomposesReference = req.getDecomposesReference();
    for (final Requirement r : _decomposesReference) {
      result = ReqSpecUtilExtension.scopeForValComputeReq(r, result);
    }
    EList<Requirement> _refinesReference = req.getRefinesReference();
    for (final Requirement r_1 : _refinesReference) {
      result = ReqSpecUtilExtension.scopeForValComputeReq(r_1, result);
    }
    Requirement _inheritsReference = req.getInheritsReference();
    boolean _tripleNotEquals = (_inheritsReference != null);
    if (_tripleNotEquals) {
      result = ReqSpecUtilExtension.scopeForValComputeReq(req.getInheritsReference(), result);
    }
    final RequirementSet sr = ReqSpecUtilExtension.containingRequirementSet(req);
    if ((sr != null)) {
      EList<ValDeclaration> _constants = sr.getConstants();
      EList<AVariableDeclaration> _computes = sr.getComputes();
      Iterable<AVariableDeclaration> _plus = Iterables.<AVariableDeclaration>concat(_constants, _computes);
      Iterable<IEObjectDescription> _scopedElementsFor = Scopes.<EObject>scopedElementsFor(_plus, 
        QualifiedName.<EObject>wrapper(SimpleAttributeResolver.NAME_RESOLVER));
      SimpleScope _simpleScope = new SimpleScope(result, _scopedElementsFor, false);
      result = _simpleScope;
    }
    EList<AVariableDeclaration> _computes_1 = req.getComputes();
    EList<ValDeclaration> _constants_1 = req.getConstants();
    Iterable<AVariableDeclaration> _plus_1 = Iterables.<AVariableDeclaration>concat(_computes_1, _constants_1);
    Iterable<IEObjectDescription> _scopedElementsFor_1 = Scopes.<EObject>scopedElementsFor(_plus_1, 
      QualifiedName.<EObject>wrapper(SimpleAttributeResolver.NAME_RESOLVER));
    SimpleScope _simpleScope_1 = new SimpleScope(result, _scopedElementsFor_1, false);
    result = _simpleScope_1;
    return result;
  }

  /**
   * collect val definitions from goals up the req refinement hierarchy
   */
  public static IScope scopeForValGoal(final Requirement req, final IScope parentscope) {
    IScope _xblockexpression = null;
    {
      IScope result = parentscope;
      EList<Requirement> _refinesReference = req.getRefinesReference();
      for (final Requirement r : _refinesReference) {
        result = ReqSpecUtilExtension.scopeForValGoal(r, result);
      }
      EList<Goal> _goalReference = req.getGoalReference();
      for (final Goal g : _goalReference) {
        result = ReqSpecUtilExtension.scopeForVal(g, result);
      }
      _xblockexpression = result;
    }
    return _xblockexpression;
  }

  /**
   * collect val for goal incl. refinement & stakeholder goals container
   */
  public static IScope scopeForVal(final Goal goal, final IScope parentscope) {
    IScope result = parentscope;
    EList<Goal> _refinesReference = goal.getRefinesReference();
    for (final Goal r : _refinesReference) {
      result = ReqSpecUtilExtension.scopeForVal(r, result);
    }
    final StakeholderGoals sr = ReqSpecUtilExtension.containingStakeholderGoals(goal);
    if ((sr != null)) {
      Iterable<IEObjectDescription> _scopedElementsFor = Scopes.<EObject>scopedElementsFor(sr.getConstants(), 
        QualifiedName.<EObject>wrapper(SimpleAttributeResolver.NAME_RESOLVER));
      SimpleScope _simpleScope = new SimpleScope(result, _scopedElementsFor, false);
      result = _simpleScope;
    }
    Iterable<IEObjectDescription> _scopedElementsFor_1 = Scopes.<EObject>scopedElementsFor(goal.getConstants(), 
      QualifiedName.<EObject>wrapper(SimpleAttributeResolver.NAME_RESOLVER));
    SimpleScope _simpleScope_1 = new SimpleScope(result, _scopedElementsFor_1, false);
    result = _simpleScope_1;
    return result;
  }

  /**
   * collect Val definitions in req incl. refinements & goals associated with req.
   */
  public static IScope scopeForVal(final Requirement req, final IScope parentscope) {
    IScope result = ReqSpecUtilExtension.scopeForValGoal(req, parentscope);
    return ReqSpecUtilExtension.scopeForValReq(req, result);
  }

  /**
   * collect val definitions in req including refinement
   */
  public static IScope scopeForValReq(final Requirement req, final IScope parentscope) {
    IScope result = parentscope;
    EList<Requirement> _decomposesReference = req.getDecomposesReference();
    for (final Requirement r : _decomposesReference) {
      result = ReqSpecUtilExtension.scopeForValReq(r, result);
    }
    EList<Requirement> _refinesReference = req.getRefinesReference();
    for (final Requirement r_1 : _refinesReference) {
      result = ReqSpecUtilExtension.scopeForValReq(r_1, result);
    }
    Requirement _inheritsReference = req.getInheritsReference();
    boolean _tripleNotEquals = (_inheritsReference != null);
    if (_tripleNotEquals) {
      result = ReqSpecUtilExtension.scopeForValReq(req.getInheritsReference(), result);
    }
    final RequirementSet sr = ReqSpecUtilExtension.containingRequirementSet(req);
    if ((sr != null)) {
      Iterable<IEObjectDescription> _scopedElementsFor = Scopes.<EObject>scopedElementsFor(sr.getConstants(), 
        QualifiedName.<EObject>wrapper(SimpleAttributeResolver.NAME_RESOLVER));
      SimpleScope _simpleScope = new SimpleScope(result, _scopedElementsFor, false);
      result = _simpleScope;
    }
    Iterable<IEObjectDescription> _scopedElementsFor_1 = Scopes.<EObject>scopedElementsFor(req.getConstants(), 
      QualifiedName.<EObject>wrapper(SimpleAttributeResolver.NAME_RESOLVER));
    SimpleScope _simpleScope_1 = new SimpleScope(result, _scopedElementsFor_1, false);
    result = _simpleScope_1;
    return result;
  }

  /**
   * collect compute variable in req including refinement
   */
  public static IScope scopeForCompute(final Requirement req, final IScope parentscope) {
    return ReqSpecUtilExtension.scopeForComputeReq(req, parentscope);
  }

  public static IScope scopeForComputeReq(final Requirement req, final IScope parentscope) {
    IScope result = parentscope;
    EList<Requirement> _decomposesReference = req.getDecomposesReference();
    for (final Requirement r : _decomposesReference) {
      result = ReqSpecUtilExtension.scopeForComputeReq(r, result);
    }
    EList<Requirement> _refinesReference = req.getRefinesReference();
    for (final Requirement r_1 : _refinesReference) {
      result = ReqSpecUtilExtension.scopeForComputeReq(r_1, result);
    }
    Requirement _inheritsReference = req.getInheritsReference();
    boolean _tripleNotEquals = (_inheritsReference != null);
    if (_tripleNotEquals) {
      result = ReqSpecUtilExtension.scopeForComputeReq(req.getInheritsReference(), result);
    }
    final RequirementSet sr = ReqSpecUtilExtension.containingRequirementSet(req);
    if ((sr != null)) {
      Iterable<IEObjectDescription> _scopedElementsFor = Scopes.<EObject>scopedElementsFor(sr.getComputes(), 
        QualifiedName.<EObject>wrapper(SimpleAttributeResolver.NAME_RESOLVER));
      SimpleScope _simpleScope = new SimpleScope(result, _scopedElementsFor, false);
      result = _simpleScope;
    }
    Iterable<IEObjectDescription> _scopedElementsFor_1 = Scopes.<EObject>scopedElementsFor(req.getComputes(), 
      QualifiedName.<EObject>wrapper(SimpleAttributeResolver.NAME_RESOLVER));
    SimpleScope _simpleScope_1 = new SimpleScope(result, _scopedElementsFor_1, false);
    result = _simpleScope_1;
    return result;
  }

  public static String constructDescription(final ContractualElement r) {
    String _xblockexpression = null;
    {
      Description _description = r.getDescription();
      boolean _tripleNotEquals = (_description != null);
      if (_tripleNotEquals) {
        return CommonUtilExtension.toText(r.getDescription(), ReqSpecUtilExtension.getContractualElementSubject(r));
      }
      String _title = r.getTitle();
      boolean _tripleNotEquals_1 = (_title != null);
      if (_tripleNotEquals_1) {
        return r.getTitle();
      }
      _xblockexpression = "";
    }
    return _xblockexpression;
  }

  public static NamedElement getContractualElementSubject(final ContractualElement req) {
    NamedElement _elvis = null;
    NamedElement _targetElement = null;
    if (req!=null) {
      _targetElement=req.getTargetElement();
    }
    if (_targetElement != null) {
      _elvis = _targetElement;
    } else {
      ComponentClassifier _targetClassifier = ReqSpecUtilExtension.targetClassifier(req);
      _elvis = _targetClassifier;
    }
    return _elvis;
  }

  public static boolean matchingCategory(final Iterable<ComponentCategory> catlist, final ComponentCategory cat) {
    boolean _xblockexpression = false;
    {
      boolean _isEmpty = IterableExtensions.isEmpty(catlist);
      if (_isEmpty) {
        return true;
      }
      if ((cat == null)) {
        return true;
      }
      final Function1<ComponentCategory, Boolean> _function = (ComponentCategory c) -> {
        return Boolean.valueOf(c.getName().equalsIgnoreCase(cat.getName()));
      };
      _xblockexpression = IterableExtensions.<ComponentCategory>exists(catlist, _function);
    }
    return _xblockexpression;
  }
}