<commit_before>package org.openlca.core.matrix;

import static java.util.Collections.emptySet;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openlca.core.Tests;
import org.openlca.core.database.IDatabase;
import org.openlca.core.database.ParameterDao;
import org.openlca.core.model.Parameter;
import org.openlca.core.model.ParameterScope;
import org.openlca.core.model.Uncertainty;
import org.openlca.core.model.UncertaintyType;
import org.openlca.expressions.FormulaInterpreter;

public class ParameterTableTest {

	@Before
	public void setUp() {
		IDatabase db = Tests.getDb();
		ParameterDao paramDao = new ParameterDao(db);

		Parameter globalInp = inpParameter();
		globalInp.scope = ParameterScope.GLOBAL;
		Parameter globalDep = depParameter();
		globalDep.scope = ParameterScope.GLOBAL;
		paramDao.insert(globalInp);
		paramDao.insert(globalDep);

	}

	private Parameter inpParameter() {
		Parameter inpParam = new Parameter();
		inpParam.setName("inp_param");
		inpParam.isInputParameter = true;
		inpParam.value = 42.0;
		Uncertainty u1 = new Uncertainty();
		u1.distributionType = UncertaintyType.UNIFORM;
		u1.parameter1 = 10.0;
		u1.parameter2 = 21.0;
		inpParam.uncertainty = u1;
		return inpParam;
	}

	private Parameter depParameter() {
		Parameter depParam = new Parameter();
		depParam.setName("dep_param");
		depParam.isInputParameter = false;
		depParam.formula = "2 * inp_param";
		return depParam;
	}

	@After
	public void tearDown() {
		IDatabase db = Tests.getDb();
		new ParameterDao(db).deleteAll();
	}

	@Test
	public void testGlobals() throws Exception {
		FormulaInterpreter fi = ParameterTable2.interpreter(
				Tests.getDb(), emptySet(), emptySet());
		assertEquals(42.0, fi.eval("inp_param"), 1e-6);
		assertEquals(2 * 42.0, fi.eval("dep_param"), 1e-6);
	}

	@Test
	public void testSimulation() throws Exception {
		ParameterTable2 table = ParameterTable2.forSimulation(
				Tests.getDb(), emptySet(), emptySet());
		FormulaInterpreter fi = table.simulate();
		double globalIn = fi.eval("inp_param");
		assertTrue(globalIn > 9 && globalIn < 22);
		assertEquals(2 * globalIn, fi.eval("dep_param"), 1e-6);
	}
}<commit_msg>more tests for new parameter table<commit_after>package org.openlca.core.matrix;

import static java.util.Collections.emptySet;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.Collections;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openlca.core.Tests;
import org.openlca.core.database.IDatabase;
import org.openlca.core.database.ParameterDao;
import org.openlca.core.database.ProcessDao;
import org.openlca.core.model.ModelType;
import org.openlca.core.model.Parameter;
import org.openlca.core.model.ParameterRedef;
import org.openlca.core.model.ParameterScope;
import org.openlca.core.model.Process;
import org.openlca.core.model.Uncertainty;
import org.openlca.core.model.UncertaintyType;
import org.openlca.expressions.FormulaInterpreter;
import org.openlca.expressions.Scope;

public class ParameterTableTest {

	private Process process;

	@Before
	public void setUp() {
		IDatabase db = Tests.getDb();

		Parameter globalInp = inpParameter(42);
		globalInp.scope = ParameterScope.GLOBAL;
		Parameter globalDep = depParameter(2);
		globalDep.scope = ParameterScope.GLOBAL;
		ParameterDao paramDao = new ParameterDao(db);
		paramDao.insert(globalInp);
		paramDao.insert(globalDep);

		process = new Process();
		Parameter ppInp = inpParameter(84);
		ppInp.scope = ParameterScope.PROCESS;
		Parameter ppDep = depParameter(3);
		ppDep.scope = ParameterScope.PROCESS;
		process.getParameters().add(ppInp);
		process.getParameters().add(ppDep);
		new ProcessDao(db).insert(process);

	}

	private Parameter inpParameter(double val) {
		Parameter inpParam = new Parameter();
		inpParam.setName("inp_param");
		inpParam.isInputParameter = true;
		inpParam.value = val;
		Uncertainty u1 = new Uncertainty();
		u1.distributionType = UncertaintyType.UNIFORM;
		u1.parameter1 = 10.0;
		u1.parameter2 = val / 2;
		inpParam.uncertainty = u1;
		return inpParam;
	}

	private Parameter depParameter(double factor) {
		Parameter depParam = new Parameter();
		depParam.setName("dep_param");
		depParam.isInputParameter = false;
		depParam.formula = factor + " * inp_param";
		return depParam;
	}

	@After
	public void tearDown() {
		IDatabase db = Tests.getDb();
		new ParameterDao(db).deleteAll();
		new ProcessDao(db).deleteAll();
	}

	@Test
	public void testGlobals() throws Exception {
		FormulaInterpreter fi = ParameterTable2.interpreter(
				Tests.getDb(), emptySet(), emptySet());
		assertEquals(42.0, fi.eval("inp_param"), 1e-6);
		assertEquals(2 * 42.0, fi.eval("dep_param"), 1e-6);
	}

	@Test
	public void testProcessParams() throws Exception {
		FormulaInterpreter fi = ParameterTable2.interpreter(Tests.getDb(),
				Collections.singleton(process.getId()), emptySet());
		// global
		assertEquals(42.0, fi.eval("inp_param"), 1e-6);
		assertEquals(2 * 42.0, fi.eval("dep_param"), 1e-6);
		// local
		Scope scope = fi.getScope(process.getId());
		assertEquals(84.0, scope.eval("inp_param"), 1e-6);
		assertEquals(3 * 84.0, scope.eval("dep_param"), 1e-6);
	}

	@Test
	public void testSimulation() throws Exception {
		ParameterTable2 table = ParameterTable2.forSimulation(
				Tests.getDb(), emptySet(), emptySet());
		FormulaInterpreter fi = table.simulate();
		double globalIn = fi.eval("inp_param");
		assertTrue(globalIn > 9 && globalIn < 22);
		assertEquals(2 * globalIn, fi.eval("dep_param"), 1e-6);
	}

	@Test
	public void testProcessSimulation() throws Exception {
		ParameterTable2 table = ParameterTable2.forSimulation(Tests.getDb(),
				Collections.singleton(process.getId()), emptySet());
		// global
		FormulaInterpreter fi = table.simulate();
		double globalIn = fi.eval("inp_param");
		assertTrue(globalIn > 9 && globalIn < 22);
		assertEquals(2 * globalIn, fi.eval("dep_param"), 1e-6);

		// local
		Scope scope = fi.getScope(process.getId());
		double ppInp = scope.eval("inp_param");
		assertTrue(ppInp > 9 && ppInp < 84 / 2);
		assertEquals(3 * ppInp, scope.eval("dep_param"), 1e-6);
	}

	@Test
	public void testGloablRedef() throws Exception {
		ParameterRedef redef = new ParameterRedef();
		redef.name = "inp_param";
		redef.value = 99;

		FormulaInterpreter fi = ParameterTable2.interpreter(Tests.getDb(),
				Collections.singleton(process.getId()),
				Collections.singleton(redef));

		// global
		assertEquals(99.0, fi.eval("inp_param"), 1e-6);
		assertEquals(2 * 99.0, fi.eval("dep_param"), 1e-6);
		// local
		Scope scope = fi.getScope(process.getId());
		assertEquals(84.0, scope.eval("inp_param"), 1e-6);
		assertEquals(3 * 84.0, scope.eval("dep_param"), 1e-6);
	}

	@Test
	public void testLocalRedef() throws Exception {
		ParameterRedef redef = new ParameterRedef();
		redef.contextId = process.getId();
		redef.contextType = ModelType.PROCESS;
		redef.name = "inp_param";
		redef.value = 99;

		FormulaInterpreter fi = ParameterTable2.interpreter(Tests.getDb(),
				Collections.singleton(process.getId()),
				Collections.singleton(redef));

		// global
		assertEquals(42.0, fi.eval("inp_param"), 1e-6);
		assertEquals(2 * 42.0, fi.eval("dep_param"), 1e-6);
		// local
		Scope scope = fi.getScope(process.getId());
		assertEquals(99.0, scope.eval("inp_param"), 1e-6);
		assertEquals(3 * 99.0, scope.eval("dep_param"), 1e-6);
	}

	@Test
	public void testGlobalRedefSimulation() throws Exception {
		ParameterRedef redef = new ParameterRedef();
		redef.name = "inp_param";
		redef.value = 99;
		redef.uncertainty = Uncertainty.uniform(1001, 2000);

		ParameterTable2 table = ParameterTable2.forSimulation(Tests.getDb(),
				Collections.singleton(process.getId()),
				Collections.singleton(redef));

		// global
		FormulaInterpreter fi = table.simulate();
		double globalIn = fi.eval("inp_param");
		assertTrue(globalIn > 1000);
		assertEquals(2 * globalIn, fi.eval("dep_param"), 1e-6);

		// local
		Scope scope = fi.getScope(process.getId());
		double ppInp = scope.eval("inp_param");
		assertTrue(ppInp > 9 && ppInp < 84 / 2);
		assertEquals(3 * ppInp, scope.eval("dep_param"), 1e-6);
	}

	@Test
	public void testLocalRedefSimulation() throws Exception {
		ParameterRedef redef = new ParameterRedef();
		redef.contextId = process.getId();
		redef.contextType = ModelType.PROCESS;
		redef.name = "inp_param";
		redef.value = 99;
		redef.uncertainty = Uncertainty.uniform(1001, 2000);

		ParameterTable2 table = ParameterTable2.forSimulation(Tests.getDb(),
				Collections.singleton(process.getId()),
				Collections.singleton(redef));

		// global
		FormulaInterpreter fi = table.simulate();
		double globalIn = fi.eval("inp_param");
		assertTrue(globalIn > 9 && globalIn < 22);
		assertEquals(2 * globalIn, fi.eval("dep_param"), 1e-6);

		// local
		Scope scope = fi.getScope(process.getId());
		double ppInp = scope.eval("inp_param");
		assertTrue(ppInp > 1000);
		assertEquals(3 * ppInp, scope.eval("dep_param"), 1e-6);
	}
}
{'fraction_non_alphanumeric': 0.13675681594218767, 'fraction_numerical': 0.02332201905179021, 'mean_word_length': 4.777356103731815, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '4741902', 'n_tokens_mistral': 3402, 'n_tokens_neox': 3110, 'n_words': 607}