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()]);
}
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;
}
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;
}
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;
}
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;
}
Aggregations