<commit_before>package edu.kit.ipd.sdq.vitruvius.framework.mir.executor.helpers;

import java.util.HashSet;
import java.util.Set;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EDataType;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.EcoreUtil;

import edu.kit.ipd.sdq.vitruvius.framework.contracts.interfaces.user.DefaultTUIDCalculatorAndResolver;
import edu.kit.ipd.sdq.vitruvius.framework.mir.executor.impl.AttributeTUIDCalculatorAndResolver;

public class EcoreHelper {
	
	// TODO: more efficient with EcoreUtil?
	public static Set<EObject> findOppositeForFeature(EObject target, EReference reference) {
		Set<EObject> result = new HashSet<EObject>();
		
		Resource resource = target.eResource();
		TreeIterator<Object> iter = EcoreUtil.getAllProperContents(resource, true);
		while (iter.hasNext()) {
			Object obj = iter.next();
			if (obj instanceof EObject) {
				EObject eobj = (EObject) obj;
				
				if (reference.getEContainingClass().isSuperTypeOf(eobj.eClass())) {
					Object referencedObject = eobj.eGet(reference);
					if (referencedObject instanceof EList<?>) {
						if (((EList<?>) referencedObject).contains(target)) {
							result.add(eobj);
						} else if (referencedObject.equals(target)) {
							result.add(eobj);
						}
					}
				}
			}
		}
		
		return result;
	}
	
	public static Set<EObject> findOppositeForFeatures(EObject target, EReference... references) {
		Set<EObject> currentLevel = new HashSet<EObject>();
		currentLevel.add(target);
		Set<EObject> nextLevel = new HashSet<EObject>();
		
		for (EReference reference : references) {
			nextLevel.clear();
			for (EObject eObj : currentLevel)
				nextLevel.addAll(findOppositeForFeature(eObj, reference));
			
			// swap references
			Set<EObject> swap = currentLevel;
			currentLevel = nextLevel;
			nextLevel = swap;
		}
		
		return currentLevel;
	}
	
	/**
	 * Copied from {@link DefaultTUIDCalculatorAndResolver#getValueOfAttribute(EObject, String)}.
	 * Tries to obtain the value of the feature named <code>featureName</code> from the given {@link EObject}.
	 * If the feature is of type <code>EString</code>, it is returned directly, if it is of type <code>EInt</code>,
	 * it is converted to the integers string representation.
	 */
	// TODO: refactor into helper class that both {@link DefaultTUIDCalculatorAndResolver} and {@link AttributeTUIDCalculatorAndResolver} use.
    public static String getStringValueOfAttribute(final EObject eObject, final String featureName) {
        EStructuralFeature idFeature = eObject.eClass().getEStructuralFeature(featureName);
        if (idFeature != null && idFeature instanceof EAttribute) {
            EAttribute idAttribute = (EAttribute) idFeature;
            EDataType eAttributeType = idAttribute.getEAttributeType();
            EDataType eString = EcorePackage.eINSTANCE.getEString();
            if (eString != null && eString.equals(eAttributeType)) {
                return (String) eObject.eGet(idFeature);
            }
            EDataType eInt = EcorePackage.eINSTANCE.getEInt();
            if (eInt != null && eString.equals(eAttributeType)) {
                return String.valueOf(eObject.eGet(idFeature));
            }
        }
        return null;
    }
}<commit_msg>Removed unused reference from JavaDoc<commit_after>package edu.kit.ipd.sdq.vitruvius.framework.mir.executor.helpers;

import java.util.HashSet;
import java.util.Set;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EDataType;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.EcoreUtil;

import edu.kit.ipd.sdq.vitruvius.framework.contracts.interfaces.user.DefaultTUIDCalculatorAndResolver;

public class EcoreHelper {
	
	// TODO: more efficient with EcoreUtil?
	public static Set<EObject> findOppositeForFeature(EObject target, EReference reference) {
		Set<EObject> result = new HashSet<EObject>();
		
		Resource resource = target.eResource();
		TreeIterator<Object> iter = EcoreUtil.getAllProperContents(resource, true);
		while (iter.hasNext()) {
			Object obj = iter.next();
			if (obj instanceof EObject) {
				EObject eobj = (EObject) obj;
				
				if (reference.getEContainingClass().isSuperTypeOf(eobj.eClass())) {
					Object referencedObject = eobj.eGet(reference);
					if (referencedObject instanceof EList<?>) {
						if (((EList<?>) referencedObject).contains(target)) {
							result.add(eobj);
						} else if (referencedObject.equals(target)) {
							result.add(eobj);
						}
					}
				}
			}
		}
		
		return result;
	}
	
	public static Set<EObject> findOppositeForFeatures(EObject target, EReference... references) {
		Set<EObject> currentLevel = new HashSet<EObject>();
		currentLevel.add(target);
		Set<EObject> nextLevel = new HashSet<EObject>();
		
		for (EReference reference : references) {
			nextLevel.clear();
			for (EObject eObj : currentLevel)
				nextLevel.addAll(findOppositeForFeature(eObj, reference));
			
			// swap references
			Set<EObject> swap = currentLevel;
			currentLevel = nextLevel;
			nextLevel = swap;
		}
		
		return currentLevel;
	}
	
	/**
	 * Copied from {@link DefaultTUIDCalculatorAndResolver#getValueOfAttribute(EObject, String)}.
	 * Tries to obtain the value of the feature named <code>featureName</code> from the given {@link EObject}.
	 * If the feature is of type <code>EString</code>, it is returned directly, if it is of type <code>EInt</code>,
	 * it is converted to the integers string representation.
	 */
	// TODO: refactor into helper class that both DefaultTUIDCalculatorAndResolver and AttributeTUIDCalculatorAndResolver use.
    public static String getStringValueOfAttribute(final EObject eObject, final String featureName) {
        EStructuralFeature idFeature = eObject.eClass().getEStructuralFeature(featureName);
        if (idFeature != null && idFeature instanceof EAttribute) {
            EAttribute idAttribute = (EAttribute) idFeature;
            EDataType eAttributeType = idAttribute.getEAttributeType();
            EDataType eString = EcorePackage.eINSTANCE.getEString();
            if (eString != null && eString.equals(eAttributeType)) {
                return (String) eObject.eGet(idFeature);
            }
            EDataType eInt = EcorePackage.eINSTANCE.getEInt();
            if (eInt != null && eString.equals(eAttributeType)) {
                return String.valueOf(eObject.eGet(idFeature));
            }
        }
        return null;
    }
}
{'fraction_non_alphanumeric': 0.10862437633642195, 'fraction_numerical': 0.0, 'mean_word_length': 3.7727891156462583, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 39, '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': '4160519', 'n_tokens_mistral': 2352, 'n_tokens_neox': 2102, 'n_words': 460}