- Timestamp:
- 01/28/20 10:19:55 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/geniusweb/profilesserver/websocket/GetProfileSocket.java
r1 r8 2 2 3 3 import java.io.IOException; 4 import java.math.BigInteger; 5 import java.util.Arrays; 6 import java.util.Collections; 7 import java.util.HashMap; 8 import java.util.HashSet; 9 import java.util.List; 10 import java.util.Map; 11 import java.util.Random; 12 import java.util.Set; 13 import java.util.stream.Collectors; 4 14 5 15 import javax.websocket.OnClose; … … 10 20 import javax.websocket.server.ServerEndpoint; 11 21 12 import com.fasterxml.jackson.databind.ObjectMapper; 13 22 import geniusweb.bidspace.AllBidsList; 23 import geniusweb.bidspace.BidsWithUtility; 24 import geniusweb.issuevalue.Bid; 25 import geniusweb.profile.DefaultPartialOrdering; 26 import geniusweb.profile.PartialOrdering; 14 27 import geniusweb.profile.Profile; 28 import geniusweb.profile.utilityspace.LinearAdditive; 29 import geniusweb.profilesserver.Jackson; 15 30 import geniusweb.profilesserver.ProfilesFactory; 16 31 import geniusweb.profilesserver.events.ChangeEvent; 32 import tudelft.utilities.immutablelist.FixedList; 33 import tudelft.utilities.immutablelist.ImmutableList; 17 34 import tudelft.utilities.listener.Listener; 18 35 … … 22 39 * profiles is sent. For each new websocket the server will create one of this 23 40 * but they all share one {@link ProfilesFactory}. 41 * 42 * <p> 43 * Query string: the websocket allows query strings. 44 * <ul> 45 * <li>partial</li> a query key that is assigned a natural number N (0 or 46 * larger). If set, the profile is converted into a Partial Profile with the 47 * given number of points. If N>0, the maximum utility bid is included .If N>1, 48 * the minimum utility bid is included. The other bids are picked at random. 49 * This filtering mechanism only works if the profile is {@link LinearAdditive} 50 * space. 24 51 */ 25 52 @ServerEndpoint("/websocket/get/{domain}/{profile}") 26 53 public class GetProfileSocket { 27 private final static ObjectMapper jackson = new ObjectMapper();28 54 private Profile prof = null; // the latest that we sent to client. 29 55 … … 33 59 private Listener<ChangeEvent> changeListener; 34 60 private Session session; 61 private Map<String, String> params = Collections.emptyMap(); 35 62 36 63 @OnOpen … … 40 67 this.profilename = domain + "/" + profile; 41 68 69 if (session.getQueryString() != null) { 70 List<String> paramstrings = Arrays 71 .asList(session.getQueryString().split("&")); 72 params = paramstrings.stream().map(str -> str.split("=")).collect( 73 Collectors.toMap(vals -> vals[0], vals -> vals[1])); 74 } 42 75 changeListener = new Listener<ChangeEvent>() { 43 76 @Override … … 56 89 prof = newprof; 57 90 try { 58 session.getBasicRemote() 59 .sendText(jackson.writeValueAsString(prof));91 session.getBasicRemote().sendText( 92 Jackson.instance().writeValueAsString(filter(prof))); 60 93 } catch (Exception e) { 61 94 e.printStackTrace(); 62 95 } 63 96 } 97 } 98 99 private Profile filter(Profile prof1) { 100 String partial = params.get("partial"); 101 if (partial == null) { 102 return prof1; 103 } 104 if (!(prof1 instanceof PartialOrdering)) 105 throw new IllegalArgumentException( 106 "profile must be partialordering but got " + prof); 107 PartialOrdering profile = (PartialOrdering) prof1; 108 final int numbids = Integer.parseInt(partial); 109 if (numbids < 0) 110 throw new IllegalArgumentException("parameter partial must be >=0"); 111 112 ImmutableList<Bid> allbids; 113 if (profile instanceof DefaultPartialOrdering) 114 allbids = new FixedList<Bid>( 115 ((DefaultPartialOrdering) profile).getBids()); 116 else 117 allbids = new AllBidsList(prof.getDomain()); 118 119 if (BigInteger.valueOf(numbids).compareTo(allbids.size()) > 0) 120 throw new IllegalArgumentException("Request for " + numbids 121 + " exceeds number of bids in the space " + allbids.size()); 122 123 BidsWithUtility info = new BidsWithUtility((LinearAdditive) prof); 124 Set<Bid> selected = new HashSet<>(); 125 if (numbids > 0) { 126 selected.add(info.getExtremeBid(true)); 127 } 128 if (numbids > 1) { 129 selected.add(info.getExtremeBid(false)); 130 } 131 Random random = new Random(); 132 for (int nr = 2; nr < numbids; nr++) { 133 int attempt = 0; 134 Bid bid; 135 do { 136 long i = random.nextInt(allbids.size().intValue()); 137 bid = allbids.get(BigInteger.valueOf(i)); 138 } while (attempt++ < 10 && selected.contains(bid)); 139 selected.add(bid); 140 } 141 142 // put comparison info for these bids in the map 143 Map<Bid, Set<Bid>> isBetterMap = new HashMap<>(); 144 for (Bid bid : selected) { 145 Set<Bid> worse = selected.stream().filter( 146 otherbid -> profile.isPreferredOrEqual(bid, otherbid) 147 && !profile.isPreferredOrEqual(otherbid, bid)) 148 .collect(Collectors.toSet()); 149 isBetterMap.put(bid, worse); 150 } 151 return new DefaultPartialOrdering( 152 prof.getName() + "-partial-" + numbids, prof.getDomain(), 153 prof.getReservationBid(), isBetterMap); 64 154 } 65 155
Note:
See TracChangeset
for help on using the changeset viewer.