Search in sources :

Example 6 with VariableID

use of pcgen.base.formula.base.VariableID in project pcgen by PCGen.

the class LookupFunctionTest method testNoLookup.

@Test
public void testNoLookup() {
    Finder finder = new Finder();
    DataTable dt = doTableSetup();
    finder.map.put(DataTable.class, "A", dt);
    finder.map.put(TableColumn.class, "Name", buildColumn("Name", stringManager));
    finder.map.put(TableColumn.class, "Value", buildColumn("Value", numberManager));
    finder.map.put(TableColumn.class, "Result", buildColumn("Result", stringManager));
    VariableLibrary vl = getVariableLibrary();
    WriteableVariableStore vs = getVariableStore();
    TableFormatFactory fac = new TableFormatFactory(finder);
    FormatManager<?> tableMgr = fac.build("STRING,NUMBER", formatLibrary);
    vl.assertLegalVariableID("TableA", getGlobalScope(), tableMgr);
    ColumnFormatFactory cfac = new ColumnFormatFactory(finder);
    FormatManager<?> columnMgr = cfac.build("NUMBER", formatLibrary);
    vl.assertLegalVariableID("ResultColumn", getGlobalScope(), columnMgr);
    VariableID tableID = vl.getVariableID(getGlobalScopeInst(), "TableA");
    vs.put(tableID, tableMgr.convert("A"));
    VariableID columnID = vl.getVariableID(getGlobalScopeInst(), "ResultColumn");
    vs.put(columnID, columnMgr.convert("Value"));
    String formula = "lookup(TableA,\"Oh No\",ResultColumn)";
    SimpleNode node = TestUtilities.doParse(formula);
    isValid(formula, node, numberManager, null);
    isStatic(formula, node, false);
    EvaluationManager manager = generateManager();
    Object result = new EvaluateVisitor().visit(node, manager);
    if (!result.equals(0)) {
        TestCase.fail("Expected Invalid result, should have been zero due to invalid column: " + result);
    }
}
Also used : DataTable(pcgen.cdom.format.table.DataTable) EvaluateVisitor(pcgen.base.formula.visitor.EvaluateVisitor) WriteableVariableStore(pcgen.base.formula.base.WriteableVariableStore) ColumnFormatFactory(pcgen.cdom.format.table.ColumnFormatFactory) EvaluationManager(pcgen.base.formula.base.EvaluationManager) VariableLibrary(pcgen.base.formula.base.VariableLibrary) SimpleNode(pcgen.base.formula.parse.SimpleNode) TableFormatFactory(pcgen.cdom.format.table.TableFormatFactory) VariableID(pcgen.base.formula.base.VariableID) Test(org.junit.Test)

Example 7 with VariableID

use of pcgen.base.formula.base.VariableID in project pcgen by PCGen.

the class SetSolverManagerTest method testProcessDependentSet.

@Test
public void testProcessDependentSet() {
    sl.assertLegalVariableID("Regions", globalScope, arrayManager);
    ScopeInstance scopeInst = siFactory.getGlobalInstance(globalScope.getName());
    VariableID<String[]> regions = (VariableID<String[]>) sl.getVariableID(scopeInst, "Regions");
    manager.createChannel(regions);
    Object[] array = vc.get(regions);
    List<Object> list = Arrays.asList(array);
    assertEquals(0, array.length);
    assertTrue(vc.set.contains(regions));
    assertEquals(1, vc.set.size());
    vc.reset();
    ModifierFactory am1 = new plugin.modifier.set.AddModifierFactory<>();
    PCGenModifier mod = am1.getModifier(2000, "France,England", new ManagerFactory() {
    }, null, globalScope, arrayManager);
    manager.addModifier(regions, mod, scopeInst);
    array = vc.get(regions);
    assertThat(2, is(array.length));
    list = Arrays.asList(array);
    assertTrue(list.contains("England"));
    assertTrue(list.contains("France"));
    assertTrue(vc.set.contains(regions));
    assertEquals(1, vc.set.size());
    vc.reset();
    ModifierFactory am2 = new plugin.modifier.set.AddModifierFactory<>();
    mod = am2.getModifier(3000, "Greece,England", new ManagerFactory() {
    }, null, globalScope, arrayManager);
    manager.addModifier(regions, mod, scopeInst);
    array = vc.get(regions);
    assertThat(3, is(array.length));
    list = Arrays.asList(array);
    assertTrue(list.contains("England"));
    assertTrue(list.contains("France"));
    assertTrue(list.contains("Greece"));
    assertTrue(vc.set.contains(regions));
    assertEquals(1, vc.set.size());
    vc.reset();
}
Also used : ScopeInstance(pcgen.base.formula.base.ScopeInstance) ManagerFactory(pcgen.base.formula.base.ManagerFactory) VariableID(pcgen.base.formula.base.VariableID) PCGenModifier(pcgen.base.calculation.PCGenModifier) ModifierFactory(pcgen.rules.persistence.token.ModifierFactory) Test(org.junit.Test)

Example 8 with VariableID

use of pcgen.base.formula.base.VariableID in project pcgen by PCGen.

the class AbstractFormulaTestCase method getBooleanVariable.

protected VariableID<Boolean> getBooleanVariable(String formula) {
    VariableLibrary variableLibrary = getVariableLibrary();
    variableLibrary.assertLegalVariableID(formula, localSetup.getGlobalScopeInst().getLegalScope(), FormatUtilities.BOOLEAN_MANAGER);
    return (VariableID<Boolean>) variableLibrary.getVariableID(localSetup.getGlobalScopeInst(), formula);
}
Also used : VariableID(pcgen.base.formula.base.VariableID) VariableLibrary(pcgen.base.formula.base.VariableLibrary)

Example 9 with VariableID

use of pcgen.base.formula.base.VariableID in project pcgen by PCGen.

the class DropIntoContextFunctionTest method testDynamic.

@Test
public void testDynamic() {
    VariableLibrary vl = getVariableLibrary();
    LegalScope equipScope = getScopeLibrary().getScope("EQUIPMENT");
    LegalScope globalScope = getScopeLibrary().getScope("Global");
    vl.assertLegalVariableID("LocalVar", equipScope, numberManager);
    vl.assertLegalVariableID("EquipVar", globalScope, stringManager);
    String formula = "dropIntoContext(\"EQUIPMENT\",EquipVar,LocalVar)";
    SimpleNode node = TestUtilities.doParse(formula);
    SemanticsVisitor semanticsVisitor = new SemanticsVisitor();
    FormulaSemantics semantics = generateFormulaSemantics(getFormulaManager(), getGlobalScope(), null);
    semanticsVisitor.visit(node, semantics);
    if (!semantics.isValid()) {
        TestCase.fail("Expected Valid Formula: " + formula + " but was told: " + semantics.getReport());
    }
    isStatic(formula, node, false);
    Equipment equip = new Equipment();
    equip.setName("EquipKey");
    Equipment equipalt = new Equipment();
    equipalt.setName("EquipAlt");
    ScopeInstance scopeInste = getInstanceFactory().get("EQUIPMENT", equip);
    VariableID varIDe = vl.getVariableID(scopeInste, "LocalVar");
    getVariableStore().put(varIDe, 2);
    ScopeInstance scopeInsta = getInstanceFactory().get("EQUIPMENT", equipalt);
    VariableID varIDa = vl.getVariableID(scopeInsta, "LocalVar");
    getVariableStore().put(varIDa, 3);
    ScopeInstance globalInst = getInstanceFactory().getGlobalInstance("Global");
    VariableID varIDq = vl.getVariableID(globalInst, "EquipVar");
    getVariableStore().put(varIDq, "EquipKey");
    LoadContext context = new RuntimeLoadContext(new RuntimeReferenceContext(), new ConsolidatedListCommitStrategy());
    context.getReferenceContext().importObject(equip);
    context.getReferenceContext().importObject(equipalt);
    evaluatesTo(formula, node, 2, context);
    Object rv = new ReconstructionVisitor().visit(node, new StringBuilder());
    assertEquals(rv.toString(), formula);
    getVariableStore().put(varIDq, "EquipAlt");
    evaluatesTo(formula, node, 3, context);
}
Also used : ScopeInstance(pcgen.base.formula.base.ScopeInstance) ConsolidatedListCommitStrategy(pcgen.rules.context.ConsolidatedListCommitStrategy) FormulaSemantics(pcgen.base.formula.base.FormulaSemantics) RuntimeReferenceContext(pcgen.rules.context.RuntimeReferenceContext) VariableLibrary(pcgen.base.formula.base.VariableLibrary) SimpleNode(pcgen.base.formula.parse.SimpleNode) SemanticsVisitor(pcgen.base.formula.visitor.SemanticsVisitor) RuntimeLoadContext(pcgen.rules.context.RuntimeLoadContext) Equipment(pcgen.core.Equipment) ReconstructionVisitor(pcgen.base.formula.visitor.ReconstructionVisitor) LegalScope(pcgen.base.formula.base.LegalScope) LoadContext(pcgen.rules.context.LoadContext) RuntimeLoadContext(pcgen.rules.context.RuntimeLoadContext) VariableID(pcgen.base.formula.base.VariableID) Test(org.junit.Test)

Example 10 with VariableID

use of pcgen.base.formula.base.VariableID in project pcgen by PCGen.

the class DropIntoContextFunctionTest method testBasic.

@Test
public void testBasic() {
    VariableLibrary vl = getVariableLibrary();
    LegalScope equipScope = getScopeLibrary().getScope("EQUIPMENT");
    vl.assertLegalVariableID("LocalVar", equipScope, numberManager);
    String formula = "dropIntoContext(\"EQUIPMENT\",\"EquipKey\",LocalVar)";
    SimpleNode node = TestUtilities.doParse(formula);
    SemanticsVisitor semanticsVisitor = new SemanticsVisitor();
    FormulaSemantics semantics = generateFormulaSemantics(getFormulaManager(), getGlobalScope(), null);
    semanticsVisitor.visit(node, semantics);
    if (!semantics.isValid()) {
        TestCase.fail("Expected Valid Formula: " + formula + " but was told: " + semantics.getReport());
    }
    isStatic(formula, node, false);
    Equipment equip = new Equipment();
    equip.setName("EquipKey");
    ScopeInstance scopeInst = getInstanceFactory().get("EQUIPMENT", equip);
    VariableID varID = vl.getVariableID(scopeInst, "LocalVar");
    getVariableStore().put(varID, 2);
    LoadContext context = new RuntimeLoadContext(new RuntimeReferenceContext(), new ConsolidatedListCommitStrategy());
    context.getReferenceContext().importObject(equip);
    evaluatesTo(formula, node, 2, context);
    Object rv = new ReconstructionVisitor().visit(node, new StringBuilder());
    assertEquals(rv.toString(), formula);
}
Also used : ScopeInstance(pcgen.base.formula.base.ScopeInstance) ConsolidatedListCommitStrategy(pcgen.rules.context.ConsolidatedListCommitStrategy) FormulaSemantics(pcgen.base.formula.base.FormulaSemantics) RuntimeReferenceContext(pcgen.rules.context.RuntimeReferenceContext) VariableLibrary(pcgen.base.formula.base.VariableLibrary) SimpleNode(pcgen.base.formula.parse.SimpleNode) SemanticsVisitor(pcgen.base.formula.visitor.SemanticsVisitor) RuntimeLoadContext(pcgen.rules.context.RuntimeLoadContext) Equipment(pcgen.core.Equipment) ReconstructionVisitor(pcgen.base.formula.visitor.ReconstructionVisitor) LegalScope(pcgen.base.formula.base.LegalScope) LoadContext(pcgen.rules.context.LoadContext) RuntimeLoadContext(pcgen.rules.context.RuntimeLoadContext) VariableID(pcgen.base.formula.base.VariableID) Test(org.junit.Test)

Aggregations

VariableID (pcgen.base.formula.base.VariableID)20 VariableLibrary (pcgen.base.formula.base.VariableLibrary)16 Test (org.junit.Test)14 SimpleNode (pcgen.base.formula.parse.SimpleNode)12 WriteableVariableStore (pcgen.base.formula.base.WriteableVariableStore)10 ColumnFormatFactory (pcgen.cdom.format.table.ColumnFormatFactory)8 DataTable (pcgen.cdom.format.table.DataTable)8 TableFormatFactory (pcgen.cdom.format.table.TableFormatFactory)8 ScopeInstance (pcgen.base.formula.base.ScopeInstance)6 FormulaSemantics (pcgen.base.formula.base.FormulaSemantics)3 LegalScope (pcgen.base.formula.base.LegalScope)3 ReconstructionVisitor (pcgen.base.formula.visitor.ReconstructionVisitor)3 SemanticsVisitor (pcgen.base.formula.visitor.SemanticsVisitor)3 Equipment (pcgen.core.Equipment)3 PCGenModifier (pcgen.base.calculation.PCGenModifier)2 EvaluationManager (pcgen.base.formula.base.EvaluationManager)2 ManagerFactory (pcgen.base.formula.base.ManagerFactory)2 EvaluateVisitor (pcgen.base.formula.visitor.EvaluateVisitor)2 ConsolidatedListCommitStrategy (pcgen.rules.context.ConsolidatedListCommitStrategy)2 LoadContext (pcgen.rules.context.LoadContext)2