Example #1
0
  @Before
  public void before() {
    ePackage = FACTORY.createEPackage();
    ePackage.setName("Package");

    eClass0 = FACTORY.createEClass();
    eClass0.setName("EClass0");
    ePackage.getEClassifiers().add(eClass0);

    eClass1 = FACTORY.createEClass();
    eClass1.setName("EClass1");
    ePackage.getEClassifiers().add(eClass1);

    singleRef = FACTORY.createEReference();
    singleRef.setName("singleRef");
    singleRef.setUpperBound(1);
    singleRef.setEType(eClass1);
    singleRef.setContainment(true);
    eClass0.getEStructuralFeatures().add(singleRef);

    multiRef = FACTORY.createEReference();
    multiRef.setName("multiRef");
    multiRef.setUpperBound(-1);
    multiRef.setEType(eClass0);
    multiRef.setContainment(true);
    eClass1.getEStructuralFeatures().add(multiRef);
  }
  @Override
  public void setUp() throws Exception {

    super.setUp();

    EcoreFactory factory = EcoreFactory.eINSTANCE;
    EPackage p = factory.createEPackage();
    p.setName("TestPackage1");
    EClass class0 = factory.createEClass();
    class0.setName("Class0");
    EClass class1 = factory.createEClass();
    class1.setName("Class1");
    EClass class2 = factory.createEClass();
    class2.setName("Class2");

    p.getEClassifiers().add(class0);
    p.getEClassifiers().add(class1);
    p.getEClassifiers().add(class2);

    leftModel = EcoreUtil.copy(p);
    URI leftURI = URI.createURI("leftmodel.ecore");
    ModelUtils.attachResource(leftURI, leftModel);

    expectedModel = EcoreUtil.copy(leftModel);
    URI expectedURI = URI.createURI("expectedmodel.ecore");
    ModelUtils.attachResource(expectedURI, expectedModel);

    p.getEClassifiers().remove(class1);
    rightModel = EcoreUtil.copy(p);
    URI rightURI = URI.createURI("rightmodel.ecore");
    ModelUtils.attachResource(rightURI, rightModel);
  }
  private EPackage createDynamicEPackage() {
    final EcoreFactory efactory = EcoreFactory.eINSTANCE;
    final EcorePackage epackage = EcorePackage.eINSTANCE;

    // Create a new EPackage and add the new EClasses
    EPackage topPackage = efactory.createEPackage();
    topPackage.setName("toppackage");
    topPackage.setNsPrefix("toppackage");
    topPackage.setNsURI("http:///www.elver.org/toppackage");

    EPackage subPackage1 = efactory.createEPackage();
    subPackage1.setName("subPackage1");
    subPackage1.setNsPrefix("subPackage1");
    subPackage1.setNsURI("http:///www.elver.org/subPackage1");

    {
      EClass schoolBookEClass = efactory.createEClass();
      schoolBookEClass.setName("class1");
      // create a new attribute for this EClass
      EAttribute level = efactory.createEAttribute();
      level.setName("level");
      level.setEType(epackage.getEInt());
      schoolBookEClass.getEStructuralFeatures().add(level);
      subPackage1.getEClassifiers().add(schoolBookEClass);
    }

    {
      EEnum schoolBookEClass = efactory.createEEnum();
      schoolBookEClass.setName("enum");
      subPackage1.getEClassifiers().add(schoolBookEClass);
    }

    {
      EClass schoolBookEClass = efactory.createEClass();
      schoolBookEClass.setName("class2");
      // create a new attribute for this EClass
      subPackage1.getEClassifiers().add(schoolBookEClass);
    }

    {
      EClass schoolBookEClass = efactory.createEClass();
      schoolBookEClass.setName("class3");
      // create a new attribute for this EClass
      EAttribute level = efactory.createEAttribute();
      level.setName("level");
      level.setEType(epackage.getEInt());
      schoolBookEClass.getEStructuralFeatures().add(level);
      subPackage1.getEClassifiers().add(schoolBookEClass);
    }

    EPackage subPackage2 = efactory.createEPackage();
    subPackage2.setName("subPackage2");
    subPackage2.setNsPrefix("subPackage2");
    subPackage2.setNsURI("http:///www.elver.org/subPackage2");

    topPackage.getESubpackages().add(subPackage1);
    return topPackage;
  }
  @Override
  public EClass eClass() {
    if (eClass == null) {
      ePackage = EcoreFactory.eINSTANCE.createEPackage();
      eClass = EcoreFactory.eINSTANCE.createEClass();
      ePackage.getEClassifiers().add(eClass);

      eClass.setName("StringWrapper"); // $NON-NLS-1$
      eClass.getESuperTypes().add(XMLTypePackage.eINSTANCE.getAnyType());
      ExtendedMetaData.INSTANCE.setName(eClass, ""); // $NON-NLS-1$
      eClass.setInstanceClass(AnyType.class);

      EAttribute eAttribute = EcoreFactory.eINSTANCE.createEAttribute();
      eAttribute.setName("value"); // $NON-NLS-1$
      eAttribute.setChangeable(true);
      eAttribute.setUnsettable(true);
      eAttribute.setEType(EcorePackage.eINSTANCE.getEClassifier("EString")); // $NON-NLS-1$
      eClass.getEStructuralFeatures().add(eAttribute);

      //			ExtendedMetaData.INSTANCE.setNamespace(eAttribute, ePackage.getNsURI());
      ExtendedMetaData.INSTANCE.setFeatureKind(eAttribute, ExtendedMetaData.ATTRIBUTE_FEATURE);
      ExtendedMetaData.INSTANCE.setName(eAttribute, "value"); // $NON-NLS-1$
    }
    return eClass;
  }
Example #5
0
  /**
   * Dynamically create an {@link EObject} with a collection called <code>children</code> which
   * contains the given elements.
   *
   * @param collection The initial filling of the collection.
   * @return An {@link EObject} which just has a containment list (named <code>children</code>) of
   *     type {@link EObject}.
   */
  @SuppressWarnings("unchecked")
  public static EObject wrapInGenericContainer(Collection<? extends EObject> collection) {

    // create the wrapper containment reference
    final EReference children = EcoreFactory.eINSTANCE.createEReference();
    children.setName("children");
    children.setLowerBound(0);
    children.setUpperBound(ETypedElement.UNBOUNDED_MULTIPLICITY);
    children.setContainment(true);
    children.setEType(EcorePackage.Literals.EOBJECT);

    // create the wrapper class
    final EClass containerClass = EcoreFactory.eINSTANCE.createEClass();
    containerClass.setName("GenericEContainer");
    containerClass.getEStructuralFeatures().add(children);

    // create a package for our wrapper class
    final EPackage ePackage = EcoreFactory.eINSTANCE.createEPackage();
    ePackage.setName("dynamic");
    ePackage.setNsPrefix("dynamic");
    ePackage.setNsURI("http://www.example.org/dynamic");
    ePackage.getEClassifiers().add(containerClass);

    // validate what we just created
    if (Diagnostician.INSTANCE.validate(ePackage).getCode() == Diagnostic.OK) {

      // instantiate our class, fill the children and return it
      final EObject container = ePackage.getEFactoryInstance().create(containerClass);
      ((EList<EObject>) container.eGet(children)).addAll(collection);
      return container;
    } else {
      throw new UnknownError(
          "Dynamic EMF wrapper model creation failed for whatever reason. Developer, please debug better!");
    }
  }
  protected EClass createEClass(Diagram diagram, String className) {
    EClass eClass = EcoreFactory.eINSTANCE.createEClass();
    eClass.setName(className);

    diagram.eResource().getContents().add(eClass);

    return eClass;
  }
 public void testErrorMessage_02() {
   EClass eClass = createEClass();
   eClass.setName("EClassName");
   IEObjectDescription description =
       EObjectDescription.create(QualifiedName.create(eClass.getName()), eClass);
   String errorMessage =
       helper.getDuplicateNameErrorMessage(
           description, EcorePackage.Literals.ECLASS, EcorePackage.Literals.ENAMED_ELEMENT__NAME);
   assertEquals("Duplicate EClass 'EClassName'", errorMessage);
 }
 public void testCreatedErrors_01() {
   maxCallCount = 0;
   ImmutableList<EClass> classes = ImmutableList.of(createEClass(), createEClass());
   for (EClass clazz : classes) {
     clazz.setName("Same");
   }
   expected.addAll(classes);
   helper.checkUniqueNames(Scopes.scopedElementsFor(classes), this, this);
   assertEquals(classes.size(), callCount);
   assertTrue(expected.isEmpty());
 }
Example #9
0
  public void testClone() throws Exception {
    Resource.Factory.Registry.INSTANCE
        .getExtensionToFactoryMap()
        .put(Resource.Factory.Registry.DEFAULT_EXTENSION, new XMIResourceFactoryImpl());
    EPackage.Registry.INSTANCE.put(EcorePackage.eINSTANCE.getNsURI(), EcorePackage.eINSTANCE);

    ResourceSetImpl rs = new ResourceSetImpl();
    Resource r1 =
        rs.createResource(URI.createURI("foo.xmi"), ContentHandler.UNSPECIFIED_CONTENT_TYPE);
    Resource r2 =
        rs.createResource(URI.createURI("bar.xmi"), ContentHandler.UNSPECIFIED_CONTENT_TYPE);
    EClass a = EcoreFactory.eINSTANCE.createEClass();
    a.setName("a");
    EClass b = EcoreFactory.eINSTANCE.createEClass();
    r1.getContents().add(a);
    b.setName("b");
    b.getESuperTypes().add(a);
    r2.getContents().add(b);

    ResourceSetImpl clone = EcoreUtil2.clone(new ResourceSetImpl(), rs);
    EList<Resource> list = clone.getResources();

    Resource resA = list.get(0);
    assertEquals(URI.createURI("foo.xmi"), resA.getURI());
    assertNotSame(resA, r1);

    Resource resB = list.get(1);
    assertEquals(URI.createURI("bar.xmi"), resB.getURI());
    assertNotSame(resB, r2);

    EClass a1 = (EClass) resA.getContents().get(0);
    EClass b1 = (EClass) resB.getContents().get(0);
    assertEquals("a", a1.getName());
    assertNotSame(a, a1);
    assertEquals("b", b1.getName());
    assertNotSame(b, b1);
    assertSame(b1.getESuperTypes().get(0), a1);
    assertSame(b.getESuperTypes().get(0), a);
  }
 public void testErrorMessage_04() {
   EClass eClass = createEClass();
   eClass.setName("EClassName");
   EAttribute attribute = EcoreFactory.eINSTANCE.createEAttribute();
   attribute.setName("Attribute");
   eClass.getEStructuralFeatures().add(attribute);
   IEObjectDescription description =
       EObjectDescription.create(QualifiedName.create(attribute.getName()), attribute);
   String errorMessage =
       helper.getDuplicateNameErrorMessage(
           description,
           EcorePackage.Literals.EATTRIBUTE,
           EcorePackage.Literals.ENAMED_ELEMENT__NAME);
   assertEquals("Duplicate EAttribute 'Attribute' in EClass 'EClassName'", errorMessage);
 }
Example #11
0
  @Test
  public void inAndOutOfString() {
    EPackage ePackage = EcoreFactory.eINSTANCE.createEPackage();
    ePackage.setName("Package");

    EClass eClass0 = EcoreFactory.eINSTANCE.createEClass();
    eClass0.setName("EClass0");
    ePackage.getEClassifiers().add(eClass0);

    EClass eClass1 = EcoreFactory.eINSTANCE.createEClass();
    eClass1.setName("EClass1");
    ePackage.getEClassifiers().add(eClass1);

    String stringified = EMFUtils.convertToXML(ePackage);
    Logger logger = Logger.getLogger(TestEMFUtils.class);
    logger.debug("STRING: " + stringified);

    EObject object = EMFUtils.createFromXML(stringified);
    assertTrue(object instanceof EPackage);
    ePackage = (EPackage) object;
    assertEquals(2, ePackage.getEClassifiers().size());
    assertEquals("EClass0", ePackage.getEClassifiers().get(0).getName());
    assertEquals("EClass1", ePackage.getEClassifiers().get(1).getName());
  }
  public Object[] create(ICreateContext context) {
    // ask user for Class name
    // String newClassName = getPlatformService().askString(TITLE,
    // USER_QUESTION, "");
    // if (newClassName == null || newClassName.trim().length() == 0) {
    // return EMPTY;
    // }

    String newClassName = "NewClass";

    // create new Class
    EClass newClass = EcoreFactory.eINSTANCE.createEClass();
    newClass.setName(newClassName);
    getDiagram().eResource().getContents().add(newClass);

    // do the add
    addGraphicalRepresentation(context, newClass);

    // return newly created business object(s)
    return new Object[] {newClass};
  }
  public EPackage createPackage() {
    EcoreFactory theCoreFactory = EcoreFactory.eINSTANCE;
    EcorePackage theCorePackage = EcorePackage.eINSTANCE;

    mapContainerEClass = theCoreFactory.createEClass();
    mapContainerEClass.setName("MapContainer");

    EPackage dynamicMapEPackage = createUniquePackage();
    dynamicMapEPackage.getEClassifiers().add(mapContainerEClass);

    EStructuralFeature name = theCoreFactory.createEAttribute();
    name.setName("name");
    name.setEType(theCorePackage.getEString());

    mapContainerEClass.getEStructuralFeatures().add(name);

    if (!isConfig(LEGACY)) {
      CDOUtil.prepareDynamicEPackage(dynamicMapEPackage);
    }

    return dynamicMapEPackage;
  }
Example #14
0
 protected Result<Boolean> applyRuleEClassEObject(
     final RuleEnvironment G,
     final RuleApplicationTrace _trace_,
     final EClass eClass,
     final EObject object)
     throws RuleFailedException {
   String _string = new String();
   String _firstUpper = StringExtensions.toFirstUpper("bar");
   String _plus = (_string + _firstUpper);
   boolean _equals = Objects.equal("foo", _plus);
   /* 'foo' == new String() + 'bar'.toFirstUpper */
   if (!_equals) {
     sneakyThrowRuleFailedException("\'foo\' == new String() + \'bar\'.toFirstUpper");
   }
   final EClass eC = EcoreFactory.eINSTANCE.createEClass();
   eC.setName("MyEClass");
   /* eClass == eC */
   if (!Objects.equal(eClass, eC)) {
     sneakyThrowRuleFailedException("eClass == eC");
   }
   return new Result<Boolean>(true);
 }
Example #15
0
 private EClass createEClass(String name) {
   EClass result = EcoreFactory.eINSTANCE.createEClass();
   result.setName(name);
   return result;
 }
 private EClass createEClass() {
   EClass result = EcoreFactory.eINSTANCE.createEClass();
   result.setName(String.valueOf(created++));
   return result;
 }
  @Override
  public Object execute(Collection<? extends EObject> eObjects, ExecutionEvent event)
      throws ExecutionException {
    Collection<FeatureMappingStrategy> mappingStrategies = new ArrayList<FeatureMappingStrategy>();
    Collection<JvmGenericType> classDeclarations = new ArrayList<JvmGenericType>();
    Collection<EPackage> ePackages = new ArrayList<EPackage>();
    Collection<EClass> eClasses = new ArrayList<EClass>();
    categorizeEObjects(eObjects, mappingStrategies, classDeclarations, ePackages, eClasses);

    String eClassNamePrefix = getEventParameter(event, "eClassNamePrefix", "");

    FacadeModel facadeModel = getEObject(eObjects, FacadeModel.class, null);
    if (mappingStrategies.size() == 0) {
      mappingStrategies.addAll(facadeModel.getFeatureMappingStrategies());
    } else if (facadeModel == null) {
      facadeModel = (FacadeModel) mappingStrategies.iterator().next().eContainer();
    }
    if (ePackages.size() == 0) {
      for (Resource resource : facadeModel.eResource().getResourceSet().getResources()) {
        ePackages.addAll(getEObjects(resource.getContents(), EPackage.class, null, -1));
      }
    }
    for (JvmGenericType classDeclaration : classDeclarations) {
      String eClassName = eClassNamePrefix + classDeclaration.getSimpleName();
      EClass eClass = (EClass) findEClassifier(eClassName, ePackages, eClasses);
      if (eClass == null) {
        eClass = EcoreFactory.eINSTANCE.createEClass();
        eClass.setName(eClassName);
        getEObject(ePackages, EPackage.class, null).getEClassifiers().add(eClass);
      }
      EClassMapping classMapping =
          getEObject(facadeModel.getClassifierMappings(), EClassMapping.class, eClassName);
      if (classMapping == null) {
        classMapping = EmfacadeFactory.eINSTANCE.createEClassMapping();
        classMapping.setEClass(eClass);
        facadeModel.getClassifierMappings().add(classMapping);
      }

      EList<JvmMember> jElements = new BasicEList<JvmMember>(classDeclaration.getMembers());
      EList<JvmMember> matchedElements = new BasicEList<JvmMember>();
      for (FeatureMappingStrategy featureMappingStrategy : mappingStrategies) {
        EList<EFeatureMapping> featureMappings =
            featureMappingStrategy.createFeatureMappings(jElements);
        for (EFeatureMapping featureMapping : featureMappings) {
          EStructuralFeature feature = featureMapping.getEFeature();
          if (feature.getEType() == null) {
            resolveFeatureType(feature, facadeModel.getClassifierMappings(), ePackages);
          }
          EStructuralFeature existingFeature = eClass.getEStructuralFeature(feature.getName());
          if (existingFeature != null) {
            eClass.getEStructuralFeatures().remove(existingFeature);
          }
          eClass.getEStructuralFeatures().add(feature);
          EFeatureMapping existingMapping =
              getEObject(
                  classMapping.getFeatureMappings(),
                  EFeatureMapping.class,
                  featureMapping.getEFeature().getName());
          if (existingMapping != null) {
            classMapping.getFeatureMappings().remove(existingMapping);
          }
          classMapping.getFeatureMappings().add(featureMapping);

          //					matchedElements.addAll(featureMapping.getJElements());
        }
        jElements.removeAll(matchedElements);
      }
    }
    return null;
  }
Example #18
0
 private EClass createClass(String name) {
   EClass eClass = EcoreFactory.eINSTANCE.createEClass();
   eClass.setName(name);
   metamodelPackage.getEClassifiers().add(eClass);
   return eClass;
 }
Example #19
0
  private static EPackage getEmployeePackage() {
    EcoreFactory ecoreFactory = EcoreFactory.eINSTANCE;
    EcorePackage ecorePackage = EcorePackage.eINSTANCE;

    EPackage employeePackage = ecoreFactory.createEPackage();
    employeePackage.setName("emp");
    employeePackage.setNsPrefix("emp");
    employeePackage.setNsURI("http://www.example.com/Emp");

    employeeClass = ecoreFactory.createEClass();
    employeePackage.getEClassifiers().add(employeeClass);
    employeeClass.setName("Employee");

    EClass poClass = ecoreFactory.createEClass();
    employeePackage.getEClassifiers().add(poClass);
    poClass.setName("PurchaseOrder");

    nameAttr = ecoreFactory.createEAttribute();
    employeeClass.getEStructuralFeatures().add(nameAttr);
    nameAttr.setName("name");
    nameAttr.setEType(ecorePackage.getEString());

    // to test multiplicity-many attributes.
    ratingsAttr = ecoreFactory.createEAttribute();
    employeeClass.getEStructuralFeatures().add(ratingsAttr);
    ratingsAttr.setName("phoneNumbers");
    ratingsAttr.setUpperBound(ETypedElement.UNBOUNDED_MULTIPLICITY);
    ratingsAttr.setEType(ecorePackage.getEIntegerObject());

    // create a one-way reference to an Employee, employeesRef.
    // This is used to test cases described in cases.gif.
    employeesRef = ecoreFactory.createEReference();
    employeeClass.getEStructuralFeatures().add(employeesRef);
    employeesRef.setName("employees");
    employeesRef.setUpperBound(ETypedElement.UNBOUNDED_MULTIPLICITY);
    employeesRef.setEType(employeeClass);

    // to test FeatureMaps with EAttributes Entries
    ordersAttr = ecoreFactory.createEAttribute();
    employeeClass.getEStructuralFeatures().add(ordersAttr);
    ordersAttr.setName("orders");
    ordersAttr.setUpperBound(ETypedElement.UNBOUNDED_MULTIPLICITY);
    ordersAttr.setEType(ecorePackage.getEFeatureMapEntry());
    ExtendedMetaData.INSTANCE.setFeatureKind(ordersAttr, ExtendedMetaData.GROUP_FEATURE);

    preferredAttr = ecoreFactory.createEAttribute();
    employeeClass.getEStructuralFeatures().add(preferredAttr);
    preferredAttr.setName("preferredOrders");
    preferredAttr.setUpperBound(ETypedElement.UNBOUNDED_MULTIPLICITY);
    preferredAttr.setEType(ecorePackage.getEString());
    preferredAttr.setVolatile(true);
    preferredAttr.setTransient(true);
    preferredAttr.setDerived(true);
    ExtendedMetaData.INSTANCE.setGroup(preferredAttr, ordersAttr);

    standardAttr = ecoreFactory.createEAttribute();
    employeeClass.getEStructuralFeatures().add(standardAttr);
    standardAttr.setName("standardOrders");
    standardAttr.setUpperBound(ETypedElement.UNBOUNDED_MULTIPLICITY);
    standardAttr.setEType(ecorePackage.getEString());
    standardAttr.setVolatile(true);
    standardAttr.setTransient(true);
    standardAttr.setDerived(true);
    ExtendedMetaData.INSTANCE.setGroup(standardAttr, ordersAttr);

    return employeePackage;
  }