<commit_before>/*
 * Copyright 2016 Ecole des Mines de Saint-Etienne.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package fr.emse.ci.sparqlext.serializer;

import org.apache.jena.atlas.io.IndentedWriter;
import org.apache.jena.sparql.core.Prologue;
import fr.emse.ci.sparqlext.query.SPARQLExtQuery;
import java.util.List;
import org.apache.jena.graph.Node;
import org.apache.jena.query.SortCondition;
import org.apache.jena.sparql.core.Var;
import org.apache.jena.sparql.core.VarExprList;
import org.apache.jena.sparql.engine.binding.Binding;
import org.apache.jena.sparql.expr.Expr;
import org.apache.jena.sparql.serializer.PrologueSerializer;
import org.apache.jena.sparql.serializer.SerializationContext;
import org.apache.jena.sparql.syntax.Element;
import org.apache.jena.sparql.syntax.Template;
import org.apache.jena.sparql.util.FmtUtils;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import fr.emse.ci.sparqlext.query.SPARQLExtQueryVisitor;
import org.apache.jena.query.Query;
import org.apache.jena.sparql.expr.NodeValue;
import org.apache.jena.sparql.expr.nodevalue.NodeValueString;

/**
 * Extends the ARQ Query Serializer with SPARQL-Ext specificities.
 *
 * @author <NAME> <maxime.lefrancois at emse.fr>
 */
public class SPARQLExtQuerySerializer implements SPARQLExtQueryVisitor {

    public final int BLOCK_INDENT = 2;
    private final IndentedWriter out;
    private final SerializationContext context;
    private final SPARQLExtFormatterElement fmtElement;
    private final SPARQLExtFormatterTemplate fmtTemplate;
    private final SPARQLExtFmtExprSPARQL fmtExpr;

    private static Logger LOG = LoggerFactory.getLogger(SPARQLExtQuerySerializer.class);

    public SPARQLExtQuerySerializer(IndentedWriter out, SerializationContext context) {
        this.out = out;
        this.context = context;
        this.fmtTemplate = new SPARQLExtFormatterTemplate(out, context);
        this.fmtExpr = new SPARQLExtFmtExprSPARQL(out, context);
        this.fmtElement = new SPARQLExtFormatterElement(out, context);
    }

    @Override
    public void visitGenerateClause(SPARQLExtQuery query) {
        out.print("GENERATE ");
        printName(query);
        if(!query.hasName() || query.hasGenerateClause()) {
            out.print(" {");
            if(query.hasGenerateClause()) {
                out.newline();
                out.incIndent(BLOCK_INDENT);
                query.getGenerateClause().forEach(e -> {
                    e.visit(fmtElement);
                    out.newline();
                });
                out.decIndent(BLOCK_INDENT);
            }
            out.print("} ");
        }
    }

    @Override
    public void visitTemplateClause(SPARQLExtQuery query) {
        out.print("TEMPLATE");
        printName(query);
        if(!query.hasName() || query.hasTemplateClause()) {
            out.print("{");
            if(query.hasTemplateClause()) {
                out.newline();
                out.incIndent(BLOCK_INDENT);
                query.getTemplateClause().forEach(e -> {
                    e.visit(fmtElement);
                    out.print(" ");
                });
                if(query.hasTemplateClauseSeparator()) {
                    NodeValue n = new NodeValueString(query.getTemplateClauseSeparator());
                    out.print("; SEPARATOR = ");
                    fmtExpr.format(n);
                }
                out.decIndent(BLOCK_INDENT);
            }
            out.print("}");
        }   
    }

    @Override
    public void visitPerformClause(SPARQLExtQuery query) {
        out.print("PERFORM ");
        printName(query);
        if(!query.hasName() || query.hasPerformClause()) {
            out.print(" {");
            if(query.hasPerformClause()) {
                out.newline();
                out.incIndent(BLOCK_INDENT);
                query.getPerformClause().forEach(e -> {
                    e.visit(fmtElement);
                    out.newline();
                });<commit_msg>correct serialization of once in template<commit_after>/*
 * Copyright 2016 <NAME> Saint-Etienne.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package fr.emse.ci.sparqlext.serializer;

import org.apache.jena.atlas.io.IndentedWriter;
import org.apache.jena.sparql.core.Prologue;
import fr.emse.ci.sparqlext.query.SPARQLExtQuery;
import java.util.List;
import org.apache.jena.graph.Node;
import org.apache.jena.query.SortCondition;
import org.apache.jena.sparql.core.Var;
import org.apache.jena.sparql.core.VarExprList;
import org.apache.jena.sparql.engine.binding.Binding;
import org.apache.jena.sparql.expr.Expr;
import org.apache.jena.sparql.serializer.PrologueSerializer;
import org.apache.jena.sparql.serializer.SerializationContext;
import org.apache.jena.sparql.syntax.Element;
import org.apache.jena.sparql.syntax.Template;
import org.apache.jena.sparql.util.FmtUtils;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import fr.emse.ci.sparqlext.query.SPARQLExtQueryVisitor;
import org.apache.jena.query.Query;
import org.apache.jena.sparql.expr.NodeValue;
import org.apache.jena.sparql.expr.nodevalue.NodeValueString;

/**
 * Extends the ARQ Query Serializer with SPARQL-Ext specificities.
 *
 * @author <NAME> <maxime.lefrancois at emse.fr>
 */
public class SPARQLExtQuerySerializer implements SPARQLExtQueryVisitor {

    public final int BLOCK_INDENT = 2;
    private final IndentedWriter out;
    private final SerializationContext context;
    private final SPARQLExtFormatterElement fmtElement;
    private final SPARQLExtFormatterTemplate fmtTemplate;
    private final SPARQLExtFmtExprSPARQL fmtExpr;

    private static Logger LOG = LoggerFactory.getLogger(SPARQLExtQuerySerializer.class);

    public SPARQLExtQuerySerializer(IndentedWriter out, SerializationContext context) {
        this.out = out;
        this.context = context;
        this.fmtTemplate = new SPARQLExtFormatterTemplate(out, context);
        this.fmtExpr = new SPARQLExtFmtExprSPARQL(out, context);
        this.fmtElement = new SPARQLExtFormatterElement(out, context);
    }

    @Override
    public void visitGenerateClause(SPARQLExtQuery query) {
        out.print("GENERATE ");
        printName(query);
        if(!query.hasName() || query.hasGenerateClause()) {
            out.print(" {");
            if(query.hasGenerateClause()) {
                out.newline();
                out.incIndent(BLOCK_INDENT);
                query.getGenerateClause().forEach(e -> {
                    e.visit(fmtElement);
                    out.newline();
                });
                out.decIndent(BLOCK_INDENT);
            }
            out.print("} ");
        }
    }

    @Override
    public void visitTemplateClause(SPARQLExtQuery query) {
        out.print("TEMPLATE");
        printName(query);
        if(!query.hasName() || query.hasTemplateClause()) {
            out.print("{");
            if(query.getTemplateStart()!=null) {
                out.print("ONCE ");
                NodeValue n = new NodeValueString(query.getTemplateStart());
                fmtExpr.format(n);
                out.print(" ");
            }
            if(query.hasTemplateClause()) {
                out.newline();
                out.incIndent(BLOCK_INDENT);
                query.getTemplateClause().forEach(e -> {
                    e.visit(fmtElement);
                    out.print(" ");
                });
                if(query.hasTemplateClauseSeparator()) {
                    NodeValue n = new NodeValueString(query.getTemplateClauseSeparator());
                    out.print("; SEPARATOR = ");
                    fmtExpr.format(n);
                }
                out.decIndent(BLOCK_INDENT);
            }
            if(query.getTemplateEnd()!=null) {
                out.print("ONCE ");
                NodeValue n = new NodeValueString(query.getTemplateEnd());
                fmtExpr.format(n);
                out.print(" ");
            }
            out.print("}");
        }   
    }

    @Override
    public void visitPerformClause(SPARQLExtQuery query) {
        out.print("PERFORM ");
        printName(query);
        if(!query.hasName() || query.hasPerformClause()) {
            out.print(" {");
            if(query.hasPerformClause()) {
                out.newline();
                out.incIndent(BLOCK_INDENT);
                query.getPerformClause().forEach(e -> {
                    e.visit(fmtElement);
                    out.newline();
                });
{'fraction_non_alphanumeric': 0.10719530102790015, 'fraction_numerical': 0.0023075309418921753, 'mean_word_length': 2.4286227975548362, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4560219', 'n_tokens_mistral': 2818, 'n_tokens_neox': 2761, 'n_words': 536}