All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.reprezen.genflow.rapidml.xsd.ReferenceLinkHelper Maven / Gradle / Ivy

/**
 * Copyright © 2013, 2016 Modelsolv, Inc.
 * All Rights Reserved.
 * 
 * NOTICE: All information contained herein is, and remains the property
 * of ModelSolv, Inc. See the file license.html in the root directory of
 * this project for further information.
 */
package com.reprezen.genflow.rapidml.xsd;

import com.google.common.base.Objects;
import com.reprezen.genflow.rapidml.xsd.Helpers;
import com.reprezen.genflow.rapidml.xsd.XMLSchemaHelper;
import com.reprezen.rapidml.Extensible;
import com.reprezen.rapidml.LinkRelation;
import com.reprezen.rapidml.NamedLinkDescriptor;
import com.reprezen.rapidml.ObjectRealization;
import com.reprezen.rapidml.ReferenceElement;
import com.reprezen.rapidml.ReferenceEmbed;
import com.reprezen.rapidml.ReferenceLink;
import com.reprezen.rapidml.ReferenceProperty;
import com.reprezen.rapidml.ReferenceTreatment;
import com.reprezen.rapidml.ResourceDefinition;
import com.reprezen.rapidml.ServiceDataResource;
import com.reprezen.rapidml.TypedMessage;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
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;

@SuppressWarnings("all")
public class ReferenceLinkHelper {
  @Extension
  private XMLSchemaHelper xmlSchemaHelper;
  
  public ReferenceLinkHelper(final Helpers helpers) {
    XMLSchemaHelper _xmlSchemaHelper = helpers.getXmlSchemaHelper();
    this.xmlSchemaHelper = _xmlSchemaHelper;
  }
  
  public Iterable getContainmentReferencesAtPosition(final Iterable referenceTreatments, final Integer index) {
    final Function1 _function = (ReferenceTreatment it) -> {
      Iterable _containmentReferences = this.getContainmentReferences(it);
      int _size = IterableExtensions.size(_containmentReferences);
      return Boolean.valueOf((_size >= (index).intValue()));
    };
    Iterable _filter = IterableExtensions.filter(referenceTreatments, _function);
    final Function1 _function_1 = (ReferenceTreatment it) -> {
      Iterable _containmentReferences = this.getContainmentReferences(it);
      List _list = IterableExtensions.toList(_containmentReferences);
      return _list.get((index).intValue());
    };
    Iterable _map = IterableExtensions.map(_filter, _function_1);
    return IterableExtensions.toSet(_map);
  }
  
  protected boolean _isPropertyOverridenByReferenceLink(final ReferenceProperty referenceProperty, final ServiceDataResource dataResource, final Iterable pathToCurrentSegment) {
    EList _referenceLinks = dataResource.getReferenceLinks();
    final Function1 _function = (ReferenceLink it) -> {
      return ((ReferenceTreatment) it);
    };
    List _map = ListExtensions.map(_referenceLinks, _function);
    int _size = IterableExtensions.size(pathToCurrentSegment);
    int _plus = (_size + 1);
    Iterable _containmentReferencesAtPosition = this.getContainmentReferencesAtPosition(_map, Integer.valueOf(_plus));
    return this.isPropertyOverridenByReferenceLink(referenceProperty, _containmentReferencesAtPosition);
  }
  
  protected boolean _isPropertyOverridenByReferenceLink(final ReferenceProperty referenceProperty, final TypedMessage message, final Iterable pathToCurrentSegment) {
    EList _referenceLinks = message.getReferenceLinks();
    final Function1 _function = (ReferenceLink it) -> {
      return ((ReferenceTreatment) it);
    };
    List _map = ListExtensions.map(_referenceLinks, _function);
    int _size = IterableExtensions.size(pathToCurrentSegment);
    int _plus = (_size + 1);
    Iterable _containmentReferencesAtPosition = this.getContainmentReferencesAtPosition(_map, Integer.valueOf(_plus));
    return this.isPropertyOverridenByReferenceLink(referenceProperty, _containmentReferencesAtPosition);
  }
  
  private boolean isPropertyOverridenByReferenceLink(final ReferenceProperty referenceProperty, final Iterable featuresOverridenByReferenceLinks) {
    final Function1 _function = (ReferenceProperty it) -> {
      return Boolean.valueOf(Objects.equal(it, referenceProperty));
    };
    return IterableExtensions.exists(featuresOverridenByReferenceLinks, _function);
  }
  
  protected ObjectRealization _getLinkDescriptor(final ReferenceLink referenceLink) {
    ObjectRealization _xifexpression = null;
    ObjectRealization _linkDescriptor = referenceLink.getLinkDescriptor();
    boolean _tripleNotEquals = (_linkDescriptor != null);
    if (_tripleNotEquals) {
      _xifexpression = referenceLink.getLinkDescriptor();
    } else {
      ResourceDefinition _targetResource = referenceLink.getTargetResource();
      _xifexpression = this.getLinkDescriptor(_targetResource);
    }
    return _xifexpression;
  }
  
  protected NamedLinkDescriptor _getLinkDescriptor(final ResourceDefinition dataResource) {
    NamedLinkDescriptor _xifexpression = null;
    boolean _and = false;
    if (!(dataResource != null)) {
      _and = false;
    } else {
      _and = (dataResource instanceof ServiceDataResource);
    }
    if (_and) {
      _xifexpression = ((ServiceDataResource) dataResource).getDefaultLinkDescriptor();
    } else {
      _xifexpression = null;
    }
    return _xifexpression;
  }
  
  public String getReferenceTreatmentName(final ReferenceTreatment referenceLink) {
    Iterable _containmentReferences = this.getContainmentReferences(referenceLink);
    final Function1 _function = (ReferenceProperty it) -> {
      return it.getName();
    };
    Iterable _map = IterableExtensions.map(_containmentReferences, _function);
    String _join = IterableExtensions.join(_map, ".");
    String _plus = (_join + ".");
    ReferenceElement _referenceElement = referenceLink.getReferenceElement();
    String _name = _referenceElement.getName();
    return (_plus + _name);
  }
  
  public Iterable getContainmentReferences(final ReferenceTreatment referenceLink) {
    return new LinkedList();
  }
  
  public ReferenceElement getReferenceProperty(final ReferenceLink referenceLink) {
    return referenceLink.getReferenceElement();
  }
  
  public String getRelValue(final ReferenceLink referenceLink) {
    String _xifexpression = null;
    LinkRelation _linkRelation = referenceLink.getLinkRelation();
    boolean _tripleEquals = (_linkRelation == null);
    if (_tripleEquals) {
      _xifexpression = null;
    } else {
      LinkRelation _linkRelation_1 = referenceLink.getLinkRelation();
      _xifexpression = _linkRelation_1.getName();
    }
    return _xifexpression;
  }
  
  public ServiceDataResource getContainingServiceDataResource(final ReferenceTreatment referenceLink) {
    ServiceDataResource _xblockexpression = null;
    {
      final ServiceDataResource eServiceDataResource = this.xmlSchemaHelper.getEContainer(referenceLink, ServiceDataResource.class);
      final ReferenceEmbed eReferenceEmbed = this.xmlSchemaHelper.getEContainer(referenceLink, ReferenceEmbed.class);
      ServiceDataResource _xifexpression = null;
      if ((eServiceDataResource != null)) {
        _xifexpression = eServiceDataResource;
      } else {
        _xifexpression = this.getContainingServiceDataResource(eReferenceEmbed);
      }
      _xblockexpression = _xifexpression;
    }
    return _xblockexpression;
  }
  
  public boolean startsWithPath(final ReferenceTreatment referenceLink, final Iterable containmentPath) {
    int _size = IterableExtensions.size(containmentPath);
    Iterable _firstContainmentFragments = this.getFirstContainmentFragments(referenceLink, Integer.valueOf(_size));
    return Objects.equal(containmentPath, _firstContainmentFragments);
  }
  
  private Iterable _getFirstContainmentFragments(final ReferenceTreatment referenceTreatment, final Integer number) {
    Iterable _containmentReferences = this.getContainmentReferences(referenceTreatment);
    return this.getFirstContainmentFragments(_containmentReferences, number);
  }
  
  private Iterable _getFirstContainmentFragments(final Iterable containmentPath, final Integer number) {
    Iterable _xifexpression = null;
    int _size = IterableExtensions.size(containmentPath);
    boolean _greaterEqualsThan = (_size >= (number).intValue());
    if (_greaterEqualsThan) {
      _xifexpression = IterableExtensions.take(containmentPath, (number).intValue());
    } else {
      _xifexpression = null;
    }
    return _xifexpression;
  }
  
  public int getContainmentDepth(final ReferenceLink referenceLink) {
    return 0;
  }
  
  public boolean isPropertyOverridenByReferenceLink(final ReferenceProperty referenceProperty, final EObject dataResource, final Iterable pathToCurrentSegment) {
    if (dataResource instanceof ServiceDataResource) {
      return _isPropertyOverridenByReferenceLink(referenceProperty, (ServiceDataResource)dataResource, pathToCurrentSegment);
    } else if (dataResource instanceof TypedMessage) {
      return _isPropertyOverridenByReferenceLink(referenceProperty, (TypedMessage)dataResource, pathToCurrentSegment);
    } else {
      throw new IllegalArgumentException("Unhandled parameter types: " +
        Arrays.asList(referenceProperty, dataResource, pathToCurrentSegment).toString());
    }
  }
  
  public ObjectRealization getLinkDescriptor(final Extensible referenceLink) {
    if (referenceLink instanceof ReferenceLink) {
      return _getLinkDescriptor((ReferenceLink)referenceLink);
    } else if (referenceLink instanceof ResourceDefinition) {
      return _getLinkDescriptor((ResourceDefinition)referenceLink);
    } else {
      throw new IllegalArgumentException("Unhandled parameter types: " +
        Arrays.asList(referenceLink).toString());
    }
  }
  
  private Iterable getFirstContainmentFragments(final Object referenceTreatment, final Integer number) {
    if (referenceTreatment instanceof ReferenceTreatment) {
      return _getFirstContainmentFragments((ReferenceTreatment)referenceTreatment, number);
    } else if (referenceTreatment instanceof Iterable) {
      return _getFirstContainmentFragments((Iterable)referenceTreatment, number);
    } else {
      throw new IllegalArgumentException("Unhandled parameter types: " +
        Arrays.asList(referenceTreatment, number).toString());
    }
  }
}