<commit_before>package edu.kit.ipd.sdq.vitruvius.dsls.mapping.generator

import com.google.inject.Inject
import edu.kit.ipd.sdq.vitruvius.dsls.mapping.api.AbstractMappingRealization
import edu.kit.ipd.sdq.vitruvius.dsls.mapping.api.CandidateGeneratorImpl
import edu.kit.ipd.sdq.vitruvius.dsls.mapping.api.MappedCorrespondenceInstance
import edu.kit.ipd.sdq.vitruvius.dsls.mapping.api.MappingExecutionState
import edu.kit.ipd.sdq.vitruvius.dsls.mapping.api.MappingUtil
import edu.kit.ipd.sdq.vitruvius.dsls.mapping.api.interfaces.Candidate
import edu.kit.ipd.sdq.vitruvius.dsls.mapping.api.interfaces.CandidateGenerator
import edu.kit.ipd.sdq.vitruvius.dsls.mapping.api.interfaces.MappingRealization
import edu.kit.ipd.sdq.vitruvius.dsls.mapping.api.interfaces.MatchUpdate
import edu.kit.ipd.sdq.vitruvius.dsls.mapping.helpers.TemplateGenerator
import edu.kit.ipd.sdq.vitruvius.dsls.mapping.mappingLanguage.Mapping
import edu.kit.ipd.sdq.vitruvius.dsls.mapping.mappingLanguage.MappingFile
import edu.kit.ipd.sdq.vitruvius.dsls.mapping.util.PreProcessingFileSystemAccess
import edu.kit.ipd.sdq.vitruvius.dsls.response.api.generator.ResponseBuilderFactory
import edu.kit.ipd.sdq.vitruvius.framework.contracts.datatypes.Blackboard
import edu.kit.ipd.sdq.vitruvius.framework.contracts.meta.correspondence.Correspondence
import edu.kit.ipd.sdq.vitruvius.framework.meta.change.EChange
import edu.kit.ipd.sdq.vitruvius.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 edu.kit.ipd.sdq.vitruvius.dsls.mapping.helpers.MappingLanguageHelper.*
import static extension edu.kit.ipd.sdq.vitruvius.framework.util.bridges.JavaHelper.*

class MappingLanguageGenerator implements IMappingLanguageGenerator {
	public final static String PACKAGE_NAME_FIELD = "edu.kit.ipd.sdq.vitruvius.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 generateAndCreateResponses(Collection<Resource> inputResources, IFileSystemAccess inputFsa) {
		val fsa = PreProcessingFileSystemAccess.createJavaFormattingFSA(inputFsa)
		
		emfGeneratorHelper = new EMFGeneratorHelper(nameProvider)
		templateGenerator = new TemplateGenerator
		val responses = newHashMap
		
		for (resource : inputResources) {
			delegateGenerator.doGenerate(resource, fsa)
			val statefulGenerator = new StatefulMappingLanguageGenerator(resource, emfGeneratorHelper, templateGenerator, nameProvider)
			responses.put(resource, statefulGenerator.generateResource())
		}
		
		templateGenerator.generateAllTemplates(fsa)
		emfGeneratorHelper.generateCode(fsa)
		
		return responses
	}
	
	/**
	 * 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.responseBuilderFactory = new ResponseBuilderFactory
		}

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

		private extension ResponseBuilderFactory responseBuilderFactory
		
		private extension MappingLanguageGeneratorState state
		private extension ConstraintLanguageGenerator clg
	
		private def generateResource() {
			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 responses = newArrayList
			for (imp : imports) {
				val response = responseBuilderFactory.createResponseBuilder
					.setName(getResponseName(imp, resource))
					.setTrigger(imp.package)
					.setTargetChange(imp.otherImport.package)
					.setExecutionBlock('''
						final edu.kit.ipd.sdq.vitruvius.dsls.mapping.api.MappingExecutionState state =
							new edu.kit.ipd.sdq.vitruvius.dsls.mapping.api.MappingExecutionState(transformationResult, this.userInteracting, blackboard);
						«importToAllCalls.get(imp).join("\n")»
						edu.kit.ipd.sdq.vitruvius.dsls.mapping.api.DefaultContainmentMapping
							.INSTANCE.applyEChange(change, blackboard, state);
					''')
					.generateResponse<commit_msg>renamed method<commit_after>package edu.kit.ipd.sdq.vitruvius.dsls.mapping.generator

import com.google.inject.Inject
import edu.kit.ipd.sdq.vitruvius.dsls.mapping.api.AbstractMappingRealization
import edu.kit.ipd.sdq.vitruvius.dsls.mapping.api.CandidateGeneratorImpl
import edu.kit.ipd.sdq.vitruvius.dsls.mapping.api.MappedCorrespondenceInstance
import edu.kit.ipd.sdq.vitruvius.dsls.mapping.api.MappingExecutionState
import edu.kit.ipd.sdq.vitruvius.dsls.mapping.api.MappingUtil
import edu.kit.ipd.sdq.vitruvius.dsls.mapping.api.interfaces.Candidate
import edu.kit.ipd.sdq.vitruvius.dsls.mapping.api.interfaces.CandidateGenerator
import edu.kit.ipd.sdq.vitruvius.dsls.mapping.api.interfaces.MappingRealization
import edu.kit.ipd.sdq.vitruvius.dsls.mapping.api.interfaces.MatchUpdate
import edu.kit.ipd.sdq.vitruvius.dsls.mapping.helpers.TemplateGenerator
import edu.kit.ipd.sdq.vitruvius.dsls.mapping.mappingLanguage.Mapping
import edu.kit.ipd.sdq.vitruvius.dsls.mapping.mappingLanguage.MappingFile
import edu.kit.ipd.sdq.vitruvius.dsls.mapping.util.PreProcessingFileSystemAccess
import edu.kit.ipd.sdq.vitruvius.dsls.response.api.generator.ResponseBuilderFactory
import edu.kit.ipd.sdq.vitruvius.framework.contracts.datatypes.Blackboard
import edu.kit.ipd.sdq.vitruvius.framework.contracts.meta.correspondence.Correspondence
import edu.kit.ipd.sdq.vitruvius.framework.meta.change.EChange
import edu.kit.ipd.sdq.vitruvius.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 edu.kit.ipd.sdq.vitruvius.dsls.mapping.helpers.MappingLanguageHelper.*
import static extension edu.kit.ipd.sdq.vitruvius.framework.util.bridges.JavaHelper.*

class MappingLanguageGenerator implements IMappingLanguageGenerator {
	public final static String PACKAGE_NAME_FIELD = "edu.kit.ipd.sdq.vitruvius.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 generateAndCreateResponses(Collection<Resource> inputResources, IFileSystemAccess inputFsa) {
		val fsa = PreProcessingFileSystemAccess.createJavaFormattingFSA(inputFsa)
		
		emfGeneratorHelper = new EMFGeneratorHelper(nameProvider)
		templateGenerator = new TemplateGenerator
		val responses = newHashMap
		
		for (resource : inputResources) {
			delegateGenerator.doGenerate(resource, fsa)
			val statefulGenerator = new StatefulMappingLanguageGenerator(resource, emfGeneratorHelper, templateGenerator, nameProvider)
			val generatedResponses = statefulGenerator.generateResponses()
			responses.put(resource, generatedResponses)
		}
		
		templateGenerator.generateAllTemplates(fsa)
		emfGeneratorHelper.generateCode(fsa)
		
		return responses
	}
	
	/**
	 * 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.responseBuilderFactory = new ResponseBuilderFactory
		}

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

		private extension ResponseBuilderFactory responseBuilderFactory
		
		private extension MappingLanguageGeneratorState state
		private extension ConstraintLanguageGenerator clg
	
		private def generateResponses() {
			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 responses = newArrayList
			for (imp : imports) {
				val response = responseBuilderFactory.createResponseBuilder
					.setName(getResponseName(imp, resource))
					.setTrigger(imp.package)
					.setTargetChange(imp.otherImport.package)
					.setExecutionBlock('''
						final edu.kit.ipd.sdq.vitruvius.dsls.mapping.api.MappingExecutionState state =
							new edu.kit.ipd.sdq.vitruvius.dsls.mapping.api.MappingExecutionState(transformationResult, this.userInteracting, blackboard);
						«importToAllCalls.get(imp).join("\n")»
						edu.kit.ipd.sdq.vitruvius.dsls.mapping.api.DefaultContainmentMapping
							.INSTANCE.applyEChange(change, blackboard, state);
					''')
					.generateResponse
{'fraction_non_alphanumeric': 0.07973845108695653, 'fraction_numerical': 0.00016983695652173913, 'mean_word_length': 5.446086480569239, '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': '2504080', 'n_tokens_mistral': 3936, 'n_tokens_neox': 3507, 'n_words': 603}