use of pcgen.base.formula.base.VariableLibrary 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);
}
use of pcgen.base.formula.base.VariableLibrary 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);
}
}
use of pcgen.base.formula.base.VariableLibrary 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);
}
use of pcgen.base.formula.base.VariableLibrary 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);
}
}
use of pcgen.base.formula.base.VariableLibrary in project pcgen by PCGen.
the class InputFunction method allowArgs.
@Override
public final FormatManager<?> allowArgs(SemanticsVisitor visitor, Node[] args, FormulaSemantics semantics) {
int argCount = args.length;
if (argCount != 1) {
semantics.setInvalid("Function " + getFunctionName() + " received incorrect # of arguments, expected: 1 got " + args.length + ' ' + Arrays.asList(args));
return null;
}
//String node (name)
Node inputNode = args[0];
if (inputNode.getId() != FormulaParserTreeConstants.JJTQUOTSTRING) {
semantics.setInvalid("Parse Error: Invalid Value: " + ((SimpleNode) inputNode).getText() + " found in " + inputNode.getClass().getName() + " found in location requiring a literal" + " String (cannot be evaluated)");
return null;
}
String inputName = ((SimpleNode) inputNode).getText();
String varName = ChannelUtilities.createVarName(inputName);
VariableLibrary varLib = semantics.get(FormulaSemantics.FMANAGER).getFactory();
LegalScope scope = semantics.get(FormulaSemantics.SCOPE);
FormatManager<?> formatManager = varLib.getVariableFormat(scope, varName);
if (formatManager == null) {
semantics.setInvalid("Input Channel: " + varName + " was not found");
return null;
}
return formatManager;
}
Aggregations