AlisaValidator.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.alisa.workbench.validation;

import com.google.inject.Inject;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.xtext.validation.Check;
import org.eclipse.xtext.validation.CheckType;
import org.eclipse.xtext.xbase.lib.Conversions;
import org.eclipse.xtext.xbase.lib.Extension;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure2;
import org.osate.aadl2.ComponentImplementation;
import org.osate.aadl2.ComponentType;
import org.osate.alisa.common.util.CommonUtilExtension;
import org.osate.alisa.workbench.alisa.AlisaPackage;
import org.osate.alisa.workbench.alisa.AssuranceCase;
import org.osate.alisa.workbench.alisa.AssurancePlan;
import org.osate.alisa.workbench.util.AlisaWorkbenchUtilExtension;
import org.osate.verify.internal.util.IVerifyGlobalReferenceFinder;
import org.osate.verify.internal.util.VerifyUtilExtension;
import org.osate.verify.verify.VerificationPlan;

/**
 * Custom validation rules.
 * 
 * see http://www.eclipse.org/Xtext/documentation.html#validation
 */
@SuppressWarnings("all")
public class AlisaValidator extends AbstractAlisaValidator {
  public static final String ASSURANCE_PLAN_OWN_MISSING_VERIFICATION_PLANS = "org.osate.alisa.workbench.validation.assurance.plan.own.missing.verification.plans";

  public static final String ASSURANCE_PLAN_OWN_INVALID_VERIFICATION_PLANS = "org.osate.alisa.workbench.validation.assurance.plan.own.invalid.verification.plans";

  public static final String ASSURANCE_PLAN_TARGET_INCORRECT = "org.osate.alisa.workbench.validation.assurance.plan.target.incorrect";

  @Inject
  @Extension
  private IVerifyGlobalReferenceFinder referenceFinder;

  @Check(CheckType.NORMAL)
  public void checkAssurancePlanNormal(final AssurancePlan assurancePlan) {
    this.checkAssurancePlanOwnOmissions(assurancePlan);
  }

  @Check(CheckType.NORMAL)
  public void checkAssurancePlanFast(final AssurancePlan assurancePlan) {
    this.checkConsistentAssurancePlanTarget(assurancePlan);
    this.checkModelPlanOwnForInvalid(assurancePlan);
  }

  public void checkAssurancePlanOwnOmissions(final AssurancePlan assurancePlan) {
    final Function1<VerificationPlan, Boolean> _function = (VerificationPlan avp) -> {
      boolean _contains = assurancePlan.getAssure().contains(avp);
      return Boolean.valueOf((!_contains));
    };
    final Iterable<VerificationPlan> res = IterableExtensions.<VerificationPlan>filter(this.referenceFinder.getVerificationPlans(assurancePlan.getTarget(), assurancePlan), _function);
    int _size = IterableExtensions.size(res);
    boolean _greaterThan = (_size > 0);
    if (_greaterThan) {
      int _length = ((Object[])Conversions.unwrapArray(res, Object.class)).length;
      int _multiply = (_length * 2);
      final String[] namesAndURI = new String[_multiply];
      final Procedure2<VerificationPlan, Integer> _function_1 = (VerificationPlan vp, Integer counter) -> {
        namesAndURI[((counter).intValue() * 2)] = vp.getName();
        namesAndURI[(((counter).intValue() * 2) + 1)] = EcoreUtil.getURI(vp).toString();
      };
      IterableExtensions.<VerificationPlan>forEach(res, _function_1);
      String _name = assurancePlan.getName();
      String _plus = ("Assurance Plan \'" + _name);
      String _plus_1 = (_plus + "\' missing Verification Plans in \'assure\' statement \'");
      this.warning(_plus_1, assurancePlan, AlisaPackage.Literals.ASSURANCE_PLAN__NAME, 
        AlisaValidator.ASSURANCE_PLAN_OWN_MISSING_VERIFICATION_PLANS, namesAndURI);
    }
  }

  public void checkModelPlanOwnForInvalid(final AssurancePlan assurancePlan) {
    final ComponentImplementation assTarget = assurancePlan.getTarget();
    EList<VerificationPlan> _assure = assurancePlan.getAssure();
    for (final VerificationPlan vp : _assure) {
      boolean _isSameorExtends = CommonUtilExtension.isSameorExtends(assTarget, VerifyUtilExtension.getTargetClassifier(vp));
      boolean _not = (!_isSameorExtends);
      if (_not) {
        final int idx = assurancePlan.getAssure().indexOf(vp);
        String _name = vp.getName();
        String _plus = ("Verification Plan \'" + _name);
        String _plus_1 = (_plus + "\' for \'");
        String _name_1 = VerifyUtilExtension.getTargetClassifier(vp).getName();
        String _plus_2 = (_plus_1 + _name_1);
        String _plus_3 = (_plus_2 + "\' is not valid for Assurance Plan \'");
        String _name_2 = assurancePlan.getName();
        String _plus_4 = (_plus_3 + _name_2);
        String _plus_5 = (_plus_4 + "\' with target \'");
        String _name_3 = assTarget.getName();
        String _plus_6 = (_plus_5 + _name_3);
        String _plus_7 = (_plus_6 + "\'");
        this.error(_plus_7, assurancePlan, 
          AlisaPackage.Literals.ASSURANCE_PLAN__ASSURE, idx, AlisaValidator.ASSURANCE_PLAN_OWN_INVALID_VERIFICATION_PLANS, 
          vp.getName(), EcoreUtil.getURI(vp).toString());
      }
    }
  }

  public void checkConsistentAssurancePlanTarget(final AssurancePlan assurancePlan) {
    final ComponentImplementation assTarget = assurancePlan.getTarget();
    final AssuranceCase assCase = AlisaWorkbenchUtilExtension.getAssuranceCase(assurancePlan);
    final ComponentType caseTarget = assCase.getSystem();
    boolean _isSameorExtends = CommonUtilExtension.isSameorExtends(assTarget, caseTarget);
    boolean _not = (!_isSameorExtends);
    if (_not) {
      String _name = assurancePlan.getName();
      String _plus = ("Assurance Plan \'" + _name);
      String _plus_1 = (_plus + "\' for \'");
      String _name_1 = assTarget.getName();
      String _plus_2 = (_plus_1 + _name_1);
      String _plus_3 = (_plus_2 + "\' is not valid for Assurance Case \'");
      String _name_2 = assCase.getName();
      String _plus_4 = (_plus_3 + _name_2);
      String _plus_5 = (_plus_4 + "\' with target \'");
      String _name_3 = caseTarget.getName();
      String _plus_6 = (_plus_5 + _name_3);
      String _plus_7 = (_plus_6 + "\'");
      this.error(_plus_7, assurancePlan, 
        AlisaPackage.Literals.ASSURANCE_PLAN__TARGET, AlisaValidator.ASSURANCE_PLAN_TARGET_INCORRECT, 
        assurancePlan.getName(), EcoreUtil.getURI(assurancePlan).toString());
    }
  }
}