<commit_before>package es.um.nosql.schemainference.m2t.gen.mongoose

import es.um.nosql.schemainference.entitydifferentiation.EntityDifferentiation
import es.um.nosql.schemainference.util.emf.ResourceManager
import es.um.nosql.schemainference.entitydifferentiation.EntitydifferentiationPackage
import es.um.nosql.schemainference.NoSQLSchema.NoSQLSchemaPackage
import java.io.File
import es.um.nosql.schemainference.entitydifferentiation.EntityDiffSpec
import java.util.List
import java.io.PrintStream
import es.um.nosql.schemainference.entitydifferentiation.PropertySpec
import es.um.nosql.schemainference.NoSQLSchema.PrimitiveType
import es.um.nosql.schemainference.NoSQLSchema.Attribute
import es.um.nosql.schemainference.NoSQLSchema.Tuple
import es.um.nosql.schemainference.NoSQLSchema.Reference
import es.um.nosql.schemainference.NoSQLSchema.Aggregate
import es.um.nosql.schemainference.NoSQLSchema.Entity
import java.util.HashMap
import java.util.Set
import java.util.regex.Pattern
import java.util.Map

class DiffToMongoose
{
	static class Label
	{
		var label = ""

		new(String l) {
			label = l
		}

		override toString() {
			label
		}
	}
	
	var modelName = "";

	final val EXACT_TYPE = true
	final val DUCK_TYPE = !EXACT_TYPE

	final val SPECIAL_TYPE_IDENTIFIER = "type"
	
	// list of entities
	List<Entity> entities
	
	HashMap<Entity, Set<Entity>> entityDeps
	HashMap<Entity, Set<Entity>> inverseEntityDeps
	HashMap<Entity, EntityDiffSpec> diffByEntity
	
	static File outputDir

	def static void main(String[] args)
    {
		if (args.length < 1)
		{
			System.out.println("Usage: DiffToMongoose model [outdir]")
			System.exit(-1)
		}

        val inputModel = new File(args.head)
        val ResourceManager rm = new ResourceManager(EntitydifferentiationPackage.eINSTANCE,
        	NoSQLSchemaPackage.eINSTANCE)
        rm.loadResourcesAsStrings(inputModel.getPath())
        val EntityDifferentiation td = rm.resources.head.contents.head as EntityDifferentiation

		outputDir = new File(if (args.length > 1) args.get(1) else ".")
								.toPath().resolve(td.name).toFile()
		// Create destination directory if it does not exist
		outputDir.mkdirs()
        System.out.println("Generating Javascript for "
        					+ inputModel.getPath()
        					+ " in "
        					+ outputDir.getPath())

		val diff_to_mongoose = new DiffToMongoose()
		diff_to_mongoose.generate(td)
		
        System.exit(0)
    }

	def static void writeToFile(String filename, CharSequence toWrite)
	{
		val outFile = outputDir.toPath().resolve(filename).toFile()
		val outFileWriter = new PrintStream(outFile)
        outFileWriter.print(toWrite)
        outFileWriter.close()
	}

	/**
	 * Method used to generate an Inclusive/Exclusive differences file for a NoSQLDifferences object.
	 */
	def generate(EntityDifferentiation diff)
	{
		modelName = diff.name;
		diffByEntity = newHashMap(diff.entityDiffSpecs.map[ed | ed.entity -> ed])

		// Calc dependencies between entities
		val order = calcDeps(diff)

		fillTypeCompatibilityMatrix(diff)
	
		order.forEach[e | writeToFile(schemaFileName(e), generateSchema(e))]
	}
	
	// Fill, for each property of each entity that appear in more than 
	// one entity version *with different type* (those that hold the needsTypeCheck
	// boolean attribute), the set of types, to check possible type folding in
	// a latter pass
	def fillTypeCompatibilityMatrix(EntityDifferentiation differentiation) {
		throw new UnsupportedOperationException("TODO: auto-generated method stub")
	}

	def generateSchema(Entity e) '''
		'use strict'

		var mongoose = require('mongoose');
		«genIncludes(e)»

		var «e.name»Schema = new mongoose.Schema({
			«genSpecs(diffByEntity.get(e))»
		});

		module.exports = «e.name»Schema;
	'''
	
	def genIncludes(Entity entity) '''
		«FOR e : entityDeps.get(entity)»
			var «e.name»Schema = require('./«schemaFileName(e)»');
		«ENDFOR»
	'''
	
	def schemaFileName(Entity e) {
		e.name + "Schema.js"
	}
		<commit_msg>Correct order of includes in the generated schemas.<commit_after>package es.um.nosql.schemainference.m2t.gen.mongoose

import es.um.nosql.schemainference.entitydifferentiation.EntityDifferentiation
import es.um.nosql.schemainference.util.emf.ResourceManager
import es.um.nosql.schemainference.entitydifferentiation.EntitydifferentiationPackage
import es.um.nosql.schemainference.NoSQLSchema.NoSQLSchemaPackage
import java.io.File
import es.um.nosql.schemainference.entitydifferentiation.EntityDiffSpec
import java.util.List
import java.io.PrintStream
import es.um.nosql.schemainference.entitydifferentiation.PropertySpec
import es.um.nosql.schemainference.NoSQLSchema.PrimitiveType
import es.um.nosql.schemainference.NoSQLSchema.Attribute
import es.um.nosql.schemainference.NoSQLSchema.Tuple
import es.um.nosql.schemainference.NoSQLSchema.Reference
import es.um.nosql.schemainference.NoSQLSchema.Aggregate
import es.um.nosql.schemainference.NoSQLSchema.Entity
import java.util.HashMap
import java.util.Set
import java.util.regex.Pattern
import java.util.Map
import org.eclipse.collections.impl.block.factory.Comparators
import java.util.Comparator

class DiffToMongoose
{
	static class Label
	{
		var label = ""

		new(String l) {
			label = l
		}

		override toString() {
			label
		}
	}
	
	var modelName = "";

	final val EXACT_TYPE = true
	final val DUCK_TYPE = !EXACT_TYPE

	final val SPECIAL_TYPE_IDENTIFIER = "type"
	
	// list of entities
	List<Entity> entities
	List<Entity> topOrderEntities
	
	HashMap<Entity, Set<Entity>> entityDeps
	HashMap<Entity, Set<Entity>> inverseEntityDeps
	HashMap<Entity, EntityDiffSpec> diffByEntity
	
	static File outputDir

	def static void main(String[] args)
    {
		if (args.length < 1)
		{
			System.out.println("Usage: DiffToMongoose model [outdir]")
			System.exit(-1)
		}

        val inputModel = new File(args.head)
        val ResourceManager rm = new ResourceManager(EntitydifferentiationPackage.eINSTANCE,
        	NoSQLSchemaPackage.eINSTANCE)
        rm.loadResourcesAsStrings(inputModel.getPath())
        val EntityDifferentiation td = rm.resources.head.contents.head as EntityDifferentiation

		outputDir = new File(if (args.length > 1) args.get(1) else ".")
								.toPath().resolve(td.name).toFile()
		// Create destination directory if it does not exist
		outputDir.mkdirs()
        System.out.println("Generating Javascript for "
        					+ inputModel.getPath()
        					+ " in "
        					+ outputDir.getPath())

		val diff_to_mongoose = new DiffToMongoose()
		diff_to_mongoose.generate(td)
		
        System.exit(0)
    }

	def static void writeToFile(String filename, CharSequence toWrite)
	{
		val outFile = outputDir.toPath().resolve(filename).toFile()
		val outFileWriter = new PrintStream(outFile)
        outFileWriter.print(toWrite)
        outFileWriter.close()
	}

	/**
	 * Method used to generate an Inclusive/Exclusive differences file for a NoSQLDifferences object.
	 */
	def generate(EntityDifferentiation diff)
	{
		modelName = diff.name;
		diffByEntity = newHashMap(diff.entityDiffSpecs.map[ed | ed.entity -> ed])

		// Calc dependencies between entities
		topOrderEntities = calcDeps(diff)

		fillTypeCompatibilityMatrix(diff)
	
		topOrderEntities.forEach[e | writeToFile(schemaFileName(e), generateSchema(e))]
	}
	
	// Fill, for each property of each entity that appear in more than 
	// one entity version *with different type* (those that hold the needsTypeCheck
	// boolean attribute), the set of types, to check possible type folding in
	// a latter pass
	def fillTypeCompatibilityMatrix(EntityDifferentiation differentiation)
	{
	}

	def generateSchema(Entity e) '''
		'use strict'

		var mongoose = require('mongoose');
		«genIncludes(e)»

		var «e.name»Schema = new mongoose.Schema({
			«genSpecs(diffByEntity.get(e))»
		});

		module.exports = «e.name»Schema;
	'''
	
	def genIncludes(Entity entity) '''
		«FOR e : entityDeps.get(entity).sortWith(Comparator.comparing[e | topOrderEntities.indexOf(e)])»
			var «e.name»Schema = require('./«schemaFileName(e)»');
		«ENDFOR»
	'''
	
	def schemaFileName(Entity e) {
		e.name + "Schema.js"
	}
		
{'fraction_non_alphanumeric': 0.10125801677355697, 'fraction_numerical': 0.0014800197335964479, 'mean_word_length': 4.158396946564886, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 20, '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': '383000', 'n_tokens_mistral': 2734, 'n_tokens_neox': 2530, 'n_words': 587}