<commit_before>package fr.inria.diverse.trace.commons

import org.eclipse.emf.ecore.EReference
import org.eclipse.emf.ecore.EcoreFactory
import org.eclipse.emf.ecore.EClass
import org.eclipse.emf.ecore.EClassifier
import org.eclipse.emf.ecore.EAttribute
import org.eclipse.emf.ecore.EStructuralFeature
import org.eclipse.emf.ecore.EDataType
import org.eclipse.emf.ecore.EPackage
import org.eclipse.emf.codegen.ecore.genmodel.GenClassifier
import org.eclipse.emf.codegen.ecore.genmodel.GenPackage
import java.util.Set
import org.eclipse.emf.ecore.EOperation

class EcoreCraftingUtil {
	public static def EReference addReferenceToClass(EClass clazz, String refName, EClassifier refType) {
		val res = EcoreFactory.eINSTANCE.createEReference
		res.name = refName
		res.EType = refType
		clazz.EStructuralFeatures.add(res)
		return res
	}

	public static def EReference addReferenceToClass(EClass clazz, String refName, EClass refType) {
		if (clazz != null && refName != null && refName != "" && refType != null) {
			val res = EcoreFactory.eINSTANCE.createEReference
			res.name = refName
			res.EType = refType
			clazz.EStructuralFeatures.add(res)
			return res
		} else {
			return null
		}

	}

	public static def String getBaseFQN(EOperation o) {
		val EClass c = o.EContainingClass
		return EcoreCraftingUtil.getBaseFQN(c) + "." + o.name
	}

	public static def EAttribute addAttributeToClass(EClass clazz, String attName, EDataType attType) {
		val res = EcoreFactory.eINSTANCE.createEAttribute
		res.name = attName
		res.EType = attType
		clazz.EStructuralFeatures.add(res)
		return res
	}

	public static def EStructuralFeature addFeatureToClass(EClass clazz, String name, EClassifier type) {
		var EStructuralFeature res = null
		if (type instanceof EDataType)
			res = EcoreFactory.eINSTANCE.createEAttribute
		else if (type instanceof EClass)
			res = EcoreFactory.eINSTANCE.createEReference
		res.name = name
		res.EType = type
		clazz.EStructuralFeatures.add(res)
		return res
	}

	public static def String getFQN(EClassifier c, String separator) {
		val EPackage p = c.getEPackage
		if (p != null) {
			return getEPackageFQN(p, separator) + separator + c.name
		} else {
			return c.name
		}
	}

	public static def String getEPackageFQN(EPackage p, String separator) {
		val EPackage superP = p.getESuperPackage
		if (superP != null) {
			return getEPackageFQN(superP, separator) + separator + p.name
		} else {
			return p.name
		}
	}

	public static def String getBaseFQN(EClassifier c) {
		if (c != null) {
			val EPackage p = c.getEPackage
			if (p != null) {
				return getBaseFQN(p) + "." + c.name
			} else {
				return c.name
			}
		} else {
			return ""
		}
	}

	public static def String getJavaFQN(EClassifier c, Set<GenPackage> refGenPackages) {

		if (c.instanceClassName != null && c.instanceClassName != "")
			return c.instanceClassName

		var String base = ""
		val gc = getGenClassifier(c, refGenPackages)
		if (gc != null) {
			if (gc.genPackage.basePackage != null) {
				base = gc.genPackage.basePackage + "."
			}
		}
		return base + getBaseFQN(c);
	}

	public static def String getBaseFQN(EPackage p) {
		val EPackage superP = p.getESuperPackage
		if (superP != null) {
			return getBaseFQN(superP) + "." + p.name
		} else {
			return p.name
		}
	}

	public static def String getJavaFQN(EPackage p, Set<GenPackage> refGenPackages) {

		var String base = ""
		val gp = getGenPackage(p, refGenPackages)
		if (gp != null) {
			if (gp.basePackage != null) {
				base = gp.basePackage + "."
			}
		}
		return base + getBaseFQN(p);
	}

	public static def GenClassifier getGenClassifier(EClassifier c, Set<GenPackage> refGenPackages) {
		if (c != null) {
			for (gp : refGenPackages) {
				for (gc : gp.eAllContents.filter(GenClassifier).toSet) {
					val ecoreClass = gc.ecoreClassifier
					if (ecoreClass != null) {
						val s1 = getBaseFQN(ecoreClass)
						val s2 = getBaseFQN(c)
						if (s1 != null && s2 != null && s1.equalsIgnoreCase(s2)) {
							return gc
						}
					}
				}
			}

		}
		return null
	}

	public static def GenPackage getGenPackage(EPackage p, Set<GenPackage> refGenPackages) {
		if (p != null) {
			for (gp : refGenPackages) {
				val packageInGenpackage = gp.getEcorePackage
				if (packageInGenpackage != null) {
					val s1 = getBaseFQN(p)
					val s2 = getBaseFQN(packageInGenpackage)
					if (s1 != null && s2 != null && s1.equalsIgnoreCase(s2)) {
						return gp
					}
				}
			}
		}
		return null
	}

	public static def String stringCreate(EClass c) {
		val EPackage p = c.EPackage
		return EcoreCraftingUtil.getBaseFQN(p) + "." + p.name.toFirstUpper + "Factory.eINSTANCE.create" + c.name + "()"
	}

	public static def String stringCreateImplicitStep(EClass c) {
		val EPackage p = c.EPackage
		return EcoreCraftingUtil.getBaseFQN(p) + "." + p.name.toFirstUpper + "Factory.eINSTANCE.create" + c.name + "_ImplicitStep()"
	}

	public static def String stringGetter(EStructuralFeature f) {
		if (f instanceof EAttribute) {
			if (f.EAttributeType.name.equals("EBoolean")) {
				return "is" + f.name.toFirstUpper + "()"
			}
		}
		return "get" + f.name.toFirstUpper + "()"
	}

	public static def String stringGetter(String s) {
		return "get" + s.toFirstUpper + "()"
	}

	public static def String stringSetter(EStructuralFeature f, String value) {
		return "set" + f.name.toFirstUpper + "(" + value + ")"
	}

	public static def String stringSetter(String f, String value) {
		return "set" + f.toFirstUpper + "(" + value + ")"
	}

}<commit_msg>Add ecore crafting methods parameters to choose to enforce java primitives fqns<commit_after>package fr.inria.diverse.trace.commons

import org.eclipse.emf.ecore.EReference
import org.eclipse.emf.ecore.EcoreFactory
import org.eclipse.emf.ecore.EClass
import org.eclipse.emf.ecore.EClassifier
import org.eclipse.emf.ecore.EAttribute
import org.eclipse.emf.ecore.EStructuralFeature
import org.eclipse.emf.ecore.EDataType
import org.eclipse.emf.ecore.EPackage
import org.eclipse.emf.codegen.ecore.genmodel.GenClassifier
import org.eclipse.emf.codegen.ecore.genmodel.GenPackage
import java.util.Set
import org.eclipse.emf.ecore.EOperation

class EcoreCraftingUtil {
	public static def EReference addReferenceToClass(EClass clazz, String refName, EClassifier refType) {
		val res = EcoreFactory.eINSTANCE.createEReference
		res.name = refName
		res.EType = refType
		clazz.EStructuralFeatures.add(res)
		return res
	}

	public static def EReference addReferenceToClass(EClass clazz, String refName, EClass refType) {
		if (clazz != null && refName != null && refName != "" && refType != null) {
			val res = EcoreFactory.eINSTANCE.createEReference
			res.name = refName
			res.EType = refType
			clazz.EStructuralFeatures.add(res)
			return res
		} else {
			return null
		}

	}

	public static def String getBaseFQN(EOperation o) {
		val EClass c = o.EContainingClass
		return EcoreCraftingUtil.getBaseFQN(c) + "." + o.name
	}

	public static def EAttribute addAttributeToClass(EClass clazz, String attName, EDataType attType) {
		val res = EcoreFactory.eINSTANCE.createEAttribute
		res.name = attName
		res.EType = attType
		clazz.EStructuralFeatures.add(res)
		return res
	}

	public static def EStructuralFeature addFeatureToClass(EClass clazz, String name, EClassifier type) {
		var EStructuralFeature res = null
		if (type instanceof EDataType)
			res = EcoreFactory.eINSTANCE.createEAttribute
		else if (type instanceof EClass)
			res = EcoreFactory.eINSTANCE.createEReference
		res.name = name
		res.EType = type
		clazz.EStructuralFeatures.add(res)
		return res
	}

	public static def String getFQN(EClassifier c, String separator) {
		val EPackage p = c.getEPackage
		if (p != null) {
			return getEPackageFQN(p, separator) + separator + c.name
		} else {
			return c.name
		}
	}

	public static def String getEPackageFQN(EPackage p, String separator) {
		val EPackage superP = p.getESuperPackage
		if (superP != null) {
			return getEPackageFQN(superP, separator) + separator + p.name
		} else {
			return p.name
		}
	}

	public static def String getBaseFQN(EClassifier c) {
		if (c != null) {
			val EPackage p = c.getEPackage
			if (p != null) {
				return getBaseFQN(p) + "." + c.name
			} else {
				return c.name
			}
		} else {
			return ""
		}
	}

	public static def String getJavaFQN(EClassifier c, Set<GenPackage> refGenPackages) {
		getJavaFQN(c, refGenPackages, false)
	}

	public static def String getJavaFQN(EClassifier c, Set<GenPackage> refGenPackages,
		boolean enforcePrimitiveJavaClasses) {

		if (c.instanceClass != null) {
			if (enforcePrimitiveJavaClasses) {
				switch (c.instanceClass.canonicalName) {
					case "int": return "java.lang.Integer"
					case "boolean": return "java.lang.Boolean"
				// TODO
				}
			}
			return c.instanceClass.canonicalName

		}

		if (c.instanceClassName != null && c.instanceClassName != "")
			return c.instanceClassName

		var String base = ""
		val gc = getGenClassifier(c, refGenPackages)
		if (gc != null) {
			if (gc.genPackage.basePackage != null) {
				base = gc.genPackage.basePackage + "."
			}
		}
		return base + getBaseFQN(c);
	}

	public static def String getBaseFQN(EPackage p) {
		val EPackage superP = p.getESuperPackage
		if (superP != null) {
			return getBaseFQN(superP) + "." + p.name
		} else {
			return p.name
		}
	}

	public static def String getJavaFQN(EPackage p, Set<GenPackage> refGenPackages) {

		var String base = ""
		val gp = getGenPackage(p, refGenPackages)
		if (gp != null) {
			if (gp.basePackage != null) {
				base = gp.basePackage + "."
			}
		}
		return base + getBaseFQN(p);
	}

	public static def GenClassifier getGenClassifier(EClassifier c, Set<GenPackage> refGenPackages) {
		if (c != null) {
			for (gp : refGenPackages) {
				for (gc : gp.eAllContents.filter(GenClassifier).toSet) {
					val ecoreClass = gc.ecoreClassifier
					if (ecoreClass != null) {
						val s1 = getBaseFQN(ecoreClass)
						val s2 = getBaseFQN(c)
						if (s1 != null && s2 != null && s1.equalsIgnoreCase(s2)) {
							return gc
						}
					}
				}
			}

		}
		return null
	}

	public static def GenPackage getGenPackage(EPackage p, Set<GenPackage> refGenPackages) {
		if (p != null) {
			for (gp : refGenPackages) {
				val packageInGenpackage = gp.getEcorePackage
				if (packageInGenpackage != null) {
					val s1 = getBaseFQN(p)
					val s2 = getBaseFQN(packageInGenpackage)
					if (s1 != null && s2 != null && s1.equalsIgnoreCase(s2)) {
						return gp
					}
				}
			}
		}
		return null
	}

	public static def String stringCreate(EClass c) {
		val EPackage p = c.EPackage
		return EcoreCraftingUtil.getBaseFQN(p) + "." + p.name.toFirstUpper + "Factory.eINSTANCE.create" + c.name + "()"
	}

	public static def String stringCreateImplicitStep(EClass c) {
		val EPackage p = c.EPackage
		return EcoreCraftingUtil.getBaseFQN(p) + "." + p.name.toFirstUpper + "Factory.eINSTANCE.create" + c.name +
			"_ImplicitStep()"
	}

	public static def String stringGetter(EStructuralFeature f) {
		if (f instanceof EAttribute) {
			if (f.EAttributeType.name.equals("EBoolean")) {
				return "is" + f.name.toFirstUpper + "()"
			}
		}
		return "get" + f.name.toFirstUpper + "()"
	}

	public static def String stringFeatureID(EStructuralFeature feature, EClassifier containingClass,
		Set<GenPackage> refGenPackages) {
		val EPackage p = containingClass.EPackage
		val GenPackage gp = getGenPackage(p, refGenPackages)
		return EcoreCraftingUtil.getBaseFQN(p) + "." + gp.prefix + "Package.eINSTANCE.get" +
			containingClass.name.toFirstUpper + "_" + feature.name.toFirstUpper + "().getFeatureID()";
	}

	public static def String stringGetter(String s) {
		return "get" + s.toFirstUpper + "()"
	}

	public static def String stringSetter(EStructuralFeature f, String value) {
		return "set" + f.name.toFirstUpper + "(" + value + ")"
	}

	public static def String stringSetter(String f, String value) {
		return "set" + f.toFirstUpper + "(" + value + ")"
	}

}
{'fraction_non_alphanumeric': 0.10995729000921196, 'fraction_numerical': 0.002009881919437233, 'mean_word_length': 3.6197292069632496, 'pattern_counts': {'":': 2, '<': 13, '<?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': '7493632', 'n_tokens_mistral': 4723, 'n_tokens_neox': 4167, 'n_words': 968}