Search in sources :

Example 11 with LegalScope

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;
}
Also used : LegalScope(pcgen.base.formula.base.LegalScope) CaseInsensitiveString(pcgen.base.lang.CaseInsensitiveString) VariableContext(pcgen.rules.context.VariableContext) CaseInsensitiveString(pcgen.base.lang.CaseInsensitiveString)

Example 12 with LegalScope

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;
}
Also used : ParseResult(pcgen.rules.persistence.token.ParseResult) LegalScope(pcgen.base.formula.base.LegalScope)

Example 13 with LegalScope

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;
}
Also used : ParseResult(pcgen.rules.persistence.token.ParseResult) LegalScope(pcgen.base.formula.base.LegalScope) VariableContext(pcgen.rules.context.VariableContext)

Example 14 with LegalScope

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));
}
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 15 with LegalScope

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;
}
Also used : SimpleScopeInstance(pcgen.base.formula.inst.SimpleScopeInstance) LegalScope(pcgen.base.formula.base.LegalScope)

Aggregations

LegalScope (pcgen.base.formula.base.LegalScope)20 ScopeInstance (pcgen.base.formula.base.ScopeInstance)8 ParseResult (pcgen.rules.persistence.token.ParseResult)8 VarModifier (pcgen.cdom.content.VarModifier)4 Test (org.junit.Test)3 VariableID (pcgen.base.formula.base.VariableID)3 VariableLibrary (pcgen.base.formula.base.VariableLibrary)3 SimpleNode (pcgen.base.formula.parse.SimpleNode)3 FormatManager (pcgen.base.util.FormatManager)3 Equipment (pcgen.core.Equipment)3 LoadContext (pcgen.rules.context.LoadContext)3 VariableContext (pcgen.rules.context.VariableContext)3 FormulaSemantics (pcgen.base.formula.base.FormulaSemantics)2 SimpleScopeInstance (pcgen.base.formula.inst.SimpleScopeInstance)2 ReconstructionVisitor (pcgen.base.formula.visitor.ReconstructionVisitor)2 SemanticsVisitor (pcgen.base.formula.visitor.SemanticsVisitor)2 OrderedPair (pcgen.base.math.OrderedPair)2 ParsingSeparator (pcgen.base.text.ParsingSeparator)2 Campaign (pcgen.core.Campaign)2 ConsolidatedListCommitStrategy (pcgen.rules.context.ConsolidatedListCommitStrategy)2