PropertiesQuickfixProvider.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.properties.ui.quickfix;

import java.util.Iterator;
import java.util.List;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.xtext.ui.editor.model.edit.IModificationContext;
import org.eclipse.xtext.ui.editor.model.edit.ISemanticModification;
import org.eclipse.xtext.ui.editor.quickfix.DefaultQuickfixProvider;
import org.eclipse.xtext.ui.editor.quickfix.Fix;
import org.eclipse.xtext.ui.editor.quickfix.IssueResolutionAcceptor;
import org.eclipse.xtext.validation.Issue;
import org.eclipse.xtext.xbase.lib.Conversions;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.osate.aadl2.Aadl2Package;
import org.osate.aadl2.ArrayRange;
import org.osate.aadl2.IntegerLiteral;
import org.osate.aadl2.ModalPropertyValue;
import org.osate.aadl2.ModelUnit;
import org.osate.aadl2.Namespace;
import org.osate.aadl2.NumberValue;
import org.osate.aadl2.PackageSection;
import org.osate.aadl2.Property;
import org.osate.aadl2.PropertyAssociation;
import org.osate.aadl2.PropertyExpression;
import org.osate.aadl2.PropertySet;
import org.osate.aadl2.RangeValue;
import org.osate.aadl2.RealLiteral;
import org.osate.aadl2.UnitLiteral;
import org.osate.aadl2.modelsupport.scoping.Aadl2GlobalScopeUtil;
import org.osate.xtext.aadl2.properties.util.AadlProject;
import org.osate.xtext.aadl2.properties.util.CommunicationProperties;
import org.osate.xtext.aadl2.properties.util.GetProperties;
import org.osate.xtext.aadl2.properties.util.MemoryProperties;
import org.osate.xtext.aadl2.properties.validation.PropertiesValidator;

@SuppressWarnings("all")
public class PropertiesQuickfixProvider extends DefaultQuickfixProvider {
  /**
   * QuickFix for adding a required with statement for a referenced package or property set.
   * The issue data array is expected to have three elements:
   * 
   * issue.getData()[0]: The name of the package or property set
   * issue.getData()[1]: The URI String of the referenced AadlPackage or PropertySet.
   * issue.getData()[2]: The URI String of the Namespace where the with statement should be inserted.
   */
  @Fix(PropertiesValidator.MISSING_WITH)
  public void fixMissingWith(final Issue issue, final IssueResolutionAcceptor acceptor) {
    String _get = issue.getData()[0];
    String _plus = ("Add \'" + _get);
    String _plus_1 = (_plus + "\' to the with clauses");
    acceptor.accept(issue, _plus_1, null, null, 
      new ISemanticModification() {
        @Override
        public void apply(final EObject element, final IModificationContext context) throws Exception {
          final ResourceSet resourceSet = element.eResource().getResourceSet();
          EObject _eObject = resourceSet.getEObject(
            URI.createURI(issue.getData()[1]), true);
          final ModelUnit requiredModelUnit = ((ModelUnit) _eObject);
          EObject _eObject_1 = resourceSet.getEObject(URI.createURI(issue.getData()[2]), 
            true);
          final Namespace contextNS = ((Namespace) _eObject_1);
          EList<ModelUnit> imports = null;
          if ((contextNS instanceof PropertySet)) {
            imports = ((PropertySet)contextNS).getImportedUnits();
          } else {
            imports = ((PackageSection) contextNS).getImportedUnits();
          }
          imports.add(requiredModelUnit);
        }
      });
  }

  /**
   * QuickFix for swapping Upper and Lower bounds in a range value when the upper is less than the lower
   */
  @Fix(PropertiesValidator.UPPER_LESS_THAN_LOWER)
  public void fixUpperLessThanLower(final Issue issue, final IssueResolutionAcceptor acceptor) {
    acceptor.accept(issue, "Switch upper and lower bounds of the range", null, null, new ISemanticModification() {
      @Override
      public void apply(final EObject element, final IModificationContext context) throws Exception {
        PropertyExpression oldMin = ((RangeValue) element).getMinimum();
        PropertyExpression oldMax = ((RangeValue) element).getMaximum();
        ((RangeValue) element).setMinimum(oldMax);
        ((RangeValue) element).setMaximum(oldMin);
      }
    });
  }

  /**
   * QuickFix for swapping Upper and Lower bounds in an array index range value when the upper is less than the lower
   */
  @Fix(PropertiesValidator.ARRAY_RANGE_UPPER_LESS_THAN_LOWER)
  public void fixArrayRangeUpperLessThanLower(final Issue issue, final IssueResolutionAcceptor acceptor) {
    acceptor.accept(issue, "Switch upper and lower bounds of the range", null, null, new ISemanticModification() {
      @Override
      public void apply(final EObject element, final IModificationContext context) throws Exception {
        long oldMin = ((ArrayRange) element).getLowerBound();
        long oldMax = ((ArrayRange) element).getUpperBound();
        ((ArrayRange) element).setLowerBound(oldMax);
        ((ArrayRange) element).setUpperBound(oldMin);
      }
    });
  }

  /**
   * QuickFix for changing Upper bounds in an array index range value to maximum allowed by dimension of the type
   * 
   * issue.getData(0) = maximum dimension value;
   */
  @Fix(PropertiesValidator.ARRAY_RANGE_UPPER_GREATER_THAN_MAXIMUM)
  public void fixArrayRangeUpperGreaterThanMaximum(final Issue issue, final IssueResolutionAcceptor acceptor) {
    final Long maxAllowed = Long.valueOf(IterableExtensions.<String>head(((Iterable<String>)Conversions.doWrapArray(issue.getData()))));
    acceptor.accept(issue, 
      (("Change upper bound of the range to maximum defined by type\'s dimension (" + maxAllowed) + ")"), null, null, 
      new ISemanticModification() {
        @Override
        public void apply(final EObject element, final IModificationContext context) throws Exception {
          ((ArrayRange) element).setUpperBound((maxAllowed).longValue());
        }
      });
  }

  /**
   * QuickFix for changing index in an array to maximum allowed by dimension of the type
   * 
   * issue.getData(0) = maximum dimension value;
   */
  @Fix(PropertiesValidator.ARRAY_INDEX_GREATER_THAN_MAXIMUM)
  public void fixArrayIndexGreaterThanMaximum(final Issue issue, final IssueResolutionAcceptor acceptor) {
    final Long maxAllowed = Long.valueOf(IterableExtensions.<String>head(((Iterable<String>)Conversions.doWrapArray(issue.getData()))));
    acceptor.accept(issue, (("Change index of array to maximum defined by type\'s dimension (" + maxAllowed) + ")"), 
      null, null, new ISemanticModification() {
      @Override
      public void apply(final EObject element, final IModificationContext context) throws Exception {
        ((ArrayRange) element).setLowerBound((maxAllowed).longValue());
      }
    });
  }

  /**
   * QuickFix for changing Lower bound in an array index range from 1 to 0
   */
  @Fix(PropertiesValidator.ARRAY_LOWER_BOUND_IS_ZERO)
  public void fixArrayRangeLowerBoundIsZero(final Issue issue, final IssueResolutionAcceptor acceptor) {
    acceptor.accept(issue, "Change \'0\' to \'1\'", null, null, new ISemanticModification() {
      @Override
      public void apply(final EObject element, final IModificationContext context) throws Exception {
        ((ArrayRange) element).setLowerBound(1);
      }
    });
  }

  /**
   * QuickFix for making a negative delta positive
   */
  @Fix(PropertiesValidator.DELTA_NEGATIVE)
  public void fixNegativeDelta(final Issue issue, final IssueResolutionAcceptor acceptor) {
    acceptor.accept(issue, "Make delta value positive", null, null, new ISemanticModification() {
      @Override
      public void apply(final EObject element, final IModificationContext context) throws Exception {
        boolean _matched = false;
        if (element instanceof IntegerLiteral) {
          _matched=true;
          long _value = ((IntegerLiteral)element).getValue();
          long _minus = (-_value);
          ((IntegerLiteral)element).setValue(_minus);
        }
        if (!_matched) {
          if (element instanceof RealLiteral) {
            _matched=true;
            double _value = ((RealLiteral)element).getValue();
            double _minus = (-_value);
            ((RealLiteral)element).setValue(_minus);
          }
        }
      }
    });
  }

  /**
   * QuickFixes for adding unit
   * 
   * 	 * issue.getData() unitTypeNames
   */
  @Fix(PropertiesValidator.MISSING_NUMBERVALUE_UNITS)
  public void fixMissingUnits(final Issue issue, final IssueResolutionAcceptor acceptor) {
    final Iterator<String> iter = ((List<String>)Conversions.doWrapArray(issue.getData())).iterator();
    while (iter.hasNext()) {
      {
        final String utName = iter.next();
        final String nextUri = iter.next();
        acceptor.accept(issue, 
          (("Add units \'" + utName) + "\' to number"), 
          null, 
          null, 
          new ISemanticModification() {
            @Override
            public void apply(final EObject element, final IModificationContext context) throws Exception {
              final ResourceSet resourceSet = element.eResource().getResourceSet();
              EObject _eObject = resourceSet.getEObject(URI.createURI(nextUri), 
                true);
              final UnitLiteral unitLiteral = ((UnitLiteral) _eObject);
              ((NumberValue) element).setUnit(unitLiteral);
            }
          });
      }
    }
  }

  /**
   * QuickFix for changing deprecate Byte_Count to Memory_Size
   */
  @Fix(PropertiesValidator.BYTE_COUNT_DEPRECATED)
  public void fixDeprecatedByteCount(final Issue issue, final IssueResolutionAcceptor acceptor) {
    acceptor.accept(issue, 
      "Replace Byte_Count values with Memory_Size", 
      null, 
      null, 
      new ISemanticModification() {
        @Override
        public void apply(final EObject element, final IModificationContext context) throws Exception {
          final PropertyAssociation pa = ((PropertyAssociation) element);
          final EList<ModalPropertyValue> ownedValues = pa.getOwnedValues();
          pa.setProperty(Aadl2GlobalScopeUtil.<Property>get(pa, Aadl2Package.eINSTANCE.getProperty(), MemoryProperties.MEMORY_SIZE));
          for (final ModalPropertyValue mpv : ownedValues) {
            {
              final PropertyExpression ownedVal = mpv.getOwnedValue();
              boolean _matched = false;
              if (ownedVal instanceof NumberValue) {
                _matched=true;
                ((NumberValue)ownedVal).setUnit(GetProperties.findUnitLiteral(pa, AadlProject.SIZE_UNITS, 
                  ((NumberValue)ownedVal).getUnit().getName()));
              }
            }
          }
        }
      });
  }

  /**
   * QuickFix for changing deprecate Data_Volume to Data_Rate
   */
  @Fix(PropertiesValidator.DATA_VOLUME_DEPRECATED)
  public void fixDeprecatedDataVolume(final Issue issue, final IssueResolutionAcceptor acceptor) {
    acceptor.accept(issue, 
      "Replace Data_Volume values with Data_Rate", 
      null, 
      null, 
      new ISemanticModification() {
        @Override
        public void apply(final EObject element, final IModificationContext context) throws Exception {
          final PropertyAssociation pa = ((PropertyAssociation) element);
          final EList<ModalPropertyValue> ownedValues = pa.getOwnedValues();
          pa.setProperty(Aadl2GlobalScopeUtil.<Property>get(pa, Aadl2Package.eINSTANCE.getProperty(), CommunicationProperties.DATA_RATE));
          for (final ModalPropertyValue mpv : ownedValues) {
            {
              final PropertyExpression ownedVal = mpv.getOwnedValue();
              boolean _matched = false;
              if (ownedVal instanceof NumberValue) {
                _matched=true;
                ((NumberValue)ownedVal).setUnit(GetProperties.findUnitLiteral(pa, AadlProject.DATA_RATE_UNITS, 
                  ((NumberValue)ownedVal).getUnit().getName()));
              }
            }
          }
        }
      });
  }

  /**
   * QuickFix for changing deprecate SEI::Data_Rate to Data_Rate
   */
  @Fix(PropertiesValidator.SEI_DATA_RATE_DEPRECATED)
  public void fixDeprecatedSEIDataRate(final Issue issue, final IssueResolutionAcceptor acceptor) {
    acceptor.accept(issue, 
      "Replace SEI::Data_Rate with SEI::Message_Rate", 
      null, 
      null, 
      new ISemanticModification() {
        @Override
        public void apply(final EObject element, final IModificationContext context) throws Exception {
          final PropertyAssociation pa = ((PropertyAssociation) element);
          final EList<ModalPropertyValue> ownedValues = pa.getOwnedValues();
          pa.setProperty(Aadl2GlobalScopeUtil.<Property>get(pa, Aadl2Package.eINSTANCE.getProperty(), "SEI::Message_Rate"));
          for (final ModalPropertyValue mpv : ownedValues) {
            {
              final PropertyExpression ownedVal = mpv.getOwnedValue();
              boolean _matched = false;
              if (ownedVal instanceof NumberValue) {
                _matched=true;
                ((NumberValue)ownedVal).setUnit(GetProperties.findUnitLiteral(pa.getProperty(), ((NumberValue)ownedVal).getUnit().getName()));
              }
            }
          }
        }
      });
  }

  /**
   * QuickFix for changing deprecate Source_Code_Size to Code_Size
   */
  @Fix(PropertiesValidator.SOURCE_CODE_SIZE_DEPRECATED)
  public void fixDeprecatedSourceCodeSize(final Issue issue, final IssueResolutionAcceptor acceptor) {
    acceptor.accept(issue, 
      "Replace Source_Code_Size with Code_Size", 
      null, 
      null, 
      new ISemanticModification() {
        @Override
        public void apply(final EObject element, final IModificationContext context) throws Exception {
          final PropertyAssociation pa = ((PropertyAssociation) element);
          pa.setProperty(Aadl2GlobalScopeUtil.<Property>get(pa, Aadl2Package.eINSTANCE.getProperty(), MemoryProperties.CODE_SIZE));
        }
      });
  }

  /**
   * QuickFix for changing deprecate Source_Data_Size to Data_Size
   */
  @Fix(PropertiesValidator.SOURCE_DATA_SIZE_DEPRECATED)
  public void fixDeprecatedSourceDataSize(final Issue issue, final IssueResolutionAcceptor acceptor) {
    acceptor.accept(issue, 
      "Replace Source_Data_Size with Data_Size", 
      null, 
      null, 
      new ISemanticModification() {
        @Override
        public void apply(final EObject element, final IModificationContext context) throws Exception {
          final PropertyAssociation pa = ((PropertyAssociation) element);
          pa.setProperty(Aadl2GlobalScopeUtil.<Property>get(pa, Aadl2Package.eINSTANCE.getProperty(), MemoryProperties.DATA_SIZE));
        }
      });
  }

  /**
   * QuickFix for changing deprecate Source_Heap_Size to Heap_Size
   */
  @Fix(PropertiesValidator.SOURCE_HEAP_SIZE_DEPRECATED)
  public void fixDeprecatedSourceHeapSize(final Issue issue, final IssueResolutionAcceptor acceptor) {
    acceptor.accept(issue, 
      "Replace Source_Heap_Size with Heap_Size", 
      null, 
      null, 
      new ISemanticModification() {
        @Override
        public void apply(final EObject element, final IModificationContext context) throws Exception {
          final PropertyAssociation pa = ((PropertyAssociation) element);
          pa.setProperty(Aadl2GlobalScopeUtil.<Property>get(pa, Aadl2Package.eINSTANCE.getProperty(), MemoryProperties.HEAP_SIZE));
        }
      });
  }

  /**
   * QuickFix for changing deprecate Source_Stack_Size to Stack_Size
   */
  @Fix(PropertiesValidator.SOURCE_STACK_SIZE_DEPRECATED)
  public void fixDeprecatedSourceStackSize(final Issue issue, final IssueResolutionAcceptor acceptor) {
    acceptor.accept(issue, 
      "Replace Source_Stack_Size with Stack_Size", 
      null, 
      null, 
      new ISemanticModification() {
        @Override
        public void apply(final EObject element, final IModificationContext context) throws Exception {
          final PropertyAssociation pa = ((PropertyAssociation) element);
          pa.setProperty(Aadl2GlobalScopeUtil.<Property>get(pa, Aadl2Package.eINSTANCE.getProperty(), MemoryProperties.STACK_SIZE));
        }
      });
  }
}