@Override public ComputationalRoundComplete cloneCondition(final Node<Object> n, final Reaction<Object> r) { final List<RunProtelisProgram> possibleRefs = n.getReactions().stream() .map(Reaction::getActions) .flatMap(List::stream) .filter(a -> a instanceof RunProtelisProgram) .map(a -> (RunProtelisProgram) a) .collect(Collectors.toList()); if (possibleRefs.size() == 1) { return new ComputationalRoundComplete((ProtelisNode) n, possibleRefs.get(0)); } throw new IllegalStateException("There must be one and one only unconfigured " + RunProtelisProgram.class.getSimpleName()); }
@Override public SendToNeighbor cloneAction(final Node<Object> n, final Reaction<Object> r) { final List<RunProtelisProgram> possibleRefs = n.getReactions().stream() .map(Reaction::getActions) .flatMap(List::stream) .filter(a -> a instanceof RunProtelisProgram) .map(a -> (RunProtelisProgram) a) .collect(Collectors.toList()); if (possibleRefs.size() == 1) { return new SendToNeighbor((ProtelisNode) n, reaction, possibleRefs.get(0)); } throw new IllegalStateException("There must be one and one only unconfigured " + RunProtelisProgram.class.getSimpleName()); }
@Override public void nodeMoved(final Node<T> node) { checkCaller(); for (final Reaction<T> r : node.getReactions()) { updateReaction(handlers.get(r)); } }
private void finalizeConstructor() { for (final Node<T> n : env.getNodes()) { for (final Reaction<T> r : n.getReactions()) { scheduleReaction(r); } } }
@Override public void nodeRemoved(final Node<T> node, final Neighborhood<T> oldNeighborhood) { checkCaller(); for (final Reaction<T> r : node.getReactions()) { removeReaction(r); } }
@Override public void stepDone(final Environment<T> env, final Reaction<T> exec, final Time time, final long step) { if (exec == null || exec.getNode().equals(n)) { final StringBuilder sb = new StringBuilder(stringLength); sb.append(POSITION); sb.append('\n'); sb.append(env.getPosition(n)); sb.append("\n\n\n"); sb.append(CONTENT); sb.append('\n'); sb.append(n.getContents().entrySet().stream() .map(e -> e.getKey().getName() + " > " + e.getValue() + '\n') .sorted() .collect(Collectors.joining()) ); sb.append("\n\n\n"); sb.append(PROGRAM); sb.append("\n\n"); for (final Reaction<T> r : n.getReactions()) { sb.append(r.toString()); sb.append("\n\n"); } stringLength = sb.length() + MARGIN; SwingUtilities.invokeLater(() -> { txt.setText(sb.toString()); }); } } }
private void updateNeighborhood(final Node<T> n) { for (final Reaction<T> r : n.getReactions()) { if (r.getInputContext().equals(Context.NEIGHBORHOOD)) { updateReaction(handlers.get(r)); } } }
@Override public void removeNeighbor(final Node<T> n1, final Node<T> n2) { DependencyHandler<T> rh1, rh2; for (final Reaction<T> r1 : n1.getReactions()) { rh1 = hndlrs.get(r1); for (final Reaction<T> r2 : n2.getReactions()) { rh2 = hndlrs.get(r2); if (!mayInfluence(r2, r1)) { rh1.removeInDependency(rh2); rh2.removeOutDependency(rh1); /* * Dependencies addition or removal should not influence the * next scheduled execution time. */ } if (!mayInfluence(r1, r2)) { rh2.removeInDependency(rh1); rh1.removeOutDependency(rh2); /* * Dependencies addition or removal should not influence the * next scheduled execution time. */ } } } } }
private void updateDependenciesForOperationOnNode(final Neighborhood<T> oldNeighborhood) { /* * A reaction in the neighborhood may have changed due to the content of * this new node. Must check. */ for (final Node<T> n : oldNeighborhood) { for (final Reaction<T> r : n.getReactions()) { if (r.getInputContext().equals(Context.NEIGHBORHOOD)) { updateReaction(handlers.get(r)); } } } /* * It is possible that some global reaction is changed due to the * creation of a new node. Checking. */ for (final Node<T> n : env) { for (final Reaction<T> r : n.getReactions()) { if (r.getInputContext().equals(Context.GLOBAL)) { updateReaction(handlers.get(r)); } } } }
@Override public void addNeighbor(final Node<T> n1, final Node<T> n2) { DependencyHandler<T> rh1, rh2; for (final Reaction<T> r1 : n1.getReactions()) { rh1 = hndlrs.get(r1); for (int j = 0; j < n2.getReactions().size(); j++) { final Reaction<T> r2 = n2.getReactions().get(j); if (mayInfluence(r2, r1) && influences(r2, r1.getInfluencingMolecules())) { rh2 = hndlrs.get(r2); if (!rh1.isInfluenced().contains(rh2)) { rh1.addInDependency(rh2); } if (!rh2.influences().contains(rh1)) { rh2.addOutDependency(rh1); } } if (mayInfluence(r1, r2) && influences(r1, r2.getInfluencingMolecules())) { rh2 = hndlrs.get(r2); if (!rh2.isInfluenced().contains(rh1)) { rh2.addInDependency(rh1); } if (!rh1.influences().contains(rh2)) { rh1.addOutDependency(rh2); } } } } }
@Override public void nodeAdded(final Node<T> node) { checkCaller(); if (status != Status.INIT) { for (final Reaction<T> r : node.getReactions()) { scheduleReaction(r); } updateDependenciesForOperationOnNode(env.getNeighborhood(node)); } }