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