<commit_before>package anatlyzer.atl.witness;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EEnum;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.EcoreFactory;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.impl.ResourceImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.util.EcoreUtil.Copier;

import anatlyzer.atl.analyser.Analyser;
import anatlyzer.atl.analyser.IAnalyserResult;
import anatlyzer.atl.analyser.namespaces.MetamodelNamespace;
import anatlyzer.atl.util.ATLUtils;
import anatlyzer.atl.util.ATLUtils.ModelInfo;

public class SourceMetamodelsData implements IMetamodelRewrite {

	private EPackage root;
	private HashMap<EObject, EObject> trace;
	private IAnalyserResult result;

	protected SourceMetamodelsData(IAnalyserResult result, EPackage newRoot, HashMap<EObject, EObject> trace) {
		this.root = newRoot;
		this.trace = trace;
		this.result = result;
	}
	
	@Override
	public IAnalyserResult getAnalysis() {
		return result;
	}

	public static SourceMetamodelsData get(Analyser 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);
		}
		
		MetamodelNamespace mmm = analyser.getNamespaces().getMetaMetamodel();		
		addToCopier(copier, r, mmm.getName(), mmm);
		
		
		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);
	}

	private static void addToCopier(Copier copier, Resource r, String metamodelName,
			MetamodelNamespace ns) {
		copier.copyAll(ns.getResource().getContents());
		copier.copyReferences();<commit_msg>Hopefully fixed issue with datatypes and implicitly copied ecore meta-model (for constraint solving meta-model adaptation)<commit_after>package anatlyzer.atl.witness;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EEnum;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.EcoreFactory;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.impl.ResourceImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.util.EcoreUtil.Copier;

import anatlyzer.atl.analyser.Analyser;
import anatlyzer.atl.analyser.IAnalyserResult;
import anatlyzer.atl.analyser.namespaces.MetamodelNamespace;
import anatlyzer.atl.util.ATLUtils;
import anatlyzer.atl.util.ATLUtils.ModelInfo;

public class SourceMetamodelsData implements IMetamodelRewrite {

	private EPackage root;
	private HashMap<EObject, EObject> trace;
	private IAnalyserResult result;

	protected SourceMetamodelsData(IAnalyserResult result, EPackage newRoot, HashMap<EObject, EObject> trace) {
		this.root = newRoot;
		this.trace = trace;
		this.result = result;
	}
	
	@Override
	public IAnalyserResult getAnalysis() {
		return result;
	}

	public static SourceMetamodelsData get(Analyser 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);
	}


	private static void addToCopier(Copier copier, Resource r, String metamodelName,
			MetamodelNamespace ns) {
		copier.copyAll(ns.getResource().getContents());
		copier.copyReferences();
{'fraction_non_alphanumeric': 0.1221273801707157, 'fraction_numerical': 0.0006565988181221273, 'mean_word_length': 4.758979206049149, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, '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': '1894294', 'n_tokens_mistral': 2309, 'n_tokens_neox': 2073, 'n_words': 382}