Search in sources :

Example 6 with SimpleNode

use of pcgen.base.formula.parse.SimpleNode 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;
}
Also used : LegalScope(pcgen.base.formula.base.LegalScope) SimpleNode(pcgen.base.formula.parse.SimpleNode) Node(pcgen.base.formula.parse.Node) ASTQuotString(pcgen.base.formula.parse.ASTQuotString) SimpleNode(pcgen.base.formula.parse.SimpleNode) VariableLibrary(pcgen.base.formula.base.VariableLibrary)

Example 7 with SimpleNode

use of pcgen.base.formula.parse.SimpleNode in project pcgen by PCGen.

the class DropIntoContextFunctionTest method testInvalidWrongArg.

@Test
public void testInvalidWrongArg() {
    String formula = "dropIntoContext(\"EQUIPMENT\")";
    SimpleNode node = TestUtilities.doParse(formula);
    isNotValid(formula, node, numberManager, null);
    String s = "dropIntoContext(\"EQUIPMENT\", \"Foo\", 4, 5)";
    SimpleNode simpleNode = TestUtilities.doParse(s);
    isNotValid(s, simpleNode, numberManager, null);
}
Also used : SimpleNode(pcgen.base.formula.parse.SimpleNode) Test(org.junit.Test)

Example 8 with SimpleNode

use of pcgen.base.formula.parse.SimpleNode 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 9 with SimpleNode

use of pcgen.base.formula.parse.SimpleNode in project pcgen by PCGen.

the class DropIntoContextFunctionTest method testInvalidWrongFormat2.

@Test
public void testInvalidWrongFormat2() {
    String formula = "dropIntoContext(\"EQUIPMENT\",3,3)";
    SimpleNode node = TestUtilities.doParse(formula);
    isNotValid(formula, node, numberManager, null);
}
Also used : SimpleNode(pcgen.base.formula.parse.SimpleNode) Test(org.junit.Test)

Example 10 with SimpleNode

use of pcgen.base.formula.parse.SimpleNode 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

SimpleNode (pcgen.base.formula.parse.SimpleNode)24 Test (org.junit.Test)22 VariableLibrary (pcgen.base.formula.base.VariableLibrary)14 VariableID (pcgen.base.formula.base.VariableID)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 FormulaSemantics (pcgen.base.formula.base.FormulaSemantics)4 ReconstructionVisitor (pcgen.base.formula.visitor.ReconstructionVisitor)4 SemanticsVisitor (pcgen.base.formula.visitor.SemanticsVisitor)4 LegalScope (pcgen.base.formula.base.LegalScope)3 ScopeInstance (pcgen.base.formula.base.ScopeInstance)3 EvaluationManager (pcgen.base.formula.base.EvaluationManager)2 EvaluateVisitor (pcgen.base.formula.visitor.EvaluateVisitor)2 Equipment (pcgen.core.Equipment)2 ConsolidatedListCommitStrategy (pcgen.rules.context.ConsolidatedListCommitStrategy)2 LoadContext (pcgen.rules.context.LoadContext)2 RuntimeLoadContext (pcgen.rules.context.RuntimeLoadContext)2 RuntimeReferenceContext (pcgen.rules.context.RuntimeReferenceContext)2