package geniusweb.profile; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; import org.eclipse.jdt.annotation.NonNull; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.KeyDeserializer; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonSerialize; import geniusweb.issuevalue.Bid; import geniusweb.issuevalue.Domain; /** * Strange that we need to do this. Bid is completely standard. The complication * that this solves is that the keys are places as STRING in the json code * because json allows only strings as key. */ @SuppressWarnings("serial") class BidDeserializer extends KeyDeserializer { private ObjectMapper jackson = new ObjectMapper(); @Override public Bid deserializeKey(String key, DeserializationContext ctxt) throws IOException { return jackson.readValue(key, Bid.class); } } /** * Serializes a Bid to string. Unfortunately by default jackson uses * key.toString() for serializing (rather than * {@link ObjectMapper#writeValueAsString(Object)}). * */ class BidSerializer extends JsonSerializer { private ObjectMapper jackson = new ObjectMapper(); @Override public void serialize(Bid bid, JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeFieldName(jackson.writeValueAsString(bid)); } } /** * Default implementation of partial ordering that stores all is-better * relations explicitly in a map. * * NOTICE this can handle profiles of max size Integer. This is because various * functions used here rely on basic java functions that can handle only int. * Besides, the size of partial maps grows very rapidly so this approach is * limited anyway. */ @JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE, isGetterVisibility = Visibility.NONE) public class DefaultPartialOrdering extends DefaultProfile implements PartialOrdering { /** * Set is sparsely filled. If a Bid is not a key, it is not better than any * other bid. */ //#PY # not needed in pyson, has fallback for key (de)serialization @JsonDeserialize(keyUsing = BidDeserializer.class) //#PY # not needed in pyson, has fallback for key (de)serialization @JsonSerialize(keyUsing = BidSerializer.class) private final Map> better; /** * @param name the name for the profile * @param domain the {@link Domain} description * @param reservationBid the reservation {@link Bid} possibly null * @param better a map with keys = a better bid and value=a less * good bid. */ @JsonCreator public DefaultPartialOrdering( final @NonNull @JsonProperty("name") String name, final @NonNull @JsonProperty("domain") Domain domain, final @JsonProperty("reservationBid") Bid reservationBid, final @NonNull @JsonProperty("better") Map> better) { super(name, domain, reservationBid); this.better = better; } @Override public boolean isPreferredOrEqual(final @NonNull Bid bid1, final @NonNull Bid bid2) { if (!better.containsKey(bid1)) return false; return better.get(bid1).contains(bid2); } /** * * @return a list with all the bids that are referred to, either as better * or as worse than another bid */ public @NonNull List getBids() { // FIXME the iteration order may not be guaranteed! Set bids = new HashSet<>(); for (Bid bid : better.keySet()) { bids.add(bid); bids.addAll(better.get(bid)); } return new ArrayList(bids); } /** * * @return a list of tuples [bid1index, bid2index]. It indicates that * bids[bid1index] isbetterthan bids[bid2index]. */ public List> getBetter() { List> betterlist = new LinkedList<>(); List bidslist = getBids(); for (Bid bid : bidslist) { if (better.containsKey(bid)) { for (Bid worsebid : better.get(bid)) { betterlist.add(Arrays.asList(bidslist.indexOf(bid), bidslist.indexOf(worsebid))); } } } return betterlist; } @Override public String toString() { return "DefaultPartialOrdering[" + getValuesString() + "," + better + "]"; } @Override public int hashCode() { final int prime = 31; int result = super.hashCode(); result = prime * result + ((better == null) ? 0 : better.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (!super.equals(obj)) return false; if (getClass() != obj.getClass()) return false; DefaultPartialOrdering other = (DefaultPartialOrdering) obj; if (better == null) { if (other.better != null) return false; } else if (!better.equals(other.better)) return false; return true; } }