Search in sources :

Example 16 with AssociatedPrereqObject

use of pcgen.cdom.base.AssociatedPrereqObject in project pcgen by PCGen.

the class DomainsTokenTest method testRoundRobinUnparseDirect.

@Test
public void testRoundRobinUnparseDirect() throws PersistenceLayerException {
    construct(primaryContext, "TestWP1");
    construct(primaryContext, "TestWP2");
    AssociatedPrereqObject apo = new SimpleAssociatedObject();
    apo.setAssociation(AssociationKey.TOKEN, "DOMAINS");
    primaryProf.putToList(Deity.DOMAINLIST, CDOMDirectSingleRef.getRef(primaryContext.getReferenceContext().silentlyGetConstructedCDOMObject(getTargetClass(), "TestWP1")), apo);
    primaryProf.putToList(Deity.DOMAINLIST, CDOMDirectSingleRef.getRef(primaryContext.getReferenceContext().silentlyGetConstructedCDOMObject(getTargetClass(), "TestWP2")), apo);
    String[] unparsed = getToken().unparse(primaryContext, primaryProf);
    assertNotNull(unparsed);
    assertEquals(1, unparsed.length);
    assertEquals("TestWP1,TestWP2", unparsed[0]);
}
Also used : AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject) SimpleAssociatedObject(pcgen.cdom.base.SimpleAssociatedObject) Test(org.junit.Test)

Example 17 with AssociatedPrereqObject

use of pcgen.cdom.base.AssociatedPrereqObject in project pcgen by PCGen.

the class SpelllevelLst method subParse.

private <CL extends Loadable & CDOMList<Spell>> boolean subParse(LoadContext context, CDOMObject obj, Class<CL> tagType, String tokString, String spellString, List<Prerequisite> prereqs) {
    int equalLoc = tokString.indexOf(Constants.EQUALS);
    if (equalLoc == -1) {
        Logging.errorPrint("Expected an = in SPELLLEVEL " + "definition: " + tokString);
        return false;
    }
    String casterString = tokString.substring(0, equalLoc);
    String spellLevel = tokString.substring(equalLoc + 1);
    Integer splLevel;
    try {
        splLevel = Integer.decode(spellLevel);
    } catch (NumberFormatException nfe) {
        Logging.errorPrint("Expected a number for SPELLLEVEL, found: " + spellLevel);
        return false;
    }
    if (isEmpty(casterString) || hasIllegalSeparator(',', casterString)) {
        return false;
    }
    StringTokenizer clTok = new StringTokenizer(casterString, Constants.COMMA);
    List<CDOMReference<? extends CDOMList<Spell>>> slList = new ArrayList<>();
    while (clTok.hasMoreTokens()) {
        String classString = clTok.nextToken();
        CDOMReference<CL> ref;
        if (classString.startsWith("SPELLCASTER.")) {
            /*
				 * This is actually a TYPE
				 */
            ref = context.getReferenceContext().getCDOMTypeReference(tagType, classString.substring(12));
        } else {
            ref = context.getReferenceContext().getCDOMReference(tagType, classString);
        }
        slList.add(ref);
    }
    if (hasIllegalSeparator(',', spellString)) {
        return false;
    }
    StringTokenizer spTok = new StringTokenizer(spellString, ",");
    while (spTok.hasMoreTokens()) {
        String spellName = spTok.nextToken();
        CDOMReference<Spell> sp = context.getReferenceContext().getCDOMReference(Spell.class, spellName);
        for (CDOMReference<? extends CDOMList<Spell>> sl : slList) {
            AssociatedPrereqObject tpr = context.getListContext().addToList(getTokenName(), obj, sl, sp);
            tpr.setAssociation(AssociationKey.SPELL_LEVEL, splLevel);
            tpr.addAllPrerequisites(prereqs);
        }
    }
    return true;
}
Also used : ArrayList(java.util.ArrayList) Spell(pcgen.core.spell.Spell) StringTokenizer(java.util.StringTokenizer) CDOMList(pcgen.cdom.base.CDOMList) CDOMReference(pcgen.cdom.base.CDOMReference) AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject)

Example 18 with AssociatedPrereqObject

use of pcgen.cdom.base.AssociatedPrereqObject in project pcgen by PCGen.

the class SpellknownLst method subParse.

/**
	 * Parse the tag contents after the SPELLKNOWN:CLASS| section.
	 *
	 * @param context the context under which the tag is being parsed.
	 * @param obj the obj The object owning the tag.
	 * @param tagType the type of object the tag creates
	 * @param tokString the tok string The string defining the caster type/class and spell level.
	 * @param spellString the spell string The string containing the spell name(s)
	 * @param prereqs the prereqs The prerequisites to be applied.
	 *
	 * @return true, if successful
	 */
private <CL extends Loadable & CDOMList<Spell>> boolean subParse(LoadContext context, CDOMObject obj, Class<CL> tagType, String tokString, String spellString, List<Prerequisite> prereqs) {
    int equalLoc = tokString.indexOf(Constants.EQUALS);
    if (equalLoc == -1) {
        Logging.errorPrint("Expected an = in SPELLKNOWN " + "definition: " + tokString);
        return false;
    }
    String casterString = tokString.substring(0, equalLoc);
    String spellLevel = tokString.substring(equalLoc + 1);
    Integer splLevel;
    try {
        splLevel = Integer.decode(spellLevel);
    } catch (NumberFormatException nfe) {
        Logging.errorPrint("Expected a number for SPELLKNOWN, found: " + spellLevel);
        return false;
    }
    if (isEmpty(casterString) || hasIllegalSeparator(',', casterString)) {
        return false;
    }
    StringTokenizer clTok = new StringTokenizer(casterString, Constants.COMMA);
    List<CDOMReference<? extends CDOMList<Spell>>> slList = new ArrayList<>();
    while (clTok.hasMoreTokens()) {
        String classString = clTok.nextToken();
        CDOMReference<CL> ref;
        if (classString.startsWith("SPELLCASTER.")) {
            /*
				 * This is actually a TYPE
				 */
            ref = context.getReferenceContext().getCDOMTypeReference(tagType, classString.substring(12));
        } else {
            ref = context.getReferenceContext().getCDOMReference(tagType, classString);
        }
        slList.add(ref);
    }
    if (hasIllegalSeparator(',', spellString)) {
        return false;
    }
    StringTokenizer spTok = new StringTokenizer(spellString, ",");
    while (spTok.hasMoreTokens()) {
        String spellName = spTok.nextToken();
        CDOMReference<Spell> sp = context.getReferenceContext().getCDOMReference(Spell.class, spellName);
        for (CDOMReference<? extends CDOMList<Spell>> sl : slList) {
            AssociatedPrereqObject tpr = context.getListContext().addToList(getTokenName(), obj, sl, sp);
            tpr.setAssociation(AssociationKey.SPELL_LEVEL, splLevel);
            tpr.setAssociation(AssociationKey.KNOWN, Boolean.TRUE);
            tpr.addAllPrerequisites(prereqs);
        }
    }
    return true;
}
Also used : ArrayList(java.util.ArrayList) Spell(pcgen.core.spell.Spell) StringTokenizer(java.util.StringTokenizer) CDOMList(pcgen.cdom.base.CDOMList) CDOMReference(pcgen.cdom.base.CDOMReference) AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject)

Example 19 with AssociatedPrereqObject

use of pcgen.cdom.base.AssociatedPrereqObject in project pcgen by PCGen.

the class VisionLst method parseTokenWithSeparator.

@Override
protected ParseResult parseTokenWithSeparator(LoadContext context, CDOMObject obj, String value) {
    if (obj instanceof Ungranted) {
        return new ParseResult.Fail("Cannot use " + getTokenName() + " on an Ungranted object type: " + obj.getClass().getSimpleName(), context);
    }
    StringTokenizer aTok = new StringTokenizer(value, Constants.PIPE);
    String visionString = aTok.nextToken();
    if (looksLikeAPrerequisite(visionString)) {
        return new ParseResult.Fail("Cannot have only PRExxx subtoken in " + getTokenName() + ": " + value, context);
    }
    ArrayList<AssociatedPrereqObject> edgeList = new ArrayList<>();
    boolean foundClear = false;
    while (true) {
        if (Constants.LST_DOT_CLEAR.equals(visionString)) {
            context.getListContext().removeAllFromList(getTokenName(), obj, Vision.VISIONLIST);
            foundClear = true;
        } else if (visionString.startsWith(Constants.LST_DOT_CLEAR_DOT)) {
            try {
                Vision vis = Vision.getVision(visionString.substring(7));
                context.getListContext().removeFromList(getTokenName(), obj, Vision.VISIONLIST, new CDOMDirectSingleRef<>(vis));
            } catch (IllegalArgumentException e) {
                ComplexParseResult cpr = new ComplexParseResult();
                cpr.addErrorMessage("Bad Syntax for Cleared Vision in " + getTokenName());
                cpr.addErrorMessage(e.getMessage());
                return cpr;
            }
            foundClear = true;
        } else if (looksLikeAPrerequisite(visionString)) {
            break;
        } else {
            try {
                Vision vision = Vision.getVision(visionString);
                AssociatedPrereqObject edge = context.getListContext().addToList(getTokenName(), obj, Vision.VISIONLIST, new CDOMDirectSingleRef<>(vision));
                edgeList.add(edge);
            } catch (IllegalArgumentException e) {
                ComplexParseResult cpr = new ComplexParseResult();
                cpr.addErrorMessage("Bad Syntax for Vision in " + getTokenName());
                cpr.addErrorMessage(e.getMessage());
                return cpr;
            }
        }
        if (!aTok.hasMoreTokens()) {
            return ParseResult.SUCCESS;
        }
        visionString = aTok.nextToken();
    }
    if (foundClear) {
        return new ParseResult.Fail("Cannot use PREREQs when using .CLEAR or .CLEAR. in " + getTokenName(), context);
    }
    while (true) {
        Prerequisite prereq = getPrerequisite(visionString);
        if (prereq == null) {
            return new ParseResult.Fail("   (Did you put vision after the " + "PRExxx tags in " + getTokenName() + ":?)", context);
        }
        for (AssociatedPrereqObject edge : edgeList) {
            edge.addPrerequisite(prereq);
        }
        if (!aTok.hasMoreTokens()) {
            break;
        }
        visionString = aTok.nextToken();
    }
    return ParseResult.SUCCESS;
}
Also used : CDOMDirectSingleRef(pcgen.cdom.reference.CDOMDirectSingleRef) ArrayList(java.util.ArrayList) ComplexParseResult(pcgen.rules.persistence.token.ComplexParseResult) Ungranted(pcgen.cdom.base.Ungranted) StringTokenizer(java.util.StringTokenizer) Vision(pcgen.core.Vision) AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 20 with AssociatedPrereqObject

use of pcgen.cdom.base.AssociatedPrereqObject in project pcgen by PCGen.

the class DomainsToken method unparse.

@Override
public String[] unparse(LoadContext context, Spell spell) {
    DoubleKeyMapToList<Prerequisite, Integer, CDOMReference<DomainSpellList>> dkmtl = new DoubleKeyMapToList<>();
    List<String> list = new ArrayList<>();
    Changes<CDOMReference<DomainSpellList>> masterChanges = context.getListContext().getMasterListChanges(getTokenName(), spell, SPELLLIST_CLASS);
    if (masterChanges.includesGlobalClear()) {
        list.add(Constants.LST_DOT_CLEAR_ALL);
    }
    if (masterChanges.hasRemovedItems()) {
        for (CDOMReference<DomainSpellList> swl : masterChanges.getRemoved()) {
            AssociatedChanges<Spell> changes = context.getListContext().getChangesInMasterList(getTokenName(), spell, swl);
            MapToList<Spell, AssociatedPrereqObject> map = changes.getRemovedAssociations();
            if (map != null && !map.isEmpty()) {
                for (Spell added : map.getKeySet()) {
                    if (!spell.getLSTformat().equals(added.getLSTformat())) {
                        context.addWriteMessage("Spell " + getTokenName() + " token cannot remove another Spell " + "(must only remove itself)");
                        return null;
                    }
                    for (AssociatedPrereqObject assoc : map.getListFor(added)) {
                        List<Prerequisite> prereqs = assoc.getPrerequisiteList();
                        if (prereqs != null && !prereqs.isEmpty()) {
                            context.addWriteMessage("Incoming Remove " + "Edge to " + spell.getKeyName() + " had a " + "Prerequisite: " + prereqs.size());
                            return null;
                        }
                        dkmtl.addToListFor(null, -1, swl);
                    }
                }
            }
        }
    }
    for (CDOMReference<DomainSpellList> swl : masterChanges.getAdded()) {
        AssociatedChanges<Spell> changes = context.getListContext().getChangesInMasterList(getTokenName(), spell, swl);
        Collection<Spell> removedItems = changes.getRemoved();
        if (removedItems != null && !removedItems.isEmpty() || changes.includesGlobalClear()) {
            context.addWriteMessage(getTokenName() + " does not support .CLEAR.");
            return null;
        }
        MapToList<Spell, AssociatedPrereqObject> map = changes.getAddedAssociations();
        if (map != null && !map.isEmpty()) {
            for (Spell added : map.getKeySet()) {
                if (!spell.getLSTformat().equals(added.getLSTformat())) {
                    context.addWriteMessage("Spell " + getTokenName() + " token cannot allow another Spell " + "(must only allow itself)");
                    return null;
                }
                for (AssociatedPrereqObject assoc : map.getListFor(added)) {
                    List<Prerequisite> prereqs = assoc.getPrerequisiteList();
                    Prerequisite prereq;
                    if (prereqs == null || prereqs.isEmpty()) {
                        prereq = null;
                    } else if (prereqs.size() == 1) {
                        prereq = prereqs.get(0);
                    } else {
                        context.addWriteMessage("Incoming Edge to " + spell.getKeyName() + " had more than one " + "Prerequisite: " + prereqs.size());
                        return null;
                    }
                    Integer level = assoc.getAssociation(AssociationKey.SPELL_LEVEL);
                    if (level == null) {
                        context.addWriteMessage("Incoming Allows Edge to " + spell.getKeyName() + " had no Spell Level defined");
                        return null;
                    }
                    if (level.intValue() < 0) {
                        context.addWriteMessage("Incoming Allows Edge to " + spell.getKeyName() + " had invalid Level: " + level + ". Must be >= 0.");
                        return null;
                    }
                    dkmtl.addToListFor(prereq, level, swl);
                }
            }
        }
    }
    if (dkmtl.isEmpty()) {
        if (list.isEmpty()) {
            // Legal if no DOMAINS was present in the Spell
            return null;
        } else {
            return list.toArray(new String[list.size()]);
        }
    }
    PrerequisiteWriter prereqWriter = new PrerequisiteWriter();
    SortedSet<CDOMReference<DomainSpellList>> set = new TreeSet<>(ReferenceUtilities.REFERENCE_SORTER);
    SortedSet<Integer> levelSet = new TreeSet<>();
    for (Prerequisite prereq : dkmtl.getKeySet()) {
        StringBuilder sb = new StringBuilder();
        boolean needPipe = false;
        levelSet.clear();
        levelSet.addAll(dkmtl.getSecondaryKeySet(prereq));
        for (Integer i : levelSet) {
            set.clear();
            set.addAll(dkmtl.getListFor(prereq, i));
            if (needPipe) {
                sb.append(Constants.PIPE);
            }
            sb.append(ReferenceUtilities.joinLstFormat(set, Constants.COMMA));
            sb.append('=').append(i);
            needPipe = true;
        }
        if (prereq != null) {
            sb.append('[');
            StringWriter swriter = new StringWriter();
            try {
                prereqWriter.write(swriter, prereq);
            } catch (PersistenceLayerException e) {
                context.addWriteMessage("Error writing Prerequisite: " + e);
                return null;
            }
            sb.append(swriter.toString());
            sb.append(']');
        }
        list.add(sb.toString());
    }
    return list.toArray(new String[list.size()]);
}
Also used : PrerequisiteWriter(pcgen.persistence.lst.output.prereq.PrerequisiteWriter) ArrayList(java.util.ArrayList) Spell(pcgen.core.spell.Spell) StringWriter(java.io.StringWriter) TreeSet(java.util.TreeSet) AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject) DoubleKeyMapToList(pcgen.base.util.DoubleKeyMapToList) DomainSpellList(pcgen.cdom.list.DomainSpellList) PersistenceLayerException(pcgen.persistence.PersistenceLayerException) CDOMReference(pcgen.cdom.base.CDOMReference) Prerequisite(pcgen.core.prereq.Prerequisite)

Aggregations

AssociatedPrereqObject (pcgen.cdom.base.AssociatedPrereqObject)50 CDOMReference (pcgen.cdom.base.CDOMReference)34 ArrayList (java.util.ArrayList)18 Prerequisite (pcgen.core.prereq.Prerequisite)18 Spell (pcgen.core.spell.Spell)14 StringTokenizer (java.util.StringTokenizer)11 TreeSet (java.util.TreeSet)10 Domain (pcgen.core.Domain)8 CDOMObject (pcgen.cdom.base.CDOMObject)7 ClassSpellList (pcgen.cdom.list.ClassSpellList)7 CDOMList (pcgen.cdom.base.CDOMList)6 PersistenceLayerException (pcgen.persistence.PersistenceLayerException)6 ParseResult (pcgen.rules.persistence.token.ParseResult)6 HashSet (java.util.HashSet)5 MasterListInterface (pcgen.cdom.base.MasterListInterface)5 AssociationKey (pcgen.cdom.enumeration.AssociationKey)5 ClassSkillList (pcgen.cdom.list.ClassSkillList)5 DomainSpellList (pcgen.cdom.list.DomainSpellList)5 StringWriter (java.io.StringWriter)4 PCClass (pcgen.core.PCClass)4