/** * Updates {@link IEObjectLocation#getSavedURIFragment() saved URI fragment}. * * @param container * the {@link ILocationContainer} * @param eObjectContainer * the {@link IEObjectContainer} * @param newResource * the new {@link Resource} * @throws Exception * if the XMI serialization failed or elements couldn't be created */ private static void updateSavedURIFragment(ILocationContainer container, IEObjectContainer eObjectContainer, Resource newResource) throws Exception { final IBase base = MappingUtils.getBase(container); final Copier copier = new Copier(); final Collection<EObject> copiedContents = copier.copyAll(newResource.getContents()); copier.copyReferences(); final XMIResourceImpl newXMIResource = new XMIResourceImpl(); newXMIResource.getContents().addAll(copiedContents); for (Entry<EObject, EObject> entry : copier.entrySet()) { final EObject newEObject = entry.getKey(); final EObject savedEObject = entry.getValue(); final ICouple couple = base.getFactory().createElement(ICouple.class); couple.setKey(newResource.getURIFragment(newEObject)); couple.setValue(newXMIResource.getURIFragment(savedEObject)); eObjectContainer.getSavedURIFragments().add(couple); } }
@Override public BTSConfiguration createNew(BTSConfiguration originalconfiguration) { BTSConfiguration config = createNew(); String dbcoll = config.getDBCollectionKey(); Copier copier = new Copier(); config = (BTSConfiguration) copier.copy(originalconfiguration); // copyChildrenRecursively(config, originalconfiguration, copier); // config = EmfModelHelper.mergeChanges(config, originalconfiguration); config.getRevisions().clear(); config.getReaders().clear(); config.getUpdaters().clear(); config.setDBCollectionKey(dbcoll); config.setProject(main_project); super.setId(config, config.getDBCollectionKey()); super.setRevision(config); return config; }
@Override public E reload(K key, String path) { URI uri = URI.createURI(getRemoteDBURL() + "/" + path + "/" + key.toString()); Resource resource = connectionProvider.getEmfResourceSet().getResource(uri, true); EObject eObject = resource.getContents().get(0); Copier copier = new Copier(); EClass eClass = eObject.eClass(); if (resource.getContents().size() > 0) { ResourceSet tempResourceSet = new ResourceSetImpl(); tempResourceSet.getURIConverter().getURIHandlers().add(0, new CouchDBHandler()); Resource tempResource = tempResourceSet.getResource(uri, true); EObject copyEObject = tempResource.getContents().get(0); if (copyEObject instanceof BTSDBBaseObject) { eObject = EmfModelHelper.mergeChanges(eObject, copyEObject); return (E) eObject; } } return null; }
private static BinaryTemporalConstraint makeCopyOfBinaryConstraintForNewPlan( BinaryTemporalConstraint constraint, Map<String, EPlanChild> idsInDestination) { BinaryTemporalConstraint result = (BinaryTemporalConstraint) new Copier().copy(constraint); EPlanChild newA = idsInDestination.get(getDiffId(constraint.getPointA().getElement())); if (newA == null) newA = getSameActivityFromPlanMap(idsInDestination, constraint.getPointA().getElement()); EPlanChild newB = idsInDestination.get(getDiffId(constraint.getPointB().getElement())); if (newB == null) newB = getSameActivityFromPlanMap(idsInDestination, constraint.getPointB().getElement()); if (newA == null || newB == null) return null; result.getPointA().setElement(newA); result.getPointB().setElement(newB); return result; }
@Test public void initLocationEObject() throws Exception { final IBase base = new BaseRegistryTests.TestBase(); base.getFactory().addDescriptor(ICouple.class, new BaseElementFactory.FactoryDescriptor<TestCouple>( TestCouple.class)); final TestEObjectContainerLocation container = new TestEObjectContainerLocation(); container.setContainer(base); final Copier copier = new Copier(); final List<EObject> eObjects = new ArrayList<EObject>(copier.copyAll(AnydslPackage.eINSTANCE .eContents())); copier.copyReferences(); final EClass producerEClass = (EClass)copier.get(AnydslPackage.eINSTANCE.getProducer()); final Resource resource = createResource("test.xmi"); resource.getContents().addAll(eObjects); container.setResource(resource); eObjectContainerHelper.updateEObjectContainer(container.getContainer(), container, resource); final IEObjectLocation location = new TestEObjectLocation(); location.setContainer(container); final TestEObjectContainerConnector testEObjectContainerConnector = new TestEObjectContainerConnector(); MappingUtils.getConnectorRegistry().register(testEObjectContainerConnector); final EObjectConnector eObjectConnector = new EObjectConnector(); MappingUtils.getConnectorRegistry().register(eObjectConnector); super.initLocation(container, location, producerEClass); final Object element = eObjectConnector.getElement(location); assertTrue(element instanceof EObject); assertEquals(producerEClass, element); assertNull(location.getFeatureName()); MappingUtils.getConnectorRegistry().unregister(testEObjectContainerConnector); MappingUtils.getConnectorRegistry().unregister(eObjectConnector); }
@Test public void initLocationSetting() throws Exception { final IBase base = new BaseRegistryTests.TestBase(); base.getFactory().addDescriptor(ICouple.class, new BaseElementFactory.FactoryDescriptor<TestCouple>( TestCouple.class)); final TestEObjectContainerLocation container = new TestEObjectContainerLocation(); container.setContainer(base); final Copier copier = new Copier(); final List<EObject> eObjects = new ArrayList<EObject>(copier.copyAll(AnydslPackage.eINSTANCE .eContents())); copier.copyReferences(); final EClass producerEClass = (EClass)copier.get(AnydslPackage.eINSTANCE.getProducer()); final Resource resource = createResource("test.xmi"); resource.getContents().addAll(eObjects); container.setResource(resource); eObjectContainerHelper.updateEObjectContainer(container.getContainer(), container, resource); final IEObjectLocation location = new TestEObjectLocation(); location.setContainer(container); final TestEObjectContainerConnector testEObjectContainerConnector = new TestEObjectContainerConnector(); MappingUtils.getConnectorRegistry().register(testEObjectContainerConnector); final EObjectConnector eObjectConnector = new EObjectConnector(); MappingUtils.getConnectorRegistry().register(eObjectConnector); super.initLocation(container, location, ((InternalEObject)producerEClass).eSetting( EcorePackage.eINSTANCE.getENamedElement_Name())); final Object element = eObjectConnector.getElement(location); assertTrue(element instanceof Setting); assertEquals(producerEClass, ((Setting)element).getEObject()); assertEquals(EcorePackage.eINSTANCE.getENamedElement_Name(), ((Setting)element) .getEStructuralFeature()); assertEquals("Producer", ((Setting)element).get(true)); assertEquals(EcorePackage.eINSTANCE.getENamedElement_Name().getName(), location.getFeatureName()); MappingUtils.getConnectorRegistry().unregister(testEObjectContainerConnector); MappingUtils.getConnectorRegistry().unregister(eObjectConnector); }
@Test public void getLocationEObject() throws Exception { final IBase base = new BaseRegistryTests.TestBase(); base.getFactory().addDescriptor(ICouple.class, new BaseElementFactory.FactoryDescriptor<TestCouple>( TestCouple.class)); final TestEObjectContainerLocation container = new TestEObjectContainerLocation(); container.setContainer(base); final Copier copier = new Copier(); final List<EObject> eObjects = new ArrayList<EObject>(copier.copyAll(AnydslPackage.eINSTANCE .eContents())); copier.copyReferences(); final EClass producerEClass = (EClass)copier.get(AnydslPackage.eINSTANCE.getProducer()); final Resource resource = createResource("test.xmi"); resource.getContents().addAll(eObjects); container.setResource(resource); eObjectContainerHelper.updateEObjectContainer(container.getContainer(), container, resource); final IEObjectLocation location = new TestEObjectLocation(); location.setContainer(container); container.getContents().add(location); final TestEObjectContainerConnector connector = new TestEObjectContainerConnector(); MappingUtils.getConnectorRegistry().register(connector); super.initLocation(container, location, producerEClass); assertEquals(location, super.getLocation(container, producerEClass)); assertEquals(null, super.getLocation(container, ((InternalEObject)producerEClass).eSetting( EcorePackage.eINSTANCE.getENamedElement_Name()))); MappingUtils.getConnectorRegistry().unregister(connector); }
@Test public void getLocationSetting() throws Exception { final IBase base = new BaseRegistryTests.TestBase(); base.getFactory().addDescriptor(ICouple.class, new BaseElementFactory.FactoryDescriptor<TestCouple>( TestCouple.class)); final TestEObjectContainerLocation container = new TestEObjectContainerLocation(); container.setContainer(base); final Copier copier = new Copier(); final List<EObject> eObjects = new ArrayList<EObject>(copier.copyAll(AnydslPackage.eINSTANCE .eContents())); final EClass producerEClass = (EClass)copier.get(AnydslPackage.eINSTANCE.getProducer()); copier.copyReferences(); final Resource resource = createResource("test.xmi"); resource.getContents().addAll(eObjects); container.setResource(resource); eObjectContainerHelper.updateEObjectContainer(container.getContainer(), container, resource); final IEObjectLocation location = new TestEObjectLocation(); location.setContainer(container); container.getContents().add(location); final TestEObjectContainerConnector connector = new TestEObjectContainerConnector(); MappingUtils.getConnectorRegistry().register(connector); super.initLocation(container, location, ((InternalEObject)producerEClass).eSetting( EcorePackage.eINSTANCE.getENamedElement_Name())); assertEquals(null, super.getLocation(container, producerEClass)); assertEquals(location, super.getLocation(container, ((InternalEObject)producerEClass).eSetting( EcorePackage.eINSTANCE.getENamedElement_Name()))); MappingUtils.getConnectorRegistry().unregister(connector); }
@Test public void updateEObjects() throws Exception { final IBase base = new BaseRegistryTests.TestBase(); base.getFactory().addDescriptor(IEObjectLocation.class, new BaseElementFactory.FactoryDescriptor<TestEObjectLocation>(TestEObjectLocation.class)); base.getFactory().addDescriptor(ICouple.class, new BaseElementFactory.FactoryDescriptor<TestCouple>( TestCouple.class)); final TestEObjectContainerLocation container = new TestEObjectContainerLocation(); container.setContainer(base); final Copier copier = new Copier(); EPackage copy = (EPackage)copier.copy(AnydslPackage.eINSTANCE); copier.copyReferences(); final List<EObject> testEObjects = new ArrayList<EObject>(copy.getEClassifiers()); final Resource initialResource = createResource("initialResource"); container.setResource(initialResource); initialResource.getContents().addAll(testEObjects); eObjectContainerHelper.updateEObjectContainer(container.getContainer(), container, initialResource); final IEObjectLocation location = createEObjectLocation(copier, container); final EObject expectedEObject = copier.get(original[0]); final EStructuralFeature expectedFeature = (EStructuralFeature)original[1]; final Object expectedValue; if (original[2] instanceof EObject) { expectedValue = copier.get(original[2]); } else { expectedValue = original[2]; } List<EObject> newEObjects = testEObjects; final Resource newResource = createResource("newResource"); newResource.getContents().addAll(newEObjects); eObjectContainerHelper.updateEObjectContainer(container.getContainer(), container, newResource); container.setResource(newResource); assertEObjectLocation(location, expectedEObject, expectedFeature, expectedValue); }
@Test public void updateEObjectsPlusShift() throws Exception { final IBase base = new BaseRegistryTests.TestBase(); base.getFactory().addDescriptor(IEObjectLocation.class, new BaseElementFactory.FactoryDescriptor<TestEObjectLocation>(TestEObjectLocation.class)); base.getFactory().addDescriptor(ICouple.class, new BaseElementFactory.FactoryDescriptor<TestCouple>( TestCouple.class)); final TestEObjectContainerLocation container = new TestEObjectContainerLocation(); container.setContainer(base); final Copier copier = new Copier(); EPackage copy = (EPackage)copier.copy(AnydslPackage.eINSTANCE); copier.copyReferences(); final List<EObject> testEObjects = new ArrayList<EObject>(copy.getEClassifiers()); final Resource initialResource = createResource("initialResource"); container.setResource(initialResource); initialResource.getContents().addAll(testEObjects); eObjectContainerHelper.updateEObjectContainer(container.getContainer(), container, initialResource); final IEObjectLocation location = createEObjectLocation(copier, container); final EObject expectedEObject = copier.get(original[0]); final EStructuralFeature expectedFeature = (EStructuralFeature)original[1]; final Object expectedValue; if (original[2] instanceof EObject) { expectedValue = copier.get(original[2]); } else { expectedValue = original[2]; } List<EObject> newEObjects = new ArrayList<EObject>(MappingPackage.eINSTANCE.getEClassifiers()); newEObjects.addAll(testEObjects); final Resource newResource = createResource("newResource"); newResource.getContents().addAll(newEObjects); eObjectContainerHelper.updateEObjectContainer(container.getContainer(), container, newResource); container.setResource(newResource); assertEObjectLocation(location, expectedEObject, expectedFeature, expectedValue); }
@Test public void updateEObjectsMinusShift() throws Exception { final IBase base = new BaseRegistryTests.TestBase(); base.getFactory().addDescriptor(IEObjectLocation.class, new BaseElementFactory.FactoryDescriptor<TestEObjectLocation>(TestEObjectLocation.class)); base.getFactory().addDescriptor(ICouple.class, new BaseElementFactory.FactoryDescriptor<TestCouple>( TestCouple.class)); final TestEObjectContainerLocation container = new TestEObjectContainerLocation(); container.setContainer(base); final Copier copier = new Copier(); EPackage copy = (EPackage)copier.copy(AnydslPackage.eINSTANCE); copier.copyReferences(); final List<EObject> testEObjects = new ArrayList<EObject>(copy.getEClassifiers()); final Resource initialResource = createResource("initialResource"); container.setResource(initialResource); initialResource.getContents().addAll(testEObjects); eObjectContainerHelper.updateEObjectContainer(container.getContainer(), container, initialResource); final IEObjectLocation location = createEObjectLocation(copier, container); final EObject expectedEObject = copier.get(original[0]); final EStructuralFeature expectedFeature = (EStructuralFeature)original[1]; final Object expectedValue; if (original[2] instanceof EObject) { expectedValue = copier.get(original[2]); } else { expectedValue = original[2]; } List<EObject> newEObjects = new ArrayList<EObject>(testEObjects); newEObjects.remove(0); newEObjects.remove(0); final Resource newResource = createResource("newResource"); newResource.getContents().addAll(newEObjects); eObjectContainerHelper.updateEObjectContainer(container.getContainer(), container, newResource); container.setResource(newResource); assertEObjectLocation(location, expectedEObject, expectedFeature, expectedValue); }
@Test public void updateEObjectsRemoved() throws Exception { final IBase base = new BaseRegistryTests.TestBase(); base.getFactory().addDescriptor(IEObjectLocation.class, new BaseElementFactory.FactoryDescriptor<TestEObjectLocation>(TestEObjectLocation.class)); base.getFactory().addDescriptor(ICouple.class, new BaseElementFactory.FactoryDescriptor<TestCouple>( TestCouple.class)); final TestEObjectContainerLocation container = new TestEObjectContainerLocation(); container.setContainer(base); final Copier copier = new Copier(); EPackage copy = (EPackage)copier.copy(AnydslPackage.eINSTANCE); copier.copyReferences(); final List<EObject> testEObjects = new ArrayList<EObject>(copy.getEClassifiers()); final Resource initialResource = createResource("initialResource"); container.setResource(initialResource); initialResource.getContents().addAll(testEObjects); eObjectContainerHelper.updateEObjectContainer(container.getContainer(), container, initialResource); final IEObjectLocation location = createEObjectLocation(copier, container); final EObject expectedEObject = null; final EStructuralFeature expectedFeature = null; final Object expectedValue = null; List<EObject> newEObjects = testEObjects; if (testEObjects.contains(copier.get(original[0]))) { testEObjects.remove(copier.get(original[0])); } else { EcoreUtil.delete(copier.get(original[0])); } final Resource newResource = createResource("newResource"); newResource.getContents().addAll(newEObjects); eObjectContainerHelper.updateEObjectContainer(container.getContainer(), container, newResource); container.setResource(newResource); assertEObjectLocation(location, expectedEObject, expectedFeature, expectedValue); }
@Test public void updateEObjectsPlusShiftRemoved() throws Exception { final IBase base = new BaseRegistryTests.TestBase(); base.getFactory().addDescriptor(IEObjectLocation.class, new BaseElementFactory.FactoryDescriptor<TestEObjectLocation>(TestEObjectLocation.class)); base.getFactory().addDescriptor(ICouple.class, new BaseElementFactory.FactoryDescriptor<TestCouple>( TestCouple.class)); final TestEObjectContainerLocation container = new TestEObjectContainerLocation(); container.setContainer(base); final Copier copier = new Copier(); EPackage copy = (EPackage)copier.copy(AnydslPackage.eINSTANCE); copier.copyReferences(); final List<EObject> testEObjects = new ArrayList<EObject>(copy.getEClassifiers()); final Resource initialResource = createResource("initialResource"); container.setResource(initialResource); initialResource.getContents().addAll(testEObjects); eObjectContainerHelper.updateEObjectContainer(container.getContainer(), container, initialResource); final IEObjectLocation location = createEObjectLocation(copier, container); final EObject expectedEObject = null; final EStructuralFeature expectedFeature = null; final Object expectedValue = null; List<EObject> newEObjects = testEObjects; if (testEObjects.contains(copier.get(original[0]))) { testEObjects.remove(copier.get(original[0])); } else { EcoreUtil.delete(copier.get(original[0])); } newEObjects.addAll(0, MappingPackage.eINSTANCE.getEClassifiers()); final Resource newResource = createResource("newResource"); newResource.getContents().addAll(newEObjects); eObjectContainerHelper.updateEObjectContainer(container.getContainer(), container, newResource); container.setResource(newResource); assertEObjectLocation(location, expectedEObject, expectedFeature, expectedValue); }
@Test public void updateEObjectsMinusShiftRemoved() throws Exception { final IBase base = new BaseRegistryTests.TestBase(); base.getFactory().addDescriptor(IEObjectLocation.class, new BaseElementFactory.FactoryDescriptor<TestEObjectLocation>(TestEObjectLocation.class)); base.getFactory().addDescriptor(ICouple.class, new BaseElementFactory.FactoryDescriptor<TestCouple>( TestCouple.class)); final TestEObjectContainerLocation container = new TestEObjectContainerLocation(); container.setContainer(base); final Copier copier = new Copier(); EPackage copy = (EPackage)copier.copy(AnydslPackage.eINSTANCE); copier.copyReferences(); final List<EObject> testEObjects = new ArrayList<EObject>(copy.getEClassifiers()); final Resource initialResource = createResource("initialResource"); container.setResource(initialResource); initialResource.getContents().addAll(testEObjects); eObjectContainerHelper.updateEObjectContainer(container.getContainer(), container, initialResource); final IEObjectLocation location = createEObjectLocation(copier, container); final EObject expectedEObject = null; final EStructuralFeature expectedFeature = null; final Object expectedValue = null; List<EObject> newEObjects = testEObjects; if (testEObjects.contains(copier.get(original[0]))) { testEObjects.remove(copier.get(original[0])); } else { EcoreUtil.delete(copier.get(original[0])); } newEObjects.remove(0); newEObjects.remove(0); final Resource newResource = createResource("newResource"); newResource.getContents().addAll(newEObjects); eObjectContainerHelper.updateEObjectContainer(container.getContainer(), container, newResource); container.setResource(newResource); assertEObjectLocation(location, expectedEObject, expectedFeature, expectedValue); }
@Override public E reload(K key, String path) { URI uri = URI.createURI(getLocalDBURL() + "/" + path + "/" + key.toString()); // Resource resource = connectionProvider.getEmfResourceSet().getResource(uri, true); // URI uri = URI.createURI(getLocalDBURL() + "/" + dbPath + "/" + objectId); Map map = ((ResourceSetImpl) connectionProvider.getEmfResourceSet()).getURIResourceMap(); if (map.containsKey(uri)) { Resource resource = (Resource) map.get(uri); EObject eObject = resource.getContents().get(0); Copier copier = new Copier(); EClass eClass = eObject.eClass(); if (resource.getContents().size() > 0) { ResourceSet tempResourceSet = new ResourceSetImpl(); tempResourceSet.getURIConverter().getURIHandlers() .add(0, new CouchDBHandler()); Resource tempResource = tempResourceSet.getResource(uri, true); // if empty return old if (tempResource.getContents().isEmpty()) return (E) eObject; EObject copyEObject = tempResource.getContents().get(0); if (copyEObject instanceof BTSDBBaseObject) { eObject = EmfModelHelper.mergeChanges(eObject, copyEObject); checkForConflicts( (E) eObject, path); return (E) eObject; } } } else { return find(key, path); } return null; }
public static <T extends EObject> T mergeChanges(T target, T objectWithChanges) { Copier copier = new Copier(); for (int i = 0, size = target.eClass().getFeatureCount(); i < size; ++i) { EStructuralFeature eStructuralFeature = target.eClass().getEStructuralFeature(i); if (eStructuralFeature.isChangeable() && !eStructuralFeature.isDerived()) { if (eStructuralFeature instanceof EAttribute) { // if (eStructuralFeature.getName().equals("name")) // { // System.out.println("name"); // } copyAttribute((EAttribute) eStructuralFeature, objectWithChanges, target, copier); } else { EReference eReference = (EReference) eStructuralFeature; if (eReference.isContainment()) { copyContainment(eReference, objectWithChanges, target, copier); } } } } return (T) target; }
private static Copier createModelResource(Copier copier, ResourceSet currentResourceSet, Resource historicResource) { Resource newResource = currentResourceSet.getResource(historicResource.getURI(), true); newResource.getContents().clear(); newResource.getContents().addAll(copier.copyAll(historicResource.getContents())); return copier; }
private static TemporalChain makeCopyOfChainConstraintForNewPlan(TemporalChain constraint, Map<String, EPlanChild> idsInDestination) { if (constraint == null) return null; TemporalChain result = (TemporalChain) new Copier().copy(constraint); List<EPlanElement> elements = constraint.getElements(); List<EPlanElement> newElements = new ArrayList<EPlanElement>(); for (EPlanElement element : elements){ EPlanChild newElement = idsInDestination.get(getDiffId(element)); newElements.add(newElement); } EStructuralFeature eStructuralFeature = ConstraintsPackage.Literals.TEMPORAL_CHAIN__ELEMENTS; result.eSet(eStructuralFeature, newElements); return result; }
private static void addToCopier(Copier copier, Resource r, String metamodelName, MetamodelNamespace ns) { copier.copyAll(ns.getResource().getContents()); copier.copyReferences(); EPackage newRoot = getRoot(metamodelName, ns.getResource(), copier); r.getContents().add(newRoot); }
/** * Copies everything that is not already in the errorMM from the languageMM. */ public void extend(EPackage errorMM, MetaModel effectiveMM, MetaModel languageMM) { System.out.println("Using 'Full Metamodel' strategy"); Copier copier = new Copier(); for (EClassifier lc : languageMM.getEClassifiers()) { EClassifier errorClassifier = errorMM.getEClassifier(lc.getName()); if ( errorClassifier == null ) { EClassifier newClass = (EClassifier) copier.copy(lc); errorMM.getEClassifiers().add(newClass); continue; } // Now map lc to errorClassifier in the copier in case they need to be resolved copier.put(lc, errorClassifier); if ( errorClassifier instanceof EClass ){ EClass errorClass = (EClass) errorClassifier; EClass langClass = (EClass) lc; for (EStructuralFeature f : langClass.getEStructuralFeatures()) { if ( errorClass.getEStructuralFeature(f.getName()) == null ) { EStructuralFeature newFeature = (EStructuralFeature) copier.copy(f); errorClass.getEStructuralFeatures().add(newFeature); } else { copier.put(f, errorClass.getEStructuralFeature(f.getName())); } } } } copier.copyReferences(); removeUnnecessaryElements(errorMM); }
/** * Copies the collection. * * @param <T> * a collection type * @return the copied collection instance */ @SuppressWarnings("unchecked") public <T extends IdEObjectCollection> T copy() { final Copier copier = new IdEObjectCollectionCopier(); final T result = (T) copier.copy(this); ((IdEObjectCollectionImpl) result).cachesInitialized = true; copier.copyReferences(); return result; }
/** * Create a CreateDeleteOperation. * * @param modelElement * the model element to delete or create * @param delete * whether the element is deleted or created * @return the operation */ private CreateDeleteOperation createCreateDeleteOperation(EObject modelElement, boolean delete) { final CreateDeleteOperation createDeleteOperation = OperationsFactory.eINSTANCE.createCreateDeleteOperation(); createDeleteOperation.setDelete(delete); final EObject element = modelElement; final List<EObject> allContainedModelElements = ModelUtil.getAllContainedModelElementsAsList(element, false); allContainedModelElements.add(element); final Copier copier = new Copier(true, false); final EObject copiedElement = copier.copy(element); copier.copyReferences(); final List<EObject> copiedAllContainedModelElements = ModelUtil.getAllContainedModelElementsAsList( copiedElement, false); copiedAllContainedModelElements.add(copiedElement); for (int i = 0; i < allContainedModelElements.size(); i++) { final EObject child = allContainedModelElements.get(i); if (ModelUtil.isIgnoredDatatype(child)) { continue; } final EObject copiedChild = copiedAllContainedModelElements.get(i); final ModelElementId childId = collection.getModelElementId(child); ((CreateDeleteOperationImpl) createDeleteOperation).getEObjectToIdMap().put(copiedChild, childId); } createDeleteOperation.setModelElement(copiedElement); createDeleteOperation.setModelElementId(collection.getModelElementId(modelElement)); createDeleteOperation.setClientDate(new Date()); return createDeleteOperation; }
@Test public void updateEObjectContainerEObjectLocationDeleted() throws Exception { final IBase base = new BaseRegistryTests.TestBase(); base.getFactory().addDescriptor(IReport.class, new BaseElementFactory.FactoryDescriptor<TestReport>( TestReport.class)); base.getFactory().addDescriptor(ICouple.class, new BaseElementFactory.FactoryDescriptor<TestCouple>( TestCouple.class)); final TestEObjectContainerLocation container = new TestEObjectContainerLocation(); container.setContainer(base); final Copier copier = new Copier(); final List<EObject> eObjects = new ArrayList<EObject>(copier.copyAll(AnydslPackage.eINSTANCE .eContents())); copier.copyReferences(); final EClass producerEClass = (EClass)copier.get(AnydslPackage.eINSTANCE.getProducer()); final Resource resource = createResource("test.xmi"); resource.getContents().addAll(eObjects); container.setResource(resource); eObjectContainerHelper.updateEObjectContainer(container.getContainer(), container, resource); final IEObjectLocation location = new TestEObjectLocation(); location.setContainer(container); final ILocation target = new TestTextLocation(); final ILink link = new BaseElementFactoryTests.TestLink(); location.getTargetLinks().add(link); link.setSource(location); target.getSourceLinks().add(link); link.setTarget(target); final TestEObjectContainerConnector connector = new TestEObjectContainerConnector(); MappingUtils.getConnectorRegistry().register(connector); super.initLocation(container, location, producerEClass); resource.getContents().remove(producerEClass); eObjectContainerHelper.updateEObjectContainer(container.getContainer(), container, resource); assertTrue(location.isMarkedAsDeleted()); assertEquals(1, base.getReports().size()); final IReport report = base.getReports().get(0); assertEquals(link, report.getLink()); assertTrue(report.getDescription().contains("Producer")); assertTrue(report.getDescription().contains(location.getURIFragment())); assertTrue(report.getDescription().contains("has been deleted.")); MappingUtils.getConnectorRegistry().unregister(connector); }
@Test public void updateEObjectContainerEObjectLocationChanged() throws Exception { final IBase base = new BaseRegistryTests.TestBase(); base.getFactory().addDescriptor(IReport.class, new BaseElementFactory.FactoryDescriptor<TestReport>( TestReport.class)); base.getFactory().addDescriptor(ICouple.class, new BaseElementFactory.FactoryDescriptor<TestCouple>( TestCouple.class)); final TestEObjectContainerLocation container = new TestEObjectContainerLocation(); container.setContainer(base); final Copier copier = new Copier(); final List<EObject> eObjects = new ArrayList<EObject>(copier.copyAll(AnydslPackage.eINSTANCE .eContents())); copier.copyReferences(); final EClass producerEClass = (EClass)copier.get(AnydslPackage.eINSTANCE.getProducer()); final Resource resource = createResource("test.xmi"); resource.getContents().addAll(eObjects); container.setResource(resource); eObjectContainerHelper.updateEObjectContainer(container.getContainer(), container, resource); final IEObjectLocation location = new TestEObjectLocation(); location.setContainer(container); final ILocation target = new TestTextLocation(); final ILink link = new BaseElementFactoryTests.TestLink(); location.getTargetLinks().add(link); link.setSource(location); target.getSourceLinks().add(link); link.setTarget(target); final TestEObjectContainerConnector connector = new TestEObjectContainerConnector(); MappingUtils.getConnectorRegistry().register(connector); super.initLocation(container, location, producerEClass); producerEClass.setInterface(!producerEClass.isInterface()); eObjectContainerHelper.updateEObjectContainer(container.getContainer(), container, resource); assertEquals(1, base.getReports().size()); final IReport report = base.getReports().get(0); assertEquals(link, report.getLink()); assertTrue(report.getDescription().contains("Attribure interface was false changed to true.")); MappingUtils.getConnectorRegistry().unregister(connector); }
@Test public void updateEObjectContainerSettingLocationChanged() throws Exception { final IBase base = new BaseRegistryTests.TestBase(); base.getFactory().addDescriptor(IReport.class, new BaseElementFactory.FactoryDescriptor<TestReport>( TestReport.class)); base.getFactory().addDescriptor(ICouple.class, new BaseElementFactory.FactoryDescriptor<TestCouple>( TestCouple.class)); final TestEObjectContainerLocation container = new TestEObjectContainerLocation(); container.setContainer(base); final Copier copier = new Copier(); final List<EObject> eObjects = new ArrayList<EObject>(copier.copyAll(AnydslPackage.eINSTANCE .eContents())); copier.copyReferences(); final EClass producerEClass = (EClass)copier.get(AnydslPackage.eINSTANCE.getProducer()); final Resource resource = createResource("test.xmi"); resource.getContents().addAll(eObjects); container.setResource(resource); eObjectContainerHelper.updateEObjectContainer(container.getContainer(), container, resource); final IEObjectLocation location = new TestEObjectLocation(); location.setContainer(container); final ILocation target = new TestTextLocation(); final ILink link = new BaseElementFactoryTests.TestLink(); location.getTargetLinks().add(link); link.setSource(location); target.getSourceLinks().add(link); link.setTarget(target); final TestEObjectContainerConnector connector = new TestEObjectContainerConnector(); MappingUtils.getConnectorRegistry().register(connector); super.initLocation(container, location, new Setting() { public void unset() { // nothing to do here } public void set(Object newValue) { // nothing to do here } public boolean isSet() { return false; } public EStructuralFeature getEStructuralFeature() { return EcorePackage.eINSTANCE.getEClass_Interface(); } public EObject getEObject() { return producerEClass; } public Object get(boolean resolve) { return producerEClass.getName(); } }); producerEClass.setInterface(!producerEClass.isInterface()); eObjectContainerHelper.updateEObjectContainer(container.getContainer(), container, resource); assertEquals(1, base.getReports().size()); final IReport report = base.getReports().get(0); assertEquals(link, report.getLink()); assertTrue(report.getDescription().contains("Producer")); assertTrue(report.getDescription().contains(location.getURIFragment())); assertTrue(report.getDescription().contains( "feature interface value false has been changed to true.")); MappingUtils.getConnectorRegistry().unregister(connector); }
@Test public void updateEObjectContainerSettingLocationManyRemoved() throws Exception { final IBase base = new BaseRegistryTests.TestBase(); base.getFactory().addDescriptor(IReport.class, new BaseElementFactory.FactoryDescriptor<TestReport>( TestReport.class)); base.getFactory().addDescriptor(ICouple.class, new BaseElementFactory.FactoryDescriptor<TestCouple>( TestCouple.class)); final TestEObjectContainerLocation container = new TestEObjectContainerLocation(); container.setContainer(base); final Copier copier = new Copier(); final List<EObject> eObjects = new ArrayList<EObject>(copier.copyAll(AnydslPackage.eINSTANCE .eContents())); copier.copyReferences(); final EClass foodEClass = (EClass)copier.get(AnydslPackage.eINSTANCE.getFood()); final Resource resource = createResource("test.xmi"); resource.getContents().addAll(eObjects); container.setResource(resource); eObjectContainerHelper.updateEObjectContainer(container.getContainer(), container, resource); final IEObjectLocation location = new TestEObjectLocation(); location.setContainer(container); final ILocation target = new TestTextLocation(); final ILink link = new BaseElementFactoryTests.TestLink(); location.getTargetLinks().add(link); link.setSource(location); target.getSourceLinks().add(link); link.setTarget(target); final TestEObjectContainerConnector connector = new TestEObjectContainerConnector(); MappingUtils.getConnectorRegistry().register(connector); super.initLocation(container, location, new Setting() { public void unset() { // nothing to do here } public void set(Object newValue) { // nothing to do here } public boolean isSet() { return false; } public EStructuralFeature getEStructuralFeature() { return EcorePackage.eINSTANCE.getEClass_EOperations(); } public EObject getEObject() { return foodEClass; } public Object get(boolean resolve) { return foodEClass.getEOperations().get(1); } }); foodEClass.getEOperations().remove(1); eObjectContainerHelper.updateEObjectContainer(container.getContainer(), container, resource); assertEquals(1, base.getReports().size()); final IReport report = base.getReports().get(0); assertEquals(link, report.getLink()); assertTrue(report.getDescription().contains("Food")); assertTrue(report.getDescription().contains(location.getURIFragment())); assertTrue(report.getDescription().contains("has been removed from feature eOperations.")); MappingUtils.getConnectorRegistry().unregister(connector); }
protected IEObjectLocation createEObjectLocation(Copier copier, TestEObjectContainerLocation container) throws Exception { final TestEObjectLocation res; final EObject eObject = copier.get(original[0]); if (original[1] != null) { final EStructuralFeature feature = (EStructuralFeature)original[1]; final Object value; if (original[2] instanceof EObject) { value = copier.get(original[2]); } else { value = original[2]; } res = (TestEObjectLocation)MappingUtils.getConnectorRegistry().createLocation(container, new Setting() { public void unset() { // nothing to do here } public void set(Object newValue) { // nothing to do here } public boolean isSet() { return false; } public EStructuralFeature getEStructuralFeature() { return feature; } public EObject getEObject() { return eObject; } public Object get(boolean resolve) { return value; } }); } else { res = (TestEObjectLocation)MappingUtils.getConnectorRegistry().createLocation(container, eObject); } res.setObject(eObject); return res; }
@Test public void updateEObjectsAltered() throws Exception { final IBase base = new BaseRegistryTests.TestBase(); base.getFactory().addDescriptor(IEObjectLocation.class, new BaseElementFactory.FactoryDescriptor<TestEObjectLocation>(TestEObjectLocation.class)); base.getFactory().addDescriptor(ICouple.class, new BaseElementFactory.FactoryDescriptor<TestCouple>( TestCouple.class)); final TestEObjectContainerLocation container = new TestEObjectContainerLocation(); container.setContainer(base); final Copier copier = new Copier(); EPackage copy = (EPackage)copier.copy(AnydslPackage.eINSTANCE); if (!copier.containsKey(altered[0])) { copier.copy((EObject)altered[0]); } copier.copyReferences(); final List<EObject> testEObjects = new ArrayList<EObject>(copy.getEClassifiers()); final Resource initialResource = createResource("initialResource"); container.setResource(initialResource); initialResource.getContents().addAll(testEObjects); eObjectContainerHelper.updateEObjectContainer(container.getContainer(), container, initialResource); final IEObjectLocation location = createEObjectLocation(copier, container); final EObject expectedEObject = copier.get(altered[0]); final EStructuralFeature expectedFeature = (EStructuralFeature)altered[1]; final Object expectedValue; if (altered[2] instanceof EObject) { expectedValue = copier.get(altered[2]); } else { expectedValue = altered[2]; } List<EObject> newEObjects = testEObjects; newEObjects = alterEObjects(copier, newEObjects); final Resource newResource = createResource("newResource"); newResource.getContents().addAll(newEObjects); eObjectContainerHelper.updateEObjectContainer(container.getContainer(), container, newResource); container.setResource(newResource); assertEObjectLocation(location, expectedEObject, expectedFeature, expectedValue); }
@Test public void updateEObjectsPlusShiftAltered() throws Exception { final IBase base = new BaseRegistryTests.TestBase(); base.getFactory().addDescriptor(IEObjectLocation.class, new BaseElementFactory.FactoryDescriptor<TestEObjectLocation>(TestEObjectLocation.class)); base.getFactory().addDescriptor(ICouple.class, new BaseElementFactory.FactoryDescriptor<TestCouple>( TestCouple.class)); final TestEObjectContainerLocation container = new TestEObjectContainerLocation(); container.setContainer(base); final Copier copier = new Copier(); EPackage copy = (EPackage)copier.copy(AnydslPackage.eINSTANCE); if (!copier.containsKey(altered[0])) { copier.copy((EObject)altered[0]); } copier.copyReferences(); final List<EObject> testEObjects = new ArrayList<EObject>(copy.getEClassifiers()); final Resource initialResource = createResource("initialResource"); container.setResource(initialResource); initialResource.getContents().addAll(testEObjects); eObjectContainerHelper.updateEObjectContainer(container.getContainer(), container, initialResource); final IEObjectLocation location = createEObjectLocation(copier, container); final EObject expectedEObject = copier.get(altered[0]); final EStructuralFeature expectedFeature = (EStructuralFeature)original[1]; final Object expectedValue; if (altered[2] instanceof EObject) { expectedValue = copier.get(altered[2]); } else { expectedValue = altered[2]; } List<EObject> newEObjects = new ArrayList<EObject>(MappingPackage.eINSTANCE.getEClassifiers()); newEObjects.addAll(testEObjects); newEObjects = alterEObjects(copier, newEObjects); final Resource newResource = createResource("newResource"); newResource.getContents().addAll(newEObjects); eObjectContainerHelper.updateEObjectContainer(container.getContainer(), container, newResource); container.setResource(newResource); assertEObjectLocation(location, expectedEObject, expectedFeature, expectedValue); }
@Test public void updateEObjectsMinusShiftAltered() throws Exception { final IBase base = new BaseRegistryTests.TestBase(); base.getFactory().addDescriptor(IEObjectLocation.class, new BaseElementFactory.FactoryDescriptor<TestEObjectLocation>(TestEObjectLocation.class)); base.getFactory().addDescriptor(ICouple.class, new BaseElementFactory.FactoryDescriptor<TestCouple>( TestCouple.class)); final TestEObjectContainerLocation container = new TestEObjectContainerLocation(); container.setContainer(base); final Copier copier = new Copier(); EPackage copy = (EPackage)copier.copy(AnydslPackage.eINSTANCE); if (!copier.containsKey(altered[0])) { copier.copy((EObject)altered[0]); } copier.copyReferences(); final List<EObject> testEObjects = new ArrayList<EObject>(copy.getEClassifiers()); final Resource initialResource = createResource("initialResource"); container.setResource(initialResource); initialResource.getContents().addAll(testEObjects); eObjectContainerHelper.updateEObjectContainer(container.getContainer(), container, initialResource); final IEObjectLocation location = createEObjectLocation(copier, container); final EObject expectedEObject = copier.get(altered[0]); final EStructuralFeature expectedFeature = (EStructuralFeature)original[1]; final Object expectedValue; if (altered[2] instanceof EObject) { expectedValue = copier.get(altered[2]); } else { expectedValue = altered[2]; } List<EObject> newEObjects = new ArrayList<EObject>(testEObjects); newEObjects.remove(0); newEObjects.remove(0); newEObjects = alterEObjects(copier, newEObjects); final Resource newResource = createResource("newResource"); newResource.getContents().addAll(newEObjects); eObjectContainerHelper.updateEObjectContainer(container.getContainer(), container, newResource); container.setResource(newResource); assertEObjectLocation(location, expectedEObject, expectedFeature, expectedValue); }
/** * Called to handle the copying of a containment feature; this adds a list * of copies or sets a single copy as appropriate for the multiplicity. * * @param eReference * the feature to copy. * @param eObject * the object from which to copy. * @param copyEObject * the object to copy to. * @param copier */ protected static void copyContainment(EReference eReference, EObject objectWithChanges, EObject target, Copier copier) { if (!eReference.isTransient() && !(!target.eIsSet(eReference) && !objectWithChanges.eIsSet(eReference))) { if (eReference.isMany()) { @SuppressWarnings("unchecked") List<EObject> source = (List<EObject>) objectWithChanges.eGet(eReference); @SuppressWarnings("unchecked") List<EObject> target2 = (List<EObject>) target.eGet(getTarget(eReference)); if (source.isEmpty()) { target2.clear(); } else { mergeAll(target2, source); } } else { if (objectWithChanges.eIsSet(eReference) && target.eIsSet(eReference)) { if (!objectWithChanges.eGet(eReference).equals(target.eGet(eReference))) { EObject childEObjectWithChanges = (EObject) objectWithChanges.eGet(eReference); EObject childEObjectTarget = (EObject) target.eGet(eReference); childEObjectTarget = mergeChanges(childEObjectTarget, childEObjectWithChanges); target.eSet(getTarget(eReference), childEObjectTarget); } } else if (objectWithChanges.eIsSet(eReference)) { EObject childEObject = (EObject) objectWithChanges.eGet(eReference); target.eSet(getTarget(eReference), childEObject == null ? null : copier.copy(childEObject)); } else { target.eSet(getTarget(eReference), null); } } } }
/** * Called to handle the copying of an attribute; this adds a list of values * or sets a single value as appropriate for the multiplicity. * * @param eAttribute * the attribute to copy. * @param objectWithChanges * the object from which to copy. * @param copyEObject * the object to copy to. * @param copier */ protected static void copyAttribute(EAttribute eAttribute, EObject objectWithChanges, EObject target, Copier copier) { if (!eAttribute.isTransient() && !(!target.eIsSet(eAttribute) && !objectWithChanges.eIsSet(eAttribute))) { if (FeatureMapUtil.isFeatureMap(eAttribute)) { FeatureMap featureMap = (FeatureMap) objectWithChanges.eGet(eAttribute); for (int i = 0, size = featureMap.size(); i < size; ++i) { EStructuralFeature feature = featureMap.getEStructuralFeature(i); if (feature instanceof EReference && ((EReference) feature).isContainment()) { Object value = featureMap.getValue(i); if (value != null) { copier.copy((EObject) value); // versteh ich // nicht??? cp } } } } else if (eAttribute.isMany()) { List<?> source = (List<?>) objectWithChanges.eGet(eAttribute); @SuppressWarnings("unchecked") List<Object> target2 = (List<Object>) target.eGet(getTarget(eAttribute)); if (source.isEmpty()) { target2.clear(); } else { target2.addAll(source); } } else { if (objectWithChanges.eIsSet(eAttribute) && target.eIsSet(eAttribute)) { if (!objectWithChanges.eGet(eAttribute).equals(target.eGet(eAttribute))) { Object o = objectWithChanges.eGet(eAttribute); target.eSet(getTarget(eAttribute), o); } } else if (objectWithChanges.eIsSet(eAttribute)) { target.eSet(getTarget(eAttribute), objectWithChanges.eGet(eAttribute)); } else if(!eAttribute.isTransient()) { Object attribute = objectWithChanges.eGet(eAttribute); if (eAttribute.getEType().getInstanceTypeName().equals("java.lang.String")) { target.eSet(getTarget(eAttribute), ""); } else if (eAttribute.getEType().getInstanceTypeName().equals("java.lang.Integer") || eAttribute.getEType().getInstanceTypeName().equals("java.lang.int")) { target.eSet(getTarget(eAttribute), 0); } else { target.eUnset(getTarget(eAttribute)); } } } } }
public void Create_Modular_Diagram(IProject pro, org.eclipse.emf.common.util.URI uri_diagram) throws CoreException, IOException { //Create Resource and ResourceSet ResourceSet reset = new ResourceSetImpl(); ResourceSet newreset = new ResourceSetImpl(); //Search the root IFile file = pro.getFile(pro.getName().concat(".xmi")); PerformantXMIResourceImpl file_xmi = (PerformantXMIResourceImpl) new PerformantXMIResourceFactoryImpl(). createResource(URI.createFileURI(file.getFullPath().toString())); reset.getResources().add(file_xmi); Copier copier = new Copier(); PerformantXMIResourceImpl diagram = (PerformantXMIResourceImpl)new PerformantXMIResourceFactoryImpl(). createResource(uri_diagram); newreset.getResources().add(diagram); file_xmi.load(null); diagram.getContents().addAll(copier.copyAll(file_xmi.getContents())); copier.copyReferences(); //diagram.save(null); //If the root use extrinsic Id //Use Extrinsic ID /* String id = file_xmi.getID(file_xmi.Get_Root()); if(id!=null) { for (EObject oldObject : copier.keySet()) { EObject newObject = copier.get(oldObject); String idObject = file_xmi.getID(oldObject); if (idObject == null) { URI uri = EcoreUtil.getURI(oldObject); idObject = uri.fragment(); } diagram.setID(newObject,idObject); } } */ diagram.save(null); }
@Override protected Copier createCopier() { return new CustomCopier(); }
public static SourceMetamodelsData get(IAnalyserResult analyser) { List<ModelInfo> srcModels = ATLUtils.getModelInfo(analyser.getATLModel()).stream().filter(i -> i.isInput()).collect(Collectors.toList()); Copier copier = new EcoreUtil.Copier(); Resource r = new ResourceImpl(); for (ModelInfo mInfo : srcModels) { MetamodelNamespace ns = analyser.getNamespaces().getNamespace(mInfo.getMetamodelName()); addToCopier(copier, r, mInfo.getMetamodelName(), ns); } // The copy procedure for the meta-model needs to be separated to avoid the copier to redirect // references to the Ecore meta-model (in particular, data types) to the the copied ecore MetamodelNamespace mmm = analyser.getNamespaces().getMetaMetamodel(); Copier mmCopier = new EcoreUtil.Copier(); addToCopier(mmCopier, r, mmm.getName(), mmm); copier.putAll(mmCopier); EPackage newRoot = null; if ( r.getContents().size() == 1 ) { newRoot = (EPackage) r.getContents().get(0); } else { newRoot = EcoreFactory.eINSTANCE.createEPackage(); newRoot.setName("srcRoot"); newRoot.setNsPrefix("srcRoot"); newRoot.setNsURI("http://" + "srcRoot" + "/" + "gen"); for (EObject pkg : r.getContents()) { newRoot.getESubpackages().add((EPackage) pkg); } r.getContents().add(newRoot); } adapt(newRoot, ""); copier.forEach((src, tgt) -> { if ( src instanceof EStructuralFeature ) { checkRenaming((EStructuralFeature) src, (EStructuralFeature) tgt); } }); return new SourceMetamodelsData(analyser, newRoot, copier); }