use of pcgen.base.formula.base.LegalScope in project pcgen by PCGen.
the class InfoVarsLst method parseTokenWithSeparator.
@Override
protected ParseResult parseTokenWithSeparator(LoadContext context, CDOMObject cdo, String value) {
int pipeLoc = value.indexOf(Constants.PIPE);
if (pipeLoc == -1) {
return new ParseResult.Fail(getTokenName() + " expecting '|', format is: InfoName|Info value was: " + value, context);
}
String key = value.substring(0, pipeLoc);
//key length 0 caught by charAt(0) test above
String[] val = value.substring(pipeLoc + 1).split("\\|");
VariableContext varContext = context.getVariableContext();
for (String name : val) {
LegalScope scope = context.getActiveScope().getLegalScope();
if (!varContext.isLegalVariableID(scope, name)) {
return new ParseResult.Fail(getTokenName() + " found an error. " + name + " is not a legal variable name in scope " + scope.getName() + " in " + cdo.getClass().getSimpleName() + ' ' + cdo.getKeyName(), context);
}
}
CaseInsensitiveString cis = new CaseInsensitiveString(key);
context.getObjectContext().put(cdo, MapKey.INFOVARS, cis, val);
return ParseResult.SUCCESS;
}
use of pcgen.base.formula.base.LegalScope in project pcgen by PCGen.
the class GlobalToken method parseNonEmptyToken.
@Override
protected ParseResult parseNonEmptyToken(LoadContext context, DatasetVariable dv, String value) {
//Just a name
if (dv.getDisplayName() != null) {
return new ParseResult.Fail(getTokenName() + " must be the first token on the line");
}
String format;
String varName;
int equalLoc = value.indexOf('=');
if (equalLoc == -1) {
//Defaults to NUMBER
format = "NUMBER";
varName = value;
} else {
format = value.substring(0, equalLoc);
varName = value.substring(equalLoc + 1);
}
FormatManager<?> formatManager;
try {
formatManager = context.getReferenceContext().getFormatManager(format);
} catch (IllegalArgumentException e) {
return new ParseResult.Fail(getTokenName() + " does not support format " + format + ", found in " + value + " due to " + e.getMessage());
}
LegalScope scope = context.getActiveScope().getLegalScope();
if (!DatasetVariable.isLegalName(varName)) {
return new ParseResult.Fail(varName + " is not a valid variable name");
}
boolean legal = context.getVariableContext().assertLegalVariableID(scope, formatManager, varName);
if (!legal) {
Set<LegalScope> known = context.getVariableContext().getKnownLegalScopes(varName);
StringBuilder sb = new StringBuilder();
for (LegalScope v : known) {
sb.append(v.getName());
sb.append(", ");
}
return new ParseResult.Fail(getTokenName() + " found a var defined in incompatible variable scopes: " + value + " was requested in " + scope.getName() + " but was previously in " + sb.toString(), context);
}
dv.setName(varName);
dv.setFormat(format);
dv.setScopeName("Global Variables");
return ParseResult.SUCCESS;
}
use of pcgen.base.formula.base.LegalScope in project pcgen by PCGen.
the class LocalToken method parseNonEmptyToken.
@Override
protected ParseResult parseNonEmptyToken(LoadContext context, DatasetVariable dv, String value) {
int pipeLoc = value.indexOf(Constants.PIPE);
if (pipeLoc == -1) {
return new ParseResult.Fail(getTokenName() + " expected 2 pipe delimited arguments, found no pipe: " + value);
}
if (pipeLoc != value.lastIndexOf(Constants.PIPE)) {
return new ParseResult.Fail(getTokenName() + " expected only 2 pipe delimited arguments, found: " + value);
}
String fullscope = value.substring(0, pipeLoc);
String fvName = value.substring(pipeLoc + 1);
String format;
String varName;
int equalLoc = fvName.indexOf('=');
if (equalLoc != fvName.lastIndexOf('=')) {
return new ParseResult.Fail(getTokenName() + " expected only 2 equal delimited arguments, found: " + value);
}
if (equalLoc == -1) {
//Defaults to NUMBER
format = "NUMBER";
varName = fvName;
} else {
format = fvName.substring(0, equalLoc);
varName = fvName.substring(equalLoc + 1);
}
if (dv.getDisplayName() != null) {
return new ParseResult.Fail(getTokenName() + " must be the first token on the line");
}
VariableContext varContext = context.getVariableContext();
FormatManager<?> formatManager;
try {
formatManager = context.getReferenceContext().getFormatManager(format);
} catch (IllegalArgumentException e) {
return new ParseResult.Fail(getTokenName() + " does not support format " + format + ", found in " + value + " due to " + e.getMessage());
}
LegalScope lvs = varContext.getScope(fullscope);
if (!DatasetVariable.isLegalName(varName)) {
return new ParseResult.Fail(varName + " is not a valid variable name");
}
boolean legal = varContext.assertLegalVariableID(lvs, formatManager, varName);
if (!legal) {
Set<LegalScope> known = varContext.getKnownLegalScopes(varName);
StringBuilder sb = new StringBuilder();
for (LegalScope v : known) {
sb.append(v.getName());
sb.append(", ");
}
return new ParseResult.Fail(getTokenName() + " found a var defined in incompatible variable scopes: " + varName + " was requested in " + fullscope + " but was previously in " + sb.toString(), context);
}
dv.setName(varName);
dv.setFormat(format);
dv.setScopeName(fullscope);
return ParseResult.SUCCESS;
}
use of pcgen.base.formula.base.LegalScope 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));
}
use of pcgen.base.formula.base.LegalScope in project pcgen by PCGen.
the class LoadContextInst method getActiveScope.
@Override
public ScopeInstance getActiveScope() {
if (scopeInst == null) {
LegalScope legalScope = var.getScope("Global");
scopeInst = new SimpleScopeInstance(null, legalScope, new DummyVarScoped(legalScope));
}
return scopeInst;
}
Aggregations