Search in sources :

Example 71 with CDOMReference

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

the class ListChanges method getRemovedAssociations.

@Override
public MapToList<CDOMReference<T>, AssociatedPrereqObject> getRemovedAssociations() {
    MapToList<CDOMReference<T>, AssociatedPrereqObject> owned = new TreeMapToList<>(ReferenceUtilities.REFERENCE_SORTER);
    if (negative == null) {
        return owned;
    }
    Collection<CDOMReference<T>> mods = negative.getListMods(list);
    if (mods == null) {
        return owned;
    }
    for (CDOMReference<T> lw : mods) {
        Collection<AssociatedPrereqObject> assocs = negative.getListAssociations(list, lw);
        for (AssociatedPrereqObject assoc : assocs) {
            if (tokenName.equals(assoc.getAssociation(AssociationKey.TOKEN))) {
                owned.addToListFor(lw, assoc);
            }
        }
    }
    if (owned.isEmpty()) {
        return null;
    }
    return owned;
}
Also used : TreeMapToList(pcgen.base.util.TreeMapToList) CDOMReference(pcgen.cdom.base.CDOMReference) AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject)

Example 72 with CDOMReference

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

the class AbilityLst method unparse.

@Override
public String[] unparse(LoadContext context, CDOMObject obj) {
    Collection<CDOMReference<? extends CDOMList<?>>> changedLists = context.getListContext().getChangedLists(obj, AbilityList.class);
    Changes<ListKey<ChooseSelectionActor<?>>> actors = context.getObjectContext().getListChanges(obj, ListKey.GA_CAKEYS);
    Set<String> returnSet = new TreeSet<>();
    TripleKeyMapToList<Nature, CDOMSingleRef<AbilityCategory>, List<Prerequisite>, CDOMReference<Ability>> m = new TripleKeyMapToList<>();
    TripleKeyMapToList<Nature, CDOMSingleRef<AbilityCategory>, List<Prerequisite>, CDOMReference<Ability>> clear = new TripleKeyMapToList<>();
    Changes<ChooseSelectionActor<?>> listChanges = context.getObjectContext().getListChanges(obj, ListKey.NEW_CHOOSE_ACTOR);
    Collection<ChooseSelectionActor<?>> listAdded = listChanges.getAdded();
    if (listAdded != null && !listAdded.isEmpty()) {
        for (ChooseSelectionActor<?> csa : listAdded) {
            if (csa.getSource().equals(getTokenName())) {
                try {
                    AbilitySelector as = (AbilitySelector) csa;
                    StringBuilder sb = new StringBuilder();
                    sb.append(as.getAbilityCategory().getLSTformat(false)).append(Constants.PIPE);
                    sb.append(as.getNature()).append(Constants.PIPE);
                    sb.append(as.getLstFormat());
                    returnSet.add(sb.toString());
                } catch (PersistenceLayerException e) {
                    context.addWriteMessage(getTokenName() + " encountered error: " + e.getMessage());
                    return null;
                }
            }
        }
    }
    for (CDOMReference ref : changedLists) {
        AssociatedChanges<CDOMReference<Ability>> changes = context.getListContext().getChangesInList(getTokenName(), obj, ref);
        if (changes.includesGlobalClear()) {
            CDOMDirectSingleRef<AbilityList> dr = (CDOMDirectSingleRef<AbilityList>) ref;
            AbilityList al = dr.get();
            StringBuilder sb = new StringBuilder();
            sb.append(al.getCategory().getLSTformat(false)).append(Constants.PIPE);
            sb.append(al.getNature()).append(Constants.PIPE);
            sb.append(Constants.LST_DOT_CLEAR);
            returnSet.add(sb.toString());
        }
        MapToList<CDOMReference<Ability>, AssociatedPrereqObject> mtl = changes.getAddedAssociations();
        if (mtl != null) {
            for (CDOMReference<Ability> ab : mtl.getKeySet()) {
                for (AssociatedPrereqObject assoc : mtl.getListFor(ab)) {
                    Nature nature = assoc.getAssociation(AssociationKey.NATURE);
                    CDOMSingleRef<AbilityCategory> cat = assoc.getAssociation(AssociationKey.CATEGORY);
                    m.addToListFor(nature, cat, assoc.getPrerequisiteList(), ab);
                }
            }
        }
        mtl = changes.getRemovedAssociations();
        if (mtl != null) {
            for (CDOMReference<Ability> ab : mtl.getKeySet()) {
                for (AssociatedPrereqObject assoc : mtl.getListFor(ab)) {
                    Nature nature = assoc.getAssociation(AssociationKey.NATURE);
                    CDOMSingleRef<AbilityCategory> cat = assoc.getAssociation(AssociationKey.CATEGORY);
                    clear.addToListFor(nature, cat, assoc.getPrerequisiteList(), ab);
                }
            }
        }
    }
    for (Nature nature : m.getKeySet()) {
        for (CDOMSingleRef<AbilityCategory> category : m.getSecondaryKeySet(nature)) {
            for (List<Prerequisite> prereqs : m.getTertiaryKeySet(nature, category)) {
                StringBuilder sb = new StringBuilder();
                sb.append(category.getLSTformat(false)).append(Constants.PIPE);
                sb.append(nature);
                List<CDOMReference<Ability>> clearList = clear.removeListFor(nature, category, prereqs);
                if (clearList != null && !clearList.isEmpty()) {
                    sb.append(Constants.PIPE);
                    sb.append(Constants.LST_DOT_CLEAR_DOT);
                    sb.append(ReferenceUtilities.joinLstFormat(clearList, Constants.PIPE + Constants.LST_DOT_CLEAR_DOT));
                }
                sb.append(Constants.PIPE);
                sb.append(ReferenceUtilities.joinLstFormat(m.getListFor(nature, category, prereqs), Constants.PIPE));
                if (prereqs != null && !prereqs.isEmpty()) {
                    sb.append(Constants.PIPE);
                    sb.append(getPrerequisiteString(context, prereqs));
                }
                returnSet.add(sb.toString());
            }
        }
    }
    for (Nature nature : clear.getKeySet()) {
        for (CDOMSingleRef<AbilityCategory> category : clear.getSecondaryKeySet(nature)) {
            for (List<Prerequisite> prereqs : clear.getTertiaryKeySet(nature, category)) {
                StringBuilder sb = new StringBuilder();
                sb.append(category.getLSTformat(false)).append(Constants.PIPE);
                sb.append(nature).append(Constants.PIPE).append(Constants.LST_DOT_CLEAR_DOT);
                sb.append(ReferenceUtilities.joinLstFormat(clear.getListFor(nature, category, prereqs), Constants.PIPE + Constants.LST_DOT_CLEAR_DOT));
                if (prereqs != null && !prereqs.isEmpty()) {
                    sb.append(Constants.PIPE);
                    sb.append(getPrerequisiteString(context, prereqs));
                }
                returnSet.add(sb.toString());
            }
        }
    }
    Collection<ListKey<ChooseSelectionActor<?>>> addedActors = actors.getAdded();
    if (addedActors != null) {
        for (ListKey<ChooseSelectionActor<?>> lk : addedActors) {
            Changes<ChooseSelectionActor<?>> cras = context.getObjectContext().getListChanges(obj, lk);
            for (ChooseSelectionActor<?> cra : cras.getAdded()) {
                if (getTokenName().equals(cra.getSource())) {
                    try {
                        AbilityTargetSelector ats = (AbilityTargetSelector) cra;
                        StringBuilder sb = new StringBuilder();
                        sb.append(ats.getAbilityCategory().getLSTformat(false)).append(Constants.PIPE);
                        sb.append(ats.getNature()).append(Constants.PIPE).append(cra.getLstFormat());
                        List<Prerequisite> prereqs = ats.getPrerequisiteList();
                        if (prereqs != null && !prereqs.isEmpty()) {
                            sb.append(Constants.PIPE);
                            sb.append(getPrerequisiteString(context, prereqs));
                        }
                        returnSet.add(sb.toString());
                    } catch (PersistenceLayerException e) {
                        context.addWriteMessage(getTokenName() + " encountered error: " + e.getMessage());
                        return null;
                    }
                }
            }
        }
    }
    if (returnSet.isEmpty()) {
        return null;
    }
    return returnSet.toArray(new String[returnSet.size()]);
}
Also used : ChooseSelectionActor(pcgen.cdom.base.ChooseSelectionActor) AbilitySelector(pcgen.cdom.helper.AbilitySelector) ListKey(pcgen.cdom.enumeration.ListKey) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) TripleKeyMapToList(pcgen.base.util.TripleKeyMapToList) CDOMList(pcgen.cdom.base.CDOMList) List(java.util.List) MapToList(pcgen.base.util.MapToList) AbilityList(pcgen.cdom.list.AbilityList) AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject) Nature(pcgen.cdom.enumeration.Nature) Ability(pcgen.core.Ability) AbilityTargetSelector(pcgen.cdom.helper.AbilityTargetSelector) CDOMDirectSingleRef(pcgen.cdom.reference.CDOMDirectSingleRef) AbilityList(pcgen.cdom.list.AbilityList) CDOMSingleRef(pcgen.cdom.reference.CDOMSingleRef) PersistenceLayerException(pcgen.persistence.PersistenceLayerException) TripleKeyMapToList(pcgen.base.util.TripleKeyMapToList) CDOMList(pcgen.cdom.base.CDOMList) CDOMReference(pcgen.cdom.base.CDOMReference) AbilityCategory(pcgen.core.AbilityCategory) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 73 with CDOMReference

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

the class AbilityLst 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 tok = new StringTokenizer(value, Constants.PIPE);
    String cat = tok.nextToken();
    CDOMSingleRef<AbilityCategory> acRef = context.getReferenceContext().getCDOMReference(ABILITY_CATEGORY_CLASS, cat);
    if (!tok.hasMoreTokens()) {
        return new ParseResult.Fail(getTokenName() + " must have a Nature, " + "Format is: CATEGORY|NATURE|AbilityName: " + value, context);
    }
    final String natureKey = tok.nextToken();
    Nature nature;
    try {
        nature = Nature.valueOf(natureKey);
    } catch (IllegalArgumentException iae) {
        return new ParseResult.Fail(getTokenName() + " refers to invalid Ability Nature: " + natureKey, context);
    }
    if (Nature.ANY.equals(nature)) {
        return new ParseResult.Fail(getTokenName() + " refers to ANY Ability Nature, cannot be used in " + getTokenName() + ": " + value, context);
    }
    if (!tok.hasMoreTokens()) {
        return new ParseResult.Fail(getTokenName() + " must have abilities, Format is: " + "CATEGORY|NATURE|AbilityName: " + value, context);
    }
    String token = tok.nextToken();
    if (looksLikeAPrerequisite(token)) {
        return new ParseResult.Fail("Cannot have only PRExxx subtoken in " + getTokenName() + ": " + value, context);
    }
    String lkString = "GA_CA_" + cat + '_' + natureKey;
    ListKey glk = ListKey.getKeyFor(ChooseSelectionActor.class, lkString);
    ListKey<ChooseSelectionActor<?>> lk = glk;
    ArrayList<PrereqObject> edgeList = new ArrayList<>();
    CDOMReference<AbilityList> abilList = AbilityList.getAbilityListReference(acRef, nature);
    boolean first = true;
    boolean removed = false;
    ReferenceManufacturer<Ability> rm = context.getReferenceContext().getManufacturer(ABILITY_CLASS, ABILITY_CATEGORY_CLASS, cat);
    if (rm == null) {
        return new ParseResult.Fail("Could not get Reference Manufacturer for Category: " + cat, context);
    }
    boolean prereqsAllowed = true;
    while (true) {
        if (Constants.LST_DOT_CLEAR.equals(token)) {
            if (!first) {
                return new ParseResult.Fail("  Non-sensical " + getTokenName() + ": .CLEAR was not the first list item: " + value, context);
            }
            context.getListContext().removeAllFromList(getTokenName(), obj, abilList);
            context.getObjectContext().removeFromList(obj, ListKey.GA_CAKEYS, lk);
            context.getObjectContext().removeList(obj, lk);
            removed = true;
        } else if (token.startsWith(Constants.LST_DOT_CLEAR_DOT)) {
            String clearText = token.substring(7);
            CDOMReference<Ability> ref = TokenUtilities.getTypeOrPrimitive(rm, clearText);
            if (ref == null) {
                return ParseResult.INTERNAL_ERROR;
            }
            AssociatedPrereqObject assoc = context.getListContext().removeFromList(getTokenName(), obj, abilList, ref);
            assoc.setAssociation(AssociationKey.NATURE, nature);
            assoc.setAssociation(AssociationKey.CATEGORY, acRef);
            removed = true;
        } else if (Constants.LST_PERCENT_LIST.equals(token)) {
            prereqsAllowed = false;
            AbilitySelector as = new AbilitySelector(getTokenName(), acRef, nature);
            context.getObjectContext().addToList(obj, ListKey.NEW_CHOOSE_ACTOR, as);
        } else {
            CDOMReference<Ability> ability = TokenUtilities.getTypeOrPrimitive(rm, token);
            if (ability == null) {
                return ParseResult.INTERNAL_ERROR;
            }
            ability.setRequiresTarget(true);
            boolean loadList = true;
            List<String> choices = null;
            if (token.indexOf('(') != -1) {
                choices = new ArrayList<>();
                AbilityUtilities.getUndecoratedName(token, choices);
                if (choices.size() == 1) {
                    if (Constants.LST_PERCENT_LIST.equals(choices.get(0)) && (ability instanceof CDOMSingleRef)) {
                        CDOMSingleRef<Ability> ref = (CDOMSingleRef<Ability>) ability;
                        AbilityTargetSelector ats = new AbilityTargetSelector(getTokenName(), acRef, ref, nature);
                        context.getObjectContext().addToList(obj, ListKey.GA_CAKEYS, lk);
                        context.getObjectContext().addToList(obj, lk, ats);
                        edgeList.add(ats);
                        loadList = false;
                    }
                }
            }
            if (loadList) {
                AssociatedPrereqObject assoc = context.getListContext().addToList(getTokenName(), obj, abilList, ability);
                assoc.setAssociation(AssociationKey.NATURE, nature);
                assoc.setAssociation(AssociationKey.CATEGORY, acRef);
                if (choices != null) {
                    assoc.setAssociation(AssociationKey.ASSOC_CHOICES, choices);
                }
                edgeList.add(assoc);
            }
        }
        if (!tok.hasMoreTokens()) {
            // No prereqs, so we're done
            return ParseResult.SUCCESS;
        }
        first = false;
        token = tok.nextToken();
        if (looksLikeAPrerequisite(token)) {
            break;
        }
    }
    if (removed || !prereqsAllowed) {
        return new ParseResult.Fail("Cannot use PREREQs when using .CLEAR, .CLEAR., or %LIST in " + getTokenName(), context);
    }
    while (true) {
        Prerequisite prereq = getPrerequisite(token);
        if (prereq == null) {
            return new ParseResult.Fail("   (Did you put feats after the " + "PRExxx tags in " + getTokenName() + ":?)", context);
        }
        for (PrereqObject edge : edgeList) {
            edge.addPrerequisite(prereq);
        }
        if (!tok.hasMoreTokens()) {
            break;
        }
        token = tok.nextToken();
    }
    return ParseResult.SUCCESS;
}
Also used : ChooseSelectionActor(pcgen.cdom.base.ChooseSelectionActor) ArrayList(java.util.ArrayList) Ungranted(pcgen.cdom.base.Ungranted) AbilitySelector(pcgen.cdom.helper.AbilitySelector) ListKey(pcgen.cdom.enumeration.ListKey) AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject) Nature(pcgen.cdom.enumeration.Nature) Ability(pcgen.core.Ability) AbilityTargetSelector(pcgen.cdom.helper.AbilityTargetSelector) ParseResult(pcgen.rules.persistence.token.ParseResult) AbilityList(pcgen.cdom.list.AbilityList) CDOMSingleRef(pcgen.cdom.reference.CDOMSingleRef) PrereqObject(pcgen.cdom.base.PrereqObject) AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject) StringTokenizer(java.util.StringTokenizer) AbilityCategory(pcgen.core.AbilityCategory) CDOMReference(pcgen.cdom.base.CDOMReference) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 74 with CDOMReference

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

the class CcskillLst 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);
    }
    boolean first = true;
    boolean foundAny = false;
    boolean foundOther = false;
    StringTokenizer tok = new StringTokenizer(value, Constants.PIPE);
    while (tok.hasMoreTokens()) {
        String tokText = tok.nextToken();
        if (Constants.LST_DOT_CLEAR.equals(tokText)) {
            if (!first) {
                return new ParseResult.Fail("  Non-sensical " + getTokenName() + ": .CLEAR was not the first list item", context);
            }
            context.getObjectContext().removeList(obj, ListKey.CCSKILL);
        } else if (tokText.startsWith(Constants.LST_DOT_CLEAR_DOT)) {
            String clearText = tokText.substring(7);
            if (Constants.LST_ALL.equals(clearText)) {
                context.getObjectContext().removeFromList(obj, ListKey.CCSKILL, context.getReferenceContext().getCDOMAllReference(SKILL_CLASS));
            } else if (Constants.LST_LIST.equals(clearText)) {
                context.getObjectContext().removeFromList(obj, ListKey.NEW_CHOOSE_ACTOR, this);
            } else {
                CDOMReference<Skill> ref = TokenUtilities.getTypeOrPrimitive(context, SKILL_CLASS, clearText);
                if (ref == null) {
                    return new ParseResult.Fail("  Error was encountered while parsing " + getTokenName(), context);
                }
                context.getObjectContext().removeFromList(obj, ListKey.CCSKILL, ref);
            }
        } else {
            /*
				 * Note this HAS to be done one-by-one, because the
				 * .clearChildNodeOfClass method above does NOT recognize the
				 * C/CC Skill object and therefore doesn't know how to search
				 * the sublists
				 */
            if (Constants.LST_ALL.equals(tokText)) {
                foundAny = true;
                context.getObjectContext().addToList(obj, ListKey.CCSKILL, context.getReferenceContext().getCDOMAllReference(SKILL_CLASS));
            } else {
                foundOther = true;
                if (Constants.LST_LIST.equals(tokText)) {
                    context.getObjectContext().addToList(obj, ListKey.NEW_CHOOSE_ACTOR, this);
                } else {
                    CDOMReference<Skill> ref = getSkillReference(context, tokText);
                    if (ref == null) {
                        return new ParseResult.Fail("  Error was encountered " + "while parsing " + getTokenName(), context);
                    }
                    context.getObjectContext().addToList(obj, ListKey.CCSKILL, ref);
                }
            }
        }
        first = false;
    }
    if (foundAny && foundOther) {
        return new ParseResult.Fail("Non-sensical " + getTokenName() + ": Contains ANY and a specific reference: " + value, context);
    }
    return ParseResult.SUCCESS;
}
Also used : StringTokenizer(java.util.StringTokenizer) Skill(pcgen.core.Skill) ParseResult(pcgen.rules.persistence.token.ParseResult) Ungranted(pcgen.cdom.base.Ungranted) CDOMReference(pcgen.cdom.base.CDOMReference)

Example 75 with CDOMReference

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

the class ChangeprofLst method unparse.

@Override
public String[] unparse(LoadContext context, CDOMObject obj) {
    Changes<ChangeProf> changes = context.getObjectContext().getListChanges(obj, ListKey.CHANGEPROF);
    Collection<ChangeProf> added = changes.getAdded();
    if (added == null || added.isEmpty()) {
        // Zero indicates no Token
        return null;
    }
    HashMapToList<CDOMGroupRef<WeaponProf>, CDOMReference<WeaponProf>> m = new HashMapToList<>();
    for (ChangeProf cp : added) {
        CDOMReference<WeaponProf> source = cp.getSource();
        CDOMGroupRef<WeaponProf> result = cp.getResult();
        m.addToListFor(result, source);
    }
    SortedSet<CDOMReference<WeaponProf>> set = new TreeSet<>(ReferenceUtilities.REFERENCE_SORTER);
    Set<String> returnSet = new TreeSet<>();
    for (CDOMGroupRef<WeaponProf> result : m.getKeySet()) {
        StringBuilder sb = new StringBuilder();
        boolean needComma = false;
        set.clear();
        set.addAll(m.getListFor(result));
        for (CDOMReference<WeaponProf> source : set) {
            if (needComma) {
                sb.append(Constants.COMMA);
            }
            needComma = true;
            String sourceLst = source.getLSTformat(false);
            if (sourceLst.startsWith(Constants.LST_TYPE_EQUAL)) {
                sb.append(Constants.LST_TYPE_DOT);
                sb.append(sourceLst.substring(5));
            } else {
                sb.append(sourceLst);
            }
        }
        sb.append(Constants.EQUALS).append(result.getLSTformat(false).substring(5));
        returnSet.add(sb.toString());
    }
    return new String[] { StringUtil.join(returnSet, Constants.PIPE) };
}
Also used : WeaponProf(pcgen.core.WeaponProf) HashMapToList(pcgen.base.util.HashMapToList) ChangeProf(pcgen.cdom.content.ChangeProf) CDOMGroupRef(pcgen.cdom.reference.CDOMGroupRef) TreeSet(java.util.TreeSet) CDOMReference(pcgen.cdom.base.CDOMReference)

Aggregations

CDOMReference (pcgen.cdom.base.CDOMReference)125 ArrayList (java.util.ArrayList)57 StringTokenizer (java.util.StringTokenizer)40 AssociatedPrereqObject (pcgen.cdom.base.AssociatedPrereqObject)35 ParseResult (pcgen.rules.persistence.token.ParseResult)30 Skill (pcgen.core.Skill)24 ChoiceSet (pcgen.cdom.base.ChoiceSet)22 ConcretePersistentTransitionChoice (pcgen.cdom.base.ConcretePersistentTransitionChoice)21 ReferenceChoiceSet (pcgen.cdom.choiceset.ReferenceChoiceSet)19 Prerequisite (pcgen.core.prereq.Prerequisite)18 Formula (pcgen.base.formula.Formula)16 CDOMObject (pcgen.cdom.base.CDOMObject)15 Spell (pcgen.core.spell.Spell)15 TreeSet (java.util.TreeSet)14 Test (org.junit.Test)14 ClassSkillChoiceActor (pcgen.cdom.helper.ClassSkillChoiceActor)12 CharID (pcgen.cdom.enumeration.CharID)11 ObjectMatchingReference (pcgen.cdom.reference.ObjectMatchingReference)11 ParsingSeparator (pcgen.base.text.ParsingSeparator)10 CDOMList (pcgen.cdom.base.CDOMList)10