Search in sources :

Example 6 with Prerequisite

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

the class PreParserFactory method parse.

public static List<Prerequisite> parse(final List<String> preStrings) {
    final List<Prerequisite> ret = new ArrayList<>(preStrings.size());
    for (String prestr : preStrings) {
        try {
            final PreParserFactory factory = PreParserFactory.getInstance();
            final Prerequisite prereq = factory.parse(prestr);
            ret.add(prereq);
        } catch (PersistenceLayerException ple) {
            //The message is now produced at a lower level, and thus has to be localised there.
            Logging.errorPrint(ple.getMessage(), ple);
        //Logging.errorPrintLocalised(PropertyFactory.getString("PrereqHandler.Unable_to_parse"), object); //$NON-NLS-1$
        }
    }
    return ret;
}
Also used : PersistenceLayerException(pcgen.persistence.PersistenceLayerException) ArrayList(java.util.ArrayList) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 7 with Prerequisite

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

the class AbstractPrerequisiteParser method parse.

/**
	 * Parses the PreRequisite.
	 * 
	 * @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 {
    // Check to make sure that this class can parse this token
    boolean foundTag = false;
    for (int i = 0; i < kindsHandled().length; i++) {
        String arrayElement = kindsHandled()[i];
        if (arrayElement.equalsIgnoreCase(kind)) {
            foundTag = true;
            break;
        }
    }
    if (!foundTag) {
        throw new PersistenceLayerException(this.getClass().getName() + " can not parse a Prerequisite tag of '" + kind + ":" + formula + "'");
    }
    // If we can parse this token then set the kind and invert flag.
    Prerequisite prereq = new Prerequisite();
    prereq.setKind(kind);
    prereq.setOverrideQualify(overrideQualify);
    return prereq;
}
Also used : PersistenceLayerException(pcgen.persistence.PersistenceLayerException) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 8 with Prerequisite

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

the class AbstractPrerequisiteSimpleParser method parse.

/**
	 * Parse PreReq 
	 * 
	 * @param kind 
	 * @param formula 
	 * @param invertResult 
	 * @param overrideQualify 
	 * @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);
    prereq.setOperator(PrerequisiteOperator.EQ);
    if (invertResult) {
        prereq.setOperator(prereq.getOperator().invert());
    }
    return prereq;
}
Also used : Prerequisite(pcgen.core.prereq.Prerequisite)

Example 9 with Prerequisite

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

the class PreMultParser method parse.

@Override
public Prerequisite parse(String kind, String formula, boolean invertResult, boolean overrideQualify) throws PersistenceLayerException {
    Prerequisite prereq = super.parse(kind, formula, invertResult, overrideQualify);
    prereq.setKind(null);
    prereq.setCharacterRequired(false);
    int commaIndex = formula.indexOf(",");
    if (commaIndex > 0) {
        String minFormula = formula.substring(0, commaIndex);
        formula = formula.substring(commaIndex + 1);
        prereq.setOperator(PrerequisiteOperator.GTEQ);
        prereq.setOperand(minFormula);
    }
    // [PREARMORPROF:1,TYPE.Medium],[PREFEAT:1,Armor Proficiency (Medium)]
    PreParserFactory parser = PreParserFactory.getInstance();
    for (String s : splitOnTopLevelToken(formula, '[', ']')) {
        prereq.addPrerequisite(parser.parse(s));
    }
    if (invertResult) {
        prereq.setOperator(prereq.getOperator().invert());
    }
    return prereq;
}
Also used : Prerequisite(pcgen.core.prereq.Prerequisite)

Example 10 with Prerequisite

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

the class PrerequisiteMultWriter method write.

@Override
public void write(Writer writer, Prerequisite prereq) throws PersistenceLayerException {
    checkValidOperator(prereq, operatorsHandled());
    try {
        Prerequisite subreq;
        //
        if (isSpecialCase(prereq)) {
            handleSpecialCase(writer, prereq);
            return;
        }
        if (isNegatedPreability(prereq)) {
            handleNegatedPreAbility(writer, prereq);
            return;
        }
        if (prereq.getPrerequisiteCount() != 0) {
            subreq = prereq.getPrerequisites().get(0);
            final PrerequisiteWriterInterface test = PrerequisiteWriterFactory.getInstance().getWriter(subreq.getKind());
            if ((test != null) && (test instanceof AbstractPrerequisiteWriter) && ((AbstractPrerequisiteWriter) test).specialCase(writer, prereq)) {
                return;
            }
        }
        if (prereq.getOperator().equals(PrerequisiteOperator.LT)) {
            writer.write('!');
        }
        writer.write("PREMULT:");
        writer.write(prereq.getOperand());
        writer.write(',');
        int i = 0;
        for (Prerequisite pre : prereq.getPrerequisites()) {
            if (i > 0) {
                writer.write(',');
            }
            writer.write('[');
            PrerequisiteWriterFactory factory = PrerequisiteWriterFactory.getInstance();
            PrerequisiteWriterInterface w = factory.getWriter(pre.getKind());
            if (w != null) {
                w.write(writer, pre);
            } else {
                writer.write("unrecognized kind:" + pre.getKind());
            }
            writer.write(']');
            i++;
        }
    } catch (IOException e) {
        throw new PersistenceLayerException(e.getMessage());
    }
}
Also used : PersistenceLayerException(pcgen.persistence.PersistenceLayerException) IOException(java.io.IOException) 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