<commit_before>package uk.ac.man.cs.openphacts.queryexpander.visitor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.openrdf.model.URI;
import org.openrdf.model.Value;
import org.openrdf.model.impl.URIImpl;
import org.openrdf.query.BindingSet;
import org.openrdf.query.Dataset;
import org.openrdf.query.algebra.Add;
import org.openrdf.query.algebra.And;
import org.openrdf.query.algebra.ArbitraryLengthPath;
import org.openrdf.query.algebra.Avg;
import org.openrdf.query.algebra.BNodeGenerator;
import org.openrdf.query.algebra.BindingSetAssignment;
import org.openrdf.query.algebra.Bound;
import org.openrdf.query.algebra.Clear;
import org.openrdf.query.algebra.Coalesce;
import org.openrdf.query.algebra.Compare;
import org.openrdf.query.algebra.CompareAll;
import org.openrdf.query.algebra.CompareAny;
import org.openrdf.query.algebra.Copy;
import org.openrdf.query.algebra.Count;
import org.openrdf.query.algebra.Create;
import org.openrdf.query.algebra.Datatype;
import org.openrdf.query.algebra.DeleteData;
import org.openrdf.query.algebra.Difference;
import org.openrdf.query.algebra.Distinct;
import org.openrdf.query.algebra.EmptySet;
import org.openrdf.query.algebra.Exists;
import org.openrdf.query.algebra.Extension;
import org.openrdf.query.algebra.ExtensionElem;
import org.openrdf.query.algebra.Filter;
import org.openrdf.query.algebra.FunctionCall;
import org.openrdf.query.algebra.Group;
import org.openrdf.query.algebra.GroupConcat;
import org.openrdf.query.algebra.GroupElem;
import org.openrdf.query.algebra.IRIFunction;
import org.openrdf.query.algebra.If;
import org.openrdf.query.algebra.In;
import org.openrdf.query.algebra.InsertData;
import org.openrdf.query.algebra.Intersection;
import org.openrdf.query.algebra.IsBNode;
import org.openrdf.query.algebra.IsLiteral;
import org.openrdf.query.algebra.IsNumeric;
import org.openrdf.query.algebra.IsResource;
import org.openrdf.query.algebra.IsURI;
import org.openrdf.query.algebra.Join;
import org.openrdf.query.algebra.Label;
import org.openrdf.query.algebra.Lang;
import org.openrdf.query.algebra.LangMatches;
import org.openrdf.query.algebra.LeftJoin;
import org.openrdf.query.algebra.Like;
import org.openrdf.query.algebra.Load;
import org.openrdf.query.algebra.LocalName;
import org.openrdf.query.algebra.MathExpr;
import org.openrdf.query.algebra.Max;
import org.openrdf.query.algebra.Min;
import org.openrdf.query.algebra.Modify;
import org.openrdf.query.algebra.Move;
import org.openrdf.query.algebra.MultiProjection;
import org.openrdf.query.algebra.Namespace;
import org.openrdf.query.algebra.Not;
import org.openrdf.query.algebra.Or;
import org.openrdf.query.algebra.Order;
import org.openrdf.query.algebra.OrderElem;
import org.openrdf.query.algebra.Projection;
import org.openrdf.query.algebra.ProjectionElem;
import org.openrdf.query.algebra.ProjectionElemList;
import org.openrdf.query.algebra.QueryModelNode;
import org.openrdf.query.algebra.QueryModelVisitor;
import org.openrdf.query.algebra.QueryRoot;
import org.openrdf.query.algebra.Reduced;
import org.openrdf.query.algebra.Regex;
import org.openrdf.query.algebra.SameTerm;
import org.openrdf.query.algebra.Sample;
import org.openrdf.query.algebra.Service;
import org.openrdf.query.algebra.SingletonSet;
import org.openrdf.query.algebra.Slice;
import org.openrdf.query.algebra.StatementPattern;
import org.openrdf.query.algebra.Str;
import org.openrdf.query.algebra.Sum;
import org.openrdf.query.algebra.TupleExpr;
import org.openrdf.query.algebra.Union;
import org.openrdf.query.algebra.ValueConstant;
import org.openrdf.query.algebra.ValueExpr;
import org.openrdf.query.algebra.Var;
import org.openrdf.query.algebra.ZeroLengthPath;
import uk.ac.man.cs.openphacts.queryexpander.QueryExpansionException;
import uk.ac.man.cs.openphacts.queryexpander.QueryType;

/**
 *
 * @author Christian
 */
public class QueryWriterModelVisitor implements QueryModelVisitor<QueryExpansionException>{
    
    //Builder to write the query to bit by bit
    StringBuilder queryString = new StringBuilder();
   
    //Used to add the FROM clause
    Dataset originalDataSet;
    
    //The current context (GRAPH clause) the writer is in. (or null if not in a context.
    Var context = null;
    
    //List of attributes to be used or null to include all.
    //     WARNING: this functionality is in an early stage of development 
    //     so can only handle queries where each attribute comes from exactly one statement.
    //private List<String>  requiredAttributes;
    
    //List of attributes to be removed. See requiredAttributes warning!
    //private Set<String> eliminatedAttributes;
    
    //Flag that the writing of an Optional has been delayed until a Graph clause is added
    boolean swapGraphAndOptional = false;
    
    //List of the Contexts (including null) for all the Statements not yet met.
    ArrayList<Var> contexts;
    
    //This is the name of all the functions found to go in the select clause.
    ArrayList<String> namesInExtensions;
    
    //This names temp variables to the functions that should go in their place 
    Map<String, String> extensionMappings;
    
    //List of the number of options clauses pushed under the graph clause.
    int optionInGraph = 0;
    
    private boolean whereOpen = false;
    
    private boolean inConstruct = false;
    
    private String propertyPath = null;
    
    final boolean SHOW_DEBUG_IN_QUERY = true;
    
    //private int nextAnon = 1;
    
    //private Map<String,String> anonMapper;
    
    /**
     * Sets up the visitor for writing the query.
     * 
     * @param dataSet dataSets listed in the original Queries FROM clause.
     * @param contexts List of Contexts retrieved using the ContextListerVisitor.
     */
    QueryWriterModelVisitor(Dataset dataSet){
        originalDataSet = dataSet;
    }

    @Override
    public QueryWriterModelVisitor clone(){
        return new QueryWriterModelVisitor(originalDataSet);
    }
    
    @Override
    public void meet(Add add) throws QueryExpansionException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void meet(ArbitraryLengthPath alp) throws QueryExpansionException {
        //Limited implenation expanded as discovered so far
        TupleExpr path = alp.getPathExpression();
        if (path instanceof StatementPattern){<commit_msg>set debug comment to false in commit.<commit_after>package uk.ac.man.cs.openphacts.queryexpander.visitor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.openrdf.model.URI;
import org.openrdf.model.Value;
import org.openrdf.model.impl.URIImpl;
import org.openrdf.query.BindingSet;
import org.openrdf.query.Dataset;
import org.openrdf.query.algebra.Add;
import org.openrdf.query.algebra.And;
import org.openrdf.query.algebra.ArbitraryLengthPath;
import org.openrdf.query.algebra.Avg;
import org.openrdf.query.algebra.BNodeGenerator;
import org.openrdf.query.algebra.BindingSetAssignment;
import org.openrdf.query.algebra.Bound;
import org.openrdf.query.algebra.Clear;
import org.openrdf.query.algebra.Coalesce;
import org.openrdf.query.algebra.Compare;
import org.openrdf.query.algebra.CompareAll;
import org.openrdf.query.algebra.CompareAny;
import org.openrdf.query.algebra.Copy;
import org.openrdf.query.algebra.Count;
import org.openrdf.query.algebra.Create;
import org.openrdf.query.algebra.Datatype;
import org.openrdf.query.algebra.DeleteData;
import org.openrdf.query.algebra.Difference;
import org.openrdf.query.algebra.Distinct;
import org.openrdf.query.algebra.EmptySet;
import org.openrdf.query.algebra.Exists;
import org.openrdf.query.algebra.Extension;
import org.openrdf.query.algebra.ExtensionElem;
import org.openrdf.query.algebra.Filter;
import org.openrdf.query.algebra.FunctionCall;
import org.openrdf.query.algebra.Group;
import org.openrdf.query.algebra.GroupConcat;
import org.openrdf.query.algebra.GroupElem;
import org.openrdf.query.algebra.IRIFunction;
import org.openrdf.query.algebra.If;
import org.openrdf.query.algebra.In;
import org.openrdf.query.algebra.InsertData;
import org.openrdf.query.algebra.Intersection;
import org.openrdf.query.algebra.IsBNode;
import org.openrdf.query.algebra.IsLiteral;
import org.openrdf.query.algebra.IsNumeric;
import org.openrdf.query.algebra.IsResource;
import org.openrdf.query.algebra.IsURI;
import org.openrdf.query.algebra.Join;
import org.openrdf.query.algebra.Label;
import org.openrdf.query.algebra.Lang;
import org.openrdf.query.algebra.LangMatches;
import org.openrdf.query.algebra.LeftJoin;
import org.openrdf.query.algebra.Like;
import org.openrdf.query.algebra.Load;
import org.openrdf.query.algebra.LocalName;
import org.openrdf.query.algebra.MathExpr;
import org.openrdf.query.algebra.Max;
import org.openrdf.query.algebra.Min;
import org.openrdf.query.algebra.Modify;
import org.openrdf.query.algebra.Move;
import org.openrdf.query.algebra.MultiProjection;
import org.openrdf.query.algebra.Namespace;
import org.openrdf.query.algebra.Not;
import org.openrdf.query.algebra.Or;
import org.openrdf.query.algebra.Order;
import org.openrdf.query.algebra.OrderElem;
import org.openrdf.query.algebra.Projection;
import org.openrdf.query.algebra.ProjectionElem;
import org.openrdf.query.algebra.ProjectionElemList;
import org.openrdf.query.algebra.QueryModelNode;
import org.openrdf.query.algebra.QueryModelVisitor;
import org.openrdf.query.algebra.QueryRoot;
import org.openrdf.query.algebra.Reduced;
import org.openrdf.query.algebra.Regex;
import org.openrdf.query.algebra.SameTerm;
import org.openrdf.query.algebra.Sample;
import org.openrdf.query.algebra.Service;
import org.openrdf.query.algebra.SingletonSet;
import org.openrdf.query.algebra.Slice;
import org.openrdf.query.algebra.StatementPattern;
import org.openrdf.query.algebra.Str;
import org.openrdf.query.algebra.Sum;
import org.openrdf.query.algebra.TupleExpr;
import org.openrdf.query.algebra.Union;
import org.openrdf.query.algebra.ValueConstant;
import org.openrdf.query.algebra.ValueExpr;
import org.openrdf.query.algebra.Var;
import org.openrdf.query.algebra.ZeroLengthPath;
import uk.ac.man.cs.openphacts.queryexpander.QueryExpansionException;
import uk.ac.man.cs.openphacts.queryexpander.QueryType;

/**
 *
 * @author Christian
 */
public class QueryWriterModelVisitor implements QueryModelVisitor<QueryExpansionException>{
    
    //Builder to write the query to bit by bit
    StringBuilder queryString = new StringBuilder();
   
    //Used to add the FROM clause
    Dataset originalDataSet;
    
    //The current context (GRAPH clause) the writer is in. (or null if not in a context.
    Var context = null;
    
    //List of attributes to be used or null to include all.
    //     WARNING: this functionality is in an early stage of development 
    //     so can only handle queries where each attribute comes from exactly one statement.
    //private List<String>  requiredAttributes;
    
    //List of attributes to be removed. See requiredAttributes warning!
    //private Set<String> eliminatedAttributes;
    
    //Flag that the writing of an Optional has been delayed until a Graph clause is added
    boolean swapGraphAndOptional = false;
    
    //List of the Contexts (including null) for all the Statements not yet met.
    ArrayList<Var> contexts;
    
    //This is the name of all the functions found to go in the select clause.
    ArrayList<String> namesInExtensions;
    
    //This names temp variables to the functions that should go in their place 
    Map<String, String> extensionMappings;
    
    //List of the number of options clauses pushed under the graph clause.
    int optionInGraph = 0;
    
    private boolean whereOpen = false;
    
    private boolean inConstruct = false;
    
    private String propertyPath = null;
    
    final boolean SHOW_DEBUG_IN_QUERY = false;
    
    //private int nextAnon = 1;
    
    //private Map<String,String> anonMapper;
    
    /**
     * Sets up the visitor for writing the query.
     * 
     * @param dataSet dataSets listed in the original Queries FROM clause.
     * @param contexts List of Contexts retrieved using the ContextListerVisitor.
     */
    QueryWriterModelVisitor(Dataset dataSet){
        originalDataSet = dataSet;
    }

    @Override
    public QueryWriterModelVisitor clone(){
        return new QueryWriterModelVisitor(originalDataSet);
    }
    
    @Override
    public void meet(Add add) throws QueryExpansionException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void meet(ArbitraryLengthPath alp) throws QueryExpansionException {
        //Limited implenation expanded as discovered so far
        TupleExpr path = alp.getPathExpression();
        if (path instanceof StatementPattern){
{'fraction_non_alphanumeric': 0.09375723435450266, 'fraction_numerical': 0.00030866579211358904, 'mean_word_length': 4.246963562753036, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '4829444', 'n_tokens_mistral': 4145, 'n_tokens_neox': 3968, 'n_words': 917}