<commit_before>package tools.vitruv.dsls.mapping.generator

import com.google.inject.Inject
import tools.vitruv.extensions.dslsruntime.mapping.AbstractMappingRealization
import tools.vitruv.extensions.dslsruntime.mapping.CandidateGeneratorImpl
import tools.vitruv.extensions.dslsruntime.mapping.MappedCorrespondenceModel
import tools.vitruv.extensions.dslsruntime.mapping.MappingExecutionState
import tools.vitruv.extensions.dslsruntime.mapping.MappingUtil
import tools.vitruv.extensions.dslsruntime.mapping.interfaces.Candidate
import tools.vitruv.extensions.dslsruntime.mapping.interfaces.CandidateGenerator
import tools.vitruv.extensions.dslsruntime.mapping.interfaces.MappingRealization
import tools.vitruv.extensions.dslsruntime.mapping.interfaces.MatchUpdate
import tools.vitruv.dsls.mapping.helpers.TemplateGenerator
import tools.vitruv.dsls.mapping.mappingLanguage.Mapping
import tools.vitruv.dsls.mapping.mappingLanguage.MappingFile
import tools.vitruv.dsls.mapping.util.PreProcessingFileSystemAccess
import tools.vitruv.framework.correspondence.Correspondence
import tools.vitruv.framework.util.bridges.JavaHelper
import java.util.Arrays
import java.util.Collection
import java.util.Collections
import java.util.List
import java.util.Optional
import java.util.Set
import org.eclipse.emf.ecore.EClass
import org.eclipse.emf.ecore.EObject
import org.eclipse.emf.ecore.EPackage
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.generator.IFileSystemAccess
import org.eclipse.xtext.generator.IGenerator

import static extension tools.vitruv.dsls.mapping.helpers.MappingLanguageHelper.*
import static extension tools.vitruv.framework.util.bridges.JavaHelper.*
import tools.vitruv.framework.change.echange.EChange
import tools.vitruv.framework.modelsynchronization.blackboard.Blackboard
import tools.vitruv.dsls.reactions.api.generator.ReactionBuilderFactory

class MappingLanguageGenerator implements IMappingLanguageGenerator {
	public final static String PACKAGE_NAME_FIELD = "tools.vitruv.dsls.mapping.generator.MappingLanguageGenerator.PACKAGE_NAME_FIELD"
	
	@Inject
	private IGenerator delegateGenerator

	@Inject
	private extension MappingLanguageGeneratorNameProvider nameProvider

	private extension EMFGeneratorHelper emfGeneratorHelper
	private extension TemplateGenerator templateGenerator
	

	
	override def void initialize() {
		nameProvider.initialize
	}

	override generateAndCreateReactions(Collection<Resource> inputResources, IFileSystemAccess inputFsa) {
		val fsa = PreProcessingFileSystemAccess.createJavaFormattingFSA(inputFsa)
		
		emfGeneratorHelper = new EMFGeneratorHelper(nameProvider)
		templateGenerator = new TemplateGenerator
		val reactions = newHashMap
		
		for (resource : inputResources) {
			delegateGenerator.doGenerate(resource, fsa)
			val statefulGenerator = new StatefulMappingLanguageGenerator(resource, emfGeneratorHelper, templateGenerator, nameProvider)
			val generatedReactions = statefulGenerator.generateReactions()
			reactions.put(resource, generatedReactions)
		}
		
		templateGenerator.generateAllTemplates(fsa)
		emfGeneratorHelper.generateCode(fsa)
		
		return reactions
	}
	
	/**
	 * This method is called during the editing for the JvmModelInferrer
	 */
	override doGenerate(Resource input, IFileSystemAccess inputFsa) {
		// to always achieve the same naming, the name provider has to be reset here
		nameProvider.initialize
		delegateGenerator.doGenerate(input, inputFsa)
	}
	
	/**
	 * Nested static class, to make the state local to the
	 * stateful generator.
	 * Is instantiated for each resource.
	 */
	private static class StatefulMappingLanguageGenerator {
		public new(Resource resource, EMFGeneratorHelper emfGeneratorHelper, TemplateGenerator templateGenerator, MappingLanguageGeneratorNameProvider nameProvider) {
			this.resource = resource
			this.emfGeneratorHelper = emfGeneratorHelper
			this.templateGenerator = templateGenerator
			this.nameProvider = nameProvider
			
			this.reactionBuilderFactory = new ReactionBuilderFactory
		}

		private Resource resource
		
		private extension EMFGeneratorHelper emfGeneratorHelper
		private extension MappingLanguageGeneratorNameProvider nameProvider
		private extension TemplateGenerator templateGenerator

		private extension ReactionBuilderFactory reactionBuilderFactory
		
		private extension MappingLanguageGeneratorState state
		private extension ConstraintLanguageGenerator clg
	
		private def generateReactions() {
			val mappingFile = resource.contents.get(0).requireType(MappingFile)
	
			this.state = new MappingLanguageGeneratorState(mappingFile)
			this.clg = new ConstraintLanguageGenerator(emfGeneratorHelper, nameProvider, this.state)
			
			val importToAllCalls = newHashMap(imports.map[it -> newArrayList])
			for (mapping : mappings) {
				val mappingCalls = mapping.generateMappingClass
				mappingCalls.forEach[imp, call |
					importToAllCalls.get(imp).add(call)
				]
			}
			
			(new DefaultContainmentGenerator(state, templateGenerator, nameProvider, clg)).generate
	
			val reactions = newArrayList
			for (imp : imports) {
				val reaction = reactionBuilderFactory.createReactionBuilder
					.setName(getReactionName(imp, resource))
					.setTrigger(imp.package)
					.setTargetChange(imp.otherImport.package)
					.setExecutionBlock('''
						final tools.vitruv.extensions.dslsruntime.mapping.MappingExecutionState state =
							new tools.vitruv.extensions.dslsruntime.mapping.MappingExecutionState(transformationResult, this.userInteracting, blackboard);
						«importToAllCalls.get(imp).join("\n")»
						tools.vitruv.extensions.dslsruntime.mapping.DefaultContainmentMapping
							.INSTANCE.applyEChange(change, blackboard, state);
					''')
					.generateReaction
				reactions.add(reaction)
			}
			return reactions
		}
	
<commit_msg>Adapt mappings language to external Reactions generation API<commit_after>package tools.vitruv.dsls.mapping.generator

import com.google.inject.Inject
import tools.vitruv.extensions.dslsruntime.mapping.AbstractMappingRealization
import tools.vitruv.extensions.dslsruntime.mapping.CandidateGeneratorImpl
import tools.vitruv.extensions.dslsruntime.mapping.MappedCorrespondenceModel
import tools.vitruv.extensions.dslsruntime.mapping.MappingExecutionState
import tools.vitruv.extensions.dslsruntime.mapping.MappingUtil
import tools.vitruv.extensions.dslsruntime.mapping.interfaces.Candidate
import tools.vitruv.extensions.dslsruntime.mapping.interfaces.CandidateGenerator
import tools.vitruv.extensions.dslsruntime.mapping.interfaces.MappingRealization
import tools.vitruv.extensions.dslsruntime.mapping.interfaces.MatchUpdate
import tools.vitruv.dsls.mapping.helpers.TemplateGenerator
import tools.vitruv.dsls.mapping.mappingLanguage.Mapping
import tools.vitruv.dsls.mapping.mappingLanguage.MappingFile
import tools.vitruv.dsls.mapping.util.PreProcessingFileSystemAccess
import tools.vitruv.framework.correspondence.Correspondence
import tools.vitruv.framework.util.bridges.JavaHelper
import java.util.Arrays
import java.util.Collection
import java.util.Collections
import java.util.List
import java.util.Optional
import java.util.Set
import org.eclipse.emf.ecore.EClass
import org.eclipse.emf.ecore.EObject
import org.eclipse.emf.ecore.EPackage
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.generator.IFileSystemAccess
import org.eclipse.xtext.generator.IGenerator

import static extension tools.vitruv.dsls.mapping.helpers.MappingLanguageHelper.*
import static extension tools.vitruv.framework.util.bridges.JavaHelper.*
import tools.vitruv.framework.change.echange.EChange
import tools.vitruv.framework.modelsynchronization.blackboard.Blackboard
import tools.vitruv.dsls.reactions.api.generator.ReactionBuilderFactory

class MappingLanguageGenerator implements IMappingLanguageGenerator {
	public final static String PACKAGE_NAME_FIELD = "tools.vitruv.dsls.mapping.generator.MappingLanguageGenerator.PACKAGE_NAME_FIELD"
	
	@Inject
	private IGenerator delegateGenerator

	@Inject
	private extension MappingLanguageGeneratorNameProvider nameProvider

	private extension EMFGeneratorHelper emfGeneratorHelper
	private extension TemplateGenerator templateGenerator
	

	
	override def void initialize() {
		nameProvider.initialize
	}

	override generateAndCreateReactions(Collection<Resource> inputResources, IFileSystemAccess inputFsa) {
		val fsa = PreProcessingFileSystemAccess.createJavaFormattingFSA(inputFsa)
		
		emfGeneratorHelper = new EMFGeneratorHelper(nameProvider)
		templateGenerator = new TemplateGenerator
		val reactions = newHashMap
		
		for (resource : inputResources) {
			delegateGenerator.doGenerate(resource, fsa)
			val statefulGenerator = new StatefulMappingLanguageGenerator(resource, emfGeneratorHelper, templateGenerator, nameProvider)
			val generatedReactions = statefulGenerator.generateReactions()
			reactions.put(resource, generatedReactions)
		}
		
		templateGenerator.generateAllTemplates(fsa)
		emfGeneratorHelper.generateCode(fsa)
		
		return reactions
	}
	
	/**
	 * This method is called during the editing for the JvmModelInferrer
	 */
	override doGenerate(Resource input, IFileSystemAccess inputFsa) {
		// to always achieve the same naming, the name provider has to be reset here
		nameProvider.initialize
		delegateGenerator.doGenerate(input, inputFsa)
	}
	
	/**
	 * Nested static class, to make the state local to the
	 * stateful generator.
	 * Is instantiated for each resource.
	 */
	private static class StatefulMappingLanguageGenerator {
		public new(Resource resource, EMFGeneratorHelper emfGeneratorHelper, TemplateGenerator templateGenerator, MappingLanguageGeneratorNameProvider nameProvider) {
			this.resource = resource
			this.emfGeneratorHelper = emfGeneratorHelper
			this.templateGenerator = templateGenerator
			this.nameProvider = nameProvider
			
			this.reactionBuilderFactory = new ReactionBuilderFactory
		}

		private Resource resource
		
		private extension EMFGeneratorHelper emfGeneratorHelper
		private extension MappingLanguageGeneratorNameProvider nameProvider
		private extension TemplateGenerator templateGenerator

		private extension ReactionBuilderFactory reactionBuilderFactory
		
		private extension MappingLanguageGeneratorState state
		private extension ConstraintLanguageGenerator clg
	
		private def generateReactions() {
			val mappingFile = resource.contents.get(0).requireType(MappingFile)
	
			this.state = new MappingLanguageGeneratorState(mappingFile)
			this.clg = new ConstraintLanguageGenerator(emfGeneratorHelper, nameProvider, this.state)
			
			val importToAllCalls = newHashMap(imports.map[it -> newArrayList])
			for (mapping : mappings) {
				val mappingCalls = mapping.generateMappingClass
				mappingCalls.forEach[imp, call |
					importToAllCalls.get(imp).add(call)
				]
			}
			
			(new DefaultContainmentGenerator(state, templateGenerator, nameProvider, clg)).generate
	
			val reactions = newArrayList
			for (imp : imports) {
				val reaction = reactionBuilderFactory.createReactionBuilder
					.setName(getReactionName(imp, resource))
					.setTrigger(null)// TODO MK: Set correct domain here, before: imp.package
					.setTargetChange(null) // TODO MK: Set correct domain here, before: imp.otherImport.package
					.setExecutionBlock('''
						final tools.vitruv.extensions.dslsruntime.mapping.MappingExecutionState state =
							new tools.vitruv.extensions.dslsruntime.mapping.MappingExecutionState(transformationResult, this.userInteracting, blackboard);
						«importToAllCalls.get(imp).join("\n")»
						tools.vitruv.extensions.dslsruntime.mapping.DefaultContainmentMapping
							.INSTANCE.applyEChange(change, blackboard, state);
					''')
					.generateReaction
				reactions.add(reaction)
			}
			return reactions
		}
	

{'fraction_non_alphanumeric': 0.06846755011892626, 'fraction_numerical': 0.00016989466530750936, 'mean_word_length': 5.731275014293883, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '146347', 'n_tokens_mistral': 3577, 'n_tokens_neox': 3114, 'n_words': 635}