<commit_before>package tools.vitruv.framework.change.description.impl

import tools.vitruv.framework.change.echange.EChange
import java.util.List
import tools.vitruv.framework.change.interaction.UserInteractionBase
import java.util.ArrayList
import static com.google.common.base.Preconditions.checkNotNull
import org.eclipse.emf.ecore.EObject
import tools.vitruv.framework.change.echange.feature.reference.RemoveEReference
import tools.vitruv.framework.change.echange.feature.reference.InsertEReference
import tools.vitruv.framework.change.echange.feature.reference.ReplaceSingleValuedEReference
import tools.vitruv.framework.change.echange.eobject.EObjectExistenceEChange
import tools.vitruv.framework.change.echange.feature.FeatureEChange
import tools.vitruv.framework.change.echange.root.RemoveRootEObject
import tools.vitruv.framework.change.echange.root.InsertRootEObject
import org.eclipse.emf.ecore.util.EcoreUtil
import org.eclipse.emf.ecore.InternalEObject
import tools.vitruv.framework.change.echange.root.RootEChange
import tools.vitruv.framework.change.echange.eobject.EObjectAddedEChange
import tools.vitruv.framework.change.echange.eobject.EObjectSubtractedEChange
import tools.vitruv.framework.change.echange.eobject.CreateEObject
import tools.vitruv.framework.change.echange.eobject.DeleteEObject
import tools.vitruv.framework.change.echange.feature.UnsetFeature
import tools.vitruv.framework.change.echange.feature.attribute.ReplaceSingleValuedEAttribute
import tools.vitruv.framework.change.echange.feature.reference.AdditiveReferenceEChange
import tools.vitruv.framework.change.echange.feature.reference.SubtractiveReferenceEChange
import tools.vitruv.framework.change.echange.feature.attribute.InsertEAttributeValue
import tools.vitruv.framework.change.echange.feature.attribute.RemoveEAttributeValue
import java.util.Set
import tools.vitruv.framework.change.echange.feature.attribute.UpdateAttributeEChange
import org.eclipse.emf.common.util.URI
import static extension tools.vitruv.framework.change.echange.resolve.EChangeResolverAndApplicator.*
import static extension edu.kit.ipd.sdq.commons.util.java.lang.IterableUtil.mapFixed
import tools.vitruv.framework.change.description.TransactionalChange
import java.util.Collections
import org.eclipse.emf.ecore.resource.ResourceSet
import tools.vitruv.framework.change.echange.id.IdResolver
import java.util.HashSet
import org.eclipse.emf.ecore.EPackage
import static com.google.common.base.Preconditions.checkState

class TransactionalChangeImpl implements TransactionalChange {
	var List<? extends EChange> eChanges
	val List<UserInteractionBase> userInteractions = new ArrayList()

	new(Iterable<? extends EChange> eChanges) {
		this.eChanges = checkNotNull(eChanges, "eChanges").toList
	}
	
	override getEChanges() {
		return Collections.unmodifiableList(eChanges)
	}

	override containsConcreteChange() {
		return !eChanges.empty
	}
	
	private static def getChangedURI(EChange eChange) {
		switch(eChange) {
			FeatureEChange<?, ?>: eChange.affectedEObject?.objectUri
			EObjectExistenceEChange<?>: eChange.affectedEObject?.objectUri
			RootEChange: URI.createURI(eChange.uri)
		}
	}
	
	override getChangedURIs() {
		eChanges.map[changedURI].filterNull.toSet
	}
	
	override Set<String> getAffectedEObjectsMetamodelRootNsUris() {
		val changedPackages = affectedEObjects.fold(new HashSet<EPackage>) [ affectedPackages, changedObject |
			var currentPackage = changedObject.eClass.EPackage
			while (currentPackage.ESuperPackage !== null)
				currentPackage = currentPackage.ESuperPackage
			if (currentPackage !== null) {
				affectedPackages += currentPackage
			}
			affectedPackages
		]
		checkState(!changedPackages.empty, "Cannot identify the packages of this change:%s%s", System.lineSeparator,
			this)
		return changedPackages.map[nsURI].toSet
	}

	override resolveAndApply(ResourceSet resourceSet) {
		val idResolver = IdResolver.create(resourceSet)
		val resolvedChanges = eChanges.mapFixed[
			val resolvedChange = resolveBefore(idResolver)
			resolvedChange.applyForward(idResolver)
			resolvedChange<commit_msg>Directly gather namespace URIs of affected packages<commit_after>package tools.vitruv.framework.change.description.impl

import tools.vitruv.framework.change.echange.EChange
import java.util.List
import tools.vitruv.framework.change.interaction.UserInteractionBase
import java.util.ArrayList
import static com.google.common.base.Preconditions.checkNotNull
import org.eclipse.emf.ecore.EObject
import tools.vitruv.framework.change.echange.feature.reference.RemoveEReference
import tools.vitruv.framework.change.echange.feature.reference.InsertEReference
import tools.vitruv.framework.change.echange.feature.reference.ReplaceSingleValuedEReference
import tools.vitruv.framework.change.echange.eobject.EObjectExistenceEChange
import tools.vitruv.framework.change.echange.feature.FeatureEChange
import tools.vitruv.framework.change.echange.root.RemoveRootEObject
import tools.vitruv.framework.change.echange.root.InsertRootEObject
import org.eclipse.emf.ecore.util.EcoreUtil
import org.eclipse.emf.ecore.InternalEObject
import tools.vitruv.framework.change.echange.root.RootEChange
import tools.vitruv.framework.change.echange.eobject.EObjectAddedEChange
import tools.vitruv.framework.change.echange.eobject.EObjectSubtractedEChange
import tools.vitruv.framework.change.echange.eobject.CreateEObject
import tools.vitruv.framework.change.echange.eobject.DeleteEObject
import tools.vitruv.framework.change.echange.feature.UnsetFeature
import tools.vitruv.framework.change.echange.feature.attribute.ReplaceSingleValuedEAttribute
import tools.vitruv.framework.change.echange.feature.reference.AdditiveReferenceEChange
import tools.vitruv.framework.change.echange.feature.reference.SubtractiveReferenceEChange
import tools.vitruv.framework.change.echange.feature.attribute.InsertEAttributeValue
import tools.vitruv.framework.change.echange.feature.attribute.RemoveEAttributeValue
import java.util.Set
import tools.vitruv.framework.change.echange.feature.attribute.UpdateAttributeEChange
import org.eclipse.emf.common.util.URI
import static extension tools.vitruv.framework.change.echange.resolve.EChangeResolverAndApplicator.*
import static extension edu.kit.ipd.sdq.commons.util.java.lang.IterableUtil.mapFixed
import tools.vitruv.framework.change.description.TransactionalChange
import java.util.Collections
import org.eclipse.emf.ecore.resource.ResourceSet
import tools.vitruv.framework.change.echange.id.IdResolver
import java.util.HashSet
import static com.google.common.base.Preconditions.checkState

class TransactionalChangeImpl implements TransactionalChange {
	var List<? extends EChange> eChanges
	val List<UserInteractionBase> userInteractions = new ArrayList()

	new(Iterable<? extends EChange> eChanges) {
		this.eChanges = checkNotNull(eChanges, "eChanges").toList
	}
	
	override getEChanges() {
		return Collections.unmodifiableList(eChanges)
	}

	override containsConcreteChange() {
		return !eChanges.empty
	}
	
	private static def getChangedURI(EChange eChange) {
		switch(eChange) {
			FeatureEChange<?, ?>: eChange.affectedEObject?.objectUri
			EObjectExistenceEChange<?>: eChange.affectedEObject?.objectUri
			RootEChange: URI.createURI(eChange.uri)
		}
	}
	
	override getChangedURIs() {
		eChanges.map[changedURI].filterNull.toSet
	}
	
	override Set<String> getAffectedEObjectsMetamodelRootNsUris() {
		val namespaceUrisOfChangedPackages = affectedEObjects.fold(new HashSet<String>) [ namespaceUrisOfAffectedPackages, changedObject |
			var currentPackage = changedObject.eClass.EPackage
			while (currentPackage.ESuperPackage !== null)
				currentPackage = currentPackage.ESuperPackage
			if (currentPackage !== null) {
				namespaceUrisOfAffectedPackages += currentPackage.nsURI
			}
			namespaceUrisOfAffectedPackages
		]
		checkState(!namespaceUrisOfChangedPackages.empty, "Cannot identify the packages of this change:%s%s",
			System.lineSeparator, this)
		return namespaceUrisOfChangedPackages
	}

	override resolveAndApply(ResourceSet resourceSet) {
		val idResolver = IdResolver.create(resourceSet)
		val resolvedChanges = eChanges.mapFixed[
			val resolvedChange = resolveBefore(idResolver)
			resolvedChange.applyForward(idResolver)
			resolvedChange
{'fraction_non_alphanumeric': 0.08620481199371298, 'fraction_numerical': 0.0, 'mean_word_length': 6.630996309963099, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2776531', 'n_tokens_mistral': 2596, 'n_tokens_neox': 2421, 'n_words': 359}