/** * This method creates a model that reflects the state that would be obtained if * this proposal was accepted. This model can differ from the current model, * because different proposals can result in different models. The code that is * passed as argument is executed once the (changed) model was created. After * executing the given code, all changes are reverted. */ public void materialize(Runnable code) { EObject root = EcoreUtil.getRootContainer(getContainer()); if (root == null) { code.run(); return; } ChangeRecorder recorder = new ChangeRecorder(); recorder.beginRecording(Collections.singleton(root)); // attach proposal model fragment to main model Runnable attachmentCode = getAttachmentCode(); if (attachmentCode != null) { // Applying attachment code attachmentCode.run(); } ChangeDescription changes = recorder.endRecording(); code.run(); // revert changes changes.apply(); }
public static void printChangeDescription(ChangeDescription cd) { System.err.println("* Saved, detected a change."); System.err.println("* Saved, got change desc: " + cd.toString()); System.err.println("* CD Class: " + cd.getClass().getCanonicalName()); EMap<EObject, EList<FeatureChange>> changes = cd.getObjectChanges(); for (EObject obj : changes.keySet()) { if (obj == null) { System.err.println("Got null object"); continue; } System.err.println("Object: " + obj.toString()); EList<FeatureChange> fcList = changes.get(obj); for (FeatureChange fc : fcList) { processFeatureChange(obj, fc, " "); } } }
@Override public void applyChange(Deltas deltas, IAcceptor<IEmfResourceChange> changeAcceptor) { XtextResource res = (XtextResource) lifecycleManager.openAndApplyReferences(getResourceSet(), getResource()); if (!referenceUpdater.isAffected(deltas, getResource())) { return; } ITextRegionAccess base = textRegionBuilderProvider.get().forNodeModel(res).create(); ITextRegionDiffBuilder rewriter = new StringBasedTextRegionAccessDiffBuilder(base); ReferenceUpdaterContext context = new ReferenceUpdaterContext(deltas, rewriter); referenceUpdater.update(context); if (!context.getModifications().isEmpty()) { ChangeRecorder rec = new ChangeRecorder(res); for (Runnable run : context.getModifications()) { run.run(); } ChangeDescription recording = rec.endRecording(); ResourceSet rs = res.getResourceSet(); ResourceSetRecording tree = changeTreeProvider.createChangeTree(rs, Collections.emptyList(), recording); ResourceRecording recordedResource = tree.getRecordedResource(res); serializer.serializeChanges(recordedResource, rewriter); } for (IUpdatableReference upd : context.getUpdatableReferences()) { referenceUpdater.updateReference(rewriter, upd); } ITextRegionAccessDiff rewritten = rewriter.create(); List<ITextReplacement> rep = formatter.format(rewritten); TextDocumentChange change = new TextDocumentChange(rewritten, getResource().getUri(), rep); changeAcceptor.accept(change); }
@Override public void applyChange(Deltas deltas, IAcceptor<IEmfResourceChange> changeAcceptor) { Resource resource = snapshot.getResource(); ResourceSet rs = resource.getResourceSet(); ReferenceUpdaterContext ctx = new ReferenceUpdaterContext(deltas, document); if (serializer.isUpdateCrossReferences()) { referenceUpdater.update(ctx); for (Runnable run : ctx.getModifications()) { run.run(); } } ChangeDescription recording = recorder.endRecording(); if (recording != null) { List<IResourceSnapshot> snapshots = Collections.singletonList(snapshot); ResourceSetRecording tree = changeTreeProvider.createChangeTree(rs, snapshots, recording); ResourceRecording recordedResource = tree.getRecordedResource(resource); partialSerializer.serializeChanges(recordedResource, document); } List<IUpdatableReference> updatableReferences = ctx.getUpdatableReferences(); for (IUpdatableReference upd : updatableReferences) { referenceUpdater.updateReference(document, upd); } ITextRegionAccessDiff rewritten = document.create(); List<ITextReplacement> rep = formatter.format(rewritten); URI oldUri = snapshot.getURI(); TextDocumentChange change = new TextDocumentChange(rewritten, oldUri, rep); changeAcceptor.accept(change); }
/** * rollbackResourceSet * * @param recorder void */ public static void rollbackResourceSet(final ChangeRecorder recorder) { if (recorder == null || !recorder.isRecording()) { return; } DomainRegistry.getEditingDomain().getCommandStack().execute(new CompoundCommand() { /** * @see org.eclipse.emf.common.command.CompoundCommand#execute() */ @Override public void execute() { try { ChangeDescription endRecording = recorder.endRecording(); if (endRecording != null) { endRecording.apply(); } } catch (Exception e) { e.printStackTrace(); } } /** * @see org.eclipse.emf.common.command.AbstractCommand#canExecute() */ @Override public boolean canExecute() { return true; } }); }
@Override public ChangeDescription endRecording() { try { return super.endRecording(); } catch (Exception e) { Logger logger = Logger.getLogger(FixedTransactionChangeRecorder.class); logger.error("consolidateChanges error", e); return null; } }
void doit() throws Exception { ResourceSet originalResourceSet = new ResourceSetImpl(); loadResources(originalResourceSet); @SuppressWarnings("unused") Map<EObject, URI> eObjectToProxyURIMap = new HashMap<EObject, URI>(); ChangeRecorder changeRecorder = new ChangeRecorder(); /* Uncomment this in 3.7.x for the test to work/pass changeRecorder.setRecordingTransientFeatures(false); changeRecorder.setEObjectToProxyURIMap(eObjectToProxyURIMap); */ changeRecorder.beginRecording(Collections.singleton(originalResourceSet)); makeChanges(); ChangeDescription changeDescription = changeRecorder.endRecording(); /* Uncomment this in 3.7.x for the test to work/pass changeDescription.copyAndReverse(eObjectToProxyURIMap); */ ResourceSet finalResourceSet = new ResourceSetImpl(); Resource changeDescriptionResource = finalResourceSet.createResource(URI.createURI("changes.change")); changeDescriptionResource.getContents().add(changeDescription); ByteArrayOutputStream out = new ByteArrayOutputStream(); changeDescriptionResource.save(System.out, null); changeDescriptionResource.save(out, null); changeDescriptionResource.getContents().clear(); changeDescriptionResource.unload(); changeDescriptionResource.load(new ByteArrayInputStream(out.toByteArray()), null); ChangeDescription finalChangeDescription = (ChangeDescription) changeDescriptionResource.getContents().get(0); finalChangeDescription.apply(); finalResourceSet.getResources().remove(0); assertEquals(originalResourceSet, finalResourceSet); }
private void applyAndReverseChangeDescription(ChangeDescription changeDescription){ final ChangeDescription inputChangeDescription = changeDescription; TransactionUtils.writeIfNecessary(existingPlan, new Runnable() { @Override public void run() { if(inputChangeDescription != null) { inputChangeDescription.applyAndReverse(); } } }); }
public ChangeDescription createAddActivityToPlanChangeDescription(EPlan plan, EActivity activity) { ChangeDescription cd = ChangeFactory.eINSTANCE.createChangeDescription(); EStructuralFeature feature = PlanPackage.Literals.EPLAN_PARENT__CHILDREN; List activitiesToAdd = new BasicEList(); activitiesToAdd.add(activity); FeatureChange featureChange = createFeatureChangeForMultivalueFeatures(plan, feature, activitiesToAdd, null); if(featureChange != null) { EList<FeatureChange> allFeatureChanges = new BasicEList(); allFeatureChanges.add(featureChange); cd.getObjectChanges().put(plan, allFeatureChanges); } return cd; }
/** * 모델 rollback 후 모델 저장 * * * @param recorder * @param eObject * void */ public static void rollbackWithSave(final ChangeRecorder recorder, final EObject eObject) { if (recorder == null || !recorder.isRecording() || eObject == null) { return; } DomainRegistry.getEditingDomain().getCommandStack().execute(new CompoundCommand() { /** * @see org.eclipse.emf.common.command.CompoundCommand#execute() */ @Override public void execute() { try { if (recorder.isRecording()) { ChangeDescription endRecording = recorder.endRecording(); if (endRecording != null) { endRecording.apply(); } } } catch (Exception e) { e.printStackTrace(); } finally { DomainModelHandlerUtil.save(eObject); if( eObject instanceof Package) { TreeMap<String, Package> fragmentedPackageList = ProjectUtil.getFragmentedPackageList((Package) eObject); for (Iterator<Package> iterator = fragmentedPackageList.values().iterator(); iterator.hasNext();) { Package pkg = (Package) iterator.next(); DomainModelHandlerUtil.save(pkg); } } } } /** * @see org.eclipse.emf.common.command.AbstractCommand#canExecute() */ @Override public boolean canExecute() { return true; } }); }
protected ChangeDescription createChangeDescriptionInstance(){ ChangeDescription changeDescription = ChangeFactory.eINSTANCE.createChangeDescription(); return changeDescription; }