001package org.biopax.paxtools.query.model;
002
003import java.util.Collection;
004import java.util.Collections;
005import java.util.HashSet;
006import java.util.Set;
007
008/**
009 * @author Ozgun Babur
010 */
011public abstract class AbstractNode implements Node
012{
013        /**
014         * Owner graph.
015         */
016        protected Graph graph;
017
018        /**
019         * Flag to remember if upstream links were created.
020         */
021        protected boolean upstreamInited;
022
023        /**
024         * Flag to remember if downstream links were created.
025         */
026        protected boolean downstreamInited;
027
028        /**
029         * If there are equivalent nodes in the graph, and they have a hierarchy, like an homology node
030         * and members, then this set is for the parent equivalents of this node.
031         */
032        protected Set<Node> upperEquivalent;
033
034        /**
035         * If there are equivalent nodes in the graph, and they have a hierarchy, like an homology node
036         * and members, then this set is for the child equivalents of this node.
037         */
038        protected Set<Node> lowerEquivalent;
039
040        /**
041         * Set of upstream edges.
042         */
043        protected Set<Edge> upstream;
044
045        /**
046         * Set of downstream edges.
047         */
048        protected Set<Edge> downstream;
049
050        /**
051         * This variable can be used by algorithms that need to label nodes with a path sign
052         * (typically the current path).
053         */
054        protected int pathSign;
055
056        /**
057         * For saying: "If the algorithm traverses this node, it cannot traverse those others". If this
058         * set will be used, then initBanned() should be called. Otherwise getBanned() will return an
059         * immutable empty set.
060         */
061        protected Set<Node> banned;
062
063        /**
064         * Constructor with the owner graph.
065         * @param graph Owner graph
066         */
067        protected AbstractNode(Graph graph)
068        {
069                this.graph = graph;
070                this.upstream = new HashSet<Edge>();
071                this.downstream = new HashSet<Edge>();
072                this.upstreamInited = false;
073                this.downstreamInited = false;
074        }
075
076        /**
077         * @return The owner graph
078         */
079        public Graph getGraph()
080        {
081                return graph;
082        }
083
084        /**
085         * @return Set of banned-to-traverse nodes if this node is traversed.
086         */
087        public Set<Node> getBanned()
088        {
089                if (banned == null) return Collections.emptySet();
090                return banned;
091        }
092
093        /**
094         * Initializes the set of banned nodes. If the algorithm will use this set, then this method
095         * or setBanned method should be called.
096         */
097        public void initBanned()
098        {
099                if (banned == null) banned = new HashSet<Node>();
100        }
101
102        /**
103         * @param banned Set of banned nodes
104         */
105        public void setBanned(Set<Node> banned)
106        {
107                this.banned = banned;
108        }
109
110        /**
111         * Gets the upstream edges. Initializes if necessary.
112         * @return upstream edges
113         */
114        public Collection<Edge> getUpstream()
115        {
116                if (!upstreamInited)
117                {
118                        initUpstream();
119                        upstreamInited = true;
120                }
121                return upstream;
122        }
123
124        /**
125         * Gets the downstream edges. Initializes if necessary.
126         * @return downstream edges
127         */
128        public Collection<Edge> getDownstream()
129        {
130                if (!downstreamInited)
131                {
132                        initDownstream();
133                        downstreamInited = true;
134                }
135                return downstream;
136        }
137
138        // These two methods should be overriden if any upstream and downstream initing is required.
139
140        /**
141         * Initializes the upstream connections.
142         */
143        public void initUpstream(){}
144
145        /**
146         * Initializes the downstream connections.
147         */
148        public void initDownstream(){}
149
150        /**
151         * This class gets the upstream links but does not initialize.
152         * @return Upstream links
153         */
154        public Collection<Edge> getUpstreamNoInit()
155        {
156                return upstream;
157        }
158
159        /**
160         * This class gets the downstream links but does not initialize.
161         * @return Downstream links
162         */
163        public Collection<Edge> getDownstreamNoInit()
164        {
165                return downstream;
166        }
167
168        /**
169         * @return Parent equivalent nodes.
170         */
171        public Collection<Node> getUpperEquivalent()
172        {
173                return upperEquivalent;
174        }
175
176        /**
177         * @return Child equivalent nodes
178         */
179        public Collection<Node> getLowerEquivalent()
180        {
181                return lowerEquivalent;
182        }
183
184        /**
185         * Does nothing yet.
186         */
187        public void init()
188        {
189        }
190
191        /**
192         * @return path sign
193         */
194        public int getPathSign()
195        {
196                return pathSign;
197        }
198
199        /**
200         * @param pathSign The path sign
201         */
202        public void setPathSign(int pathSign)
203        {
204                this.pathSign = pathSign;
205        }
206
207        /**
208         * Resets the path sign.
209         */
210        public void clear()
211        {
212                this.pathSign = 0;
213        }
214
215        /**
216         * Nodes are not transcription by default.
217         * @return False
218         */
219        @Override
220        public boolean isTranscription()
221        {
222                return false;
223        }
224}