Home » Modeling » EMF » Model building using only Java code
Model building using only Java code [message #415998] |
Tue, 15 January 2008 22:08 |
Eclipse User |
|
|
|
Originally posted by: m_lehmeier.gmx.de
Hello!
I am trying to create a model from an existing Java object using
reflection mechanism.
First I thought about creating an XMI file manually (with ordinary XML
tools) and then load the file. For this I used Dennis Wagelaar's ATL
command line tool. It works. (save for a strange but apparently harmless
exception)
However, I would like to build a model directly without having to use
ordinary XML.
I got this far:
AtlModelHandler amh = AtlModelHandler.getDefault("EMF");
ModelLoader ml = new EMFModelLoader();
ml.addInjector("xml", XMLInjector.class);
ml.addInjector("ebnf2", TCSInjector.class); // necessary?
ASMModel metaModel = amh.getMof();
ASMModel model = ASMEMFModel.newASMEMFModel(sModel, sMetamodel,
(ASMEMFModel)metaModel, ml);
XMIResource extent = (XMIResource)((ASMEMFModel)model).getExtent();
// Saving
amh.saveModel(model, "file:/tmp/out.ecore");
But I don't know how to continue.
I don't know how to add EPackages or all other child elements in that
model.
I would be very thankful if anybody can point me to a code example so
that I can continue from there.
--
Lehmeier Michael
|
|
|
Re: Model building using only Java code [message #415999 is a reply to message #415998] |
Tue, 15 January 2008 23:52 |
Ed Merks Messages: 33141 Registered: July 2009 |
Senior Member |
|
|
Michael,
Unfortunately I don't know much about ATL. :-(
But they have an extremely active newgroup, so I've added it to the "to"
list of my reply...
Michael Lehmeier wrote:
> Hello!
>
> I am trying to create a model from an existing Java object using
> reflection mechanism.
> First I thought about creating an XMI file manually (with ordinary XML
> tools) and then load the file. For this I used Dennis Wagelaar's ATL
> command line tool. It works. (save for a strange but apparently harmless
> exception)
>
> However, I would like to build a model directly without having to use
> ordinary XML.
>
> I got this far:
>
> AtlModelHandler amh = AtlModelHandler.getDefault("EMF");
> ModelLoader ml = new EMFModelLoader();
> ml.addInjector("xml", XMLInjector.class);
> ml.addInjector("ebnf2", TCSInjector.class); // necessary?
> ASMModel metaModel = amh.getMof();
> ASMModel model = ASMEMFModel.newASMEMFModel(sModel, sMetamodel,
> (ASMEMFModel)metaModel, ml);
> XMIResource extent = (XMIResource)((ASMEMFModel)model).getExtent();
>
> // Saving
> amh.saveModel(model, "file:/tmp/out.ecore");
>
> But I don't know how to continue.
> I don't know how to add EPackages or all other child elements in that
> model.
>
> I would be very thankful if anybody can point me to a code example so
> that I can continue from there.
>
>
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
Re: Model building using only Java code [message #416000 is a reply to message #415999] |
Wed, 16 January 2008 00:23 |
Eclipse User |
|
|
|
Originally posted by: m_lehmeier.gmx.de
On 2008-01-15, Ed Merks <merks@ca.ibm.com> wrote:
> Michael,
>
> Unfortunately I don't know much about ATL. :-(
>
> But they have an extremely active newgroup, so I've added it to the "to"
> list of my reply...
The Problem isn't ATL-specific at all, so I thought the emf newsgroup
was the right one, because it only touches emf-concerns.
Once I have my model ready, I can deal with the ATL-part myself. (I
hope)
--
Lehmeier Michael
|
|
|
Re: Model building using only Java code [message #416001 is a reply to message #416000] |
Wed, 16 January 2008 01:11 |
Ed Merks Messages: 33141 Registered: July 2009 |
Senior Member |
|
|
This is a multi-part message in MIME format.
--------------000003050301090002020102
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Michael,
I didn't get the EMF part though. Here's an example of a JUnit test
that creates a Ecore model. Maybe that helps...
package org.eclipse.emf.test.core.dynamic;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.eclipse.emf.common.EMFPlugin;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EFactory;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.ETypedElement;
import org.eclipse.emf.ecore.EcoreFactory;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceImpl;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.xmi.XMLResource;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
import org.eclipse.emf.test.common.TestUtil;
import org.eclipse.emf.test.core.AllSuites;
public class SimpleModelTest extends TestCase
{
private EPackage companyPackage;
private EClass employeeClass;
private EAttribute employeeName;
private EAttribute employeeManager;
private EClass departmentClass;
private EAttribute departmentName;
private EAttribute departmentNumber;
private EReference departmentEmployees;
public SimpleModelTest(String name)
{
super(name);
}
public static Test suite()
{
TestSuite ts = new TestSuite("SimpleModelTest");
ts.addTest(new SimpleModelTest("testPackageAndFactory"));
ts.addTest(new SimpleModelTest("testAttributes"));
ts.addTest(new SimpleModelTest("testReference"));
ts.addTest(new SimpleModelTest("testMetaData"));
ts.addTest(new SimpleModelTest("testSaveAndLoad"));
ts.addTest(new SimpleModelTest("testSaveAndLoadZip"));
ts.addTest(new SimpleModelTest("testProxy"));
ts.addTest(new SimpleModelTest("testTrackingModificaiton"));
ts.addTest(new SimpleModelTest("testAddingDuplicates"));
ts.addTest(new SimpleModelTest("testRenamingStructuralFeature"));
ts.addTest(new SimpleModelTest("testRenamingClassifier"));
return ts;
}
/**
* @see junit.framework.TestCase#setUp()
*/
@Override
protected void setUp() throws Exception
{
EcoreFactory ecoreFactory = EcoreFactory.eINSTANCE;
EcorePackage ecorePackage = EcorePackage.eINSTANCE;
employeeClass = ecoreFactory.createEClass();
employeeClass.setName("Employee");
employeeName = ecoreFactory.createEAttribute();
employeeName.setName("name");
employeeName.setEType(ecorePackage.getEString());
employeeClass.getEStructuralFeatures().add(employeeName);
employeeManager = ecoreFactory.createEAttribute();
employeeManager.setName("manager");
employeeManager.setEType(ecorePackage.getEBoolean());
employeeClass.getEStructuralFeatures().add(employeeManager);
departmentClass = ecoreFactory.createEClass();
departmentClass.setName("Department");
departmentName = ecoreFactory.createEAttribute();
departmentName.setName("name");
departmentName.setEType(ecorePackage.getEString());
departmentClass.getEStructuralFeatures().add(departmentName) ;
departmentNumber = ecoreFactory.createEAttribute();
departmentNumber.setName("number");
departmentNumber.setEType(ecorePackage.getEInt());
departmentClass.getEStructuralFeatures().add(departmentNumbe r);
departmentEmployees = ecoreFactory.createEReference();
departmentEmployees.setName("employees");
departmentEmployees.setEType(employeeClass);
departmentEmployees.setUpperBound(ETypedElement.UNBOUNDED_MU LTIPLICITY);
departmentEmployees.setContainment(true);
departmentClass.getEStructuralFeatures().add(departmentEmplo yees);
companyPackage = ecoreFactory.createEPackage();
companyPackage.setName("company");
companyPackage.setNsPrefix("company");
companyPackage.setNsURI("http:///com.example.company.ecore");
companyPackage.getEClassifiers().add(employeeClass);
companyPackage.getEClassifiers().add(departmentClass);
EPackage.Registry.INSTANCE.put(companyPackage.getNsURI(),
companyPackage);
}
/**
* @see junit.framework.TestCase#tearDown()
*/
@Override
protected void tearDown() throws Exception
{
employeeName = null;
employeeManager = null;
employeeClass.getEStructuralFeatures().clear();
employeeClass = null;
departmentEmployees = null;
departmentName = null;
departmentNumber = null;
departmentClass.getEStructuralFeatures().clear();
departmentClass = null;
companyPackage = null;
}
public void testPackageAndFactory()
{
assertNotNull(companyPackage);
assertEquals("company", companyPackage.getName());
assertEquals("company", companyPackage.getNsPrefix());
assertEquals("http:///com.example.company.ecore",
companyPackage.getNsURI());
EFactory companyFactory = companyPackage.getEFactoryInstance();
assertNotNull(companyFactory);
assertEquals(companyPackage, companyFactory.getEPackage());
EObject employee = companyFactory.create(employeeClass);
assertNotNull(employee);
assertEquals(companyPackage, employee.eClass().getEPackage());
}
public void testAttributes()
{
EFactory companyFactory = companyPackage.getEFactoryInstance();
EObject employee1 = companyFactory.create(employeeClass);
employee1.eSet(employeeName, "John");
assertEquals("John", employee1.eGet(employeeName));
assertEquals(Boolean.FALSE, employee1.eGet(employeeManager));
EObject employee2 = companyFactory.create(employeeClass);
employee2.eSet(employeeName, "Katherine");
assertEquals("Katherine", employee2.eGet(employeeName));
employee2.eSet(employeeManager, Boolean.TRUE);
assertEquals(Boolean.TRUE, employee2.eGet(employeeManager));
EObject department = companyFactory.create(departmentClass);
department.eSet(departmentName, "ABC");
assertEquals("ABC", department.eGet(departmentName));
department.eSet(departmentNumber, new Integer(123));
assertEquals(new Integer(123), department.eGet(departmentNumber));
}
public void testReference()
{
EFactory companyFactory = companyPackage.getEFactoryInstance();
EObject employee1 = companyFactory.create(employeeClass);
EObject employee2 = companyFactory.create(employeeClass);
EObject department1 = companyFactory.create(departmentClass);
@SuppressWarnings("unchecked")
List<EObject> department1Employees =
(List<EObject>)department1.eGet(departmentEmployees);
assertNotNull(department1Employees);
department1Employees.add(employee1);
assertEquals(1, department1Employees.size());
assertEquals(employee1, department1Employees.get(0));
assertEquals(department1Employees,
department1.eGet(departmentEmployees));
department1Employees.add(employee2);
assertEquals(2, department1Employees.size());
assertEquals(employee2, department1Employees.get(1));
assertEquals(department1Employees,
department1.eGet(departmentEmployees));
//Should not affect the list
department1Employees.add(employee2);
assertEquals(2, department1Employees.size());
assertEquals(employee2, department1Employees.get(1));
assertEquals(department1Employees,
department1.eGet(departmentEmployees));
EObject department2 = companyFactory.create(departmentClass);
@SuppressWarnings("unchecked")
List<EObject> department2Employees =
(List<EObject>)department2.eGet(departmentEmployees);
assertNotNull(department2Employees);
department2Employees.add(employee1);
assertEquals(1, department2Employees.size());
assertEquals(employee1, department2Employees.get(0));
assertEquals(department2Employees,
department2.eGet(departmentEmployees));
//since departmentEmployees is an aggregation, employee1 should
be removed
//from department1Employees
assertEquals(1, department1Employees.size());
assertEquals(employee2, department1Employees.get(0));
assertEquals(department1Employees,
department1.eGet(departmentEmployees));
}
public void testMetaData()
{
EFactory companyFactory = companyPackage.getEFactoryInstance();
EObject employee1 = companyFactory.create(employeeClass);
EObject employee2 = companyFactory.create(employeeClass);
EObject department1 = companyFactory.create(departmentClass);
@SuppressWarnings("unchecked")
List<EObject> department1Employees =
(List<EObject>)department1.eGet(departmentEmployees);
EObject department2 = companyFactory.create(departmentClass);
@SuppressWarnings("unchecked")
List<EObject> department2Employees =
(List<EObject>)department2.eGet(departmentEmployees);
department1Employees.add(employee1);
assertEquals(department1, employee1.eContainer());
assertEquals(department1Employees.size(),
department1.eContents().size());
department1Employees.add(employee2);
assertEquals(department1, employee1.eContainer());
assertEquals(department1Employees.size(),
department1.eContents().size());
department2Employees.add(employee1);
assertEquals(department2, employee1.eContainer());
assertEquals(department1Employees.size(),
department1.eContents().size());
assertEquals(department2Employees.size(),
department2.eContents().size());
}
public void testSaveAndLoadZip() throws Exception
{
//Instanciating the model
EFactory companyFactory = companyPackage.getEFactoryInstance();
EObject employee1 = companyFactory.create(employeeClass);
employee1.eSet(employeeName, "John");
EObject employee2 = companyFactory.create(employeeClass);
employee2.eSet(employeeName, "Jane");
EObject department = companyFactory.create(departmentClass);
department.eSet(departmentName, "ACME1");
@SuppressWarnings("unchecked")
List<EObject> employeesList =
(List<EObject>)department.eGet(departmentEmployees);
employeesList.add(employee1);
employeesList.add(employee2);
URI departmentsURI =
URI.createFileURI(TestUtil.getPluginDirectory(AllSuites.PLUG IN_ID) +
"/departments.dept");
XMLResource departmentsResource = (XMLResource)new
XMIResourceFactoryImpl().createResource(departmentsURI);
departmentsResource.setUseZip(true);
departmentsResource.getContents().add(department);
//Saving
departmentsResource.save(Collections.EMPTY_MAP);
assertTrue(new File(departmentsURI.toFileString()).exists());
//Loading department in ResourceSet
ResourceSet resourceSet = new ResourceSetImpl();
resourceSet.getResourceFactoryRegistry().getExtensionToFacto ryMap().put( "dept",
new XMIResourceFactoryImpl()
{
/* (non-Javadoc)
* @see
org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl#create Resource(org.eclipse.emf.common.util.URI)
*/
@Override
public Resource createResource(URI uri)
{
XMLResource result = (XMLResource)super.createResource(uri);
result.setUseZip(true);
return result;
}
});
Resource loadedResource =
resourceSet.getResource(departmentsURI, true);
assertEquals(1, resourceSet.getResources().size());
assertEquals(1, loadedResource.getContents().size());
EObject loadedDepartment = loadedResource.getContents().get(0);
assertEquals(department.eClass(), loadedDepartment.eClass());
assertEquals(department.eGet(departmentName),
loadedDepartment.eGet(departmentName));
//Get Employess
@SuppressWarnings("unchecked")
List<EObject> loadedEmployees =
(List<EObject>)department.eGet(departmentEmployees);
assertEquals(2, loadedEmployees.size());
EObject loadedEmployee = loadedEmployees.get(0);
assertEquals(employee1.eClass(), loadedEmployee.eClass());
assertEquals(employee1.eGet(employeeName),
loadedEmployee.eGet(employeeName));
loadedEmployee = loadedEmployees.get(1);
assertEquals(employee2.eClass(), loadedEmployee.eClass());
assertEquals(employee2.eGet(employeeName),
loadedEmployee.eGet(employeeName));
//Deleting created files
new File(departmentsURI.toFileString()).delete();
assertFalse(new File(departmentsURI.toFileString()).exists());
}
public void testSaveAndLoad() throws Exception
{
//Instanciating the model
EFactory companyFactory = companyPackage.getEFactoryInstance();
EObject employee1 = companyFactory.create(employeeClass);
employee1.eSet(employeeName, "John");
EObject employee2 = companyFactory.create(employeeClass);
employee2.eSet(employeeName, "Jane");
EObject department = companyFactory.create(departmentClass);
department.eSet(departmentName, "ACME1");
@SuppressWarnings("unchecked")
List<EObject> employeesList =
(List<EObject>)department.eGet(departmentEmployees);
employeesList.add(employee1);
employeesList.add(employee2);
URI departmentsURI =
URI.createFileURI(TestUtil.getPluginDirectory(AllSuites.PLUG IN_ID) +
"/departments.xmi");
Resource departmentsResource = new
XMIResourceFactoryImpl().createResource(departmentsURI);
departmentsResource.getContents().add(department);
//Saving
Map<Object, Object> options = new HashMap<Object, Object>();
options.put(XMLResource.OPTION_USE_FILE_BUFFER, Boolean.TRUE);
departmentsResource.save(options);
assertTrue(new File(departmentsURI.toFileString()).exists());
//Loading department in ResourceSet
ResourceSet resourceSet = new ResourceSetImpl();
if (!EMFPlugin.IS_ECLIPSE_RUNNING)
{
resourceSet.getResourceFactoryRegistry().getExtensionToFacto ryMap().put( "xmi",
new XMIResourceFactoryImpl());
}
Resource loadedResource =
resourceSet.getResource(departmentsURI, true);
assertEquals(1, resourceSet.getResources().size());
assertEquals(1, loadedResource.getContents().size());
EObject loadedDepartment = loadedResource.getContents().get(0);
assertEquals(department.eClass(), loadedDepartment.eClass());
assertEquals(department.eGet(departmentName),
loadedDepartment.eGet(departmentName));
//Get Employess
@SuppressWarnings("unchecked")
List<EObject> loadedEmployees =
(List<EObject>)department.eGet(departmentEmployees);
assertEquals(2, loadedEmployees.size());
EObject loadedEmployee = loadedEmployees.get(0);
assertEquals(employee1.eClass(), loadedEmployee.eClass());
assertEquals(employee1.eGet(employeeName),
loadedEmployee.eGet(employeeName));
loadedEmployee = loadedEmployees.get(1);
assertEquals(employee2.eClass(), loadedEmployee.eClass());
assertEquals(employee2.eGet(employeeName),
loadedEmployee.eGet(employeeName));
//Deleting created files
new File(departmentsURI.toFileString()).delete();
assertFalse(new File(departmentsURI.toFileString()).exists());
}
public void testProxy() throws Exception
{
EFactory companyFactory = companyPackage.getEFactoryInstance();
//Adding a not-contained association
EReference associateDepartments =
EcoreFactory.eINSTANCE.createEReference();
associateDepartments.setName("associateDepartments");
associateDepartments.setContainment(false);
associateDepartments.setEType(departmentClass);
associateDepartments.setUpperBound(ETypedElement.UNBOUNDED_M ULTIPLICITY);
departmentClass.getEStructuralFeatures().add(associateDepart ments);
//Instanciating the model
EObject department = companyFactory.create(departmentClass);
department.eSet(departmentName, "ACME1");
@SuppressWarnings("unchecked")
List<EObject> associateDepartmentsList =
(List<EObject>)department.eGet(associateDepartments);
URI departmentURI =
URI.createFileURI(TestUtil.getPluginDirectory(AllSuites.PLUG IN_ID) +
"/department.xmi");
Resource departmentResource = new
XMIResourceFactoryImpl().createResource(departmentURI);
departmentResource.getContents().add(department);
EObject department1 = companyFactory.create(departmentClass);
department1.eSet(departmentName, "ACME1");
associateDepartmentsList.add(department1);
URI department1URI =
URI.createFileURI(TestUtil.getPluginDirectory(AllSuites.PLUG IN_ID) +
"/department1.xmi");
Resource department1Resource = new
XMIResourceFactoryImpl().createResource(department1URI);
department1Resource.getContents().add(department1);
EObject department2 = companyFactory.create(departmentClass);
department2.eSet(departmentName, "ACME2");
associateDepartmentsList.add(department2);
URI department2URI =
URI.createFileURI(TestUtil.getPluginDirectory(AllSuites.PLUG IN_ID) +
"/department2.xmi");
Resource department2Resource = new
XMIResourceFactoryImpl().createResource(department2URI);
department2Resource.getContents().add(department2);
//Saving
departmentResource.save(Collections.EMPTY_MAP);
assertTrue(new File(departmentURI.toFileString()).exists());
department1Resource.save(Collections.EMPTY_MAP);
assertTrue(new File(department1URI.toFileString()).exists());
department2Resource.save(Collections.EMPTY_MAP);
assertTrue(new File(department2URI.toFileString()).exists());
//Loading department into a resource set
ResourceSet resourceSet = new ResourceSetImpl();
if (!EMFPlugin.IS_ECLIPSE_RUNNING)
{
resourceSet.getResourceFactoryRegistry().getExtensionToFacto ryMap().put( "xmi",
new XMIResourceFactoryImpl());
}
Resource loadedResource = resourceSet.getResource(departmentURI,
true);
assertEquals(1, loadedResource.getContents().size());
EObject loadedDepartment = loadedResource.getContents().get(0);
@SuppressWarnings("unchecked")
BasicEList<EObject> loadedAssociateDepartmentsList =
(BasicEList<EObject>)loadedDepartment.eGet(associateDepartments, false);
assertEquals(2, loadedAssociateDepartmentsList.size());
EObject loadedDepartment1 =
loadedAssociateDepartmentsList.basicGet(0);
EObject loadedDepartment2 =
loadedAssociateDepartmentsList.basicGet(1);
assertTrue(loadedDepartment1.eIsProxy());
assertEquals(department1URI.toFileString(),
((InternalEObject)loadedDepartment1).eProxyURI().toFileStrin g());
assertNull(loadedDepartment1.eGet(departmentName));
assertTrue(loadedDepartment2.eIsProxy());
assertEquals(department2URI.toFileString(),
((InternalEObject)loadedDepartment2).eProxyURI().toFileStrin g());
assertNull(loadedDepartment2.eGet(departmentName));
//Resolving Proxy
loadedDepartment1 = EcoreUtil.resolve(loadedDepartment1,
resourceSet);
assertFalse(loadedDepartment1.eIsProxy());
assertEquals(department1.eGet(departmentName),
loadedDepartment1.eGet(departmentName));
loadedDepartment2 = EcoreUtil.resolve(loadedDepartment2,
resourceSet);
assertFalse(loadedDepartment2.eIsProxy());
assertEquals(department2.eGet(departmentName),
loadedDepartment2.eGet(departmentName));
//Deleting created files
new File(departmentURI.toFileString()).delete();
assertFalse(new File(departmentURI.toFileString()).exists());
new File(department1URI.toFileString()).delete();
assertFalse(new File(department1URI.toFileString()).exists());
new File(department2URI.toFileString()).delete();
assertFalse(new File(department2URI.toFileString()).exists());
}
/*
* Bugzilla 80110
*/
public void testTrackingModificaiton() throws Exception
{
Resource resource = new ResourceImpl()
{
@Override
protected void doSave(OutputStream outputStream, Map<?, ?>
options) throws IOException
{
// Ignore
}
};
assertFalse(resource.isTrackingModification());
assertFalse(resource.isModified());
EFactory companyFactory = companyPackage.getEFactoryInstance();
EObject employee = companyFactory.create(employeeClass);
EObject department = companyFactory.create(departmentClass);
@SuppressWarnings("unchecked")
List<EObject> employess =
((List<EObject>)department.eGet(departmentEmployees));
employess.add(employee);
resource.getContents().add(department);
assertFalse(resource.isTrackingModification());
assertFalse(resource.isModified());
resource.setTrackingModification(true);
assertTrue(resource.isTrackingModification());
assertFalse(resource.isModified());
employee.eSet(employeeName, "John");
assertTrue(resource.isTrackingModification());
assertTrue(resource.isModified());
resource.save(new ByteArrayOutputStream(), null);
assertTrue(resource.isTrackingModification());
assertFalse(resource.isModified());
resource.setTrackingModification(false);
assertFalse(resource.isTrackingModification());
assertFalse(resource.isModified());
employee.eSet(employeeName, "Joe");
assertEquals(resource, employee.eResource());
assertFalse(resource.isTrackingModification());
assertFalse(resource.isModified());
resource.setTrackingModification(true);
assertTrue(resource.isTrackingModification());
assertFalse(resource.isModified());
EObject employee1 = companyFactory.create(employeeClass);
EObject department1 = companyFactory.create(departmentClass);
@SuppressWarnings("unchecked")
List<EObject> department1Employees =
((List<EObject>)department1.eGet(departmentEmployees));
department1Employees.add(employee1);
resource.getContents().add(department1);
assertTrue(resource.isTrackingModification());
assertTrue(resource.isModified());
resource.save(new ByteArrayOutputStream(), null);
assertTrue(resource.isTrackingModification());
assertFalse(resource.isModified());
employee1.eSet(employeeName, "Mike");
assertTrue(resource.isTrackingModification());
assertTrue(resource.isModified());
resource.save(new ByteArrayOutputStream(), null);
assertTrue(resource.isTrackingModification());
assertFalse(resource.isModified());
resource.setTrackingModification(false);
assertFalse(resource.isTrackingModification());
assertFalse(resource.isModified());
employee1.eSet(employeeName, "Mark");
assertEquals(resource, employee1.eResource());
assertFalse(resource.isTrackingModification());
assertFalse(resource.isModified());
}
/*
* Bugzilla 106702
*/
public void testAddingDuplicates() throws Exception
{
EFactory companyFactory = companyPackage.getEFactoryInstance();
EObject department1 = companyFactory.create(departmentClass);
@SuppressWarnings("unchecked")
List<EObject> department1Employees =
(List<EObject>)department1.eGet(departmentEmployees);
assertEquals(0, department1Employees.size());
EObject employee1 = companyFactory.create(employeeClass);
department1Employees.add(employee1);
Exception exception = null;
try
{
department1Employees.add(0, null);
}
catch(IllegalArgumentException iae)
{
exception = iae;
}
assertNotNull(exception);
assertEquals(1, department1Employees.size());
assertEquals(0, department1Employees.indexOf(employee1));
Object[] data = ((BasicEList<?>)department1Employees).data();
assertEquals(employee1, data[0]);
assertNull(data[1]);
}
public void testRenamingStructuralFeature()
{
String COST_CENTER_NAME = "costCenter";
String CHANGED_COST_CENTER_NAME = "changedCostCenter";
EAttribute costCenterAttribute =
EcoreFactory.eINSTANCE.createEAttribute();
costCenterAttribute.setName( COST_CENTER_NAME );
departmentClass.getEStructuralFeatures().add( costCenterAttribute );
assertNotNull( departmentClass.getEStructuralFeature(
COST_CENTER_NAME ) );
costCenterAttribute.setName( CHANGED_COST_CENTER_NAME );
assertEquals( CHANGED_COST_CENTER_NAME,
costCenterAttribute.getName() );
assertNotNull( departmentClass.getEStructuralFeature(
CHANGED_COST_CENTER_NAME ) );
}
public void testRenamingClassifier()
{
assertNotNull(companyPackage.getEClassifier("Employee"));
employeeClass.setName("Employee1");
assertNotNull(companyPackage.getEClassifier("Employee1"));
}
}
Michael Lehmeier wrote:
> On 2008-01-15, Ed Merks <merks@ca.ibm.com> wrote:
>
>> Michael,
>>
>> Unfortunately I don't know much about ATL. :-(
>>
>> But they have an extremely active newgroup, so I've added it to the "to"
>> list of my reply...
>>
>
> The Problem isn't ATL-specific at all, so I thought the emf newsgroup
> was the right one, because it only touches emf-concerns.
>
> Once I have my model ready, I can deal with the ATL-part myself. (I
> hope)
>
>
--------------000003050301090002020102
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Michael,<br>
<br>
I didn't get the EMF part though. Here's an example of a JUnit test
that creates a Ecore model. Maybe that helps...<small></small><br>
<blockquote><small>package org.eclipse.emf.test.core.dynamic;</small><br>
<br>
<small>import java.io.ByteArrayOutputStream;</small><br>
<small>import java.io.File;</small><br>
<small>import java.io.IOException;</small><br>
<small>import java.io.OutputStream;</small><br>
<small>import java.util.Collections;</small><br>
<small>import java.util.HashMap;</small><br>
<small>import java.util.List;</small><br>
<small>import java.util.Map;</small><br>
<br>
<small>import junit.framework.Test;</small><br>
<small>import junit.framework.TestCase;</small><br>
<small>import junit.framework.TestSuite;</small><br>
<br>
<small>import org.eclipse.emf.common.EMFPlugin;</small><br>
<small>import org.eclipse.emf.common.util.BasicEList;</small><br>
<small>import org.eclipse.emf.common.util.URI;</small><br>
<small>import org.eclipse.emf.ecore.EAttribute;</small><br>
<small>import org.eclipse.emf.ecore.EClass;</small><br>
<small>import org.eclipse.emf.ecore.EFactory;</small><br>
<small>import org.eclipse.emf.ecore.EObject;</small><br>
<small>import org.eclipse.emf.ecore.EPackage;</small><br>
<small>import org.eclipse.emf.ecore.EReference;</small><br>
<small>import org.eclipse.emf.ecore.ETypedElement;</small><br>
<small>import org.eclipse.emf.ecore.EcoreFactory;</small><br>
<small>import org.eclipse.emf.ecore.EcorePackage;</small><br>
<small>import org.eclipse.emf.ecore.InternalEObject;</small><br>
<small>import org.eclipse.emf.ecore.resource.Resource;</small><br>
<small>import org.eclipse.emf.ecore.resource.ResourceSet;</small><br>
<small>import org.eclipse.emf.ecore.resource.impl.ResourceImpl;</small><br >
<small>import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;</small ><br>
<small>import org.eclipse.emf.ecore.util.EcoreUtil;</small><br>
<small>import org.eclipse.emf.ecore.xmi.XMLResource;</small><br>
<small>import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;</small ><br>
<small>import org.eclipse.emf.test.common.TestUtil;</small><br>
<small>import org.eclipse.emf.test.core.AllSuites;</small><br>
<br>
<br>
<small>public class SimpleModelTest extends TestCase</small><br>
<small>{</small><br>
<small> private EPackage companyPackage;</small><br>
<br>
<small> private EClass employeeClass;</small><br>
<br>
<small> private EAttribute employeeName;</small><br>
<br>
<small> private EAttribute employeeManager;</small><br>
<br>
<small> private EClass departmentClass;</small><br>
<br>
<small> private EAttribute departmentName;</small><br>
<br>
<small> private EAttribute departmentNumber;</small><br>
<br>
<small> private EReference departmentEmployees;</small><br>
<br>
<small> public SimpleModelTest(String name)</small><br>
<small> {</small><br>
<small> super(name);</small><br>
<small> }</small><br>
<br>
<small> public static Test suite()</small><br>
<small> {</small><br>
<small> TestSuite ts = new TestSuite("SimpleModelTest");</small><br>
<small> ts.addTest(new SimpleModelTest("testPackageAndFactory"));</small><br>
<small> ts.addTest(new SimpleModelTest("testAttributes"));</small><br>
<small> ts.addTest(new SimpleModelTest("testReference"));</small><br>
<small> ts.addTest(new SimpleModelTest("testMetaData"));</small><br>
<small> ts.addTest(new SimpleModelTest("testSaveAndLoad"));</small><br>
<small> ts.addTest(new SimpleModelTest("testSaveAndLoadZip"));</small><br>
<small> ts.addTest(new SimpleModelTest("testProxy"));</small><br>
<small> ts.addTest(new
SimpleModelTest("testTrackingModificaiton"));</small><br >
<small> ts.addTest(new SimpleModelTest("testAddingDuplicates"));</small><br>
<small> ts.addTest(new
SimpleModelTest("testRenamingStructuralFeature"));</small ><br>
<small> ts.addTest(new SimpleModelTest("testRenamingClassifier"));</small><br>
<small> return ts;</small><br>
<small> }</small><br>
<br>
<small> /**</small><br>
<small> * @see junit.framework.TestCase#setUp()</small><br>
<small> */</small><br>
<small> @Override</small><br>
<small> protected void setUp() throws Exception</small><br>
<small> {</small><br>
<small> EcoreFactory ecoreFactory = EcoreFactory.eINSTANCE;</small><br>
<small> EcorePackage ecorePackage = EcorePackage.eINSTANCE;</small><br>
<br>
<small> employeeClass = ecoreFactory.createEClass();</small><br>
<small> employeeClass.setName("Employee");</small><br>
<br>
<small> employeeName = ecoreFactory.createEAttribute();</small><br>
<small> employeeName.setName("name");</small><br>
<small> employeeName.setEType(ecorePackage.getEString());</small><br >
<small> employeeClass.getEStructuralFeatures().add(employeeName);</small ><br>
<br>
<small> employeeManager = ecoreFactory.createEAttribute();</small><br>
<small> employeeManager.setName("manager");</small><br>
<small> employeeManager.setEType(ecorePackage.getEBoolean());</small ><br>
<small>
employeeClass.getEStructuralFeatures().add(employeeManager); </small><br>
<br>
<small> departmentClass = ecoreFactory.createEClass();</small><br>
<small> departmentClass.setName("Department");</small><br>
<br>
<small> departmentName = ecoreFactory.createEAttribute();</small><br>
<small> departmentName.setName("name");</small><br>
<small> departmentName.setEType(ecorePackage.getEString());</small> <br>
<small>
departmentClass.getEStructuralFeatures().add(departmentName) ; </small><br>
<br>
<small> departmentNumber = ecoreFactory.createEAttribute();</small><br>
<small> departmentNumber.setName("number");</small><br>
<small> departmentNumber.setEType(ecorePackage.getEInt());</small><br >
<small>
departmentClass.getEStructuralFeatures().add(departmentNumbe r); </small><br>
<br>
<small> departmentEmployees = ecoreFactory.createEReference();</small><br>
<small> departmentEmployees.setName("employees");</small><br>
<small> departmentEmployees.setEType(employeeClass);</small><br>
<small>
departmentEmployees.setUpperBound(ETypedElement.UNBOUNDED_MU LTIPLICITY); </small><br>
<small> departmentEmployees.setContainment(true);</small><br>
<small>
departmentClass.getEStructuralFeatures().add(departmentEmplo yees); </small><br>
<br>
<small> companyPackage = ecoreFactory.createEPackage();</small><br>
<small> companyPackage.setName("company");</small><br>
<small> companyPackage.setNsPrefix("company");</small><br>
<small>
companyPackage.setNsURI(<a class="moz-txt-link-rfc2396E" href="http:///com.example.company.ecore">"http:///com.example.company.ecore"</a>);</small><br>
<small> companyPackage.getEClassifiers().add(employeeClass);</small ><br>
<small> companyPackage.getEClassifiers().add(departmentClass);</small ><br>
<small> EPackage.Registry.INSTANCE.put(companyPackage.getNsURI(),
companyPackage);</small><br>
<small> }</small><br>
<br>
<small> /**</small><br>
<small> * @see junit.framework.TestCase#tearDown()</small><br>
<small> */</small><br>
<small> @Override</small><br>
<small> protected void tearDown() throws Exception</small><br>
<small> {</small><br>
<small> employeeName = null;</small><br>
<small> employeeManager = null;</small><br>
<small> employeeClass.getEStructuralFeatures().clear();</small><br >
<small> employeeClass = null;</small><br>
<br>
<small> departmentEmployees = null;</small><br>
<br>
<small> departmentName = null;</small><br>
<small> departmentNumber = null;</small><br>
<small> departmentClass.getEStructuralFeatures().clear();</small><br >
<small> departmentClass = null;</small><br>
<br>
<small> companyPackage = null;</small><br>
<small> }</small><br>
<br>
<small> public void testPackageAndFactory()</small><br>
<small> {</small><br>
<small> assertNotNull(companyPackage);</small><br>
<small> assertEquals("company", companyPackage.getName());</small><br>
<small> assertEquals("company", companyPackage.getNsPrefix());</small><br>
<small> assertEquals(<a class="moz-txt-link-rfc2396E" href="http:///com.example.company.ecore">"http:///com.example.company.ecore"</a>,
companyPackage.getNsURI());</small><br>
<br>
<small> EFactory companyFactory =
companyPackage.getEFactoryInstance();</small><br>
<small> assertNotNull(companyFactory);</small><br>
<small> assertEquals(companyPackage, companyFactory.getEPackage());</small><br>
<br>
<small> EObject employee = companyFactory.create(employeeClass);</small><br>
<small> assertNotNull(employee);</small><br>
<small> assertEquals(companyPackage,
employee.eClass().getEPackage());</small><br>
<small> }</small><br>
<br>
<small> public void testAttributes()</small><br>
<small> {</small><br>
<small> EFactory companyFactory =
companyPackage.getEFactoryInstance();</small><br>
<br>
<small> EObject employee1 = companyFactory.create(employeeClass);</small><br>
<small> employee1.eSet(employeeName, "John");</small><br>
<small> assertEquals("John", employee1.eGet(employeeName));</small><br>
<small> assertEquals(Boolean.FALSE,
employee1.eGet(employeeManager));</small><br>
<br>
<small> EObject employee2 = companyFactory.create(employeeClass);</small><br>
<small> employee2.eSet(employeeName, "Katherine");</small><br>
<small> assertEquals("Katherine", employee2.eGet(employeeName));</small><br>
<small> employee2.eSet(employeeManager, Boolean.TRUE);</small><br>
<small> assertEquals(Boolean.TRUE,
employee2.eGet(employeeManager));</small><br>
<br>
<small> EObject department =
companyFactory.create(departmentClass);</small><br>
<small> department.eSet(departmentName, "ABC");</small><br>
<small> assertEquals("ABC", department.eGet(departmentName));</small><br>
<small> department.eSet(departmentNumber, new Integer(123));</small><br>
<small> assertEquals(new Integer(123),
department.eGet(departmentNumber));</small><br>
<small> }</small><br>
<br>
<small> public void testReference()</small><br>
<small> {</small><br>
<small> EFactory companyFactory =
companyPackage.getEFactoryInstance();</small><br>
<br>
<small> EObject employee1 = companyFactory.create(employeeClass);</small><br>
<small> EObject employee2 = companyFactory.create(employeeClass);</small><br>
<br>
<small> EObject department1 =
companyFactory.create(departmentClass);</small><br>
<br>
<small> @SuppressWarnings("unchecked")</small><br>
<small> List<EObject> department1Employees =
(List<EObject>)department1.eGet(departmentEmpl oyees); </small><br>
<small> assertNotNull(department1Employees);</small><br>
<br>
<small> department1Employees.add(employee1);</small><br>
<small> assertEquals(1, department1Employees.size());</small><br>
<small> assertEquals(employee1, department1Employees.get(0));</small><br>
<small> assertEquals(department1Employees,
department1.eGet(departmentEmployees));</small><br>
<br>
<small> department1Employees.add(employee2);</small><br>
<small> assertEquals(2, department1Employees.size());</small><br>
<small> assertEquals(employee2, department1Employees.get(1));</small><br>
<small> assertEquals(department1Employees,
department1.eGet(departmentEmployees));</small><br>
<br>
<small> //Should not affect the list</small><br>
<small> department1Employees.add(employee2);</small><br>
<small> assertEquals(2, department1Employees.size());</small><br>
<small> assertEquals(employee2, department1Employees.get(1));</small><br>
<small> assertEquals(department1Employees,
department1.eGet(departmentEmployees));</small><br>
<br>
<small> EObject department2 =
companyFactory.create(departmentClass);</small><br>
<br>
<small> @SuppressWarnings("unchecked")</small><br>
<small> List<EObject> department2Employees =
(List<EObject>)department2.eGet(departmentEmpl oyees); </small><br>
<small> assertNotNull(department2Employees);</small><br>
<br>
<small> department2Employees.add(employee1);</small><br>
<small> assertEquals(1, department2Employees.size());</small><br>
<small> assertEquals(employee1, department2Employees.get(0));</small><br>
<small> assertEquals(department2Employees,
department2.eGet(departmentEmployees));</small><br>
<br>
<small> //since departmentEmployees is an aggregation, employee1
should be removed</small><br>
<small> //from department1Employees</small><br>
<small> assertEquals(1, department1Employees.size());</small><br>
<small> assertEquals(employee2, department1Employees.get(0));</small><br>
<small> assertEquals(department1Employees,
department1.eGet(departmentEmployees));</small><br>
<small> }</small><br>
<br>
<small> public void testMetaData()</small><br>
<small> {</small><br>
<small> EFactory companyFactory =
companyPackage.getEFactoryInstance();</small><br>
<br>
<small> EObject employee1 = companyFactory.create(employeeClass);</small><br>
<small> EObject employee2 = companyFactory.create(employeeClass);</small><br>
<br>
<small> EObject department1 =
companyFactory.create(departmentClass);</small><br>
<small> @SuppressWarnings("unchecked")</small><br>
<small> List<EObject> department1Employees =
(List<EObject>)department1.eGet(departmentEmpl oyees); </small><br>
<small> EObject department2 =
companyFactory.create(departmentClass);</small><br>
<small> @SuppressWarnings("unchecked")</small><br>
<small> List<EObject> department2Employees =
(List<EObject>)department2.eGet(departmentEmpl oyees); </small><br>
<br>
<small> department1Employees.add(employee1);</small><br>
<small> assertEquals(department1, employee1.eContainer());</small><br>
<small> assertEquals(department1Employees.size(),
department1.eContents().size());</small><br>
<br>
<small> department1Employees.add(employee2);</small><br>
<small> assertEquals(department1, employee1.eContainer());</small><br>
<small> assertEquals(department1Employees.size(),
department1.eContents().size());</small><br>
<br>
<small> department2Employees.add(employee1);</small><br>
<small> assertEquals(department2, employee1.eContainer());</small><br>
<small> assertEquals(department1Employees.size(),
department1.eContents().size());</small><br>
<small> assertEquals(department2Employees.size(),
department2.eContents().size());</small><br>
<small> }</small><br>
<small> </small><br>
<small> public void testSaveAndLoadZip() throws Exception</small><br>
<small> {</small><br>
<small> //Instanciating the model</small><br>
<small> EFactory companyFactory =
companyPackage.getEFactoryInstance();</small><br>
<br>
<small> EObject employee1 = companyFactory.create(employeeClass);</small><br>
<small> employee1.eSet(employeeName, "John");</small><br>
<small> EObject employee2 = companyFactory.create(employeeClass);</small><br>
<small> employee2.eSet(employeeName, "Jane");</small><br>
<br>
<small> EObject department =
companyFactory.create(departmentClass);</small><br>
<small> department.eSet(departmentName, "ACME1");</small><br>
<small> @SuppressWarnings("unchecked")</small><br>
<small> List<EObject> employeesList =
(List<EObject>)department.eGet(departmentEmplo yees); </small><br>
<small> employeesList.add(employee1);</small><br>
<small> employeesList.add(employee2);</small><br>
<br>
<small> URI departmentsURI =
URI.createFileURI(TestUtil.getPluginDirectory(AllSuites.PLUG IN_ID) +
"/departments.dept");</small><br>
<small> XMLResource departmentsResource = (XMLResource)new
XMIResourceFactoryImpl().createResource(departmentsURI);</small ><br>
<small> departmentsResource.setUseZip(true);</small><br>
<small> departmentsResource.getContents().add(department);</small><br >
<br>
<small> //Saving</small><br>
<small> departmentsResource.save(Collections.EMPTY_MAP);</small><br >
<small> assertTrue(new
File(departmentsURI.toFileString()).exists());</small><br>
<br>
<small> //Loading department in ResourceSet</small><br>
<small> ResourceSet resourceSet = new ResourceSetImpl();</small><br>
<small>
resourceSet.getResourceFactoryRegistry().getExtensionToFacto ryMap().put( "dept",
new XMIResourceFactoryImpl()</small><br>
<small> {</small><br>
<small> /* (non-Javadoc)</small><br>
<small> * @see
org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl#create Resource(org.eclipse.emf.common.util.URI) </small><br>
<small> */</small><br>
<small> @Override</small><br>
<small> public Resource createResource(URI uri)</small><br>
<small> {</small><br>
<small> XMLResource result =
(XMLResource)super.createResource(uri);</small><br>
<small> result.setUseZip(true);</small><br>
<small> return result;</small><br>
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
| |
Goto Forum:
Current Time: Thu Apr 25 18:56:40 GMT 2024
Powered by FUDForum. Page generated in 0.03224 seconds
|