Exemple #1
0
 /** Gets a copy of this variable. */
 Term copy(AbstractMap vMap, AbstractMap substMap) {
   Var v;
   Object temp = vMap.get(this);
   if (temp == null) {
     v =
         new Var(
             null,
             Var.PROGRESSIVE,
             vMap.size(),
             timestamp); // name,Var.PROGRESSIVE,vMap.size(),timestamp);
     vMap.put(this, v);
   } else {
     v = (Var) temp;
   }
   Term t = getTerm();
   if (t instanceof Var) {
     Object tt = substMap.get(t);
     if (tt == null) {
       substMap.put(t, v);
       v.link = null;
     } else {
       v.link = (tt != v) ? (Var) tt : null;
     }
   }
   if (t instanceof Struct) {
     v.link = t.copy(vMap, substMap);
   }
   if (t instanceof Number) v.link = t;
   return v;
 }
Exemple #2
0
  /**
   * Back-end to pick using BFS.
   *
   * @param limit maximum value of combinations
   * @param items object value table
   * @return solution
   */
  public static <T> Pack<T> breadthFirstSearch(Long limit, AbstractMap<T, Long> items) {
    if (Pack.needSearch(limit, items)) {
      return new Pack<T>(limit, new ArrayList<T>(items.keySet()));
    }

    ArrayList<Pack<T>> table = new ArrayList<Pack<T>>();
    table.add(new Pack<T>());

    for (Entry<T, Long> e : items.entrySet()) {
      ArrayList<Pack<T>> tmp = new ArrayList<Pack<T>>();
      for (Pack<T> p : table) {
        Long newSize = p.getScore() + e.getValue();
        if (newSize <= limit) {
          ArrayList<T> newDirs = new ArrayList<T>(p.getItems());
          newDirs.add(e.getKey());
          tmp.add(new Pack<T>(newSize, newDirs));
        }
      }
      table.addAll(table.size(), tmp);
    }

    Pack<T> max = new Pack<T>();
    for (Pack<T> p : table) {
      if (p.getScore() >= max.getScore()) {
        max = p;
      }
    }
    return max;
  }
Exemple #3
0
 @SuppressWarnings("unchecked")
 @Override
 protected Object clone() throws CloneNotSupportedException {
   AbstractMap<K, V> result = (AbstractMap<K, V>) super.clone();
   result.keySet = null;
   result.valuesCollection = null;
   return result;
 }
 @Override
 public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) {
   @SuppressWarnings("unchecked")
   AbstractMap<String, String> map = (AbstractMap<String, String>) value;
   for (Entry<String, String> entry : map.entrySet()) {
     writer.startNode(entry.getKey().toString());
     writer.setValue(entry.getValue().toString());
     writer.endNode();
   }
 }
Exemple #5
0
 /**
  * Gets a copy of this variable.
  *
  * <p>if the variable is not present in the list passed as argument, a copy of this variable is
  * returned and added to the list. If instead a variable with the same time identifier is found in
  * the list, then the variable in the list is returned.
  */
 Term copy(AbstractMap vMap, int idExecCtx) {
   Term tt = getTerm();
   if (tt == this) {
     Var v = (Var) (vMap.get(this));
     if (v == null) {
       // No occurence of v before
       v = new Var(name, idExecCtx, 0, timestamp);
       vMap.put(this, v);
     }
     return v;
   } else {
     return tt.copy(vMap, idExecCtx);
   }
 }
Exemple #6
0
 /**
  * Main pick function. If table size is greater then or equal to 16, it will use heuristic
  * algorithm.
  *
  * @param limit maximum value of combinations
  * @param items object value table
  * @return solution
  */
 public static <T> Pack<T> pick(Long limit, AbstractMap<T, Long> items) {
   if (items.size() < 26) {
     return Pack.binarySearch(limit, items);
   } else {
     return Pack.geneticAlgorithm(limit, items);
   }
 }
Exemple #7
0
 private static <T> Boolean needSearch(Long limit, AbstractMap<T, Long> items) {
   Long sum = 0L;
   for (Long v : items.values()) {
     sum += v;
   }
   return sum > limit;
 }
Exemple #8
0
  private void joinChat() {
    String userColor;

    sessionService.addOnSessionDestroyedListener(callback);

    defaultSessionTimeout = httpSession.getMaxInactiveInterval();
    httpSession.setMaxInactiveInterval(0);
    lastActivityTime = System.currentTimeMillis();

    String username = ((User) authToken.getPrincipal()).getUsername();
    LOG.debug("joinChat() user: "******"USER", username);

    int userNb = usersLoggedIn.incrementAndGet();
    // If a user is active more than once, give him the same color:
    if (userColorMap.containsKey(username)) {
      userColor = userColorMap.get(username);
    } else {
      userColor = PEER_COLORS[userNb % PEER_COLOR_NB];
      userColorMap.put(username, userColor);
    }

    thisSession.getUserProperties().put("COLOR", userColor);

    Message joinMsg = new Message();
    joinMsg.TYPE = "JOIN";
    joinMsg.SUBTYPE = "JOIN";
    joinMsg.USER_LIST = buildUserList(true);
    joinMsg.STATS_MSG = userNb + " User" + (userNb > 1 ? "s " : " ") + "online!";

    sendMessage(joinMsg);

    Message infoMsg = new Message();
    infoMsg.TYPE = "INFO";
    infoMsg.SUBTYPE = "JOIN";
    infoMsg.INFO_MSG = username + " has entered the building";
    infoMsg.STATS_MSG = userNb + " User" + (userNb > 1 ? "s " : " ") + "online!";
    infoMsg.USER_LIST = buildUserList(true);

    broadcastMessage(infoMsg, false);
  }
Exemple #9
0
 public static AbstractMap<String, String> parser(Object params) throws JSONException {
   AbstractMap<String, String> localParams = new HashMap<>();
   if (params != "") {
     JSONObject json = new JSONObject(params.toString());
     Iterator<String> iter = json.keys();
     while (iter.hasNext()) {
       String key = iter.next();
       try {
         Object value = json.get(key);
         localParams.put(key, value.toString());
       } catch (JSONException e) {
         exceptionMessages.put(
             "parser",
             "There has been a problem parsing your custom values (params, request headers or tests).");
       }
     }
   }
   return localParams;
 }
Exemple #10
0
    public GeneticAlgorithm(Long limit, AbstractMap<T, Long> items) {
      this.limit_ = limit;
      this.table_ = items;

      this.population_ = new ArrayList<Cell<T>>();
      for (int i = 0; i < items.size(); ++i) {
        this.population_.add(this.generatePopulation());
      }
      Collections.sort(this.population_);
    }
 /**
  * Pick the best node from which to stream the data. Entries in <i>nodes</i> are already in the
  * priority order
  */
 static DatanodeInfo bestNode(
     DatanodeInfo nodes[], AbstractMap<DatanodeInfo, DatanodeInfo> deadNodes) throws IOException {
   if (nodes != null) {
     for (int i = 0; i < nodes.length; i++) {
       if (!deadNodes.containsKey(nodes[i])) {
         return nodes[i];
       }
     }
   }
   throw new IOException("No live nodes contain current block");
 }
Exemple #12
0
 public BinarySearch(Long limit, AbstractMap<T, Long> items) {
   this.limit_ = limit;
   this.items_ = items;
   this.map_ = new ArrayList<T>(items.keySet());
   Collections.sort(
       this.map_,
       new Comparator<T>() {
         @Override
         public int compare(T l, T r) {
           return BinarySearch.this.items_.get(l).compareTo(BinarySearch.this.items_.get(r));
         }
       });
 }
  @Override
  public void sessionDestroyed(HttpSessionEvent sessionEvent) {

    HttpSession session = sessionEvent.getSession();
    String sessionID = session.getId();
    ApplicationContext applicationContext =
        WebApplicationContextUtils.getWebApplicationContext(session.getServletContext());
    AbstractMap<String, Future> futureHolder =
        (AbstractMap<String, Future>) applicationContext.getBean("futureHolder");
    Set<String> keySet = futureHolder.keySet();
    List<String> keysToRemove = new ArrayList<String>();

    for (String key : keySet) {
      if (key.endsWith("@" + sessionID)) {
        keysToRemove.add(key);
      }
    }

    for (String removeMe : keysToRemove) {
      futureHolder.remove(removeMe);
    }
  }
Exemple #14
0
    public DepthFirstSearch(Long limit, AbstractMap<T, Long> items) {
      ArrayList<Entry<T, Long>> tmp = new ArrayList<Entry<T, Long>>(items.entrySet());
      Collections.sort(
          tmp,
          new Comparator<Entry<T, Long>>() {
            @Override
            public int compare(Entry<T, Long> r, Entry<T, Long> l) {
              return l.getValue().compareTo(r.getValue());
            }
          });

      this.keys_ = new ArrayList<T>();
      this.values_ = new ArrayList<Long>();
      for (Entry<T, Long> e : tmp) {
        this.keys_.add(e.getKey());
        this.values_.add(e.getValue());
      }

      this.limit_ = limit;
    }
 private void putAllImpl(Map<? extends K, ? extends V> map) {
   if (map.entrySet() != null) {
     super.putAll(map);
   }
 }
Exemple #16
0
 public static <T> Pack<T> binarySearch(Long limit, AbstractMap<T, Long> items) {
   return (Pack.needSearch(limit, items))
       ? new BinarySearch<T>(limit, items).call()
       : new Pack<T>(limit, new ArrayList<T>(items.keySet()));
 }
Exemple #17
0
 /**
  * Back-end to pick using DFS.
  *
  * @param limit maximum value of combinations
  * @param items object value table
  * @return solution
  */
 public static <T> Pack<T> depthFirstSearch(Long limit, AbstractMap<T, Long> items) {
   return (Pack.needSearch(limit, items))
       ? new DepthFirstSearch<T>(limit, items).call(0, new Pack<T>())
       : new Pack<T>(limit, new ArrayList<T>(items.keySet()));
 }
Exemple #18
0
 /**
  * Back-end to pick using heuristic algorithm. The complexity is O(2^n).
  *
  * @param limit maximum value of combinations
  * @param items object value table
  * @return solution
  */
 public static <T> Pack<T> geneticAlgorithm(Long limit, AbstractMap<T, Long> items) {
   return (Pack.needSearch(limit, items))
       ? new GeneticAlgorithm<T>(limit, items).call()
       : new Pack<T>(limit, new ArrayList<T>(items.keySet()));
 }
 /**
  * Returns a shallow copy of this <tt>AbstractMap</tt> instance: the keys and values themselves
  * are not cloned.
  *
  * @return a shallow copy of this map.
  */
 protected Object clone() throws CloneNotSupportedException {
   AbstractMap result = (AbstractMap) super.clone();
   result.keySet = null;
   result.values = null;
   return result;
 }
 /**
  * Replaces all unquantified variables with the unique copy stored as a value of the given map;
  * also instantiates all quantified variables and stores them in the given map.
  *
  * @param m The map in which to find unique copies of the variables.
  */
 public void consolidateVariables(java.util.AbstractMap m) {
   variableMap = m;
   if (m.containsKey(left)) left = (FirstOrderVariable) m.get(left);
   else m.put(left, left);
 }