[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 |
|
---|
| 31 | /**
|
---|
[579] | 32 | * Strange that we need to do this. Bid is completely standard. The complication
|
---|
| 33 | * that this solves is that the keys are places as STRING in the json code
|
---|
| 34 | * because json allows only strings as key.
|
---|
| 35 | */
|
---|
| 36 | @SuppressWarnings("serial")
|
---|
[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 |
|
---|
| 67 | /**
|
---|
[519] | 68 | * Default implementation of partial ordering that stores all is-better
|
---|
| 69 | * relations explicitly in a map.
|
---|
| 70 | *
|
---|
| 71 | * NOTICE this can handle profiles of max size Integer. This is because various
|
---|
| 72 | * functions used here rely on basic java functions that can handle only int.
|
---|
| 73 | * Besides, the size of partial maps grows very rapidly so this approach is
|
---|
| 74 | * limited anyway.
|
---|
| 75 | */
|
---|
[579] | 76 | @JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE, isGetterVisibility = Visibility.NONE)
|
---|
[519] | 77 | public class DefaultPartialOrdering extends DefaultProfile
|
---|
| 78 | implements PartialOrdering {
|
---|
| 79 |
|
---|
| 80 | /**
|
---|
| 81 | * Set is sparsely filled. If a Bid is not a key, it is not better than any
|
---|
| 82 | * other bid.
|
---|
| 83 | */
|
---|
[579] | 84 | //#PY # not needed in pyson, has fallback for key (de)serialization
|
---|
| 85 | @JsonDeserialize(keyUsing = BidDeserializer.class)
|
---|
| 86 | //#PY # not needed in pyson, has fallback for key (de)serialization
|
---|
| 87 | @JsonSerialize(keyUsing = BidSerializer.class)
|
---|
[818] | 88 | private final @NonNull Map<@NonNull Bid, @NonNull Set<@NonNull Bid>> better = new HashMap<>();
|
---|
[519] | 89 |
|
---|
| 90 | /**
|
---|
[579] | 91 | * @param name the name for the profile
|
---|
| 92 | * @param domain the {@link Domain} description
|
---|
[804] | 93 | * @param reservationBid the reservation {@link Bid} possibly null
|
---|
[579] | 94 | * @param better a map with keys = a better bid and value=a less
|
---|
| 95 | * good bid.
|
---|
[519] | 96 | */
|
---|
| 97 | @JsonCreator
|
---|
[804] | 98 | public DefaultPartialOrdering(
|
---|
| 99 | final @NonNull @JsonProperty("name") String name,
|
---|
| 100 | final @NonNull @JsonProperty("domain") Domain domain,
|
---|
| 101 | final @JsonProperty("reservationBid") Bid reservationBid,
|
---|
[818] | 102 | final @NonNull @JsonProperty("better") Map<@NonNull Bid, @NonNull Set<@NonNull Bid>> better) {
|
---|
[742] | 103 | super(name, domain, reservationBid);
|
---|
[818] | 104 | this.better.putAll(better);
|
---|
[519] | 105 | }
|
---|
| 106 |
|
---|
| 107 | @Override
|
---|
[804] | 108 | public boolean isPreferredOrEqual(final @NonNull Bid bid1,
|
---|
| 109 | final @NonNull Bid bid2) {
|
---|
[579] | 110 | if (!better.containsKey(bid1))
|
---|
[519] | 111 | return false;
|
---|
[579] | 112 | return better.get(bid1).contains(bid2);
|
---|
[519] | 113 | }
|
---|
| 114 |
|
---|
| 115 | /**
|
---|
| 116 | *
|
---|
| 117 | * @return a list with all the bids that are referred to, either as better
|
---|
| 118 | * or as worse than another bid
|
---|
| 119 | */
|
---|
[818] | 120 | public @NonNull List<@NonNull Bid> getBids() {
|
---|
[519] | 121 | // FIXME the iteration order may not be guaranteed!
|
---|
[818] | 122 | final @NonNull Set<@NonNull Bid> bids = new HashSet<>();
|
---|
| 123 | for (@NonNull
|
---|
| 124 | Bid bid : better.keySet()) {
|
---|
[519] | 125 | bids.add(bid);
|
---|
[579] | 126 | bids.addAll(better.get(bid));
|
---|
[519] | 127 | }
|
---|
| 128 | return new ArrayList<Bid>(bids);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | /**
|
---|
| 132 | *
|
---|
| 133 | * @return a list of tuples [bid1index, bid2index]. It indicates that
|
---|
| 134 | * bids[bid1index] isbetterthan bids[bid2index].
|
---|
| 135 | */
|
---|
[818] | 136 | public @NonNull List<@NonNull List<@NonNull Integer>> getBetter() {
|
---|
| 137 | final @NonNull List<@NonNull List<@NonNull Integer>> betterlist = new LinkedList<>();
|
---|
| 138 | final @NonNull List<@NonNull Bid> bidslist = getBids();
|
---|
[519] | 139 |
|
---|
[818] | 140 | for (final @NonNull Bid bid : bidslist) {
|
---|
[579] | 141 | if (better.containsKey(bid)) {
|
---|
[818] | 142 | for (final @NonNull Bid worsebid : better.get(bid)) {
|
---|
[519] | 143 | betterlist.add(Arrays.asList(bidslist.indexOf(bid),
|
---|
| 144 | bidslist.indexOf(worsebid)));
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | return betterlist;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | @Override
|
---|
[818] | 153 | public @NonNull String toString() {
|
---|
[579] | 154 | return "DefaultPartialOrdering[" + getValuesString() + "," + better
|
---|
[519] | 155 | + "]";
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | @Override
|
---|
| 159 | public int hashCode() {
|
---|
| 160 | final int prime = 31;
|
---|
| 161 | int result = super.hashCode();
|
---|
[579] | 162 | result = prime * result + ((better == null) ? 0 : better.hashCode());
|
---|
[519] | 163 | return result;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | @Override
|
---|
| 167 | public boolean equals(Object obj) {
|
---|
| 168 | if (this == obj)
|
---|
| 169 | return true;
|
---|
| 170 | if (!super.equals(obj))
|
---|
| 171 | return false;
|
---|
| 172 | if (getClass() != obj.getClass())
|
---|
| 173 | return false;
|
---|
| 174 | DefaultPartialOrdering other = (DefaultPartialOrdering) obj;
|
---|
[579] | 175 | if (better == null) {
|
---|
| 176 | if (other.better != null)
|
---|
[519] | 177 | return false;
|
---|
[579] | 178 | } else if (!better.equals(other.better))
|
---|
[519] | 179 | return false;
|
---|
| 180 | return true;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | }
|
---|