Search in sources :

Example 16 with ScopeInstance

use of pcgen.base.formula.base.ScopeInstance 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 17 with ScopeInstance

use of pcgen.base.formula.base.ScopeInstance 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)

Example 18 with ScopeInstance

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

the class InputFunctionTest method testGlobalChannelStrength.

@Test
public void testGlobalChannelStrength() {
    VariableLibrary varLib = variableLibraryFacet.get(id.getDatasetID());
    ScopeInstanceFactory instFactory = scopeFacet.get(id);
    ScopeInstance globalInstance = instFactory.getGlobalInstance("Global");
    varLib.assertLegalVariableID(ChannelUtilities.createVarName("STR"), globalInstance.getLegalScope(), numberManager);
    VariableChannel<Number> strChannel = (VariableChannel<Number>) ChannelUtilities.getGlobalChannel(id, "STR");
    String formula = "input(\"STR\")";
    SimpleNode node = TestUtilities.doParse(formula);
    isValid(formula, node, numberManager, null);
    isStatic(formula, node, false);
    evaluatesTo(formula, node, 0);
    strChannel.set(2);
    evaluatesTo(formula, node, 2);
    Object rv = new ReconstructionVisitor().visit(node, new StringBuilder());
    assertEquals(formula, rv.toString());
}
Also used : ScopeInstance(pcgen.base.formula.base.ScopeInstance) VariableChannel(pcgen.cdom.formula.VariableChannel) ReconstructionVisitor(pcgen.base.formula.visitor.ReconstructionVisitor) ScopeInstanceFactory(pcgen.base.formula.inst.ScopeInstanceFactory) VariableLibrary(pcgen.base.formula.base.VariableLibrary) SimpleNode(pcgen.base.formula.parse.SimpleNode) Test(org.junit.Test)

Example 19 with ScopeInstance

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

the class SetSolverManagerTest method testProcessDynamicSet.

@Test
public void testProcessDynamicSet() {
    LegalScope equipScope = vsLib.getScope("EQUIPMENT");
    sl.assertLegalVariableID("LocalVar", equipScope, numberManager);
    sl.assertLegalVariableID("ResultVar", globalScope, numberManager);
    sl.assertLegalVariableID("EquipVar", globalScope, equipmentManager);
    Equipment equip = new Equipment();
    equip.setName("EquipKey");
    Equipment equipalt = new Equipment();
    equipalt.setName("EquipAlt");
    ScopeInstance scopeInste = siFactory.get("EQUIPMENT", equip);
    VariableID varIDe = sl.getVariableID(scopeInste, "LocalVar");
    manager.createChannel(varIDe);
    vc.put(varIDe, 2);
    ScopeInstance scopeInsta = siFactory.get("EQUIPMENT", equipalt);
    VariableID varIDa = sl.getVariableID(scopeInsta, "LocalVar");
    manager.createChannel(varIDa);
    vc.put(varIDa, 3);
    ScopeInstance globalInst = siFactory.getGlobalInstance("Global");
    VariableID varIDq = sl.getVariableID(globalInst, "EquipVar");
    manager.createChannel(varIDq);
    VariableID varIDr = sl.getVariableID(globalInst, "ResultVar");
    manager.createChannel(varIDr);
    ModifierFactory am1 = new plugin.modifier.number.SetModifierFactory();
    ModifierFactory amString = new plugin.modifier.string.SetModifierFactory();
    PCGenModifier mod2 = am1.getModifier(2000, "2", new ManagerFactory() {
    }, fm, globalScope, numberManager);
    PCGenModifier mod3 = am1.getModifier(2000, "3", new ManagerFactory() {
    }, fm, globalScope, numberManager);
    PCGenModifier mod4 = am1.getModifier(3000, "4", new ManagerFactory() {
    }, fm, globalScope, numberManager);
    String formula = "dropIntoContext(\"EQUIPMENT\",EquipVar,LocalVar)";
    PCGenModifier modf = am1.getModifier(2000, formula, new ManagerFactory() {
    }, fm, globalScope, numberManager);
    NEPCalculation calc1 = new ProcessCalculation<>(equip, new BasicSet(), equipmentManager);
    CalculationModifier mod_e1 = new CalculationModifier<>(calc1, 2000);
    NEPCalculation calc2 = new ProcessCalculation<>(equipalt, new BasicSet(), equipmentManager);
    CalculationModifier mod_e2 = new CalculationModifier<>(calc2, 3000);
    manager.addModifier(varIDe, mod2, scopeInste);
    manager.addModifier(varIDa, mod3, scopeInsta);
    assertEquals(2, vc.get(varIDe));
    assertEquals(3, vc.get(varIDa));
    assertEquals(0, vc.get(varIDr));
    manager.addModifier(varIDq, mod_e1, globalInst);
    manager.addModifier(varIDr, modf, globalInst);
    assertEquals(2, vc.get(varIDr));
    manager.addModifier(varIDq, mod_e2, globalInst);
    assertEquals(3, vc.get(varIDr));
    manager.addModifier(varIDa, mod4, scopeInsta);
    assertEquals(4, vc.get(varIDr));
}
Also used : ScopeInstance(pcgen.base.formula.base.ScopeInstance) CalculationModifier(pcgen.base.calculation.CalculationModifier) ProcessCalculation(pcgen.cdom.content.ProcessCalculation) NEPCalculation(pcgen.base.calculation.NEPCalculation) ModifierFactory(pcgen.rules.persistence.token.ModifierFactory) ManagerFactory(pcgen.base.formula.base.ManagerFactory) Equipment(pcgen.core.Equipment) SimpleLegalScope(pcgen.base.formula.inst.SimpleLegalScope) LegalScope(pcgen.base.formula.base.LegalScope) VariableID(pcgen.base.formula.base.VariableID) PCGenModifier(pcgen.base.calculation.PCGenModifier) Test(org.junit.Test)

Example 20 with ScopeInstance

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

the class ResultFacet method getLocalVariable.

public Object getLocalVariable(CharID id, CDOMObject cdo, String varName) {
    String localScopeName = cdo.getLocalScopeName();
    if (localScopeName == null) {
        return getGlobalVariable(id, varName);
    }
    ScopeInstance scope = scopeFacet.get(id, localScopeName, cdo);
    if (scope == null) {
        Logging.errorPrint("Improperly built " + cdo.getClass().getSimpleName() + ": " + cdo.getKeyName() + " had no VariableScope");
        return null;
    }
    VariableID<?> varID = variableLibraryFacet.getVariableID(id.getDatasetID(), scope, varName);
    return variableStoreFacet.getValue(id, varID);
}
Also used : ScopeInstance(pcgen.base.formula.base.ScopeInstance)

Aggregations

ScopeInstance (pcgen.base.formula.base.ScopeInstance)24 LegalScope (pcgen.base.formula.base.LegalScope)8 Equipment (pcgen.core.Equipment)7 VariableID (pcgen.base.formula.base.VariableID)6 CDOMObject (pcgen.cdom.base.CDOMObject)6 VarModifier (pcgen.cdom.content.VarModifier)6 Test (org.junit.Test)5 ParseResult (pcgen.rules.persistence.token.ParseResult)5 CharID (pcgen.cdom.enumeration.CharID)4 EquipmentHead (pcgen.cdom.inst.EquipmentHead)4 VarScoped (pcgen.base.formula.base.VarScoped)3 VariableLibrary (pcgen.base.formula.base.VariableLibrary)3 ScopeInstanceFactory (pcgen.base.formula.inst.ScopeInstanceFactory)3 SimpleNode (pcgen.base.formula.parse.SimpleNode)3 ReconstructionVisitor (pcgen.base.formula.visitor.ReconstructionVisitor)3 RemoteModifier (pcgen.cdom.content.RemoteModifier)3 PCGenModifier (pcgen.base.calculation.PCGenModifier)2 FormulaSemantics (pcgen.base.formula.base.FormulaSemantics)2 ManagerFactory (pcgen.base.formula.base.ManagerFactory)2 SemanticsVisitor (pcgen.base.formula.visitor.SemanticsVisitor)2