Search in sources :

Example 16 with LegalScope

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

the class ModifyLstTest method setUp.

@Override
public void setUp() throws PersistenceLayerException, URISyntaxException {
    super.setUp();
    TokenRegistration.register(new plugin.modifier.number.AddModifierFactory());
    TokenRegistration.register(new plugin.modifier.number.MultiplyModifierFactory());
    FormatManager<?> formatManager = primaryContext.getReferenceContext().getFormatManager("NUMBER");
    LegalScope pscope = primaryContext.getActiveScope().getLegalScope();
    LegalScope sscope = primaryContext.getActiveScope().getLegalScope();
    primaryContext.getVariableContext().assertLegalVariableID(pscope, formatManager, "MyVar");
    secondaryContext.getVariableContext().assertLegalVariableID(sscope, formatManager, "MyVar");
    primaryContext.getVariableContext().assertLegalVariableID(pscope, formatManager, "OtherVar");
    secondaryContext.getVariableContext().assertLegalVariableID(sscope, formatManager, "OtherVar");
}
Also used : LegalScope(pcgen.base.formula.base.LegalScope)

Example 17 with LegalScope

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

the class ModifyOtherLstTest method setUp.

@Override
public void setUp() throws PersistenceLayerException, URISyntaxException {
    super.setUp();
    TokenRegistration.register(new plugin.modifier.number.AddModifierFactory());
    TokenRegistration.register(new plugin.modifier.number.MultiplyModifierFactory());
    FormatManager<?> formatManager = primaryContext.getReferenceContext().getFormatManager("NUMBER");
    LegalScope pscope = primaryContext.getActiveScope().getLegalScope();
    LegalScope sscope = primaryContext.getActiveScope().getLegalScope();
    primaryContext.getVariableContext().assertLegalVariableID(pscope, formatManager, "MyVar");
    secondaryContext.getVariableContext().assertLegalVariableID(sscope, formatManager, "MyVar");
    primaryContext.getVariableContext().assertLegalVariableID(pscope, formatManager, "OtherVar");
    secondaryContext.getVariableContext().assertLegalVariableID(sscope, formatManager, "OtherVar");
}
Also used : LegalScope(pcgen.base.formula.base.LegalScope)

Example 18 with LegalScope

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

the class ModifyLst method parseToken.

@Override
public ParseResult parseToken(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('(', ')');
    if (!sep.hasNext()) {
        return new ParseResult.Fail(getTokenName() + " may not be empty", context);
    }
    String varName = sep.next();
    if (!sep.hasNext()) {
        return new ParseResult.Fail(getTokenName() + " needed 2nd argument: " + value, context);
    }
    String modIdentification = sep.next();
    if (!sep.hasNext()) {
        return new ParseResult.Fail(getTokenName() + " needed third argument: " + value, context);
    }
    String modInstructions = sep.next();
    //Defaults to zero
    int priorityNumber = 0;
    if (sep.hasNext()) {
        String priority = sep.next();
        if (priority.length() < 10) {
            return new ParseResult.Fail(getTokenName() + " was expecting PRIORITY= but got " + priority + " in " + value, context);
        }
        if ("PRIORITY=".equalsIgnoreCase(priority.substring(0, 9))) {
            try {
                priorityNumber = Integer.parseInt(priority.substring(9));
            } catch (NumberFormatException e) {
                return new ParseResult.Fail(getTokenName() + " requires Priority to be an integer: " + priority.substring(9) + " was not an integer");
            }
            if (priorityNumber < 0) {
                return new ParseResult.Fail(getTokenName() + " Priority requires an integer >= 0. " + priorityNumber + " was not positive");
            }
        } else {
            return new ParseResult.Fail(getTokenName() + " was expecting PRIORITY=x but got " + priority + " in " + value, context);
        }
        if (sep.hasNext()) {
            return new ParseResult.Fail(getTokenName() + " had too many arguments: " + value, context);
        }
    }
    ScopeInstance scopeInst = context.getActiveScope();
    LegalScope scope = scopeInst.getLegalScope();
    if (!context.getVariableContext().isLegalVariableID(scope, varName)) {
        return new ParseResult.Fail(getTokenName() + " found invalid var name: " + varName + "(scope: " + scope.getName() + ") Modified on " + obj.getClass().getSimpleName() + ' ' + obj.getKeyName(), context);
    }
    FormatManager<?> format = context.getVariableContext().getVariableFormat(scope, varName);
    return finishProcessing(context, obj, format, varName, modIdentification, modInstructions, priorityNumber);
}
Also used : ScopeInstance(pcgen.base.formula.base.ScopeInstance) Campaign(pcgen.core.Campaign) ParsingSeparator(pcgen.base.text.ParsingSeparator) ParseResult(pcgen.rules.persistence.token.ParseResult) LegalScope(pcgen.base.formula.base.LegalScope)

Example 19 with LegalScope

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

the class ModifyOtherLst method continueParsing.

private <GT extends VarScoped> ParseResult continueParsing(LoadContext context, CDOMObject obj, String value, ParsingSeparator sep) {
    ScopeInstance scopeInst = context.getActiveScope();
    @SuppressWarnings("unchecked") final LegalScope scope = scopeInst.getLegalScope();
    final String groupingName = sep.next();
    ObjectGrouping group;
    if (groupingName.startsWith("GROUP=")) {
        final String groupName = groupingName.substring(6);
        group = new ObjectGrouping() {

            @Override
            public boolean contains(VarScoped item) {
                return (item instanceof CDOMObject) && ((CDOMObject) item).containsInList(ListKey.GROUP, groupName);
            }

            @Override
            public LegalScope getScope() {
                return scope;
            }

            @Override
            public String getIdentifier() {
                return "GROUP=" + groupName;
            }
        };
    } else if ("ALL".equals(groupingName)) {
        group = new ObjectGrouping() {

            @Override
            public boolean contains(VarScoped cdo) {
                return true;
            }

            @Override
            public LegalScope getScope() {
                return scope;
            }

            @Override
            public String getIdentifier() {
                return "ALL";
            }
        };
    } else {
        group = new ObjectGrouping() {

            @Override
            public boolean contains(VarScoped cdo) {
                return cdo.getKeyName().equalsIgnoreCase(groupingName);
            }

            @Override
            public LegalScope getScope() {
                return scope;
            }

            @Override
            public String getIdentifier() {
                return groupingName;
            }
        };
    }
    if (!sep.hasNext()) {
        return new ParseResult.Fail(getTokenName() + " needed 3rd argument: " + value, context);
    }
    String varName = sep.next();
    if (!context.getVariableContext().isLegalVariableID(scope, varName)) {
        return new ParseResult.Fail(getTokenName() + " found invalid var name: " + varName + " Modified on " + obj.getClass().getSimpleName() + ' ' + obj.getKeyName(), context);
    }
    if (!sep.hasNext()) {
        return new ParseResult.Fail(getTokenName() + " needed 4th argument: " + value, context);
    }
    String modType = sep.next();
    if (!sep.hasNext()) {
        return new ParseResult.Fail(getTokenName() + " needed 5th argument: " + value, context);
    }
    String modValue = sep.next();
    //Defaults to zero
    int priorityNumber = 0;
    if (sep.hasNext()) {
        String priority = sep.next();
        if (priority.length() < 10) {
            return new ParseResult.Fail(getTokenName() + " was expecting PRIORITY= but got " + priority + " in " + value, context);
        }
        if ("PRIORITY=".equalsIgnoreCase(priority.substring(0, 9))) {
            try {
                priorityNumber = Integer.parseInt(priority.substring(9));
            } catch (NumberFormatException e) {
                return new ParseResult.Fail(getTokenName() + " requires Priority to be an integer: " + priority.substring(9) + " was not an integer");
            }
            if (priorityNumber < 0) {
                return new ParseResult.Fail(getTokenName() + " Priority requires an integer >= 0. " + priorityNumber + " was not positive");
            }
        } else {
            return new ParseResult.Fail(getTokenName() + " was expecting PRIORITY=x but got " + priority + " in " + value, context);
        }
        if (sep.hasNext()) {
            return new ParseResult.Fail(getTokenName() + " had too many arguments: " + value, context);
        }
    }
    PCGenModifier<?> modifier;
    try {
        FormatManager<?> format = context.getVariableContext().getVariableFormat(scope, varName);
        modifier = context.getVariableContext().getModifier(modType, modValue, priorityNumber, scope, format);
    } catch (IllegalArgumentException iae) {
        return new ParseResult.Fail(getTokenName() + " Modifier " + modType + " had value " + modValue + " but it was not valid: " + iae.getMessage(), context);
    }
    VarModifier<?> vm = new VarModifier<>(varName, scope, modifier);
    RemoteModifier<?> rm = new RemoteModifier<>(group, vm);
    context.getObjectContext().addToList(obj, ListKey.REMOTE_MODIFIER, rm);
    return ParseResult.SUCCESS;
}
Also used : ScopeInstance(pcgen.base.formula.base.ScopeInstance) ParseResult(pcgen.rules.persistence.token.ParseResult) RemoteModifier(pcgen.cdom.content.RemoteModifier) VarScoped(pcgen.base.formula.base.VarScoped) LegalScope(pcgen.base.formula.base.LegalScope) VarModifier(pcgen.cdom.content.VarModifier) ObjectGrouping(pcgen.cdom.base.ObjectGrouping) CDOMObject(pcgen.cdom.base.CDOMObject)

Example 20 with LegalScope

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

the class ChannelToken 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;
    if ("GLOBAL".equals(fullscope)) {
        lvs = context.getActiveScope().getLegalScope();
    } else {
        lvs = varContext.getScope(fullscope);
    }
    if (!DatasetVariable.isLegalName(varName)) {
        return new ParseResult.Fail(varName + " is not a valid channel name");
    }
    String channelName = ChannelUtilities.createVarName(varName);
    boolean legal = varContext.assertLegalVariableID(lvs, formatManager, channelName);
    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(channelName);
    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)

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