PropertiesProposalProvider.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.contentassist;

import com.google.common.base.Objects;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.inject.Inject;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.xtext.AbstractElement;
import org.eclipse.xtext.Assignment;
import org.eclipse.xtext.CrossReference;
import org.eclipse.xtext.EcoreUtil2;
import org.eclipse.xtext.Group;
import org.eclipse.xtext.Keyword;
import org.eclipse.xtext.RuleCall;
import org.eclipse.xtext.resource.IEObjectDescription;
import org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext;
import org.eclipse.xtext.ui.editor.contentassist.ICompletionProposalAcceptor;
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.ListExtensions;
import org.osate.aadl2.BasicProperty;
import org.osate.aadl2.BasicPropertyAssociation;
import org.osate.aadl2.ClassifierType;
import org.osate.aadl2.ClassifierValue;
import org.osate.aadl2.EnumerationLiteral;
import org.osate.aadl2.ListType;
import org.osate.aadl2.ListValue;
import org.osate.aadl2.MetaclassReference;
import org.osate.aadl2.ModalPropertyValue;
import org.osate.aadl2.Mode;
import org.osate.aadl2.Property;
import org.osate.aadl2.PropertyAssociation;
import org.osate.aadl2.PropertyConstant;
import org.osate.aadl2.PropertyType;
import org.osate.aadl2.RecordValue;
import org.osate.aadl2.modelsupport.util.AadlUtil;
import org.osate.aadl2.util.Aadl2Util;
import org.osate.xtext.aadl2.properties.services.PropertiesGrammarAccess;

/**
 * see http://www.eclipse.org/Xtext/documentation/latest/xtext.html#contentAssist on how to customize content assistant
 */
@SuppressWarnings("all")
public class PropertiesProposalProvider extends AbstractPropertiesProposalProvider {
  @Inject
  @Extension
  private PropertiesGrammarAccess _propertiesGrammarAccess;

  @Override
  public void completeOptionalModalPropertyValue_InMode(final EObject model, final Assignment assignment, final ContentAssistContext context, final ICompletionProposalAcceptor acceptor) {
    AbstractElement _terminal = assignment.getTerminal();
    final Predicate<IEObjectDescription> _function = (IEObjectDescription it) -> {
      boolean _xblockexpression = false;
      {
        final EObject proposedObj = EcoreUtil.resolve(it.getEObjectOrProxy(), model);
        EObject _eContainer = model.eContainer();
        final PropertyAssociation propertyAssoc = ((PropertyAssociation) _eContainer);
        final Function1<ModalPropertyValue, EList<Mode>> _function_1 = (ModalPropertyValue it_1) -> {
          return it_1.getInModes();
        };
        final Function1<Mode, Boolean> _function_2 = (Mode it_1) -> {
          return Boolean.valueOf(Objects.equal(it_1, proposedObj));
        };
        boolean _exists = IterableExtensions.<Mode>exists(Iterables.<Mode>concat(ListExtensions.<ModalPropertyValue, EList<Mode>>map(propertyAssoc.getOwnedValues(), _function_1)), _function_2);
        _xblockexpression = (!_exists);
      }
      return _xblockexpression;
    };
    this.lookupCrossReference(((CrossReference) _terminal), context, acceptor, _function);
  }

  @Override
  public void completeLiteralorReferenceTerm_NamedValue(final EObject model, final Assignment assignment, final ContentAssistContext context, final ICompletionProposalAcceptor acceptor) {
    AbstractElement _terminal = assignment.getTerminal();
    final Predicate<IEObjectDescription> _function = (IEObjectDescription it) -> {
      return this.showCrossReference(it, model);
    };
    this.lookupCrossReference(((CrossReference) _terminal), context, acceptor, _function);
  }

  @Override
  public void completeConstantValue_NamedValue(final EObject model, final Assignment assignment, final ContentAssistContext context, final ICompletionProposalAcceptor acceptor) {
    AbstractElement _terminal = assignment.getTerminal();
    final Predicate<IEObjectDescription> _function = (IEObjectDescription it) -> {
      return this.showCrossReference(it, model);
    };
    this.lookupCrossReference(((CrossReference) _terminal), context, acceptor, _function);
  }

  @Override
  public void completeComponentClassifierTerm_Classifier(final EObject model, final Assignment assignment, final ContentAssistContext context, final ICompletionProposalAcceptor acceptor) {
    final PropertyType propertyType = AadlUtil.getBasePropertyType(this.getPropertyType(((ClassifierValue) model)));
    AbstractElement _terminal = assignment.getTerminal();
    final Predicate<IEObjectDescription> _function = (IEObjectDescription it) -> {
      boolean _xblockexpression = false;
      {
        final EObject proposedObj = EcoreUtil.resolve(it.getEObjectOrProxy(), model);
        boolean _switchResult = false;
        boolean _matched = false;
        if (propertyType instanceof ClassifierType) {
          boolean _isNullOrEmpty = IterableExtensions.isNullOrEmpty(((ClassifierType)propertyType).getClassifierReferences());
          if (_isNullOrEmpty) {
            _matched=true;
            _switchResult = true;
          }
        }
        if (!_matched) {
          if (propertyType instanceof ClassifierType) {
            _matched=true;
            final Function1<MetaclassReference, Boolean> _function_1 = (MetaclassReference it_1) -> {
              return Boolean.valueOf(it_1.getMetaclass().isSuperTypeOf(proposedObj.eClass()));
            };
            _switchResult = IterableExtensions.<MetaclassReference>exists(((ClassifierType)propertyType).getClassifierReferences(), _function_1);
          }
        }
        if (!_matched) {
          _switchResult = false;
        }
        _xblockexpression = _switchResult;
      }
      return _xblockexpression;
    };
    this.lookupCrossReference(((CrossReference) _terminal), context, acceptor, _function);
  }

  @Override
  public void completeFieldPropertyAssociation_Property(final EObject model, final Assignment assignment, final ContentAssistContext context, final ICompletionProposalAcceptor acceptor) {
    AbstractElement _terminal = assignment.getTerminal();
    final Predicate<IEObjectDescription> _function = (IEObjectDescription it) -> {
      boolean _xblockexpression = false;
      {
        final EObject proposedObj = EcoreUtil.resolve(it.getEObjectOrProxy(), model);
        boolean _switchResult = false;
        boolean _matched = false;
        if (model instanceof RecordValue) {
          _matched=true;
          final Function1<BasicPropertyAssociation, Boolean> _function_1 = (BasicPropertyAssociation it_1) -> {
            BasicProperty _property = it_1.getProperty();
            return Boolean.valueOf(Objects.equal(_property, proposedObj));
          };
          boolean _exists = IterableExtensions.<BasicPropertyAssociation>exists(((RecordValue)model).getOwnedFieldValues(), _function_1);
          _switchResult = (!_exists);
        }
        if (!_matched) {
          _switchResult = true;
        }
        _xblockexpression = _switchResult;
      }
      return _xblockexpression;
    };
    this.lookupCrossReference(((CrossReference) _terminal), context, acceptor, _function);
  }

  @Override
  public void complete_AppliesToKeywords(final EObject model, final RuleCall ruleCall, final ContentAssistContext context, final ICompletionProposalAcceptor acceptor) {
    this.createKeywordProposal(this._propertiesGrammarAccess.getAppliesToKeywordsAccess().getGroup(), context, acceptor);
  }

  @Override
  public void complete_InBindingKeywords(final EObject model, final RuleCall ruleCall, final ContentAssistContext context, final ICompletionProposalAcceptor acceptor) {
    this.createKeywordProposal(this._propertiesGrammarAccess.getInBindingKeywordsAccess().getGroup(), context, acceptor);
  }

  @Override
  public void complete_InModesKeywords(final EObject model, final RuleCall ruleCall, final ContentAssistContext context, final ICompletionProposalAcceptor acceptor) {
    this.createKeywordProposal(this._propertiesGrammarAccess.getInModesKeywordsAccess().getGroup(), context, acceptor);
  }

  protected void createKeywordProposal(final Group group, final ContentAssistContext context, final ICompletionProposalAcceptor acceptor) {
    if ((group != null)) {
      final Function1<Keyword, String> _function = (Keyword it) -> {
        return it.getValue();
      };
      final String proposalString = IterableExtensions.join(IterableExtensions.<Keyword, String>map(Iterables.<Keyword>filter(group.getElements(), Keyword.class), _function), " ");
      acceptor.accept(this.createCompletionProposal(proposalString, proposalString, this.getImage(group), context));
    }
  }

  private boolean showCrossReference(final IEObjectDescription objDesc, final EObject model) {
    boolean _xblockexpression = false;
    {
      PropertyType _switchResult = null;
      boolean _matched = false;
      if (model instanceof PropertyAssociation) {
        _matched=true;
        _switchResult = ((PropertyAssociation)model).getProperty().getPropertyType();
      }
      if (!_matched) {
        if (model instanceof BasicPropertyAssociation) {
          _matched=true;
          _switchResult = ((BasicPropertyAssociation)model).getProperty().getPropertyType();
        }
      }
      if (!_matched) {
        if (model instanceof Property) {
          _matched=true;
          _switchResult = ((Property)model).getPropertyType();
        }
      }
      if (!_matched) {
        if (model instanceof PropertyConstant) {
          _matched=true;
          _switchResult = ((PropertyConstant)model).getPropertyType();
        }
      }
      if (!_matched) {
        if (model instanceof ListValue) {
          _matched=true;
          _switchResult = this.getListElementType(((ListValue)model));
        }
      }
      final PropertyType expectedPropertyType = _switchResult;
      boolean _switchResult_1 = false;
      EObject _resolve = EcoreUtil.resolve(objDesc.getEObjectOrProxy(), model);
      final EObject proposedObj = _resolve;
      boolean _matched_1 = false;
      if (proposedObj instanceof EnumerationLiteral) {
        _matched_1=true;
        _switchResult_1 = true;
      }
      if (!_matched_1) {
        if (proposedObj instanceof PropertyConstant) {
          _matched_1=true;
          boolean _and = false;
          boolean _arePropertyTypesEqual = Aadl2Util.arePropertyTypesEqual(expectedPropertyType, ((PropertyConstant)proposedObj).getPropertyType());
          if (!_arePropertyTypesEqual) {
            _and = false;
          } else {
            boolean _switchResult_2 = false;
            boolean _matched_2 = false;
            if (model instanceof PropertyConstant) {
              _matched_2=true;
              _switchResult_2 = (!Objects.equal(model, proposedObj));
            }
            if (!_matched_2) {
              _switchResult_2 = true;
            }
            _and = _switchResult_2;
          }
          _switchResult_1 = _and;
        }
      }
      if (!_matched_1) {
        if (proposedObj instanceof Property) {
          _matched_1=true;
          boolean _and = false;
          boolean _arePropertyTypesEqual = Aadl2Util.arePropertyTypesEqual(expectedPropertyType, ((Property)proposedObj).getPropertyType());
          if (!_arePropertyTypesEqual) {
            _and = false;
          } else {
            boolean _switchResult_2 = false;
            boolean _matched_2 = false;
            if (model instanceof PropertyAssociation) {
              _matched_2=true;
              Property _property = ((PropertyAssociation)model).getProperty();
              _switchResult_2 = (!Objects.equal(_property, proposedObj));
            }
            if (!_matched_2) {
              if (model instanceof Property) {
                _matched_2=true;
                _switchResult_2 = (!Objects.equal(model, proposedObj));
              }
            }
            if (!_matched_2) {
              _switchResult_2 = true;
            }
            _and = _switchResult_2;
          }
          _switchResult_1 = _and;
        }
      }
      if (!_matched_1) {
        _switchResult_1 = false;
      }
      _xblockexpression = _switchResult_1;
    }
    return _xblockexpression;
  }

  private PropertyType getListElementType(final ListValue listValue) {
    return this.getListElementType(listValue, 0);
  }

  private PropertyType getListElementType(final EObject model, final int listCount) {
    PropertyType _switchResult = null;
    boolean _matched = false;
    if (model instanceof ListValue) {
      _matched=true;
      _switchResult = this.getListElementType(((ListValue)model).eContainer(), (listCount + 1));
    }
    if (!_matched) {
      if (model instanceof PropertyAssociation) {
        _matched=true;
        PropertyType _propertyType = ((PropertyAssociation)model).getProperty().getPropertyType();
        _switchResult = this.getNestedElementType(((ListType) _propertyType), listCount);
      }
    }
    if (!_matched) {
      if (model instanceof BasicPropertyAssociation) {
        _matched=true;
        PropertyType _propertyType = ((BasicPropertyAssociation)model).getProperty().getPropertyType();
        _switchResult = this.getNestedElementType(((ListType) _propertyType), listCount);
      }
    }
    if (!_matched) {
      if (model instanceof Property) {
        _matched=true;
        PropertyType _propertyType = ((Property)model).getPropertyType();
        _switchResult = this.getNestedElementType(((ListType) _propertyType), listCount);
      }
    }
    if (!_matched) {
      if (model instanceof PropertyConstant) {
        _matched=true;
        PropertyType _propertyType = ((PropertyConstant)model).getPropertyType();
        _switchResult = this.getNestedElementType(((ListType) _propertyType), listCount);
      }
    }
    if (!_matched) {
      _switchResult = this.getListElementType(model.eContainer(), listCount);
    }
    return _switchResult;
  }

  private PropertyType getNestedElementType(final ListType listType, final int n) {
    PropertyType _xifexpression = null;
    if ((n == 1)) {
      _xifexpression = listType.getElementType();
    } else {
      PropertyType _elementType = listType.getElementType();
      _xifexpression = this.getNestedElementType(((ListType) _elementType), (n - 1));
    }
    return _xifexpression;
  }

  private PropertyType getPropertyType(final ClassifierValue model) {
    PropertyType _elvis = null;
    PropertyType _elvis_1 = null;
    PropertyType _elvis_2 = null;
    BasicPropertyAssociation _containerOfType = EcoreUtil2.<BasicPropertyAssociation>getContainerOfType(model, BasicPropertyAssociation.class);
    BasicProperty _property = null;
    if (_containerOfType!=null) {
      _property=_containerOfType.getProperty();
    }
    PropertyType _propertyType = null;
    if (_property!=null) {
      _propertyType=_property.getPropertyType();
    }
    if (_propertyType != null) {
      _elvis_2 = _propertyType;
    } else {
      PropertyAssociation _containerOfType_1 = EcoreUtil2.<PropertyAssociation>getContainerOfType(model, PropertyAssociation.class);
      Property _property_1 = null;
      if (_containerOfType_1!=null) {
        _property_1=_containerOfType_1.getProperty();
      }
      PropertyType _propertyType_1 = null;
      if (_property_1!=null) {
        _propertyType_1=_property_1.getPropertyType();
      }
      _elvis_2 = _propertyType_1;
    }
    if (_elvis_2 != null) {
      _elvis_1 = _elvis_2;
    } else {
      Property _containerOfType_2 = EcoreUtil2.<Property>getContainerOfType(model, Property.class);
      PropertyType _propertyType_2 = null;
      if (_containerOfType_2!=null) {
        _propertyType_2=_containerOfType_2.getPropertyType();
      }
      _elvis_1 = _propertyType_2;
    }
    if (_elvis_1 != null) {
      _elvis = _elvis_1;
    } else {
      PropertyType _propertyType_3 = EcoreUtil2.<PropertyConstant>getContainerOfType(model, PropertyConstant.class).getPropertyType();
      _elvis = _propertyType_3;
    }
    return _elvis;
  }
}