Search in sources :

Example 61 with Prerequisite

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

the class WeaponProfToken method unparse.

@Override
public String[] unparse(LoadContext context, CDOMObject obj) {
    List<String> list = new ArrayList<>();
    Changes<ChooseSelectionActor<?>> listChanges = context.getObjectContext().getListChanges(obj, ListKey.NEW_CHOOSE_ACTOR);
    Changes<WeaponProfProvider> changes = context.getObjectContext().getListChanges(obj, ListKey.WEAPONPROF);
    QualifiedObject<Boolean> deityweap = context.getObjectContext().getObject(obj, ObjectKey.HAS_DEITY_WEAPONPROF);
    Collection<WeaponProfProvider> added = changes.getAdded();
    Collection<ChooseSelectionActor<?>> listAdded = listChanges.getAdded();
    boolean foundAny = false;
    boolean foundOther = false;
    if (listAdded != null && !listAdded.isEmpty()) {
        foundOther = true;
        for (ChooseSelectionActor<?> cra : listAdded) {
            if (cra.getSource().equals(getTokenName())) {
                try {
                    list.add(cra.getLstFormat());
                } catch (PersistenceLayerException e) {
                    context.addWriteMessage("Error writing Prerequisite: " + e);
                    return null;
                }
            }
        }
    }
    if (deityweap != null && deityweap.getRawObject()) {
        foundOther = true;
        StringBuilder sb = new StringBuilder();
        sb.append("DEITYWEAPONS");
        if (deityweap.hasPrerequisites()) {
            sb.append('|');
            sb.append(context.getPrerequisiteString(deityweap.getPrerequisiteList()));
        }
        list.add(sb.toString());
    }
    if (added != null) {
        for (WeaponProfProvider wpp : added) {
            if (!wpp.isValid()) {
                context.addWriteMessage("Non-sensical " + "WeaponProfProvider in " + getFullName() + ": Had invalid contents");
                return null;
            }
            StringBuilder sb = new StringBuilder(wpp.getLstFormat());
            List<Prerequisite> prereqs = wpp.getPrerequisiteList();
            if (prereqs != null && !prereqs.isEmpty()) {
                if (prereqs.size() > 1) {
                    context.addWriteMessage("Error: " + obj.getClass().getSimpleName() + " had more than one Prerequisite for " + getFullName());
                    return null;
                }
                sb.append('|');
                sb.append(context.getPrerequisiteString(prereqs));
            }
            String lstFormat = sb.toString();
            boolean isUnconditionalAll = Constants.LST_ALL.equals(lstFormat);
            foundAny |= isUnconditionalAll;
            foundOther |= !isUnconditionalAll;
            list.add(lstFormat);
        }
    }
    if (foundAny && foundOther) {
        context.addWriteMessage("Non-sensical " + getFullName() + ": Contains ANY and a specific reference: " + list);
        return null;
    }
    if (list.isEmpty()) {
        // Empty indicates no Token
        return null;
    }
    return list.toArray(new String[list.size()]);
}
Also used : ArrayList(java.util.ArrayList) ChooseSelectionActor(pcgen.cdom.base.ChooseSelectionActor) WeaponProfProvider(pcgen.cdom.helper.WeaponProfProvider) PersistenceLayerException(pcgen.persistence.PersistenceLayerException) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 62 with Prerequisite

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

the class PreStatParser method parse.

/**
	 * Parse the pre req list
	 *
	 * @param kind The kind of the prerequisite (less the "PRE" prefix)
	 * @param formula The body of the prerequisite.
	 * @param invertResult Whether the prerequisite should invert the result.
	 * @param overrideQualify
	 *           if set true, this prerequisite will be enforced in spite
	 *           of any "QUALIFY" tag that may be present.
	 * @return PreReq
	 * @throws PersistenceLayerException
	 */
@Override
public Prerequisite parse(String kind, String formula, boolean invertResult, boolean overrideQualify) throws PersistenceLayerException {
    Prerequisite prereq = super.parse(kind, formula, invertResult, overrideQualify);
    try {
        // PREMULT
        prereq.setKind(null);
        // Get the comparator type STATGTEQ, STAT, STATNEQ etc.
        String compType = kind.substring(4);
        if (compType.isEmpty()) {
            compType = "gteq";
        }
        String[] tokens = formula.split(",|\\|");
        int currToken = 0;
        // Get the minimum match count
        String aString = tokens[currToken++];
        try {
            prereq.setOperand(Integer.toString(Integer.parseInt(aString)));
        } catch (NumberFormatException e) {
            Logging.errorPrint("Badly formed PRESTAT attribute: " + aString);
            prereq.setOperand("1");
        }
        while (currToken < tokens.length) {
            aString = tokens[currToken++];
            final int idxEquals = aString.lastIndexOf('=');
            if (idxEquals < 3) {
                throw new PersistenceLayerException("PRE" + kindsHandled()[0] + " formula '" + formula + "' is not valid.");
            }
            final String stat = aString.substring(0, Math.min(3, idxEquals));
            Prerequisite statPrereq = new Prerequisite();
            statPrereq.setKind("stat");
            statPrereq.setKey(stat);
            statPrereq.setOperator(compType);
            statPrereq.setOperand(aString.substring(idxEquals + 1));
            prereq.addPrerequisite(statPrereq);
        }
        if ((prereq.getPrerequisiteCount() == 1) && (prereq.getOperator() == PrerequisiteOperator.GTEQ) && prereq.getOperand().equals("1")) {
            prereq = prereq.getPrerequisites().get(0);
        }
        if (invertResult) {
            prereq.setOperator(prereq.getOperator().invert());
        }
    } catch (PrerequisiteException pe) {
        throw new PersistenceLayerException("Unable to parse the prerequisite :'" + kind + ':' + formula + "'. " + pe.getLocalizedMessage());
    }
    return prereq;
}
Also used : PersistenceLayerException(pcgen.persistence.PersistenceLayerException) PrerequisiteException(pcgen.core.prereq.PrerequisiteException) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 63 with Prerequisite

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

the class PreTextParser method parse.

/**
	 * Parse the pre req list
	 *
	 * @param kind The kind of the prerequisite (less the "PRE" prefix)
	 * @param formula The body of the prerequisite.
	 * @param invertResult Whether the prerequisite should invert the result.
	 * @param overrideQualify
	 *           if set true, this prerequisite will be enforced in spite
	 *           of any "QUALIFY" tag that may be present.
	 * @return PreReq
	 * @throws PersistenceLayerException
	 */
@Override
public Prerequisite parse(String kind, String formula, boolean invertResult, boolean overrideQualify) throws PersistenceLayerException {
    Prerequisite prereq = super.parse(kind, formula, invertResult, overrideQualify);
    prereq.setKey(formula);
    if (invertResult) {
        prereq.setOperator(prereq.getOperator().invert());
    }
    return prereq;
}
Also used : Prerequisite(pcgen.core.prereq.Prerequisite)

Example 64 with Prerequisite

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

the class PreTypeParser method parse.

/**
	 * Parse the pre req list
	 *
	 * @param kind The kind of the prerequisite (less the "PRE" prefix)
	 * @param formula The body of the prerequisite.
	 * @param invertResult Whether the prerequisite should invert the result.
	 * @param overrideQualify
	 *           if set true, this prerequisite will be enforced in spite
	 *           of any "QUALIFY" tag that may be present.
	 * @return PreReq
	 * @throws PersistenceLayerException
	 */
@Override
public Prerequisite parse(String kind, String formula, boolean invertResult, boolean overrideQualify) throws PersistenceLayerException {
    int aNum;
    final StringTokenizer aTok = new StringTokenizer(formula, ",");
    String aString = aTok.nextToken();
    try {
        aNum = Integer.parseInt(aString);
    } catch (NumberFormatException nfe) {
        throw new PersistenceLayerException(formula + " must start with a number in PRETYPE", nfe);
    }
    // Parse new style syntax
    Prerequisite prereq = super.parse(kind, formula, invertResult, overrideQualify);
    prereq.setOperand(Integer.toString(aNum));
    //
    if (aTok.countTokens() == 1) {
        prereq.setOperator(PrerequisiteOperator.EQ);
        prereq.setKey(aTok.nextToken());
    } else {
        // PREMULT
        prereq.setKind(null);
        while (aTok.hasMoreTokens()) {
            Prerequisite subreq = new Prerequisite();
            subreq.setOperator(PrerequisiteOperator.EQ);
            subreq.setKind("type");
            subreq.setKey(aTok.nextToken());
            prereq.addPrerequisite(subreq);
        }
    }
    if (invertResult) {
        prereq.setOperator(prereq.getOperator().invert());
    }
    //prereq.setDeprecated();
    return prereq;
}
Also used : PersistenceLayerException(pcgen.persistence.PersistenceLayerException) StringTokenizer(java.util.StringTokenizer) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 65 with Prerequisite

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

the class PreVariableParser method parse.

/**
	 * Parse the pre req list
	 *
	 * @param kind The kind of the prerequisite (less the "PRE" prefix)
	 * @param formula The body of the prerequisite.
	 * @param invertResult Whether the prerequisite should invert the result.
	 * @param overrideQualify
	 *           if set true, this prerequisite will be enforced in spite
	 *           of any "QUALIFY" tag that may be present.
	 * @return PreReq
	 * @throws PersistenceLayerException
	 */
@Override
public Prerequisite parse(String kind, String formula, boolean invertResult, boolean overrideQualify) throws PersistenceLayerException {
    Prerequisite prereq = super.parse(kind, formula, invertResult, overrideQualify);
    prereq.setKind("var");
    // Get the comparator type SIZEGTEQ, BSIZE, SIZENEQ etc.
    String compType = kind.substring(3);
    if (compType.isEmpty()) {
        compType = "gteq";
    }
    ParsingSeparator ps = new ParsingSeparator(formula, ',');
    ps.addGroupingPair('[', ']');
    ps.addGroupingPair('(', ')');
    try {
        int count = 0;
        while (ps.hasNext()) {
            String first = ps.next();
            if (!ps.hasNext()) {
                throw new PersistenceLayerException("Unable to parse prerequisite 'PRE" + kind + ':' + formula + "'. Incorrect parameter count (must be even)");
            }
            String second = ps.next();
            Prerequisite subreq;
            if (!ps.hasNext() && count == 0) {
                subreq = prereq;
            } else {
                // PREMULT
                prereq.setKind(null);
                subreq = new Prerequisite();
                subreq.setKind("var");
                prereq.addPrerequisite(subreq);
                count++;
            }
            subreq.setOperator(compType.intern());
            subreq.setKey(first.intern());
            subreq.setOperand(second.intern());
        }
        if (count > 0) {
            prereq.setOperand(Integer.toString(count));
        }
    } catch (PrerequisiteException pe) {
        throw new PersistenceLayerException("Unable to parse prerequisite 'PRE" + kind + ':' + formula + "'. " + pe.getLocalizedMessage());
    }
    if (invertResult) {
        prereq.setOperator(prereq.getOperator().invert());
    }
    prereq.setOverrideQualify(overrideQualify);
    return prereq;
}
Also used : PersistenceLayerException(pcgen.persistence.PersistenceLayerException) PrerequisiteException(pcgen.core.prereq.PrerequisiteException) ParsingSeparator(pcgen.base.text.ParsingSeparator) 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