Changeset 8
- Timestamp:
- 01/28/20 10:19:55 (5 years ago)
- Files:
-
- 1 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
pom.xml
r7 r8 52 52 <version>1.1.0</version> 53 53 </dependency> 54 55 <dependency> 56 <groupId>geniusweb</groupId> 57 <artifactId>bidspace</artifactId> 58 <version>1.1.0</version> 59 </dependency> 60 54 61 55 62 <dependency> -
src/main/java/geniusweb/profilesserver/AutoUpdatingProfilesFactory.java
r1 r8 9 9 import java.util.List; 10 10 import java.util.logging.Level; 11 12 import com.fasterxml.jackson.databind.ObjectMapper;13 11 14 12 import geniusweb.issuevalue.Domain; … … 42 40 public class AutoUpdatingProfilesFactory extends DefaultProfilesFactory { 43 41 42 private static final String PROFILES_ROOTDIR = "PROFILES_ROOTDIR"; 44 43 private static final String DOMAINSREPO = "domainsrepo"; 45 44 private static final String JSON = ".json"; … … 47 46 private Path rootdir; 48 47 private final Reporter log; 49 private static final ObjectMapper jackson = new ObjectMapper();50 48 51 49 public AutoUpdatingProfilesFactory(Reporter logger) { … … 217 215 Profile profile = null; 218 216 try { 219 profile = jackson.readValue(profilefile, Profile.class);217 profile = Jackson.instance().readValue(profilefile, Profile.class); 220 218 } catch (IOException e) { 221 219 log.log(Level.WARNING, "File " + profilefile … … 250 248 String expectedName = withoutExtension(domainfile.getName()); 251 249 try { 252 domain = jackson.readValue(domainfile, Domain.class);250 domain = Jackson.instance().readValue(domainfile, Domain.class); 253 251 } catch (IOException e) { 254 252 log.log(Level.WARNING, "File " + domainfile … … 341 339 // Search back upwards to projectroot. 342 340 Path dir; 341 String path = System.getenv(PROFILES_ROOTDIR); 342 if (path != null) { 343 dir = Paths.get(path); 344 if (!dir.toFile().exists()) 345 throw new RuntimeException("Hard set path PROFILES_ROOTDIR='" 346 + PROFILES_ROOTDIR + "' does not exist "); 347 return dir; 348 } 343 349 try { 344 350 dir = Paths.get(getClass().getProtectionDomain().getCodeSource() … … 355 361 dir = dir.getParent(); 356 362 } 357 return Paths.get(DOMAINSREPO); // bit silly, it's not there, but we need 358 // to do something. 363 throw new RuntimeException("Path to " + DOMAINSREPO + " was not found"); 359 364 } 360 365 -
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 -
src/main/java/geniusweb/profilesserver/websocket/ProfilesListSocket.java
r5 r8 24 24 import javax.websocket.server.ServerEndpoint; 25 25 26 import com.fasterxml.jackson.databind.ObjectMapper; 27 26 import geniusweb.profilesserver.Jackson; 28 27 import geniusweb.profilesserver.ProfilesFactory; 29 28 import geniusweb.profilesserver.events.ChangeEvent; … … 83 82 log.log(Level.FINER, "sending updated profiles"); 84 83 session.getBasicRemote().sendText( 85 new ObjectMapper().writeValueAsString(getDomainsProfiles()));84 Jackson.instance().writeValueAsString(getDomainsProfiles())); 86 85 } 87 86 -
src/test/java/geniusweb/profilesserver/AutoUpdatingProfilesFactoryTest.java
r3 r8 44 44 */ 45 45 public class AutoUpdatingProfilesFactoryTest { 46 46 47 private static final String JOBS = "jobs"; 47 48 private static final String JOB1 = "jobs/jobs1"; … … 242 243 } 243 244 245 @Test 246 public void testEnvVariable() throws IOException { 247 Path dir = Files.createTempDirectory("tempdir"); 248 // This test does not work because we can not set the env variable. 249 // new EnvironmentVariables().set("PROFILES_ROOTDIR", "1"); 250 System.setProperty("PROFILES_ROOTDIR", "1");// dir.toAbsolutePath().toString()); 251 System.out.println(System.getenv("PROFILES_ROOTDIR")); 252 } 253 244 254 /** 245 255 * Copy files and all sub-files.
Note:
See TracChangeset
for help on using the changeset viewer.