@Override
    public EOperation define(EcoreEnvironment env) {
      EOperation eOperation = EcoreFactory.eINSTANCE.createEOperation();
      eOperation.setName(fName);
      int pos = 0;
      for (EClassifier cls : fParamTypes) {
        EParameter eParam = EcoreFactory.eINSTANCE.createEParameter();
        String paramName = cls.getName();
        if (fParamNames != null) {
          paramName = fParamNames[pos++];
        }

        eParam.setName(paramName);
        eParam.setEType(cls);
        eOperation.getEParameters().add(eParam);
      }

      eOperation.setEType(fReturnType);

      assert fContextType instanceof EClass;
      ((EClass) fContextType).getEOperations().add(eOperation);

      CallHandlerAdapter.attach(eOperation, fDispatcher);
      return eOperation;
    }
  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;
  }
 protected void handleEOperations(
     List<EOperation> operations, Set<EPackage> visitedPackages, Set<Object> visited) {
   if (operations != null) {
     for (EOperation operation : operations) {
       handleEGenericType(operation.getEGenericType(), visitedPackages, visited);
       handleETypeParameters(operation.getETypeParameters(), visitedPackages, visited);
       handleEParameters(operation.getEParameters(), visitedPackages, visited);
       handleEGenericTypes(operation.getEGenericExceptions(), visitedPackages, visited);
     }
   }
 }
Пример #5
0
 @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;
 }
  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;
  }
Пример #7
0
  /**
   * 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;
  }
  /**
   *
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   *
   * @generated
   */
  public Relator relator() {
    if (relatorBodyOCL == null) {
      EOperation eOperation = RefOntoUMLPackage.eINSTANCE.getRoleMixin().getEOperations().get(1);
      OCL.Helper helper = OCL_ENV.createOCLHelper();
      helper.setOperationContext(RefOntoUMLPackage.eINSTANCE.getRoleMixin(), eOperation);
      EAnnotation ocl = eOperation.getEAnnotation(OCL_ANNOTATION_SOURCE);
      String body = ocl.getDetails().get("body");

      try {
        relatorBodyOCL = helper.createQuery(body);
      } catch (ParserException e) {
        throw new UnsupportedOperationException(e.getLocalizedMessage());
      }
    }

    Query<EClassifier, ?, ?> query = OCL_ENV.createQuery(relatorBodyOCL);

    return (Relator) query.evaluate(this);
  }
  /**
   *
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   *
   * @generated
   */
  public boolean isCircular() {
    if (isCircularBodyOCL == null) {
      EOperation eOperation =
          RefOntoUMLPackage.eINSTANCE.getMeasurementDimension().getEOperations().get(2);
      OCL.Helper helper = OCL_ENV.createOCLHelper();
      helper.setOperationContext(RefOntoUMLPackage.eINSTANCE.getMeasurementDimension(), eOperation);
      EAnnotation ocl = eOperation.getEAnnotation(OCL_ANNOTATION_SOURCE);
      String body = ocl.getDetails().get("body");

      try {
        isCircularBodyOCL = helper.createQuery(body);
      } catch (ParserException e) {
        throw new UnsupportedOperationException(e.getLocalizedMessage());
      }
    }

    Query<EClassifier, ?, ?> query = OCL_ENV.createQuery(isCircularBodyOCL);

    return ((Boolean) query.evaluate(this)).booleanValue();
  }
  /**
   *
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   *
   * @generated
   */
  public EList<RigidSortalClass> rigidSortals() {
    if (rigidSortalsBodyOCL == null) {
      EOperation eOperation = RefOntoUMLPackage.eINSTANCE.getRoleMixin().getEOperations().get(3);
      OCL.Helper helper = OCL_ENV.createOCLHelper();
      helper.setOperationContext(RefOntoUMLPackage.eINSTANCE.getRoleMixin(), eOperation);
      EAnnotation ocl = eOperation.getEAnnotation(OCL_ANNOTATION_SOURCE);
      String body = ocl.getDetails().get("body");

      try {
        rigidSortalsBodyOCL = helper.createQuery(body);
      } catch (ParserException e) {
        throw new UnsupportedOperationException(e.getLocalizedMessage());
      }
    }

    Query<EClassifier, ?, ?> query = OCL_ENV.createQuery(rigidSortalsBodyOCL);

    @SuppressWarnings("unchecked")
    Collection<RigidSortalClass> result = (Collection<RigidSortalClass>) query.evaluate(this);
    return new BasicEList.UnmodifiableEList<RigidSortalClass>(result.size(), result.toArray());
  }
Пример #12
0
 private boolean removeAnnotation(EOperation existingOperation) {
   List<EAnnotation> annotations = existingOperation.getEAnnotations();
   EAnnotation annotationToRemove = null;
   for (EAnnotation annotation : annotations) {
     if (annotation.getSource().equals(EcoreModelRefactorer.class.getName())) {
       annotationToRemove = annotation;
     }
   }
   if (annotationToRemove != null) {
     return annotations.remove(annotationToRemove);
   }
   return false;
 }
  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 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;
 }
Пример #15
0
 /**
  *
  * <!-- begin-user-doc -->
  * <!-- end-user-doc -->
  *
  * @generated
  */
 public EOperation getOperation() {
   if (operation != null && operation.eIsProxy()) {
     InternalEObject oldOperation = (InternalEObject) operation;
     operation = (EOperation) eResolveProxy(oldOperation);
     if (operation != oldOperation) {
       if (eNotificationRequired())
         eNotify(
             new ENotificationImpl(
                 this,
                 Notification.RESOLVE,
                 TraceabilityPackage.INPUT_ELEMENT__OPERATION,
                 oldOperation,
                 operation));
     }
   }
   return operation;
 }
Пример #16
0
 private void annotateAsGenerated(EOperation newEOperation) {
   EAnnotation eAnnotation = EcoreFactory.eINSTANCE.createEAnnotation();
   eAnnotation.setSource(EcoreModelRefactorer.class.getName());
   newEOperation.getEAnnotations().add(eAnnotation);
 }
Пример #17
0
  public void propagateEOperations(JavaResource resource, GenClass genClass) {
    GenPackage genPackage = genClass.getGenPackage();
    EPackage ePackage = genPackage.getEcorePackage();
    if (resource.getContents().isEmpty()
        || !(resource.getContents().get(0) instanceof CompilationUnit)) {
      return;
    }
    CompilationUnit cu = (CompilationUnit) resource.getContents().get(0);
    Class customClass = (Class) cu.getClassifiers().get(0);
    EClass eClass = genClass.getEcoreClass();

    if (eClass == null) {
      return;
    }

    Set<Method> annotatedMethods = getAnnotatedMethods(customClass);

    for (Method method : annotatedMethods) {
      for (AnnotationInstanceOrModifier modifier : method.getAnnotationsAndModifiers()) {
        if (modifier instanceof Public) {
          EOperation newEOperation = EcoreFactory.eINSTANCE.createEOperation();
          newEOperation.setName(method.getName());
          Type opType = method.getTypeReference().getTarget();
          newEOperation.setEType(
              eClassifierForCustomClass(opType, method.getTypeReference(), ePackage));
          if (isMulti(opType)) {
            newEOperation.setUpperBound(-1);
          }
          for (Parameter parameter : method.getParameters()) {
            EParameter newEParameter = EcoreFactory.eINSTANCE.createEParameter();
            newEParameter.setName(parameter.getName());
            Type paramType = parameter.getTypeReference().getTarget();
            newEParameter.setEType(
                eClassifierForCustomClass(paramType, parameter.getTypeReference(), ePackage));
            // TODO generics, ...
            newEOperation.getEParameters().add(newEParameter);
          }
          // TODO @jendrik: why is that needed?
          //					for (AnnotationInstanceOrModifier annotationInstance :
          // method.getAnnotationsAndModifiers()) {
          //						if (annotationInstance instanceof AnnotationInstance) {
          //							Classifier javaAnnotation = ((AnnotationInstance)
          // annotationInstance).getAnnotation();
          //							if (javaAnnotation.eIsProxy()) {
          //								continue;
          //							}
          //							EAnnotation eAnnotation = EcoreFactory.eINSTANCE.createEAnnotation();
          //							eAnnotation.setSource(javaAnnotation.getContainingCompilationUnit(
          //									).getNamespacesAsString() + javaAnnotation.getName());
          //							newEOperation.getEAnnotations().add(eAnnotation);
          //						}
          //					}
          boolean operationAlreadyExists = false;
          List<EOperation> operations = eClass.getEOperations();
          List<EOperation> existingOperations = new ArrayList<EOperation>(operations);
          // must be done here already for ensuring that compared operations have the same parent
          eClass.getEOperations().add(newEOperation);
          for (EOperation existingOperation : existingOperations) {
            boolean removed = removeAnnotation(existingOperation);
            if (EcoreUtil.equals(existingOperation, newEOperation)) {
              operationAlreadyExists = true;
              removeAnnotation(existingOperation);
              annotateAsGenerated(existingOperation);
              break;
            }
            if (removed) {
              annotateAsGenerated(existingOperation);
            }
          }
          if (!operationAlreadyExists) {
            annotateAsGenerated(newEOperation);
          } else {
            eClass.getEOperations().remove(newEOperation);
          }
          break;
        }
      }
    }

    try {
      Resource ecoreResource = ePackage.eResource();
      URI originalURI = ecoreResource.getURI();
      if (originalURI.isFile()) {
        String workspacePath = ResourcesPlugin.getWorkspace().getRoot().getLocation().toString();
        URI platformURI =
            URI.createPlatformResourceURI(
                originalURI.toFileString().substring(workspacePath.length()), true);
        ecoreResource.setURI(platformURI);
      }
      new ResourceSaver().saveResource(ecoreResource);
      ecoreResource.setURI(originalURI);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }