<commit_before>package com.sri.ai.test.grinder.performance;

public class ContextPerformanceTest {

}<commit_msg>- initial framework for context splitting tests<commit_after>package com.sri.ai.test.grinder.performance;

import static com.sri.ai.expresso.core.DefaultSymbol.createSymbol;
import static com.sri.ai.expresso.helper.Expressions.parse;
import static com.sri.ai.util.Util.println;

import java.util.ArrayList;
import java.util.List;

import static com.sri.ai.util.Util.mapIntoList;
import static com.sri.ai.util.Util.arrayList;
import static com.sri.ai.util.Util.*;

import org.junit.Test;

import com.sri.ai.expresso.api.Expression;
import com.sri.ai.expresso.api.Symbol;
import com.sri.ai.expresso.type.IntegerInterval;
import com.sri.ai.grinder.anytime.VariableComponent;
import com.sri.ai.grinder.api.Context;
import com.sri.ai.grinder.api.Registry;
import com.sri.ai.grinder.api.Theory;
import com.sri.ai.grinder.core.TrueContext;
import com.sri.ai.grinder.core.constraint.ContextSplitting;
import com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory;



public class ContextPerformanceTest {
	
	
			//////////////////////////////////////////////////////
			// GLOBAL TEST SETTINGS///////////////////////////////
			//////////////////////////////////////////////////////
		
				static final int numberOfVariables = 3;
				static final int variableCardinalities = 3;
			
			//////////////////////////////////////////////////////
			
				

	// OTHER GLOBAL CONSTANTS
	private static final Theory THEORY = new DifferenceArithmeticTheory(false, true);
	static final Context TESTINGCONTEXT = createContextBasedOnGlobalParameters();
	static final VariableAssignmentExpressions variableAssignmentExpressions = new VariableAssignmentExpressions();




	
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
//MAIN TESTING METHODS ///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

	@Test
	public void testFxn() {
		recursivelySplitContext(TESTINGCONTEXT);
	}
	
	
	
	
	
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
//SUPPORT CLASSES AND METHODS ////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////


	/// STRUCTS W/ SUPPORTING METHODS //////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	private static class VariableAssignmentExpressions {
		
		ArrayList<ArrayList<Expression>> variableAssignmentExpressions;
		
		public VariableAssignmentExpressions() {
			variableAssignmentExpressions = new ArrayList<ArrayList<Expression>>(numberOfVariables);
				variableAssignmentExpressions.add(null); //to make 1-indexed (instead of 0-indexed)
			for(int variableNumber = 1; variableNumber <= numberOfVariables; ++variableNumber)
			{
				variableAssignmentExpressions.add(constructAssignmentExpressionsForVariable(variableNumber));
			}
		}
		
		private static ArrayList<Expression> constructAssignmentExpressionsForVariable(int variableNumber) {
			ArrayList<Expression> assignmentExpressionsForVariable =  new ArrayList<Expression>(variableCardinalities);
				assignmentExpressionsForVariable.add(null); //to make 1-indexed (instead of O-indexed)
			for(int assignmentValue = 1; assignmentValue <= variableCardinalities; ++assignmentValue)
			{
				Expression assignmentExpression = parse("X" + variableNumber + " = " + assignmentValue);
				assignmentExpressionsForVariable.add(assignmentExpression);
			}
			
			return assignmentExpressionsForVariable;
		}
		
		public Expression getVariableAssignmentExpressions(int variableNumber, int assignmentValue) {
			return variableAssignmentExpressions.get(variableNumber).get(assignmentValue);
		}
		
	}
	
	
	
	
	/// CONTEXT CONSTRUCTION METHODS ////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	private static Context createContextBasedOnGlobalParameters()	{
		Context context = new TrueContext(THEORY);
		
		Expression typeExpression = new IntegerInterval(1, variableCardinalities).toExpression();
		List<Expression> variableSymbols = new ArrayList<Expression>(numberOfVariables);
		
		for(int i = 1; i <= numberOfVariables; ++i) 
		{
			variableSymbols.add(createSymbol("X"+i));
			context = context.extendWithSymbolsAndTypes(variableSymbols.get(i-1), typeExpression);
		}
		
		return context;
	}
	
	
	
	
	/// CONTEXT SPLITTING METHODS //////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	private static void recursivelySplitContext(Context contextToSplit, int variableNumber) {
		
		// BASE CASE
		if(variableNumber > numberOfVariables)
		{
			println(contextToSplit);
			return;
		}
		
		
		int nextVariable = variableNumber + 1;
		
		// RECURSIVE SPLITTING
		for(int assignmentValue = 1; assignmentValue < variableCardinalities; ++assignmentValue)
		{
			Expression literalToSplitOn = variableAssignmentExpressions.getVariableAssignmentExpressions(variableNumber, assignmentValue);
			ContextSplitting splitter = new ContextSplitting(literalToSplitOn, contextToSplit);
			
			recursivelySplitContext(splitter.getContextAndLiteral(), nextVariable);
			
			contextToSplit = splitter.getContextAndLiteralNegation();			
		}
		
		recursivelySplitContext(contextToSplit, nextVariable);
	}
	
	private static void recursivelySplitContext(Context contextToSplit) {
		
		recursivelySplitContext(contextToSplit, 1);
		
	}

}
{'fraction_non_alphanumeric': 0.2901718671783748, 'fraction_numerical': 0.0021691973969631237, 'mean_word_length': 1.8273584905660378, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 13, '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': '4558217', 'n_tokens_mistral': 1550, 'n_tokens_neox': 1323, 'n_words': 256}