<commit_before>package ingraph.relalg2tex

import ingraph.relalg.util.SchemaInferencer
import java.io.File
import java.nio.charset.Charset
import org.apache.commons.io.FileUtils
import org.eclipse.emf.common.util.EList
import relalg.AbstractJoinOperator
import relalg.AllDifferentOperator
import relalg.AntiJoinOperator
import relalg.ArithmeticComparisonExpression
import relalg.ArithmeticComparisonOperator
import relalg.AttributeVariable
import relalg.BinaryArithmeticOperator
import relalg.BinaryLogicalExpression
import relalg.BinaryLogicalOperator
import relalg.BinaryOperator
import relalg.BooleanLiteral
import relalg.Direction
import relalg.DuplicateEliminationOperator
import relalg.EdgeVariable
import relalg.ElementVariable
import relalg.ExpandOperator
import relalg.GetEdgesOperator
import relalg.GetVerticesOperator
import relalg.IntegerLiteral
import relalg.JoinOperator
import relalg.NamedElement
import relalg.Operator
import relalg.ProductionOperator
import relalg.ProjectionOperator
import relalg.RelalgContainer
import relalg.SelectionOperator
import relalg.StringLiteral
import relalg.UnaryArithmeticOperator
import relalg.UnaryLogicalExpression
import relalg.UnaryLogicalOperator
import relalg.UnionOperator
import relalg.UnwindOperator
import relalg.Variable
import relalg.VertexVariable

abstract class AbstractRelalgSerializer {

	protected val RelalgSerializerConfig config

	protected new() {
		this.config = RelalgSerializerConfig.defaultConfig
	}

	protected new(RelalgSerializerConfig config) {
		this.config = config
	}

	def serialize(RelalgContainer container, String filename) {
		val schemaInferencer = new SchemaInferencer(false)
		schemaInferencer.addSchemaInformation(container)

		val tex = serialize(container)
		if (config.standaloneDocument) {
			val file = new File("../visualization/" + filename + ".tex")
			FileUtils.writeStringToFile(file, tex.toString, Charset.forName("UTF-8"))
		}
		tex
	}

	def serialize(RelalgContainer container) {
		convertAlgebraExpression(container.rootExpression)
	}

	/**
	 * convertExpression
	 */
	def dispatch CharSequence convertAlgebraExpression(ProductionOperator op) {
		convertAlgebraExpression(op.input)
	}

	def dispatch CharSequence convertAlgebraExpression(Operator expression) {
		'''
			«IF config.standaloneDocument»
				\documentclass[varwidth=100cm,convert={density=120}]{standalone}
				\usepackage[active,tightpage]{preview}
				
				\input{../../../ingraph/visualization/inputs/relalg-packages}
				\input{../../../ingraph/visualization/inputs/relalg-commands}
				
				\begin{document}
				\begin{preview}
			«ENDIF»
			«serializeBody(expression)»
			«IF config.standaloneDocument»
				\end{preview}
				\end{document}
			«ENDIF»
		'''
	}<commit_msg>relalg2tex: refactor the serialize API: serialize(container, filename) always writes to file, and the workhorse is serialize(container).<commit_after>package ingraph.relalg2tex

import ingraph.relalg.util.SchemaInferencer
import java.io.File
import java.nio.charset.Charset
import org.apache.commons.io.FileUtils
import org.eclipse.emf.common.util.EList
import relalg.AbstractJoinOperator
import relalg.AllDifferentOperator
import relalg.AntiJoinOperator
import relalg.ArithmeticComparisonExpression
import relalg.ArithmeticComparisonOperator
import relalg.AttributeVariable
import relalg.BinaryArithmeticOperator
import relalg.BinaryLogicalExpression
import relalg.BinaryLogicalOperator
import relalg.BinaryOperator
import relalg.BooleanLiteral
import relalg.Direction
import relalg.DuplicateEliminationOperator
import relalg.EdgeVariable
import relalg.ElementVariable
import relalg.ExpandOperator
import relalg.GetEdgesOperator
import relalg.GetVerticesOperator
import relalg.IntegerLiteral
import relalg.JoinOperator
import relalg.NamedElement
import relalg.Operator
import relalg.ProductionOperator
import relalg.ProjectionOperator
import relalg.RelalgContainer
import relalg.SelectionOperator
import relalg.StringLiteral
import relalg.UnaryArithmeticOperator
import relalg.UnaryLogicalExpression
import relalg.UnaryLogicalOperator
import relalg.UnionOperator
import relalg.UnwindOperator
import relalg.Variable
import relalg.VertexVariable

abstract class AbstractRelalgSerializer {

	protected val RelalgSerializerConfig config

	protected new() {
		this.config = RelalgSerializerConfig.defaultConfig
	}

	protected new(RelalgSerializerConfig config) {
		this.config = config
	}

	def serialize(RelalgContainer container, String filename) {
		val tex = serialize(container)

		val file = new File("../visualization/" + filename + ".tex")
		FileUtils.writeStringToFile(file, tex.toString, Charset.forName("UTF-8"))

		tex
	}

	def serialize(RelalgContainer container) {
		val schemaInferencer = new SchemaInferencer(false)
		schemaInferencer.addSchemaInformation(container)

		convertAlgebraExpression(container.rootExpression)
	}

	/**
	 * convertExpression
	 */
	def dispatch CharSequence convertAlgebraExpression(ProductionOperator op) {
		convertAlgebraExpression(op.input)
	}

	def dispatch CharSequence convertAlgebraExpression(Operator expression) {
		'''
			«IF config.standaloneDocument»
				\documentclass[varwidth=100cm,convert={density=120}]{standalone}
				\usepackage[active,tightpage]{preview}
				
				\input{../../../ingraph/visualization/inputs/relalg-packages}
				\input{../../../ingraph/visualization/inputs/relalg-commands}
				
				\begin{document}
				\begin{preview}
			«ENDIF»
			«serializeBody(expression)»
			«IF config.standaloneDocument»
				\end{preview}
				\end{document}
			«ENDIF»
		'''
	}
{'fraction_non_alphanumeric': 0.0792967348403301, 'fraction_numerical': 0.0030498744169357734, 'mean_word_length': 5.916873449131514, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6081623', 'n_tokens_mistral': 1713, 'n_tokens_neox': 1534, 'n_words': 337}