Changeset 23 for protocol/src/main


Ignore:
Timestamp:
09/28/20 09:28:44 (4 years ago)
Author:
bart
Message:

Version 1.5.2

Location:
protocol/src/main/java/geniusweb/protocol
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • protocol/src/main/java/geniusweb/protocol/session/SessionResult.java

    r21 r23  
    22
    33import java.util.Collections;
    4 import java.util.List;
     4import java.util.Map;
    55
    66import com.fasterxml.jackson.annotation.JsonAutoDetect;
     
    1010import com.fasterxml.jackson.annotation.JsonTypeInfo;
    1111
     12import geniusweb.actions.PartyId;
    1213import geniusweb.inform.Agreements;
    1314import geniusweb.references.PartyWithProfile;
     
    1920@JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
    2021public class SessionResult {
    21         private final List<PartyWithProfile> participants;
     22        private final Map<PartyId, PartyWithProfile> participants;
    2223        private final Agreements agreements;
    23         private final List<Double> penalties;
     24        private final Map<PartyId, Double> penalties;
    2425
    2526        // add more type info so that jackson can deserialize the actual class
     
    2930        /**
    3031         *
    31          * @param participants the list oof {@link PartyWithProfile}, in the proper
    32          *                     order (this is relevant for the protocol etc). Should
    33          *                     never be null. Some of them may have entered later of
    34          *                     left early. This list should contain them all.
     32         * @param participants the list oof {@link PartyWithProfile}. Should never
     33         *                     be null. Some of them may have entered later of left
     34         *                     early. This list should contain them all.
    3535         * @param agreements   the final Agreements.
    36          * @param penalties    the penalties in [0,1] for each participant, same
    37          *                     order as the participants list.
     36         * @param penalties    the penalties in [0,1] for each participant.
    3837         * @param error        a fatal error that terminated the session. Non-fatal
    3938         *                     errors (warnings) are not to be reported. Null if no
     
    4645        @JsonCreator
    4746        public SessionResult(
    48                         @JsonProperty("participants") List<PartyWithProfile> participants,
     47                        @JsonProperty("participants") Map<PartyId, PartyWithProfile> participants,
    4948                        @JsonProperty("agreements") Agreements agreements,
    50                         @JsonProperty("penalties") List<Double> penalties,
     49                        @JsonProperty("penalties") Map<PartyId, Double> penalties,
    5150                        @JsonProperty("error") Throwable error) {
    5251                this.participants = participants;
     
    5857        /**
    5958         *
    60          * @return the list oof {@link PartyWithProfile}, in the proper order (this
    61          *         is relevant for the protocol etc). Should never be null. Some of
    62          *         them may have entered later of left early. This list should
    63          *         contain them all.
     59         * @return the map with for each {@link PartyId} the
     60         *         {@link PartyWithProfile}. Should never be null. Some of them may
     61         *         have entered later of left early. This list should contain them
     62         *         all.
    6463         */
    65         public List<PartyWithProfile> getParticipants() {
    66                 return Collections.unmodifiableList(participants);
     64        public Map<PartyId, PartyWithProfile> getParticipants() {
     65                return Collections.unmodifiableMap(participants);
    6766        }
    6867
     
    7776        /**
    7877         *
    79          * @return list of penalties, in same order as {@link #getParticipants()}.
     78         * @return Map of penalties,
    8079         */
    81         public List<Double> getPenalties() {
    82                 return Collections.unmodifiableList(penalties);
     80        public Map<PartyId, Double> getPenalties() {
     81                return Collections.unmodifiableMap(penalties);
    8382        }
    8483
  • protocol/src/main/java/geniusweb/protocol/session/amop/AMOPState.java

    r21 r23  
    255255        @Override
    256256        public SessionResult getResult() {
    257                 List<Double> emptylist = new LinkedList<>();
    258                 for (int n = 0; n < getConnections().size(); n++)
    259                         emptylist.add(0d);
    260                 return new SessionResult(getSettings().getAllParties(), getAgreements(),
    261                                 emptylist, null);
     257                return new SessionResult(partyprofiles, getAgreements(),
     258                                Collections.emptyMap(), null);
    262259        }
    263260
  • protocol/src/main/java/geniusweb/protocol/session/mopac/MOPAC.java

    r22 r23  
    145145                        ProtocolToPartyConnFactory connectionfactory) {
    146146                try {
    147                         //System.out.println("starting MOPAC");
     147                        // System.out.println("starting MOPAC");
    148148                        // we're in Phase.INIT still
    149149                        connect(connectionfactory);
     
    314314                        final ProtocolToPartyConn partyconn, final Action action,
    315315                        long now) {
    316                 //System.out.println("received " + action);
     316                if (finished)
     317                        return;
    317318                state = state.with(partyconn.getParty(), action, now);
    318319                checkEndPhase(System.currentTimeMillis());
     
    352353         */
    353354        private void broadcastNegotiators(Inform info) {
    354                 //System.out.println("broadcasting " + info);
     355                // System.out.println("broadcasting " + info);
    355356                for (PartyId party : state.getPhase().getPartyStates()
    356357                                .getNegotiatingParties()) {
  • protocol/src/main/java/geniusweb/protocol/session/mopac/MOPACState.java

    r22 r23  
    270270        @Override
    271271        public SessionResult getResult() {
    272                 List<Double> emptylist = new LinkedList<>();
    273                 for (int n = 0; n < getConnections().size(); n++)
    274                         emptylist.add(0d);
    275                 return new SessionResult(getSettings().getAllParties(), getAgreements(),
    276                                 emptylist, null);
     272                return new SessionResult(partyprofiles, getAgreements(),
     273                                Collections.emptyMap(), null);
    277274        }
    278275
  • protocol/src/main/java/geniusweb/protocol/session/mopac/phase/DefaultPhase.java

    r21 r23  
    33import java.util.List;
    44import java.util.stream.Collectors;
     5
     6import com.fasterxml.jackson.annotation.JsonIgnore;
    57
    68import geniusweb.actions.Action;
     
    1214
    1315public abstract class DefaultPhase implements Phase {
    14         protected final List<Class<? extends Action>> allowedActions;
    15         protected final Long deadline; // unix timestamp ms since 1970
     16        // deadline for this phase, ms since 1970
     17        protected final Long deadline;
     18        protected final PartyStates partyStates;
     19
     20        // don't serialize this, users don't need it..
     21        @JsonIgnore
     22        protected final VotingEvaluator evaluator;
     23
     24        // don't serialize this, it will cause very large file
     25        @JsonIgnore
    1626        protected final Phase prevPhase;
    17         protected final PartyStates partyStates;
    18         protected final VotingEvaluator evaluator;
    1927
    2028        /**
    2129         *
    22          * @param actions
    23          * @param prevPhases
     30         * @param actions     the actions done in this phase .
     31         * @param prevPhase   the previous phase, can be used to collect previous
     32         *                    votes etc.
    2433         * @param partyStates
    25          * @param allowed
    26          * @param deadline    deadline time ms since 1970
     34         * @param deadline    deadline for this phase, ms since 1970
    2735         */
    28         public DefaultPhase(Phase prevPhase, PartyStates partyStates,
    29                         List<Class<? extends Action>> allowed, Long deadline,
     36        public DefaultPhase(Phase prevPhase, PartyStates partyStates, Long deadline,
    3037                        VotingEvaluator evaluator) {
    31                 this.allowedActions = allowed;
    3238                this.partyStates = partyStates;
    3339                this.deadline = deadline;
     
    8288                if (isFinal(timems))
    8389                        throw new ProtocolException("passed deadline", actor);
    84                 if (!(allowedActions.contains(action.getClass())))
     90                if (!(getAllowedActions().contains(action.getClass())))
    8591                        throw new ProtocolException(
    8692                                        "Action not allowed in " + this.getClass() + ":" + action,
     
    129135                // contains prevstate.....
    130136                return getClass().getSimpleName() + "[" + partyStates + "," + deadline
    131                                 + "," + allowedActions + "," + evaluator + "]";
     137                                + "," + evaluator + "]";
    132138        }
    133139}
  • protocol/src/main/java/geniusweb/protocol/session/mopac/phase/OfferPhase.java

    r21 r23  
    2020        public OfferPhase(Phase prevPhase, PartyStates partyStates, Long deadlinems,
    2121                        VotingEvaluator evaluator) {
    22                 super(prevPhase, partyStates,
    23                                 Arrays.asList(Offer.class, EndNegotiation.class), deadlinems,
    24                                 evaluator);
     22                super(prevPhase, partyStates, deadlinems, evaluator);
    2523        }
    2624
     
    6462        }
    6563
     64        @Override
     65        public List<Class<? extends Action>> getAllowedActions() {
     66                return Arrays.asList(Offer.class, EndNegotiation.class);
     67        }
     68
    6669}
  • protocol/src/main/java/geniusweb/protocol/session/mopac/phase/OptInPhase.java

    r21 r23  
    22
    33import java.util.Arrays;
     4import java.util.List;
    45import java.util.Map;
    56import java.util.stream.Collectors;
     
    2122        protected OptInPhase(Phase prevPhase, PartyStates partyStates,
    2223                        Long deadlinems, VotingEvaluator evaluator) {
    23                 super(prevPhase, partyStates,
    24                                 Arrays.asList(Votes.class, EndNegotiation.class), deadlinems,
    25                                 evaluator);
     24                super(prevPhase, partyStates, deadlinems, evaluator);
    2625        }
    2726
     
    9493        }
    9594
     95        @Override
     96        public List<Class<? extends Action>> getAllowedActions() {
     97                return Arrays.asList(Votes.class, EndNegotiation.class);
     98        }
     99
    96100}
  • protocol/src/main/java/geniusweb/protocol/session/mopac/phase/Phase.java

    r21 r23  
    11package geniusweb.protocol.session.mopac.phase;
    22
     3import java.util.List;
    34import java.util.Set;
     5
     6import com.fasterxml.jackson.annotation.JsonAutoDetect;
     7import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
    48
    59import geniusweb.actions.Action;
     
    1519 *
    1620 */
     21@JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
    1722public interface Phase {
    1823        public static final Long PHASE_MAXTIME = 30000l; // 30sec
     
    99104        public PartyStates getPartyStates();
    100105
     106        /**
     107         * @return the allowed actinos in this phase
     108         */
     109        public abstract List<Class<? extends Action>> getAllowedActions();
     110
    101111}
  • protocol/src/main/java/geniusweb/protocol/session/mopac/phase/VotingPhase.java

    r21 r23  
    22
    33import java.util.Arrays;
     4import java.util.List;
    45
    56import geniusweb.actions.Action;
     
    1718        VotingPhase(Phase prevPhase, PartyStates partyStates, Long deadlinems,
    1819                        VotingEvaluator evaluator) {
    19                 super(prevPhase, partyStates,
    20                                 Arrays.asList(Votes.class, EndNegotiation.class), deadlinems,
    21                                 evaluator);
     20                super(prevPhase, partyStates, deadlinems, evaluator);
    2221        }
    2322
     
    5655        }
    5756
     57        @Override
     58        public List<Class<? extends Action>> getAllowedActions() {
     59                return Arrays.asList(Votes.class, EndNegotiation.class);
     60        }
     61
    5862}
  • protocol/src/main/java/geniusweb/protocol/session/saop/SAOPState.java

    r21 r23  
    266266        @Override
    267267        public SessionResult getResult() {
    268                 List<Double> emptylist = new LinkedList<>();
    269                 for (int n = 0; n < getConnections().size(); n++)
    270                         emptylist.add(0d);
    271                 return new SessionResult(getSettings().getAllParties(), getAgreements(),
    272                                 emptylist, getError());
     268                return new SessionResult(getPartyProfiles(), getAgreements(),
     269                                Collections.emptyMap(), getError());
    273270        }
    274271
  • protocol/src/main/java/geniusweb/protocol/session/shaop/BareSHAOPState.java

    r21 r23  
    11package geniusweb.protocol.session.shaop;
    22
    3 import java.util.Arrays;
    43import java.util.Collections;
     4import java.util.HashMap;
    55import java.util.LinkedList;
    66import java.util.List;
    77import java.util.Map;
     8import java.util.stream.Collectors;
    89
    910import com.fasterxml.jackson.annotation.JsonCreator;
     
    171172        @Override
    172173        public SessionResult getResult() {
    173                 Double[] penalties = new Double[partyNumbers.size()];
     174                Map<PartyId, Double> penalties = new HashMap<>();
    174175                for (PartyId party : partyNumbers.keySet()) {
    175176                        Double spent = totalSpent.get(party);
     
    179180                                spent = Math.max(0, Math.min(1.0, spent));
    180181                        }
    181                         penalties[partyNumbers.get(party)] = spent;
    182                 }
    183                 return new SessionResult(getSettings().getAllParties(), getAgreements(),
    184                                 Arrays.asList(penalties), getError());
     182                        penalties.put(party, spent);
     183                }
     184                Map<PartyId, PartyWithProfile> allparties = partyNumbers.keySet()
     185                                .stream().collect(Collectors.toMap(pid -> pid, pid -> settings
     186                                                .getAllParties().get(partyNumbers.get(pid))));
     187                return new SessionResult(allparties, getAgreements(), penalties,
     188                                getError());
    185189
    186190        }
  • protocol/src/main/java/geniusweb/protocol/tournament/allpermutations/AllPermutationsState.java

    r21 r23  
    5454         */
    5555        public AllPermutationsState with(SessionResult result) {
    56                 if (!getNextSettings().getAllParties()
    57                                 .equals(result.getParticipants())) {
    58                         throw new IllegalArgumentException("Inconsistent session result");
    59                 }
     56                // FIXME do we really need this? Why is this not working?
     57                // if (!new HashSet<PartyWithProfile>(getNextSettings().getAllParties())
     58                // .equals(result.getParticipants().values())) {
     59                // throw new IllegalArgumentException("Inconsistent session result");
     60                // }
    6061                ArrayList<SessionResult> newresults = new ArrayList<>(results);
    6162                newresults.add(result);
Note: See TracChangeset for help on using the changeset viewer.