Search in sources :

Example 1 with DataTable

use of pcgen.cdom.format.table.DataTable in project pcgen by PCGen.

the class LookupFunctionTest method testBasic.

@Test
public void testBasic() {
    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));
    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,\"That\",ResultColumn)";
    SimpleNode node = TestUtilities.doParse(formula);
    isValid(formula, node, numberManager, null);
    isStatic(formula, node, false);
    evaluatesTo(formula, node, 2);
    Object rv = new ReconstructionVisitor().visit(node, new StringBuilder());
    assertEquals(rv.toString(), formula);
}
Also used : DataTable(pcgen.cdom.format.table.DataTable) WriteableVariableStore(pcgen.base.formula.base.WriteableVariableStore) ColumnFormatFactory(pcgen.cdom.format.table.ColumnFormatFactory) VariableLibrary(pcgen.base.formula.base.VariableLibrary) SimpleNode(pcgen.base.formula.parse.SimpleNode) TableFormatFactory(pcgen.cdom.format.table.TableFormatFactory) ReconstructionVisitor(pcgen.base.formula.visitor.ReconstructionVisitor) VariableID(pcgen.base.formula.base.VariableID) Test(org.junit.Test)

Example 2 with DataTable

use of pcgen.cdom.format.table.DataTable in project pcgen by PCGen.

the class LookupFunctionTest method testBadResultColumnFormat.

@Test
public void testBadResultColumnFormat() {
    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("STRING", 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);
    SemanticsVisitor semanticsVisitor = new SemanticsVisitor();
    FormulaSemantics semantics = getManagerFactory().generateFormulaSemantics(getFormulaManager(), getGlobalScope(), null);
    semanticsVisitor.visit(node, semantics);
    if (semantics.isValid()) {
        TestCase.fail("Expected Invalid Formula: " + formula);
    }
}
Also used : TableFormatFactory(pcgen.cdom.format.table.TableFormatFactory) DataTable(pcgen.cdom.format.table.DataTable) WriteableVariableStore(pcgen.base.formula.base.WriteableVariableStore) ColumnFormatFactory(pcgen.cdom.format.table.ColumnFormatFactory) VariableID(pcgen.base.formula.base.VariableID) FormulaSemantics(pcgen.base.formula.base.FormulaSemantics) VariableLibrary(pcgen.base.formula.base.VariableLibrary) SimpleNode(pcgen.base.formula.parse.SimpleNode) SemanticsVisitor(pcgen.base.formula.visitor.SemanticsVisitor) Test(org.junit.Test)

Example 3 with DataTable

use of pcgen.cdom.format.table.DataTable in project pcgen by PCGen.

the class LookupFunctionTest method testInvalidWrongFormat3.

@Test
public void testInvalidWrongFormat3() {
    Finder finder = new Finder();
    DataTable dt = doTableSetup();
    finder.map.put(DataTable.class, "A", dt);
    VariableLibrary vl = getVariableLibrary();
    WriteableVariableStore vs = getVariableStore();
    TableFormatFactory fac = new TableFormatFactory(finder);
    FormatManager<?> tableMgr = fac.build("STRING,NUMBER", formatLibrary);
    vl.assertLegalVariableID("TableA", getGlobalScope(), tableMgr);
    VariableID tableID = vl.getVariableID(getGlobalScopeInst(), "TableA");
    vs.put(tableID, tableMgr.convert("A"));
    String formula = "lookup(TableA,\"That\",3)";
    SimpleNode node = TestUtilities.doParse(formula);
    isNotValid(formula, node, numberManager, null);
}
Also used : TableFormatFactory(pcgen.cdom.format.table.TableFormatFactory) DataTable(pcgen.cdom.format.table.DataTable) WriteableVariableStore(pcgen.base.formula.base.WriteableVariableStore) VariableID(pcgen.base.formula.base.VariableID) VariableLibrary(pcgen.base.formula.base.VariableLibrary) SimpleNode(pcgen.base.formula.parse.SimpleNode) Test(org.junit.Test)

Example 4 with DataTable

use of pcgen.cdom.format.table.DataTable 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 5 with DataTable

use of pcgen.cdom.format.table.DataTable in project pcgen by PCGen.

the class LookupFunction method evaluate.

@Override
public Object evaluate(EvaluateVisitor visitor, Node[] args, EvaluationManager manager) {
    //Table name node (must be a Table)
    DataTable dataTable = (DataTable) args[0].jjtAccept(visitor, manager.getWith(EvaluationManager.ASSERTED, DATATABLE_CLASS));
    FormatManager<?> lookupFormat = dataTable.getFormat(0);
    //Lookup value (format based on the table)
    @SuppressWarnings("PMD.PrematureDeclaration") Object lookupValue = args[1].jjtAccept(visitor, manager.getWith(EvaluationManager.ASSERTED, lookupFormat.getManagedClass()));
    //Result Column Name (must be a tableColumn)
    TableColumn column = (TableColumn) args[2].jjtAccept(visitor, manager.getWith(EvaluationManager.ASSERTED, COLUMN_CLASS));
    String columnName = column.getName();
    if (!dataTable.isColumn(columnName)) {
        FormatManager<?> fmt = column.getFormatManager();
        System.out.println("Lookup called on invalid column: '" + columnName + "' is not present on table '" + dataTable.getName() + "' assuming default for " + fmt.getIdentifierType());
        FormulaManager fm = manager.get(EvaluationManager.FMANAGER);
        return fm.getDefault(fmt.getManagedClass());
    }
    if (!dataTable.hasRow(lookupValue)) {
        FormatManager<?> fmt = column.getFormatManager();
        System.out.println("Lookup called on invalid item: '" + lookupValue + "' is not present in the first row of table '" + dataTable.getName() + "' assuming default for " + fmt.getIdentifierType());
        FormulaManager fm = manager.get(EvaluationManager.FMANAGER);
        return fm.getDefault(fmt.getManagedClass());
    }
    return dataTable.lookupExact(lookupValue, columnName);
}
Also used : DataTable(pcgen.cdom.format.table.DataTable) FormulaManager(pcgen.base.formula.base.FormulaManager) TableColumn(pcgen.cdom.format.table.TableColumn)

Aggregations

DataTable (pcgen.cdom.format.table.DataTable)13 Test (org.junit.Test)11 VariableID (pcgen.base.formula.base.VariableID)8 VariableLibrary (pcgen.base.formula.base.VariableLibrary)8 WriteableVariableStore (pcgen.base.formula.base.WriteableVariableStore)8 SimpleNode (pcgen.base.formula.parse.SimpleNode)8 TableFormatFactory (pcgen.cdom.format.table.TableFormatFactory)8 ColumnFormatFactory (pcgen.cdom.format.table.ColumnFormatFactory)6 NumberManager (pcgen.base.format.NumberManager)3 StringManager (pcgen.base.format.StringManager)3 PersistenceLayerException (pcgen.persistence.PersistenceLayerException)3 EvaluationManager (pcgen.base.formula.base.EvaluationManager)2 EvaluateVisitor (pcgen.base.formula.visitor.EvaluateVisitor)2 TableColumn (pcgen.cdom.format.table.TableColumn)2 ArrayList (java.util.ArrayList)1 FormulaManager (pcgen.base.formula.base.FormulaManager)1 FormulaSemantics (pcgen.base.formula.base.FormulaSemantics)1 ReconstructionVisitor (pcgen.base.formula.visitor.ReconstructionVisitor)1 SemanticsVisitor (pcgen.base.formula.visitor.SemanticsVisitor)1