/** * * @return a list containing the environment nodes around */ protected List<EnvironmentNode> getEnvironmentNodesSurrounding() { return (List<EnvironmentNode>) env.getNeighborhood(getNode()).getNeighbors().stream() .parallel() .flatMap(n -> n instanceof EnvironmentNode ? Stream.of((EnvironmentNode) n) : Stream.empty()) .collect(Collectors.toList()); }
private Stream<Operation> foundNeighbors(final Node<T> center, final Neighborhood<T> oldNeighborhood, final Neighborhood<T> newNeighborhood) { return newNeighborhood.getNeighbors().stream() .filter(neigh -> oldNeighborhood == null || !oldNeighborhood.contains(neigh)) .filter(neigh -> !getNeighborhood(neigh).contains(center)) .map(n -> new Operation(center, n, true)); }
/** * Execute the action on a random neighbor if the node has a neighborhood. Otherwise do nothing. */ @Override public void execute() { final Neighborhood<T> neighborhood = env.getNeighborhood(node); if (!neighborhood.isEmpty()) { execute(neighborhood.getNeighborByNumber(getRandomGenerator().nextInt(neighborhood.size()))); } }
final Neighborhood<T> oldNeighborhood = neighCache.put(node.getId(), newNeighborhood); if (oldNeighborhood != null) { final Iterator<Node<T>> iter = oldNeighborhood.iterator(); while (iter.hasNext()) { final Node<T> neighbor = iter.next(); if (!newNeighborhood.contains(neighbor)) { neighborsNeighborhood.removeNeighbor(node); Engine.neighborRemoved(this, node, neighbor); if (oldNeighborhood == null || !oldNeighborhood.contains(n)) { neighCache.get(n.getId()).addNeighbor(node); Engine.neighborAdded(this, node, n);
@Override public void execute() { final Neighborhood<Double> neighborhood = env.getNeighborhood(getNode()); final List<Integer> validTargetsIds = new ArrayList<>(); if (delta < 0) { neighborhood.getNeighbors().stream() .filter(n -> n instanceof CellNode && n.getConcentration(mol) >= delta) .mapToInt(n -> n.getId()) .forEach(i -> validTargetsIds.add(i)); } else { neighborhood.getNeighbors().stream() .filter(n -> n instanceof CellNode && n.getConcentration(mol) >= delta) .mapToInt(n -> n.getId()) .forEach(i -> validTargetsIds.add(i)); } if (!validTargetsIds.isEmpty()) { final int targetId = validTargetsIds.get(getRandomGenerator().nextInt(validTargetsIds.size())); execute(neighborhood.getNeighborByNumber(targetId)); } }
private Stream<Operation> lostNeighbors(final Node<T> center, final Neighborhood<T> oldNeighborhood, final Neighborhood<T> newNeighborhood) { return Optional.ofNullable(oldNeighborhood) .map(Neighborhood::getNeighbors) .orElse(Collections.emptyList()) .stream() .filter(neigh -> !newNeighborhood.contains(neigh)) .filter(neigh -> getNeighborhood(neigh).contains(center)) .map(n -> new Operation(center, n, false)); }
onView.keySet().parallelStream() .map(neighbors::get) .flatMap(neigh -> neigh.getNeighbors().parallelStream() .map(node -> node.compareTo(neigh.getCenter()) > 0 ? new Pair<>(neigh.getCenter(), node) : new Pair<>(node, neigh.getCenter()))) .distinct() .map(pair -> mapPair(pair, node ->
/** * Simulates the arrival of the message to other nodes. * * @param currentTime * the current simulation time (used to understand when a message * should get dropped). */ public void simulateMessageArrival(final double currentTime) { assert toBeSent != null; Objects.requireNonNull(toBeSent); if (!toBeSent.isEmpty()) { final MessageInfo msg = new MessageInfo(currentTime, node, toBeSent); env.getNeighborhood(node).forEach(n -> { if (n instanceof ProtelisNode) { final AlchemistNetworkManager destination = ((ProtelisNode) n).getNetworkManager(prog); if (destination != null) { /* * The node is running the program. Otherwise, the * program is discarded */ destination.receiveMessage(msg); } } }); } toBeSent = null; }
private void update(final Environment<T> env, final Time time) { if (Thread.holdsLock(env)) { if (envHasMobileObstacles(env)) { loadObstacles(env); } lasttime = time.toDouble(); currentEnv = env; accessData(); positions.clear(); neighbors.clear(); env.getNodes().parallelStream().forEach(node -> { positions.put(node, env.getPosition(node)); try { neighbors.put(node, env.getNeighborhood(node).clone()); } catch (Exception e) { L.error("Unable to clone neighborhood for " + node, e); } }); releaseData(); repaint(); } else { throw new IllegalStateException("Only the simulation thread can dictate GUI updates"); } }
@Override public boolean isValid() { if (neigh.isEmpty()) { return false; } else { final Neighborhood<Double> neighborhood = environment.getNeighborhood(getNode()); return neigh.entrySet().stream() .filter(n -> n.getKey() instanceof CellNode) .allMatch(n -> neighborhood.contains(n.getKey()) && n.getKey().getConcentration(mol) >= conc); } }
@Override public boolean isValid() { return environment.getNeighborhood(getNode()).getNeighbors().stream() .parallel() .filter(n -> n instanceof EnvironmentNode) .findAny() .isPresent(); }
@Override public void moveNode(final Node<Double> node, final Position direction) { if (node instanceof CellNode) { super.moveNode(node, direction); final CellNode nodeToMove = (CellNode) node; final Neighborhood<Double> neigh = getNeighborhood(nodeToMove); final Map<Junction, Map<CellNode, Integer>> jun = nodeToMove.getJunctions(); jun.entrySet().stream().forEach(e -> e.getValue().entrySet().forEach(e2 -> { if (!neigh.contains(e2.getKey())) { // there is a junction that links a node which isn't in the neighborhood after the movement for (int i = 0; i < e2.getValue(); i++) { nodeToMove.removeJunction(e.getKey(), e2.getKey()); e2.getKey().removeJunction(e.getKey().reverse(), nodeToMove); } } })); } }
@Override public boolean isValid() { return env.getNeighborhood(getNode()).getNeighbors().stream() .parallel() .filter(n -> n instanceof CellNode) .findAny() .isPresent(); }
/** * This method checks if there may be a dependency considering the * neighborhoods */ private static <T> boolean influenceNeighborCheck(final Environment<T> env, final Reaction<T> source, final Reaction<T> target, final Context in, final Context out) { final Neighborhood<T> sn = env.getNeighborhood(source.getNode()); final boolean scn = in.equals(Context.NEIGHBORHOOD); // If source reads from neighborhood and target is within if (scn && sn.contains(target.getNode())) { return true; } // If target writes in neighborhood and source is within final Neighborhood<T> tn = env.getNeighborhood(target.getNode()); final boolean tcn = out.equals(Context.NEIGHBORHOOD); if (tcn && tn.contains(source.getNode())) { return true; } // If source writes on the neighborhood, target reads on its // neighborhood and there is at least one common node return scn && tcn && commonNeighbor(env, sn, target.getNode()); }
/** * @return a list of EnvironmentNodes near to the node where this condition is located. */ protected final List<Node<Double>> getEnviromentNodesSurrounding() { return environment.getNeighborhood(getNode()).getNeighbors().stream() .parallel() .filter(n -> n instanceof EnvironmentNode) .collect(Collectors.toList()); }
private static <T> boolean commonNeighbor(final Environment<T> env, final Neighborhood<T> sl, final Node<T> t) { for (final Node<T> n : sl) { if (env.getNeighborhood(n).getNeighbors().contains(t)) { return true; } } return false; }
/** * Searches in the whole neighborhood of the current node which neighbors satisfy the condition, and * returns a list of this neighbors. * @return a map of neighbors which satisfy the condition and their propensity */ public Map<Node<T>, Double> getValidNeighbors() { return getValidNeighbors(env.getNeighborhood(getNode()).getNeighbors()); }
@Override protected void updateInternalStatus(final Time curTime, final boolean executed, final Environment<Double> env) { if (neighborConditionsPresent) { validNeighbors.clear(); validNeighbors = env.getNeighborhood(node).getNeighbors().stream().collect(Collectors.<Node<Double>, Node<Double>, Double>toMap( n -> n, n -> 0d)); for (final Condition<Double> cond : getConditions()) { if (cond instanceof AbstractNeighborCondition) { validNeighbors = intersectMap(validNeighbors, ((AbstractNeighborCondition<Double>) cond).getValidNeighbors(validNeighbors.keySet())); if (validNeighbors.isEmpty()) { // maybe speedup the check break; } } } } super.updateInternalStatus(curTime, executed, env); }
final List<Node<Double>> l = env.getNeighborhood(thisNode).getNeighbors().stream() .filter(n -> n instanceof EnvironmentNode && n.contains(biomol)) .collect(Collectors.toList());