123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- namespace GFlow {
-
- public class SimpleNode : Object, Node
- {
- private List<Source> sources;
- private List<Sink> sinks;
-
- public string name { get; set; default="SimpleNode";}
-
- public bool deletable { get; set; default=true;}
-
- public bool resizable { get; set; default=true;}
- public SimpleNode() {
- base();
- this.sources = new List<Source>();
- this.sinks = new List<Sink>();
- }
-
- public void add_source(Source s) throws NodeError {
- if (s.node != null)
- throw new NodeError.DOCK_ALREADY_BOUND_TO_NODE("This Source is already bound");
- if (this.sources.index(s) != -1)
- throw new NodeError.ALREADY_HAS_DOCK("This node already has this source");
- sources.append(s);
- s.node = this;
- source_added (s);
- }
-
- public void add_sink (Sink s) throws NodeError {
- if (s.node != null)
- throw new NodeError.DOCK_ALREADY_BOUND_TO_NODE("This Sink is already bound" );
- if (this.sinks.index(s) != -1)
- throw new NodeError.ALREADY_HAS_DOCK("This node already has this sink");
- sinks.append(s);
- s.node = this;
- sink_added (s);
- }
-
- public void remove_source(Source s) throws NodeError {
- if (this.sources.index(s) == -1)
- throw new NodeError.NO_SUCH_DOCK("This node doesn't have this source");
- sources.remove(s);
- s.node = null;
- source_removed (s);
- }
-
- public void remove_sink(Sink s) throws NodeError {
- if (this.sinks.index(s) == -1)
- throw new NodeError.NO_SUCH_DOCK("This node doesn't have this sink");
- sinks.remove(s);
- s.node = null;
- sink_removed (s);
- }
-
- public bool has_sink(Sink s) {
- return this.sinks.index(s) != -1;
- }
-
- public bool has_source(Source s) {
- return this.sources.index(s) != -1;
- }
-
- public bool has_dock(Dock d) {
- if (d is Source)
- return this.has_source(d as Source);
- else
- return this.has_sink(d as Sink);
- }
-
- public Dock? get_dock (string name) {
- foreach (Sink s in this.sinks)
- if (s.name == name)
- return s;
- foreach (Source s in this.sources)
- if (s.name == name)
- return s;
- return null;
- }
-
- public unowned List<Source> get_sources() {
- return this.sources;
- }
-
- public unowned List<Sink> get_sinks() {
- return this.sinks;
- }
-
- public bool is_recursive_forward(Node from, bool initial=true) {
- if (!initial && this == from)
- return true;
- foreach (Source source in this.get_sources()) {
- foreach (Sink sink in source.sinks) {
- if (sink.node.is_recursive_forward(from, false))
- return true;
- }
- }
- return false;
- }
-
- public bool is_recursive_backward(Node from, bool initial=true) {
- if (!initial && this == from)
- return true;
- foreach (Sink sink in this.sinks) {
- foreach (Source source in sink.sources) {
- if (source.node.is_recursive_backward(from, false))
- return true;
- }
- }
- return false;
- }
-
- public List<Node> get_neighbors() {
- var result = new List<Node>();
- foreach (Source source in this.get_sources()) {
- foreach (Sink sink in source.sinks) {
- if (sink.node != null && result.index(sink.node) == -1)
- result.append(sink.node);
- }
- }
- foreach (Sink sink in this.get_sinks()) {
- foreach (Source source in sink.sources) {
- if (source.node != null && result.index(source.node) == -1)
- result.append(source.node);
- }
- }
- return result;
- }
-
- public bool is_neighbor(Node n) {
- return this.get_neighbors().index(n) != -1;
- }
-
- public void unlink_all() {
- foreach (Source s in this.sources) {
- try {
- s.unlink_all();
- } catch (GLib.Error e) {
- warning("Could not unlink source %s from node %s", s.name, this.name);
- }
- }
- foreach (Sink s in this.sinks) {
- try {
- s.unlink_all();
- } catch (GLib.Error e) {
- warning("Could not unlink sink %s from node %s", s.name, this.name);
- }
- }
- }
- }
- }
|