Search in sources :

Example 66 with Prerequisite

use of pcgen.core.prereq.Prerequisite in project pcgen by PCGen.

the class PreVisionParser method validateNotZero.

private static boolean validateNotZero(Prerequisite prereq) {
    boolean returnValue = true;
    if (prereq.getKind() != null && prereq.getKind().equalsIgnoreCase("VISION")) {
        if ("0".equals(prereq.getOperand())) {
            Logging.deprecationPrint("Found PREVISION that is invalid (vision=0 is always true)");
            Logging.deprecationPrint("  You should use =1 or =ANY");
            returnValue &= false;
        }
    }
    for (Prerequisite element : prereq.getPrerequisites()) {
        returnValue &= validateNotZero(element);
    }
    return returnValue;
}
Also used : Prerequisite(pcgen.core.prereq.Prerequisite)

Example 67 with Prerequisite

use of pcgen.core.prereq.Prerequisite in project pcgen by PCGen.

the class DomainsToken method unparse.

@Override
public String[] unparse(LoadContext context, Deity deity) {
    CDOMReference<DomainList> dl = Deity.DOMAINLIST;
    AssociatedChanges<CDOMReference<Domain>> changes = context.getListContext().getChangesInList(getTokenName(), deity, dl);
    List<String> list = new ArrayList<>();
    Collection<CDOMReference<Domain>> removedItems = changes.getRemoved();
    if (changes.includesGlobalClear()) {
        if (removedItems != null && !removedItems.isEmpty()) {
            context.addWriteMessage("Non-sensical relationship in " + getTokenName() + ": global .CLEAR and local .CLEAR. performed");
            return null;
        }
        list.add(Constants.LST_DOT_CLEAR);
    } else if (removedItems != null && !removedItems.isEmpty()) {
        list.add(Constants.LST_DOT_CLEAR_DOT + ReferenceUtilities.joinLstFormat(removedItems, ",.CLEAR.", true));
    }
    MapToList<CDOMReference<Domain>, AssociatedPrereqObject> mtl = changes.getAddedAssociations();
    if (mtl != null && !mtl.isEmpty()) {
        MapToList<Set<Prerequisite>, CDOMReference<Domain>> m = new HashMapToList<>();
        for (CDOMReference<Domain> ab : mtl.getKeySet()) {
            for (AssociatedPrereqObject assoc : mtl.getListFor(ab)) {
                m.addToListFor(new HashSet<>(assoc.getPrerequisiteList()), ab);
            }
        }
        Set<String> set = new TreeSet<>();
        for (Set<Prerequisite> prereqs : m.getKeySet()) {
            Set<CDOMReference<Domain>> domainSet = new TreeSet<>(ReferenceUtilities.REFERENCE_SORTER);
            domainSet.addAll(m.getListFor(prereqs));
            StringBuilder sb = new StringBuilder(ReferenceUtilities.joinLstFormat(domainSet, Constants.COMMA, true));
            if (prereqs != null && !prereqs.isEmpty()) {
                sb.append(Constants.PIPE);
                sb.append(getPrerequisiteString(context, prereqs));
            }
            set.add(sb.toString());
        }
        list.addAll(set);
    }
    if (list.isEmpty()) {
        return null;
    }
    return list.toArray(new String[list.size()]);
}
Also used : Set(java.util.Set) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) ArrayList(java.util.ArrayList) DomainList(pcgen.cdom.list.DomainList) HashMapToList(pcgen.base.util.HashMapToList) TreeSet(java.util.TreeSet) Domain(pcgen.core.Domain) CDOMReference(pcgen.cdom.base.CDOMReference) AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 68 with Prerequisite

use of pcgen.core.prereq.Prerequisite in project pcgen by PCGen.

the class DomainsToken method parseTokenWithSeparator.

@Override
protected ParseResult parseTokenWithSeparator(LoadContext context, Deity deity, String value) {
    StringTokenizer pipeTok = new StringTokenizer(value, Constants.PIPE);
    StringTokenizer commaTok = new StringTokenizer(pipeTok.nextToken(), Constants.COMMA);
    CDOMReference<DomainList> dl = Deity.DOMAINLIST;
    ArrayList<AssociatedPrereqObject> proList = new ArrayList<>();
    boolean first = true;
    boolean foundAll = false;
    boolean foundOther = false;
    boolean foundClear = false;
    while (commaTok.hasMoreTokens()) {
        String tokString = commaTok.nextToken();
        if (looksLikeAPrerequisite(tokString)) {
            return new ParseResult.Fail("Invalid " + getTokenName() + ": PRExxx was comma delimited : " + value, context);
        }
        if (Constants.LST_DOT_CLEAR.equals(tokString)) {
            if (!first) {
                return new ParseResult.Fail("  Non-sensical " + getTokenName() + ": .CLEAR was not the first list item: " + value, context);
            }
            context.getListContext().removeAllFromList(getTokenName(), deity, dl);
            foundClear = true;
        } else if (tokString.startsWith(Constants.LST_DOT_CLEAR_DOT)) {
            CDOMReference<Domain> ref;
            String clearText = tokString.substring(7);
            if (Constants.LST_ALL.equals(clearText) || Constants.LST_ANY.equals(clearText)) {
                ref = context.getReferenceContext().getCDOMAllReference(DOMAIN_CLASS);
            } else {
                ref = context.getReferenceContext().getCDOMReference(DOMAIN_CLASS, clearText);
            }
            context.getListContext().removeFromList(getTokenName(), deity, dl, ref);
            foundClear = true;
        } else if (Constants.LST_ALL.equals(tokString) || Constants.LST_ANY.equals(tokString)) {
            CDOMGroupRef<Domain> ref = context.getReferenceContext().getCDOMAllReference(DOMAIN_CLASS);
            proList.add(context.getListContext().addToList(getTokenName(), deity, dl, ref));
            foundAll = true;
        } else {
            CDOMSingleRef<Domain> ref = context.getReferenceContext().getCDOMReference(DOMAIN_CLASS, tokString);
            proList.add(context.getListContext().addToList(getTokenName(), deity, dl, ref));
            foundOther = true;
        }
        first = false;
    }
    if (foundAll && foundOther) {
        return new ParseResult.Fail("Non-sensical " + getTokenName() + ": Contains ALL and a specific reference: " + value, context);
    }
    while (pipeTok.hasMoreTokens()) {
        if (foundClear) {
            return new ParseResult.Fail("Cannot use PREREQs when using .CLEAR or .CLEAR. in " + getTokenName(), context);
        }
        String tokString = pipeTok.nextToken();
        Prerequisite prereq = getPrerequisite(tokString);
        if (prereq == null) {
            return new ParseResult.Fail("   (Did you put items after the " + "PRExxx tags in " + getTokenName() + ":?)", context);
        }
        for (AssociatedPrereqObject ao : proList) {
            ao.addPrerequisite(prereq);
        }
    }
    return ParseResult.SUCCESS;
}
Also used : ArrayList(java.util.ArrayList) DomainList(pcgen.cdom.list.DomainList) StringTokenizer(java.util.StringTokenizer) Domain(pcgen.core.Domain) CDOMReference(pcgen.cdom.base.CDOMReference) AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 69 with Prerequisite

use of pcgen.core.prereq.Prerequisite in project pcgen by PCGen.

the class ClassLevelDomainBracketToken method parseTokenWithSeparator.

@Override
protected ParseResult parseTokenWithSeparator(LoadContext context, PCClassLevel level, String value) {
    Logging.deprecationPrint(getMessage(level, value));
    StringTokenizer pipeTok = new StringTokenizer(value, Constants.PIPE);
    boolean first = true;
    while (pipeTok.hasMoreTokens()) {
        String tok = pipeTok.nextToken();
        if (Constants.LST_DOT_CLEAR.equals(tok)) {
            if (!first) {
                return new ParseResult.Fail("  Non-sensical " + getTokenName() + ": .CLEAR was not the first list item", context);
            }
            context.getObjectContext().removeList(level, ListKey.DOMAIN);
            continue;
        }
        // Note: May contain PRExxx
        String domainKey;
        Prerequisite prereq = null;
        int openBracketLoc = tok.indexOf('[');
        if (openBracketLoc == -1) {
            if (tok.indexOf(']') != -1) {
                return new ParseResult.Fail("Invalid " + getTokenName() + " must have '[' if it contains a PREREQ tag", context);
            }
            domainKey = tok;
        } else {
            if (tok.lastIndexOf(']') != tok.length() - 1) {
                return new ParseResult.Fail("Invalid " + getTokenName() + " must end with ']' if it contains a PREREQ tag", context);
            }
            domainKey = tok.substring(0, openBracketLoc);
            String prereqString = tok.substring(openBracketLoc + 1, tok.length() - 1);
            if (prereqString.isEmpty()) {
                return new ParseResult.Fail(getTokenName() + " cannot have empty prerequisite : " + value, context);
            }
            prereq = getPrerequisite(prereqString);
            if (prereq == null) {
                return new ParseResult.Fail(getTokenName() + " had invalid prerequisite : " + prereqString, context);
            }
        }
        CDOMSingleRef<Domain> domain = context.getReferenceContext().getCDOMReference(DOMAIN_CLASS, domainKey);
        QualifiedObject<CDOMSingleRef<Domain>> qo = new QualifiedObject<>(domain);
        if (prereq != null) {
            qo.addPrerequisite(prereq);
        }
        context.getObjectContext().addToList(level, ListKey.DOMAIN, qo);
        first = false;
    }
    return ParseResult.SUCCESS;
}
Also used : StringTokenizer(java.util.StringTokenizer) QualifiedObject(pcgen.core.QualifiedObject) Domain(pcgen.core.Domain) CDOMSingleRef(pcgen.cdom.reference.CDOMSingleRef) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 70 with Prerequisite

use of pcgen.core.prereq.Prerequisite in project pcgen by PCGen.

the class PreEquipBothWriter method specialCase.

@Override
public boolean specialCase(Writer writer, Prerequisite prereq) throws IOException {
    PrerequisiteOperator po = getConsolidateMethod(kindHandled(), prereq, false);
    if (po == null) {
        return false;
    }
    if (!po.equals(prereq.getOperator())) {
        writer.write('!');
    }
    writer.write("PRE" + kindHandled().toUpperCase() + ':' + (prereq.isOverrideQualify() ? "Q:" : ""));
    writer.write(po.equals(PrerequisiteOperator.GTEQ) ? prereq.getOperand() : "1");
    for (Prerequisite p : prereq.getPrerequisites()) {
        writer.write(',');
        writer.write(p.getKey());
    }
    return true;
}
Also used : PrerequisiteOperator(pcgen.core.prereq.PrerequisiteOperator) Prerequisite(pcgen.core.prereq.Prerequisite)

Aggregations

Prerequisite (pcgen.core.prereq.Prerequisite)267 PersistenceLayerException (pcgen.persistence.PersistenceLayerException)62 Test (org.junit.Test)61 PrerequisiteOperator (pcgen.core.prereq.PrerequisiteOperator)50 StringTokenizer (java.util.StringTokenizer)36 ArrayList (java.util.ArrayList)35 StringWriter (java.io.StringWriter)19 AssociatedPrereqObject (pcgen.cdom.base.AssociatedPrereqObject)19 CDOMReference (pcgen.cdom.base.CDOMReference)18 ParseResult (pcgen.rules.persistence.token.ParseResult)18 BonusObj (pcgen.core.bonus.BonusObj)13 TreeSet (java.util.TreeSet)12 PreParserFactory (pcgen.persistence.lst.prereq.PreParserFactory)12 CDOMSingleRef (pcgen.cdom.reference.CDOMSingleRef)10 Domain (pcgen.core.Domain)10 QualifiedObject (pcgen.core.QualifiedObject)10 PrerequisiteWriterInterface (pcgen.persistence.lst.output.prereq.PrerequisiteWriterInterface)10 PrerequisiteWriter (pcgen.persistence.lst.output.prereq.PrerequisiteWriter)9 Ungranted (pcgen.cdom.base.Ungranted)8 PrerequisiteException (pcgen.core.prereq.PrerequisiteException)8