[519] | 1 | package geniusweb.profile;
|
---|
| 2 |
|
---|
[579] | 3 | import java.io.IOException;
|
---|
[519] | 4 | import java.util.ArrayList;
|
---|
| 5 | import java.util.Arrays;
|
---|
[818] | 6 | import java.util.HashMap;
|
---|
[519] | 7 | import java.util.HashSet;
|
---|
| 8 | import java.util.LinkedList;
|
---|
| 9 | import java.util.List;
|
---|
| 10 | import java.util.Map;
|
---|
| 11 | import java.util.Set;
|
---|
| 12 |
|
---|
[804] | 13 | import org.eclipse.jdt.annotation.NonNull;
|
---|
| 14 |
|
---|
[579] | 15 | import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
---|
| 16 | import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
|
---|
[519] | 17 | import com.fasterxml.jackson.annotation.JsonCreator;
|
---|
| 18 | import com.fasterxml.jackson.annotation.JsonProperty;
|
---|
[579] | 19 | import com.fasterxml.jackson.core.JsonGenerator;
|
---|
| 20 | import com.fasterxml.jackson.databind.DeserializationContext;
|
---|
| 21 | import com.fasterxml.jackson.databind.JsonSerializer;
|
---|
| 22 | import com.fasterxml.jackson.databind.KeyDeserializer;
|
---|
| 23 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
| 24 | import com.fasterxml.jackson.databind.SerializerProvider;
|
---|
| 25 | import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
---|
| 26 | import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
---|
[519] | 27 |
|
---|
| 28 | import geniusweb.issuevalue.Bid;
|
---|
| 29 | import geniusweb.issuevalue.Domain;
|
---|
| 30 |
|
---|
[825] | 31 | @SuppressWarnings("serial")
|
---|
[519] | 32 | /**
|
---|
[579] | 33 | * Strange that we need to do this. Bid is completely standard. The complication
|
---|
| 34 | * that this solves is that the keys are places as STRING in the json code
|
---|
| 35 | * because json allows only strings as key.
|
---|
| 36 | */
|
---|
[818] | 37 | //#PY # key deserializer not needed in python
|
---|
[579] | 38 | class BidDeserializer extends KeyDeserializer {
|
---|
| 39 | private ObjectMapper jackson = new ObjectMapper();
|
---|
| 40 |
|
---|
| 41 | @Override
|
---|
| 42 | public Bid deserializeKey(String key, DeserializationContext ctxt)
|
---|
| 43 | throws IOException {
|
---|
| 44 | return jackson.readValue(key, Bid.class);
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | /**
|
---|
| 50 | * Serializes a Bid to string. Unfortunately by default jackson uses
|
---|
| 51 | * key.toString() for serializing (rather than
|
---|
| 52 | * {@link ObjectMapper#writeValueAsString(Object)}).
|
---|
| 53 | *
|
---|
| 54 | */
|
---|
[818] | 55 | //#PY # Key serializer not needed in python
|
---|
[579] | 56 | class BidSerializer extends JsonSerializer<Bid> {
|
---|
| 57 | private ObjectMapper jackson = new ObjectMapper();
|
---|
| 58 |
|
---|
| 59 | @Override
|
---|
| 60 | public void serialize(Bid bid, JsonGenerator gen,
|
---|
| 61 | SerializerProvider serializers) throws IOException {
|
---|
| 62 | gen.writeFieldName(jackson.writeValueAsString(bid));
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[825] | 67 | @JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE, isGetterVisibility = Visibility.NONE)
|
---|
[579] | 68 | /**
|
---|
[519] | 69 | * Default implementation of partial ordering that stores all is-better
|
---|
| 70 | * relations explicitly in a map.
|
---|
| 71 | *
|
---|
| 72 | * NOTICE this can handle profiles of max size Integer. This is because various
|
---|
| 73 | * functions used here rely on basic java functions that can handle only int.
|
---|
| 74 | * Besides, the size of partial maps grows very rapidly so this approach is
|
---|
| 75 | * limited anyway.
|
---|
| 76 | */
|
---|
| 77 | public class DefaultPartialOrdering extends DefaultProfile
|
---|
| 78 | implements PartialOrdering {
|
---|
| 79 |
|
---|
[825] | 80 | //#PY # not needed in pyson, has fallback for key (de)serialization
|
---|
| 81 | @JsonDeserialize(keyUsing = BidDeserializer.class)
|
---|
| 82 | //#PY # not needed in pyson, has fallback for key (de)serialization
|
---|
| 83 | @JsonSerialize(keyUsing = BidSerializer.class)
|
---|
| 84 | @NonNull
|
---|
[519] | 85 | /**
|
---|
| 86 | * Set is sparsely filled. If a Bid is not a key, it is not better than any
|
---|
| 87 | * other bid.
|
---|
| 88 | */
|
---|
[825] | 89 | private final Map<@NonNull Bid, @NonNull Set<@NonNull Bid>> better = new HashMap<>();
|
---|
[519] | 90 |
|
---|
[825] | 91 | @JsonCreator
|
---|
[519] | 92 | /**
|
---|
[579] | 93 | * @param name the name for the profile
|
---|
| 94 | * @param domain the {@link Domain} description
|
---|
[804] | 95 | * @param reservationBid the reservation {@link Bid} possibly null
|
---|
[579] | 96 | * @param better a map with keys = a better bid and value=a less
|
---|
| 97 | * good bid.
|
---|
[519] | 98 | */
|
---|
[804] | 99 | public DefaultPartialOrdering(
|
---|
| 100 | final @NonNull @JsonProperty("name") String name,
|
---|
| 101 | final @NonNull @JsonProperty("domain") Domain domain,
|
---|
| 102 | final @JsonProperty("reservationBid") Bid reservationBid,
|
---|
[818] | 103 | final @NonNull @JsonProperty("better") Map<@NonNull Bid, @NonNull Set<@NonNull Bid>> better) {
|
---|
[742] | 104 | super(name, domain, reservationBid);
|
---|
[818] | 105 | this.better.putAll(better);
|
---|
[519] | 106 | }
|
---|
| 107 |
|
---|
| 108 | @Override
|
---|
[804] | 109 | public boolean isPreferredOrEqual(final @NonNull Bid bid1,
|
---|
| 110 | final @NonNull Bid bid2) {
|
---|
[579] | 111 | if (!better.containsKey(bid1))
|
---|
[519] | 112 | return false;
|
---|
[579] | 113 | return better.get(bid1).contains(bid2);
|
---|
[519] | 114 | }
|
---|
| 115 |
|
---|
[825] | 116 | @NonNull
|
---|
[519] | 117 | /**
|
---|
| 118 | *
|
---|
| 119 | * @return a list with all the bids that are referred to, either as better
|
---|
| 120 | * or as worse than another bid
|
---|
| 121 | */
|
---|
[825] | 122 | public List<@NonNull Bid> getBids() {
|
---|
[519] | 123 | // FIXME the iteration order may not be guaranteed!
|
---|
[818] | 124 | final @NonNull Set<@NonNull Bid> bids = new HashSet<>();
|
---|
[869] | 125 | for (final @NonNull Bid bid : better.keySet()) {
|
---|
[519] | 126 | bids.add(bid);
|
---|
[579] | 127 | bids.addAll(better.get(bid));
|
---|
[519] | 128 | }
|
---|
| 129 | return new ArrayList<Bid>(bids);
|
---|
| 130 | }
|
---|
| 131 |
|
---|
[825] | 132 | @NonNull
|
---|
[519] | 133 | /**
|
---|
| 134 | *
|
---|
| 135 | * @return a list of tuples [bid1index, bid2index]. It indicates that
|
---|
| 136 | * bids[bid1index] isbetterthan bids[bid2index].
|
---|
| 137 | */
|
---|
[825] | 138 | public List<@NonNull List<@NonNull Integer>> getBetter() {
|
---|
[818] | 139 | final @NonNull List<@NonNull List<@NonNull Integer>> betterlist = new LinkedList<>();
|
---|
| 140 | final @NonNull List<@NonNull Bid> bidslist = getBids();
|
---|
[519] | 141 |
|
---|
[818] | 142 | for (final @NonNull Bid bid : bidslist) {
|
---|
[579] | 143 | if (better.containsKey(bid)) {
|
---|
[818] | 144 | for (final @NonNull Bid worsebid : better.get(bid)) {
|
---|
[519] | 145 | betterlist.add(Arrays.asList(bidslist.indexOf(bid),
|
---|
| 146 | bidslist.indexOf(worsebid)));
|
---|
| 147 | }
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | return betterlist;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | @Override
|
---|
[818] | 155 | public @NonNull String toString() {
|
---|
[579] | 156 | return "DefaultPartialOrdering[" + getValuesString() + "," + better
|
---|
[519] | 157 | + "]";
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | @Override
|
---|
| 161 | public int hashCode() {
|
---|
| 162 | final int prime = 31;
|
---|
| 163 | int result = super.hashCode();
|
---|
[579] | 164 | result = prime * result + ((better == null) ? 0 : better.hashCode());
|
---|
[519] | 165 | return result;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | @Override
|
---|
| 169 | public boolean equals(Object obj) {
|
---|
| 170 | if (this == obj)
|
---|
| 171 | return true;
|
---|
| 172 | if (!super.equals(obj))
|
---|
| 173 | return false;
|
---|
| 174 | if (getClass() != obj.getClass())
|
---|
| 175 | return false;
|
---|
| 176 | DefaultPartialOrdering other = (DefaultPartialOrdering) obj;
|
---|
[579] | 177 | if (better == null) {
|
---|
| 178 | if (other.better != null)
|
---|
[519] | 179 | return false;
|
---|
[579] | 180 | } else if (!better.equals(other.better))
|
---|
[519] | 181 | return false;
|
---|
| 182 | return true;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | }
|
---|