use of pcgen.base.formula.base.LegalScope in project pcgen by PCGen.
the class ModifyLst method finishProcessing.
private <T> ParseResult finishProcessing(LoadContext context, CDOMObject obj, FormatManager<T> formatManager, String varName, String modIdentification, String modInstructions, int priorityNumber) {
ScopeInstance scopeInst = context.getActiveScope();
LegalScope scope = scopeInst.getLegalScope();
PCGenModifier<T> modifier;
try {
modifier = context.getVariableContext().getModifier(modIdentification, modInstructions, priorityNumber, scope, formatManager);
} catch (IllegalArgumentException iae) {
return new ParseResult.Fail(getTokenName() + " Modifier " + modIdentification + " had value " + modInstructions + " but it was not valid: " + iae.getMessage(), context);
}
VarModifier<T> vm = new VarModifier<>(varName, scope, modifier);
context.getObjectContext().addToList(obj, ListKey.MODIFY, vm);
return ParseResult.SUCCESS;
}
use of pcgen.base.formula.base.LegalScope in project pcgen by PCGen.
the class ModifyOtherLst method parseTokenWithSeparator.
//MODIFYOTHER:EQUIPMENT|GROUP=Martial|EqCritRange|ADD|1
@Override
protected ParseResult parseTokenWithSeparator(LoadContext context, CDOMObject obj, String value) {
if (obj instanceof Campaign) {
return new ParseResult.Fail(getTokenName() + " may not be used in Campaign Files. " + "Please use the Global Modifier file", context);
}
ParsingSeparator sep = new ParsingSeparator(value, '|');
sep.addGroupingPair('[', ']');
sep.addGroupingPair('(', ')');
String scopeName = sep.next();
/*
* Note lvs is implicitly defined as "not global" since the global scope
* is "" and thus would have failed the tests imposed by
* AbstractTokenWithSeparator
*/
final LegalScope lvs = context.getVariableContext().getScope(scopeName);
if (lvs == null) {
return new ParseResult.Fail(getTokenName() + " found illegal variable scope: " + scopeName + " as first argument: " + value, context);
}
if (!sep.hasNext()) {
return new ParseResult.Fail(getTokenName() + " needed 2nd argument: " + value, context);
}
LoadContext subContext = context.dropIntoContext(lvs.getName());
return continueParsing(subContext, obj, value, sep);
}
use of pcgen.base.formula.base.LegalScope in project pcgen by PCGen.
the class FaceToken method parseFace.
protected ParseResult parseFace(LoadContext context, PCTemplate fObj, String value) {
if (ControlUtilities.hasControlToken(context, CControl.FACE)) {
return new ParseResult.Fail("FACE: LST Token is disabled when FACE: control is used", context);
}
if (value.indexOf(',') == -1) {
value = value + ',' + 0;
}
@SuppressWarnings("unchecked") FormatManager<OrderedPair> formatManager = (FormatManager<OrderedPair>) context.getReferenceContext().getFormatManager("ORDEREDPAIR");
ScopeInstance scopeInst = context.getActiveScope();
LegalScope scope = scopeInst.getLegalScope();
PCGenModifier<OrderedPair> modifier;
try {
modifier = context.getVariableContext().getModifier(MOD_IDENTIFICATION, value, MOD_PRIORITY, scope, formatManager);
} catch (IllegalArgumentException iae) {
return new ParseResult.Fail(getTokenName() + " Modifier SET had value " + value + " but it was not valid: " + iae.getMessage(), context);
}
OrderedPair pair = modifier.process(null);
if (pair.getPreciseX().doubleValue() < 0.0) {
return new ParseResult.Fail(getTokenName() + " had value " + value + " but first item cannot be negative", context);
}
if (pair.getPreciseY().doubleValue() < 0.0) {
return new ParseResult.Fail(getTokenName() + " had value " + value + " but second item cannot be negative", context);
}
String varName = VAR_NAME;
if (!context.getVariableContext().isLegalVariableID(scope, varName)) {
return new ParseResult.Fail(getTokenName() + " internal error: found invalid fact name: " + varName + ", Modified on " + fObj.getClass().getSimpleName() + ' ' + fObj.getKeyName(), context);
}
VarModifier<OrderedPair> vm = new VarModifier<>(varName, scope, modifier);
context.getObjectContext().addToList(fObj, ListKey.MODIFY, vm);
return ParseResult.SUCCESS;
}
use of pcgen.base.formula.base.LegalScope 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);
}
use of pcgen.base.formula.base.LegalScope 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);
}
Aggregations