123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257 |
- /*This is a modified version of the class org.graphstream.stream.SourceBase
- * It was modified by Jascha Bohne <jaschabohne@web.de> for use in the scopviz project
- * This class is based on the 1.3 release of graphstream
- *
- * Copyright 2006 - 2015
- * Stefan Balev <stefan.balev@graphstream-project.org>
- * Julien Baudry <julien.baudry@graphstream-project.org>
- * Antoine Dutot <antoine.dutot@graphstream-project.org>
- * Yoann Pigné <yoann.pigne@graphstream-project.org>
- * Guilhelm Savin <guilhelm.savin@graphstream-project.org>
- *
- * This file is part of GraphStream <http://graphstream-project.org>.
- *
- * GraphStream is a library whose purpose is to handle static or dynamic
- * graph, create them from scratch, file or any source and display them.
- *
- * This program is free software distributed under the terms of two licenses, the
- * CeCILL-C license that fits European law, and the GNU Lesser General Public
- * License. You can use, modify and/ or redistribute the software under the terms
- * of the CeCILL-C license as circulated by CEA, CNRS and INRIA at the following
- * URL <http://www.cecill.info> or under the terms of the GNU LGPL as published by
- * the Free Software Foundation, either version 3 of the License, or (at your
- * option) any later version.
- *
- * This program is distributed in the hope that it will be useful, but WITHOUT ANY
- * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
- * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- * The fact that you are presently reading this means that you have had
- * knowledge of the CeCILL-C and LGPL licenses and that you accept their terms.
- */
- package de.tu_darmstadt.informatik.tk.scopviz.io;
- import java.util.ArrayList;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Stack;
- import org.graphstream.graph.implementations.AbstractElement.AttributeChangeEvent;
- import org.graphstream.stream.AttributeSink;
- import org.graphstream.stream.ElementSink;
- import org.graphstream.stream.Sink;
- import org.graphstream.stream.Source;
- import org.graphstream.stream.sync.SourceTime;
- import de.tu_darmstadt.informatik.tk.scopviz.debug.Debug;
- import de.tu_darmstadt.informatik.tk.scopviz.graphs.MyGraph;
- /**
- * Base implementation of an input that provide basic sink handling.
- *
- * <p>
- * This implementation can register a set of graph sinks (or separate sets of
- * attributes or elements sinks) and provides protected methods to easily
- * broadcast events to all the sinks (beginning with "send").
- * </p>
- *
- * <p>
- * Each time you want to produce an event toward all registered sinks, you call
- * one of the "send*" methods with correct parameters. The parameters of the
- * "send*" methods maps to the usual GraphStream events.
- * </p>
- *
- * <p>
- * This class is "reentrant". This means that if a send*() method is called
- * during the execution of another or the same send*() method, the event is
- * deferred until the first send*() method is finished. This avoid recursive
- * loops if a sink modifies the input during event handling.
- * </p>
- */
- public class MySourceBase implements Source {
- // Attribute
- /**
- * if the programm is currently reading a multigraph
- */
- protected boolean multiGraph = false;
- /**
- * @return if the last read Graph was a multigraph
- */
- public boolean wasMultiGraph() {
- return multiGraph;
- }
- /**
- * the sink of the complete Graph
- */
- protected Sink originalSink;
- /**
- * Enum of the different possible Types of Elements.
- */
- public enum ElementType {
- NODE, EDGE, GRAPH
- };
- /**
- * Set of graph attributes sinks.
- */
- protected ArrayList<AttributeSink> attrSinks = new ArrayList<AttributeSink>();
- /**
- * Set of graph elements sinks.
- */
- protected ArrayList<ElementSink> eltsSinks = new ArrayList<ElementSink>();
- /**
- * A queue that allow the management of events (nodes/edge
- * add/delete/change) in the right order.
- */
- protected LinkedList<GraphEvent> eventQueue = new LinkedList<GraphEvent>();
- /**
- * A boolean that indicates whether or not an Sink event is being sent
- * during another one.
- */
- protected boolean eventProcessing = false;
- /**
- * Id of this source.
- */
- protected String sourceId;
- /**
- * Time of this source.
- */
- protected SourceTime sourceTime;
- /**
- * a List of all inner Graphs of a multigraphFile.
- */
- protected LinkedList<MyGraph> subGraphs = new LinkedList<>();
- /**
- * all inner graphs that are currently being edited.
- */
- protected Stack<MyGraph> usedSubGraphs = new Stack<>();
- /**
- * the ID of the (last added) outer Graph.
- */
- protected String superID = "";
- // Construction
- /**
- * Creates a new MySourceBase Object with a random ID.
- */
- protected MySourceBase() {
- this(String.format("sourceOnThread#%d_%d", Thread.currentThread().getId(),
- System.currentTimeMillis() + ((int) (Math.random() * 1000))));
- }
- /**
- * Creates a new MySourceBase Object with a given ID.
- *
- * @param sourceId
- * the ID to use
- */
- protected MySourceBase(String sourceId) {
- this.sourceId = sourceId;
- this.sourceTime = new SourceTime(sourceId);
- }
- // Access
- /**
- * Returns an Iterable over all the attribute sinks.
- *
- * @return the Iterable
- */
- public Iterable<AttributeSink> attributeSinks() {
- return attrSinks;
- }
- /**
- * Returns an Iterable over all the element sinks.
- *
- * @return the Iterable
- */
- public Iterable<ElementSink> elementSinks() {
- return eltsSinks;
- }
- // Command
- @Override
- public void addSink(Sink sink) {
- multiGraph = false;
- originalSink = sink;
- addAttributeSink(sink);
- addElementSink(sink);
- resetSubGraphs();
- try {
- superID = ((MyGraph) sink).getId();
- } catch (Exception e) {
- Debug.out(e.toString() + "\n" + e.getStackTrace().toString());
- }
- }
- /**
- * Adds a new Sink that works with SubGraphs.
- *
- * @param sink
- * the Sink to add
- */
- private void addSubGraphSink(Sink sink) {
- addAttributeSink(sink);
- addElementSink(sink);
- }
- @Override
- public void addAttributeSink(AttributeSink sink) {
- if (!eventProcessing) {
- eventProcessing = true;
- manageEvents();
- attrSinks.add(sink);
- manageEvents();
- eventProcessing = false;
- } else {
- eventQueue.add(new AddToListEvent<AttributeSink>(attrSinks, sink));
- }
- }
- @Override
- public void addElementSink(ElementSink sink) {
- if (!eventProcessing) {
- eventProcessing = true;
- manageEvents();
- eltsSinks.add(sink);
- manageEvents();
- eventProcessing = false;
- } else {
- eventQueue.add(new AddToListEvent<ElementSink>(eltsSinks, sink));
- }
- }
- @Override
- public void clearSinks() {
- clearElementSinks();
- clearAttributeSinks();
- }
- @Override
- public void clearElementSinks() {
- if (!eventProcessing) {
- eventProcessing = true;
- manageEvents();
- eltsSinks.clear();
- manageEvents();
- eventProcessing = false;
- } else {
- eventQueue.add(new ClearListEvent<ElementSink>(eltsSinks));
- }
- }
- @Override
- public void clearAttributeSinks() {
- if (!eventProcessing) {
- eventProcessing = true;
- manageEvents();
- attrSinks.clear();
- manageEvents();
- eventProcessing = false;
- } else {
- eventQueue.add(new ClearListEvent<AttributeSink>(attrSinks));
- }
- }
- @Override
- public void removeSink(Sink sink) {
- removeAttributeSink(sink);
- removeElementSink(sink);
- }
- @Override
- public void removeAttributeSink(AttributeSink sink) {
- if (!eventProcessing) {
- eventProcessing = true;
- manageEvents();
- attrSinks.remove(sink);
- manageEvents();
- eventProcessing = false;
- } else {
- eventQueue.add(new RemoveFromListEvent<AttributeSink>(attrSinks, sink));
- }
- }
- @Override
- public void removeElementSink(ElementSink sink) {
- if (!eventProcessing) {
- eventProcessing = true;
- manageEvents();
- eltsSinks.remove(sink);
- manageEvents();
- eventProcessing = false;
- } else {
- eventQueue.add(new RemoveFromListEvent<ElementSink>(eltsSinks, sink));
- }
- }
- /**
- * Send a "graph cleared" event to all element sinks.
- *
- * @param sourceId
- * The source identifier.
- */
- public void sendGraphCleared(String sourceId) {
- sendGraphCleared(sourceId, sourceTime.newEvent());
- }
- /**
- * Send a "graph cleared" event to all element sinks.
- *
- * @param sourceId
- * The source identifier.
- * @param timeId
- */
- public void sendGraphCleared(String sourceId, long timeId) {
- if (!eventProcessing) {
- eventProcessing = true;
- manageEvents();
- for (int i = 0; i < eltsSinks.size(); i++) {
- eltsSinks.get(i).graphCleared(sourceId, timeId);
- }
- manageEvents();
- eventProcessing = false;
- } else {
- eventQueue.add(new BeforeGraphClearEvent(sourceId, timeId));
- }
- }
- /**
- * Send a "step begins" event to all element sinks.
- *
- * @param sourceId
- * The graph identifier.
- * @param step
- * The step time stamp.
- */
- public void sendStepBegins(String sourceId, double step) {
- sendStepBegins(sourceId, sourceTime.newEvent(), step);
- }
- /**
- * Send a "step begins" event to all element sinks.
- *
- * @param sourceId
- * The graph identifier.
- * @param timeId
- * @param step
- */
- public void sendStepBegins(String sourceId, long timeId, double step) {
- if (!eventProcessing) {
- eventProcessing = true;
- manageEvents();
- for (int i = 0; i < eltsSinks.size(); i++) {
- eltsSinks.get(i).stepBegins(sourceId, timeId, step);
- }
- manageEvents();
- eventProcessing = false;
- } else {
- eventQueue.add(new StepBeginsEvent(sourceId, timeId, step));
- }
- }
- /**
- * Send a "node added" event to all element sinks.
- *
- * @param sourceId
- * The source identifier.
- * @param nodeId
- * The node identifier.
- */
- public void sendNodeAdded(String sourceId, String nodeId) {
- sendNodeAdded(sourceId, sourceTime.newEvent(), nodeId);
- }
- /**
- * Send a "node added" event to all element sinks.
- *
- * @param sourceId
- * The source identifier.
- * @param timeId
- * @param nodeId
- * The node identifier.
- */
- public void sendNodeAdded(String sourceId, long timeId, String nodeId) {
- if (!eventProcessing) {
- eventProcessing = true;
- manageEvents();
- for (int i = 0; i < eltsSinks.size(); i++) {
- eltsSinks.get(i).nodeAdded(sourceId, timeId, nodeId);
- }
- manageEvents();
- eventProcessing = false;
- } else {
- eventQueue.add(new AfterNodeAddEvent(sourceId, timeId, nodeId));
- }
- }
- /**
- * Send a "node removed" event to all element sinks.
- *
- * @param sourceId
- * The graph identifier.
- * @param nodeId
- * The node identifier.
- */
- public void sendNodeRemoved(String sourceId, String nodeId) {
- sendNodeRemoved(sourceId, sourceTime.newEvent(), nodeId);
- }
- /**
- * Send a "node removed" event to all element sinks.
- *
- * @param sourceId
- * The graph identifier.
- * @param timeId
- * @param nodeId
- * The node identifier.
- */
- public void sendNodeRemoved(String sourceId, long timeId, String nodeId) {
- if (!eventProcessing) {
- eventProcessing = true;
- manageEvents();
- for (int i = 0; i < eltsSinks.size(); i++) {
- eltsSinks.get(i).nodeRemoved(sourceId, timeId, nodeId);
- }
- manageEvents();
- eventProcessing = false;
- } else {
- eventQueue.add(new BeforeNodeRemoveEvent(sourceId, timeId, nodeId));
- }
- }
- /**
- * Send an "edge added" event to all element sinks.
- *
- * @param sourceId
- * The source identifier.
- * @param edgeId
- * The edge identifier.
- * @param fromNodeId
- * The edge start node.
- * @param toNodeId
- * The edge end node.
- * @param directed
- * Is the edge directed?.
- */
- public void sendEdgeAdded(String sourceId, String edgeId, String fromNodeId, String toNodeId, boolean directed) {
- sendEdgeAdded(sourceId, sourceTime.newEvent(), edgeId, fromNodeId, toNodeId, directed);
- }
- /**
- * Send an "edge added" event to all element sinks.
- *
- * @param sourceId
- * The source identifier.
- * @param timeId
- * @param edgeId
- * The edge identifier.
- * @param fromNodeId
- * The edge start node.
- * @param toNodeId
- * The edge end node.
- * @param directed
- * Is the edge directed?.
- */
- public void sendEdgeAdded(String sourceId, long timeId, String edgeId, String fromNodeId, String toNodeId,
- boolean directed) {
- if (!eventProcessing) {
- eventProcessing = true;
- manageEvents();
- for (int i = 0; i < eltsSinks.size(); i++) {
- eltsSinks.get(i).edgeAdded(sourceId, timeId, edgeId, fromNodeId, toNodeId, directed);
- }
- manageEvents();
- eventProcessing = false;
- } else {
- eventQueue.add(new AfterEdgeAddEvent(sourceId, timeId, edgeId, fromNodeId, toNodeId, directed));
- }
- }
- /**
- * Send a "edge removed" event to all element sinks.
- *
- * @param sourceId
- * The source identifier.
- * @param edgeId
- * The edge identifier.
- */
- public void sendEdgeRemoved(String sourceId, String edgeId) {
- sendEdgeRemoved(sourceId, sourceTime.newEvent(), edgeId);
- }
- /**
- * Send a "edge removed" event to all element sinks.
- *
- * @param sourceId
- * The source identifier.
- * @param timeId
- * @param edgeId
- * The edge identifier.
- */
- public void sendEdgeRemoved(String sourceId, long timeId, String edgeId) {
- if (!eventProcessing) {
- eventProcessing = true;
- manageEvents();
- for (int i = 0; i < eltsSinks.size(); i++) {
- eltsSinks.get(i).edgeRemoved(sourceId, timeId, edgeId);
- }
- manageEvents();
- eventProcessing = false;
- } else {
- eventQueue.add(new BeforeEdgeRemoveEvent(sourceId, timeId, edgeId));
- }
- }
- /**
- * Send a "edge attribute added" event to all attribute sinks.
- *
- * @param sourceId
- * The source identifier.
- * @param edgeId
- * The edge identifier.
- * @param attribute
- * The attribute name.
- * @param value
- * The attribute value.
- */
- public void sendEdgeAttributeAdded(String sourceId, String edgeId, String attribute, Object value) {
- sendAttributeChangedEvent(sourceId, edgeId, ElementType.EDGE, attribute, AttributeChangeEvent.ADD, null, value);
- }
- /**
- * Send a "edge attribute added" event to all attribute sinks.
- *
- * @param sourceId
- * The source identifier.
- * @param timeId
- * @param edgeId
- * The edge identifier.
- * @param attribute
- * The attribute name.
- * @param value
- * The attribute value.
- */
- public void sendEdgeAttributeAdded(String sourceId, long timeId, String edgeId, String attribute, Object value) {
- sendAttributeChangedEvent(sourceId, timeId, edgeId, ElementType.EDGE, attribute, AttributeChangeEvent.ADD, null,
- value);
- }
- /**
- * Send a "edge attribute changed" event to all attribute sinks.
- *
- * @param sourceId
- * The source identifier.
- * @param edgeId
- * The edge identifier.
- * @param attribute
- * The attribute name.
- * @param oldValue
- * The old attribute value.
- * @param newValue
- * The new attribute value.
- */
- public void sendEdgeAttributeChanged(String sourceId, String edgeId, String attribute, Object oldValue,
- Object newValue) {
- sendAttributeChangedEvent(sourceId, edgeId, ElementType.EDGE, attribute, AttributeChangeEvent.CHANGE, oldValue,
- newValue);
- }
- /**
- * Send a "edge attribute changed" event to all attribute sinks.
- *
- * @param sourceId
- * The source identifier.
- * @param timeId
- * @param edgeId
- * The edge identifier.
- * @param attribute
- * The attribute name.
- * @param oldValue
- * The old attribute value.
- * @param newValue
- * The new attribute value.
- */
- public void sendEdgeAttributeChanged(String sourceId, long timeId, String edgeId, String attribute, Object oldValue,
- Object newValue) {
- sendAttributeChangedEvent(sourceId, timeId, edgeId, ElementType.EDGE, attribute, AttributeChangeEvent.CHANGE,
- oldValue, newValue);
- }
- /**
- * Send a "edge attribute removed" event to all attribute sinks.
- *
- * @param sourceId
- * The source identifier.
- * @param edgeId
- * The edge identifier.
- * @param attribute
- * The attribute name.
- */
- public void sendEdgeAttributeRemoved(String sourceId, String edgeId, String attribute) {
- sendAttributeChangedEvent(sourceId, edgeId, ElementType.EDGE, attribute, AttributeChangeEvent.REMOVE, null,
- null);
- }
- /**
- * Send a "edge attribute removed" event to all attribute sinks.
- *
- * @param sourceId
- * The source identifier.
- * @param timeId
- * @param edgeId
- * The edge identifier.
- * @param attribute
- * The attribute name.
- */
- public void sendEdgeAttributeRemoved(String sourceId, long timeId, String edgeId, String attribute) {
- sendAttributeChangedEvent(sourceId, timeId, edgeId, ElementType.EDGE, attribute, AttributeChangeEvent.REMOVE,
- null, null);
- }
- /**
- * Send a "graph attribute added" event to all attribute sinks.
- *
- * @param sourceId
- * The source identifier.
- * @param attribute
- * The attribute name.
- * @param value
- * The attribute value.
- */
- public void sendGraphAttributeAdded(String sourceId, String attribute, Object value) {
- sendAttributeChangedEvent(sourceId, null, ElementType.GRAPH, attribute, AttributeChangeEvent.ADD, null, value);
- }
- /**
- * Send a "graph attribute added" event to all attribute sinks.
- *
- * @param sourceId
- * The source identifier.
- * @param timeId
- * The time identifier.
- * @param attribute
- * The attribute name.
- * @param value
- * The attribute value.
- */
- public void sendGraphAttributeAdded(String sourceId, long timeId, String attribute, Object value) {
- sendAttributeChangedEvent(sourceId, timeId, null, ElementType.GRAPH, attribute, AttributeChangeEvent.ADD, null,
- value);
- }
- /**
- * Send a "graph attribute changed" event to all attribute sinks.
- *
- * @param sourceId
- * The source identifier.
- * @param attribute
- * The attribute name.
- * @param oldValue
- * The attribute old value.
- * @param newValue
- * The attribute new value.
- */
- public void sendGraphAttributeChanged(String sourceId, String attribute, Object oldValue, Object newValue) {
- sendAttributeChangedEvent(sourceId, null, ElementType.GRAPH, attribute, AttributeChangeEvent.CHANGE, oldValue,
- newValue);
- }
- /**
- * Send a "graph attribute changed" event to all attribute sinks.
- *
- * @param sourceId
- * The source identifier.
- * @param timeId
- * The time identifier.
- * @param attribute
- * The attribute name.
- * @param oldValue
- * The attribute old value.
- * @param newValue
- * The attribute new value.
- */
- public void sendGraphAttributeChanged(String sourceId, long timeId, String attribute, Object oldValue,
- Object newValue) {
- sendAttributeChangedEvent(sourceId, timeId, null, ElementType.GRAPH, attribute, AttributeChangeEvent.CHANGE,
- oldValue, newValue);
- }
- /**
- * Send a "graph attribute removed" event to all attribute sinks.
- *
- * @param sourceId
- * The source identifier.
- * @param attribute
- * The attribute name.
- */
- public void sendGraphAttributeRemoved(String sourceId, String attribute) {
- sendAttributeChangedEvent(sourceId, null, ElementType.GRAPH, attribute, AttributeChangeEvent.REMOVE, null,
- null);
- }
- /**
- * Send a "graph attribute removed" event to all attribute sinks.
- *
- * @param sourceId
- * The source identifier.
- * @param timeId
- * The time identifier.
- * @param attribute
- * The attribute name.
- */
- public void sendGraphAttributeRemoved(String sourceId, long timeId, String attribute) {
- sendAttributeChangedEvent(sourceId, timeId, null, ElementType.GRAPH, attribute, AttributeChangeEvent.REMOVE,
- null, null);
- }
- /**
- * Send a "node attribute added" event to all attribute sinks.
- *
- * @param sourceId
- * The source identifier.
- * @param nodeId
- * The node identifier.
- * @param attribute
- * The attribute name.
- * @param value
- * The attribute value.
- */
- public void sendNodeAttributeAdded(String sourceId, String nodeId, String attribute, Object value) {
- sendAttributeChangedEvent(sourceId, nodeId, ElementType.NODE, attribute, AttributeChangeEvent.ADD, null, value);
- }
- /**
- * Send a "node attribute added" event to all attribute sinks.
- *
- * @param sourceId
- * The source identifier.
- * @param timeId
- * The time identifier.
- * @param nodeId
- * The node identifier.
- * @param attribute
- * The attribute name.
- * @param value
- * The attribute value.
- */
- public void sendNodeAttributeAdded(String sourceId, long timeId, String nodeId, String attribute, Object value) {
- sendAttributeChangedEvent(sourceId, timeId, nodeId, ElementType.NODE, attribute, AttributeChangeEvent.ADD, null,
- value);
- }
- /**
- * Send a "node attribute changed" event to all attribute sinks.
- *
- * @param sourceId
- * The source identifier.
- * @param nodeId
- * The node identifier.
- * @param attribute
- * The attribute name.
- * @param oldValue
- * The attribute old value.
- * @param newValue
- * The attribute new value.
- */
- public void sendNodeAttributeChanged(String sourceId, String nodeId, String attribute, Object oldValue,
- Object newValue) {
- sendAttributeChangedEvent(sourceId, nodeId, ElementType.NODE, attribute, AttributeChangeEvent.CHANGE, oldValue,
- newValue);
- }
- /**
- * Send a "node attribute changed" event to all attribute sinks.
- *
- * @param sourceId
- * The source identifier.
- * @param timeId
- * The time identifier.
- * @param nodeId
- * The node identifier.
- * @param attribute
- * The attribute name.
- * @param oldValue
- * The attribute old value.
- * @param newValue
- * The attribute new value.
- */
- public void sendNodeAttributeChanged(String sourceId, long timeId, String nodeId, String attribute, Object oldValue,
- Object newValue) {
- sendAttributeChangedEvent(sourceId, timeId, nodeId, ElementType.NODE, attribute, AttributeChangeEvent.CHANGE,
- oldValue, newValue);
- }
- /**
- * Send a "node attribute removed" event to all attribute sinks.
- *
- * @param sourceId
- * The source identifier.
- * @param nodeId
- * The node identifier.
- * @param attribute
- * The attribute name.
- */
- public void sendNodeAttributeRemoved(String sourceId, String nodeId, String attribute) {
- sendAttributeChangedEvent(sourceId, nodeId, ElementType.NODE, attribute, AttributeChangeEvent.REMOVE, null,
- null);
- }
- /**
- * Send a "node attribute removed" event to all attribute sinks.
- *
- * @param sourceId
- * The source identifier.
- * @param timeId
- * @param nodeId
- * The node identifier.
- * @param attribute
- * The attribute name.
- */
- public void sendNodeAttributeRemoved(String sourceId, long timeId, String nodeId, String attribute) {
- sendAttributeChangedEvent(sourceId, timeId, nodeId, ElementType.NODE, attribute, AttributeChangeEvent.REMOVE,
- null, null);
- }
- /**
- * Send a add/change/remove attribute event on an element. This method is a
- * generic way of notifying of an attribute change and is equivalent to
- * individual send*Attribute*() methods.
- *
- * @param sourceId
- * The source identifier.
- * @param eltId
- * The changed element identifier.
- * @param eltType
- * The changed element type.
- * @param attribute
- * The changed attribute.
- * @param event
- * The add/change/remove action.
- * @param oldValue
- * The old attribute value (null if the attribute is removed or
- * added).
- * @param newValue
- * The new attribute value (null if removed).
- */
- public void sendAttributeChangedEvent(String sourceId, String eltId, ElementType eltType, String attribute,
- AttributeChangeEvent event, Object oldValue, Object newValue) {
- sendAttributeChangedEvent(sourceId, sourceTime.newEvent(), eltId, eltType, attribute, event, oldValue,
- newValue);
- }
- /**
- * Send a add/change/remove attribute event on an element. This method is a
- * generic way of notifying of an attribute change and is equivalent to
- * individual send*Attribute*() methods.
- *
- * @param sourceId
- * The source identifier.
- * @param timeId
- * The time identifier.
- * @param eltId
- * The changed element identifier.
- * @param eltType
- * The changed element type.
- * @param attribute
- * The changed attribute.
- * @param event
- * The add/change/remove action.
- * @param oldValue
- * The old attribute value (null if the attribute is removed or
- * added).
- * @param newValue
- * The new attribute value (null if removed).
- */
- public void sendAttributeChangedEvent(String sourceId, long timeId, String eltId, ElementType eltType,
- String attribute, AttributeChangeEvent event, Object oldValue, Object newValue) {
- if (!eventProcessing) {
- eventProcessing = true;
- manageEvents();
- if (event == AttributeChangeEvent.ADD) {
- if (eltType == ElementType.NODE) {
- for (int i = 0; i < attrSinks.size(); i++) {
- attrSinks.get(i).nodeAttributeAdded(sourceId, timeId, eltId, attribute, newValue);
- }
- } else if (eltType == ElementType.EDGE) {
- for (int i = 0; i < attrSinks.size(); i++) {
- attrSinks.get(i).edgeAttributeAdded(sourceId, timeId, eltId, attribute, newValue);
- }
- } else {
- for (int i = 0; i < attrSinks.size(); i++) {
- attrSinks.get(i).graphAttributeAdded(sourceId, timeId, attribute, newValue);
- }
- }
- } else if (event == AttributeChangeEvent.REMOVE) {
- if (eltType == ElementType.NODE) {
- for (int i = 0; i < attrSinks.size(); i++) {
- attrSinks.get(i).nodeAttributeRemoved(sourceId, timeId, eltId, attribute);
- }
- } else if (eltType == ElementType.EDGE) {
- for (int i = 0; i < attrSinks.size(); i++) {
- attrSinks.get(i).edgeAttributeRemoved(sourceId, timeId, eltId, attribute);
- }
- } else {
- for (int i = 0; i < attrSinks.size(); i++) {
- attrSinks.get(i).graphAttributeRemoved(sourceId, timeId, attribute);
- }
- }
- } else {
- if (eltType == ElementType.NODE) {
- for (int i = 0; i < attrSinks.size(); i++) {
- attrSinks.get(i).nodeAttributeChanged(sourceId, timeId, eltId, attribute, oldValue, newValue);
- }
- } else if (eltType == ElementType.EDGE) {
- for (int i = 0; i < attrSinks.size(); i++) {
- attrSinks.get(i).edgeAttributeChanged(sourceId, timeId, eltId, attribute, oldValue, newValue);
- }
- } else {
- for (int i = 0; i < attrSinks.size(); i++) {
- attrSinks.get(i).graphAttributeChanged(sourceId, timeId, attribute, oldValue, newValue);
- }
- }
- }
- manageEvents();
- eventProcessing = false;
- } else {
- eventQueue.add(
- new AttributeChangedEvent(sourceId, timeId, eltId, eltType, attribute, event, oldValue, newValue));
- }
- }
- // Deferred event management
- /**
- * If in "event processing mode", ensure all pending events are processed.
- */
- protected void manageEvents() {
- if (eventProcessing) {
- while (!eventQueue.isEmpty()) {
- eventQueue.remove().trigger();
- }
- }
- }
- // Events Management
- /**
- * Interface that provide general purpose classification for evens involved
- * in graph modifications.
- */
- abstract class GraphEvent {
- String sourceId;
- long timeId;
- GraphEvent(String sourceId, long timeId) {
- this.sourceId = sourceId;
- this.timeId = timeId;
- }
- abstract void trigger();
- }
- class AfterEdgeAddEvent extends GraphEvent {
- String edgeId;
- String fromNodeId;
- String toNodeId;
- boolean directed;
- AfterEdgeAddEvent(String sourceId, long timeId, String edgeId, String fromNodeId, String toNodeId,
- boolean directed) {
- super(sourceId, timeId);
- this.edgeId = edgeId;
- this.fromNodeId = fromNodeId;
- this.toNodeId = toNodeId;
- this.directed = directed;
- }
- @Override
- void trigger() {
- for (int i = 0; i < eltsSinks.size(); i++)
- eltsSinks.get(i).edgeAdded(sourceId, timeId, edgeId, fromNodeId, toNodeId, directed);
- }
- }
- class BeforeEdgeRemoveEvent extends GraphEvent {
- String edgeId;
- BeforeEdgeRemoveEvent(String sourceId, long timeId, String edgeId) {
- super(sourceId, timeId);
- this.edgeId = edgeId;
- }
- @Override
- void trigger() {
- for (int i = 0; i < eltsSinks.size(); i++)
- eltsSinks.get(i).edgeRemoved(sourceId, timeId, edgeId);
- }
- }
- class AfterNodeAddEvent extends GraphEvent {
- String nodeId;
- AfterNodeAddEvent(String sourceId, long timeId, String nodeId) {
- super(sourceId, timeId);
- this.nodeId = nodeId;
- }
- @Override
- void trigger() {
- for (int i = 0; i < eltsSinks.size(); i++)
- eltsSinks.get(i).nodeAdded(sourceId, timeId, nodeId);
- }
- }
- class BeforeNodeRemoveEvent extends GraphEvent {
- String nodeId;
- BeforeNodeRemoveEvent(String sourceId, long timeId, String nodeId) {
- super(sourceId, timeId);
- this.nodeId = nodeId;
- }
- @Override
- void trigger() {
- for (int i = 0; i < eltsSinks.size(); i++)
- eltsSinks.get(i).nodeRemoved(sourceId, timeId, nodeId);
- }
- }
- class BeforeGraphClearEvent extends GraphEvent {
- BeforeGraphClearEvent(String sourceId, long timeId) {
- super(sourceId, timeId);
- }
- @Override
- void trigger() {
- for (int i = 0; i < eltsSinks.size(); i++)
- eltsSinks.get(i).graphCleared(sourceId, timeId);
- }
- }
- class StepBeginsEvent extends GraphEvent {
- double step;
- StepBeginsEvent(String sourceId, long timeId, double step) {
- super(sourceId, timeId);
- this.step = step;
- }
- @Override
- void trigger() {
- for (int i = 0; i < eltsSinks.size(); i++)
- eltsSinks.get(i).stepBegins(sourceId, timeId, step);
- }
- }
- class AttributeChangedEvent extends GraphEvent {
- ElementType eltType;
- String eltId;
- String attribute;
- AttributeChangeEvent event;
- Object oldValue;
- Object newValue;
- AttributeChangedEvent(String sourceId, long timeId, String eltId, ElementType eltType, String attribute,
- AttributeChangeEvent event, Object oldValue, Object newValue) {
- super(sourceId, timeId);
- this.eltType = eltType;
- this.eltId = eltId;
- this.attribute = attribute;
- this.event = event;
- this.oldValue = oldValue;
- this.newValue = newValue;
- }
- @Override
- void trigger() {
- switch (event) {
- case ADD:
- switch (eltType) {
- case NODE:
- for (int i = 0; i < attrSinks.size(); i++)
- attrSinks.get(i).nodeAttributeAdded(sourceId, timeId, eltId, attribute, newValue);
- break;
- case EDGE:
- for (int i = 0; i < attrSinks.size(); i++)
- attrSinks.get(i).edgeAttributeAdded(sourceId, timeId, eltId, attribute, newValue);
- break;
- default:
- for (int i = 0; i < attrSinks.size(); i++)
- attrSinks.get(i).graphAttributeAdded(sourceId, timeId, attribute, newValue);
- }
- break;
- case REMOVE:
- switch (eltType) {
- case NODE:
- for (int i = 0; i < attrSinks.size(); i++)
- attrSinks.get(i).nodeAttributeRemoved(sourceId, timeId, eltId, attribute);
- break;
- case EDGE:
- for (int i = 0; i < attrSinks.size(); i++)
- attrSinks.get(i).edgeAttributeRemoved(sourceId, timeId, eltId, attribute);
- break;
- default:
- for (int i = 0; i < attrSinks.size(); i++)
- attrSinks.get(i).graphAttributeRemoved(sourceId, timeId, attribute);
- }
- break;
- default:
- switch (eltType) {
- case NODE:
- for (int i = 0; i < attrSinks.size(); i++)
- attrSinks.get(i).nodeAttributeChanged(sourceId, timeId, eltId, attribute, oldValue, newValue);
- break;
- case EDGE:
- for (int i = 0; i < attrSinks.size(); i++)
- attrSinks.get(i).edgeAttributeChanged(sourceId, timeId, eltId, attribute, oldValue, newValue);
- break;
- default:
- for (int i = 0; i < attrSinks.size(); i++)
- attrSinks.get(i).graphAttributeChanged(sourceId, timeId, attribute, oldValue, newValue);
- }
- }
- }
- }
- class AddToListEvent<T> extends GraphEvent {
- List<T> l;
- T obj;
- AddToListEvent(List<T> l, T obj) {
- super(null, -1);
- this.l = l;
- this.obj = obj;
- }
- @Override
- void trigger() {
- l.add(obj);
- }
- }
- class RemoveFromListEvent<T> extends GraphEvent {
- List<T> l;
- T obj;
- RemoveFromListEvent(List<T> l, T obj) {
- super(null, -1);
- this.l = l;
- this.obj = obj;
- }
- @Override
- void trigger() {
- l.remove(obj);
- }
- }
- class ClearListEvent<T> extends GraphEvent {
- List<T> l;
- ClearListEvent(List<T> l) {
- super(null, -1);
- this.l = l;
- }
- @Override
- void trigger() {
- l.clear();
- }
- }
- /**
- * how many inner graphs were generated for the current graph
- */
- private int subGraphCounter = 1;
- /**
- * adds a new sub graph, it will be populated until InnerGraphFInished is
- * called
- */
- protected void newSubGraph() {
- if (subGraphCounter > 1) {
- removeSink(originalSink);
- multiGraph = true;
- }
- MyGraph g = new MyGraph(superID + "sub" + subGraphCounter);
- subGraphCounter++;
- addSubGraphSink(g);
- usedSubGraphs.push(g);
- }
- /**
- * adds the sub Graph to the List of subGraphs and removes it from the
- * sinkLists
- */
- protected void subGraphFinished() {
- MyGraph g = usedSubGraphs.pop();
- removeSink(g);
- subGraphs.add(g);
- }
- /**
- * this returns all finished subgraphs, but does not reset the internal
- * dataStructures. This can be called until a new Graph is added using
- * addSink().
- *
- * @return all subGraphs
- */
- public LinkedList<MyGraph> getSubGraphs() {
- return subGraphs;
- }
- /**
- * resets the internal datastructures used for the subgraphs. This is
- * automatically called with addGraph().
- */
- protected void resetSubGraphs() {
- usedSubGraphs.removeAllElements();
- subGraphCounter = 1;
- subGraphs.clear();
- }
- }
|