Search in sources :

Example 1 with EvaluateVisitor

use of pcgen.base.formula.visitor.EvaluateVisitor 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 2 with EvaluateVisitor

use of pcgen.base.formula.visitor.EvaluateVisitor in project pcgen by PCGen.

the class AbstractFormulaTestCase method performEvaluation.

public void performEvaluation(String formula, SimpleNode node, Object valueOf, EvaluationManager manager) {
    Object result = new EvaluateVisitor().visit(node, manager);
    if (result.equals(valueOf)) {
        return;
    }
    //Try ints as double as well just in case (temporary)
    if (valueOf instanceof Integer) {
        if (result.equals(valueOf)) {
            return;
        }
    } else //Give Doubles a bit of fuzz
    if (valueOf instanceof Double) {
        if (TestUtilities.doubleEqual(((Double) valueOf).doubleValue(), ((Number) result).doubleValue())) {
            return;
        }
    }
    TestCase.fail("Expected " + valueOf.getClass().getSimpleName() + " (" + valueOf + ") for Formula: " + formula + ", was " + result + " (" + result.getClass().getSimpleName() + ")");
}
Also used : EvaluateVisitor(pcgen.base.formula.visitor.EvaluateVisitor)

Example 3 with EvaluateVisitor

use of pcgen.base.formula.visitor.EvaluateVisitor in project pcgen by PCGen.

the class LookupFunctionTest method testNoColumn.

@Test
public void testNoColumn() {
    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("Result"));
    String formula = "lookup(TableA,\"That\",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 instanceof Number) {
        TestCase.fail("Expected Invalid result, should have been a string 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 4 with EvaluateVisitor

use of pcgen.base.formula.visitor.EvaluateVisitor in project pcgen by PCGen.

the class AbstractFormulaTestCase method evaluatesTo.

public void evaluatesTo(String formula, SimpleNode node, Object valueOf) {
    EvaluationManager manager = generateManager();
    Object result = new EvaluateVisitor().visit(node, manager);
    if (result.equals(valueOf)) {
        return;
    }
    //Try ints as double as well just in case (temporary)
    if (valueOf instanceof Integer) {
        if (result.equals(valueOf)) {
            return;
        }
    } else //Give Doubles a bit of fuzz
    if (valueOf instanceof Double) {
        if (TestUtilities.doubleEqual(((Double) valueOf).doubleValue(), ((Number) result).doubleValue(), TestUtilities.SMALL_ERROR)) {
            return;
        }
    }
    TestCase.fail("Expected " + valueOf.getClass().getSimpleName() + " (" + valueOf + ") for Formula: " + formula + ", was " + result + " (" + result.getClass().getSimpleName() + ")");
}
Also used : EvaluateVisitor(pcgen.base.formula.visitor.EvaluateVisitor) EvaluationManager(pcgen.base.formula.base.EvaluationManager)

Aggregations

EvaluateVisitor (pcgen.base.formula.visitor.EvaluateVisitor)4 EvaluationManager (pcgen.base.formula.base.EvaluationManager)3 Test (org.junit.Test)2 VariableID (pcgen.base.formula.base.VariableID)2 VariableLibrary (pcgen.base.formula.base.VariableLibrary)2 WriteableVariableStore (pcgen.base.formula.base.WriteableVariableStore)2 SimpleNode (pcgen.base.formula.parse.SimpleNode)2 ColumnFormatFactory (pcgen.cdom.format.table.ColumnFormatFactory)2 DataTable (pcgen.cdom.format.table.DataTable)2 TableFormatFactory (pcgen.cdom.format.table.TableFormatFactory)2