private EOperation findOperation(EObject object, String className, String methodName) {

    // We try to find the corresponding EOperation in the execution
    // metamodel
    for (EOperation operation : object.eClass().getEAllOperations()) {
      // TODO !!! this is not super correct yet as overloading allows the
      // definition of 2 methods with the same name !!!
      if (operation.getName().equalsIgnoreCase(methodName)) {
        return operation;
      }
    }

    // If we didn't find it, we try to find the class that should contain
    // this operation
    EClass containingEClass = null;
    if (getFQN(object.eClass(), "").equalsIgnoreCase(className)) containingEClass = object.eClass();
    else
      for (EClass candidate : object.eClass().getEAllSuperTypes()) {
        if (getFQN(candidate, "").equalsIgnoreCase(className)) containingEClass = candidate;
      }

    // Then we create the missing operation (VERY approximatively)
    EOperation operation = EcoreFactory.eINSTANCE.createEOperation();
    if (containingEClass != null) containingEClass.getEOperations().add(operation);
    operation.setName(methodName);
    return operation;
  }
  public ModelSpecificEvent findOrCreateMSE(EObject caller, String className, String methodName) {

    EOperation operation = findOperation(caller, className, methodName);

    // TODO Should be created somewhere before...
    // at some point didier had written some code to serialize it... I think
    if (_actionModel == null) {
      _actionModel =
          fr.inria.aoste.timesquare.ecl.feedback.feedback.FeedbackFactory.eINSTANCE
              .createActionModel();
    }

    if (_actionModel != null) {
      for (ModelSpecificEvent existingMSE : _actionModel.getEvents()) {
        if (existingMSE.getCaller().equals(caller)
            && ((existingMSE.getAction() != null && existingMSE.getAction().equals(operation))
                || (existingMSE.getAction() == null && operation == null))) {
          // no need to create one, we already have it
          return existingMSE;
        }
      }
    }
    // let's create a MSE
    final ModelSpecificEvent mse = FeedbackFactory.eINSTANCE.createModelSpecificEvent();
    mse.setCaller(caller);
    mse.setAction(operation);
    if (operation != null)
      mse.setName("MSE_" + caller.getClass().getSimpleName() + "_" + operation.getName());
    else mse.setName("MSE_" + caller.getClass().getSimpleName() + "_" + methodName);
    // and add it for possible reuse
    if (_actionModel != null) {

      if (_actionModel.eResource() != null) {
        TransactionUtil.getEditingDomain(_actionModel.eResource());
        RecordingCommand command =
            new RecordingCommand(
                TransactionUtil.getEditingDomain(_actionModel.eResource()), "Saving new MSE ") {
              @Override
              protected void doExecute() {
                _actionModel.getEvents().add(mse);
                try {
                  _actionModel.eResource().save(null);
                } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
                }
              }
            };
        TransactionUtil.getEditingDomain(_actionModel.eResource())
            .getCommandStack()
            .execute(command);
      }
    } else {
      _actionModel.getEvents().add(mse);
    }
    return mse;
  }
 @Override
 public Object caseEOperation(EOperation object) {
   Operation operation = new Operation(object.getName());
   operation.type = getType(object.getEType());
   operation.setBounds(object.getLowerBound(), object.getUpperBound());
   operation.setStatic(false);
   operation.setScope(Scope.PUBLIC);
   for (EParameter epar : object.getEParameters()) {
     TypedElement fpar = new TypedElement(epar.getName());
     fpar.type = getType(epar.getEType());
     operation.addFormalParameter(fpar);
   }
   return operation;
 }
  public static String getEObjectLabel(EObject eObject) {
    String name = null;
    String label = null;
    String id = null;

    for (EAttribute attribute : eObject.eClass().getEAllAttributes()) {
      if (attribute.getName().equalsIgnoreCase("name")) {
        name = (String) eObject.eGet(attribute);
        break;
      }
    }

    for (EOperation operation : eObject.eClass().getEAllOperations()) {
      try {
        if (operation.getName().equals("getName") && name == null)
          name = (String) eObject.eInvoke(operation, null);
        if (operation.getName().equals("getLabel") && label == null)
          label = (String) eObject.eInvoke(operation, null);
        if (operation.getName().equals("getID") && id == null)
          id = (String) eObject.eInvoke(operation, null);
        if (operation.getName().equals("getId") && id == null)
          id = (String) eObject.eInvoke(operation, null);
      } catch (Exception e) {
      }
    }

    if (name != null && !name.isEmpty()) return name;
    if (label != null && !label.isEmpty()) return label;
    if (id != null && !id.isEmpty()) return id;

    if (eObject instanceof ENamedElement)
      if (((ENamedElement) eObject).getName() != null
          && !((ENamedElement) eObject).getName().equals(""))
        return ((ENamedElement) eObject).getName();
      else return eObject.toString();
    else return eObject.toString();
  }
  private String matches(
      org.eclipse.emf.ecore.EObject element, String identifier, boolean matchFuzzy) {
    // first check for attributes that have set the ID flag to true
    java.util.List<org.eclipse.emf.ecore.EStructuralFeature> features =
        element.eClass().getEStructuralFeatures();
    for (org.eclipse.emf.ecore.EStructuralFeature feature : features) {
      if (feature instanceof org.eclipse.emf.ecore.EAttribute) {
        org.eclipse.emf.ecore.EAttribute attribute = (org.eclipse.emf.ecore.EAttribute) feature;
        if (attribute.isID()) {
          Object attributeValue = element.eGet(attribute);
          String match = matches(identifier, attributeValue, matchFuzzy);
          if (match != null) {
            return match;
          }
        }
      }
    }

    // then check for an attribute that is called 'name'
    org.eclipse.emf.ecore.EStructuralFeature nameAttr =
        element.eClass().getEStructuralFeature(NAME_FEATURE);
    if (nameAttr instanceof org.eclipse.emf.ecore.EAttribute) {
      Object attributeValue = element.eGet(nameAttr);
      return matches(identifier, attributeValue, matchFuzzy);
    } else {
      // try any other string attribute found
      for (org.eclipse.emf.ecore.EAttribute stringAttribute :
          element.eClass().getEAllAttributes()) {
        if ("java.lang.String".equals(stringAttribute.getEType().getInstanceClassName())) {
          Object attributeValue = element.eGet(stringAttribute);
          String match = matches(identifier, attributeValue, matchFuzzy);
          if (match != null) {
            return match;
          }
        }
      }

      for (org.eclipse.emf.ecore.EOperation o : element.eClass().getEAllOperations()) {
        if (o.getName().toLowerCase().endsWith(NAME_FEATURE) && o.getEParameters().size() == 0) {
          String result = (String) ssl.resource.ssl.util.SslEObjectUtil.invokeOperation(element, o);
          String match = matches(identifier, result, matchFuzzy);
          if (match != null) {
            return match;
          }
        }
      }
    }
    return null;
  }
  /**
   * Returns a list of potential identifiers that may be used to reference the given element. This
   * method can be overridden to customize the identification of elements.
   */
  public List<String> getNames(EObject element) {
    List<String> names = new ArrayList<String>();

    // first check for attributes that have set the ID flag to true
    List<EAttribute> attributes = element.eClass().getEAllAttributes();
    for (EAttribute attribute : attributes) {
      if (attribute.isID()) {
        Object attributeValue = element.eGet(attribute);
        if (attributeValue != null) {
          names.add(attributeValue.toString());
        }
      }
    }

    // then check for an attribute that is called 'name'
    EStructuralFeature nameAttr = element.eClass().getEStructuralFeature(NAME_FEATURE);
    if (nameAttr instanceof EAttribute) {
      Object attributeValue = element.eGet(nameAttr);
      if (attributeValue != null) {
        names.add(attributeValue.toString());
      }
    } else {
      // try any other string attribute found
      for (EAttribute attribute : attributes) {
        if ("java.lang.String".equals(attribute.getEType().getInstanceClassName())) {
          Object attributeValue = element.eGet(attribute);
          if (attributeValue != null) {
            names.add(attributeValue.toString());
          }
        }
      }

      // try operations without arguments that return strings and which have a name that
      // ends with 'name'
      for (EOperation operation : element.eClass().getEAllOperations()) {
        if (operation.getName().toLowerCase().endsWith(NAME_FEATURE)
            && operation.getEParameters().size() == 0) {
          Object result =
              org.emftext.sdk.concretesyntax.resource.cs.util.CsEObjectUtil.invokeOperation(
                  element, operation);
          if (result != null) {
            names.add(result.toString());
          }
        }
      }
    }
    return names;
  }
  private String getCallLabel() {
    String name = null;
    Activity activity = (Activity) getRealModel().getCallee();
    if (activity != null) {
      EOperation operation = activity.getOwningOperation().getOperation();
      if (!activity.getOutParameters().isEmpty()) {
        EParameter param = activity.getOutParameters().get(0);
        for (ParameterBinding binding : getRealModel().getOwnedParameterBindings()) {
          if (binding.getParameter() == param) {
            name = Expr2String.toString(binding.getValueExpression()) + " := ";
          }
        }

        if (name == null) {
          name = "";
        }
      } else {
        name = "void ";
      }

      name += operation.getName() + "(";
      for (EParameter param : operation.getEParameters()) {
        boolean found = false;
        for (ParameterBinding binding : getRealModel().getOwnedParameterBindings()) {
          if (binding.getParameter() == param) {
            name += Expr2String.toString(binding.getValueExpression()) + ",";
            found = true;
          }
        }

        if (!found) {
          name += "null,";
        }
      }
      int lastComma = name.lastIndexOf(",");
      if (lastComma > -1) {
        name = name.substring(0, lastComma);
      }
      name += ")";
    } else {
      name = "select activity to call";
    }
    return name;
  }
 private String getName(ReferenceType element) {
   org.eclipse.emf.ecore.EStructuralFeature nameAttr =
       element.eClass().getEStructuralFeature(NAME_FEATURE);
   if (element.eIsProxy()) {
     String fragment = ((org.eclipse.emf.ecore.InternalEObject) element).eProxyURI().fragment();
     if (fragment != null
         && fragment.startsWith(
             ssl.resource.ssl.ISslContextDependentURIFragment.INTERNAL_URI_FRAGMENT_PREFIX)) {
       fragment =
           fragment.substring(
               ssl.resource.ssl.ISslContextDependentURIFragment.INTERNAL_URI_FRAGMENT_PREFIX
                   .length());
       fragment = fragment.substring(fragment.indexOf("_") + 1);
     }
     return fragment;
   } else if (nameAttr instanceof org.eclipse.emf.ecore.EAttribute) {
     return (String) element.eGet(nameAttr);
   } else {
     // try any other string attribute found
     for (org.eclipse.emf.ecore.EAttribute strAttribute : element.eClass().getEAllAttributes()) {
       if (!strAttribute.isMany()
           && strAttribute.getEType().getInstanceClassName().equals("String")) {
         return (String) element.eGet(strAttribute);
       }
     }
     for (org.eclipse.emf.ecore.EOperation o : element.eClass().getEAllOperations()) {
       if (o.getName().toLowerCase().endsWith(NAME_FEATURE) && o.getEParameters().size() == 0) {
         String result = (String) ssl.resource.ssl.util.SslEObjectUtil.invokeOperation(element, o);
         if (result != null) {
           return result;
         }
       }
     }
   }
   return null;
 }