/*!
  @header FTNodeImpl
  @abstract Module of FT
 
  @availability OS X, GNUstep
  @copyright 2004, 2005, 2006 Free Software Foundation, Inc.
  
  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library 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 library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA

  <pre>
  -------------------------------------------------------------------------
  Modification history

  31.03.05 ola     initial version
  11.08.05 ola     simpler architecture through usage of FTOrderEdgeSet
  16.08.06 ola     incoming and outgoing edges added
  23.08.06 ola     license changed
  -------------------------------------------------------------------------
  </pre>
*/

#if !defined(__FTNodeImpl_H)
#define __FTNodeImpl_H

#include <Foundation/Foundation.h>
#include <FT/FTObject.h>
#include <FT/FTId.h>
#include <FT/FTNode.h>
#include <FT/FTEdge.h>
#include <FT/FTIdImpl.h>
#include <FT/FTEdgeImpl.h>
#include <FT/FTOrderedReferenceSet.h>

@class FTGraphImpl;

/*!
 * @class FTNodeImpl
 * @abstract Implementation of the protocol {@link FTNode}
 * @discussion At present the handling of relations from one node
 *   to another is kept simple: Each node contains the underlying references 
 *   to nodes to which it links (outgoing nodes) and which point to
 *   it (incoming nodes). This (internal) representation may change in
 *   future since it may not be efficient enough wrt. to searching paths
 */
@interface FTNodeImpl : FTObject <FTNode,NSCoding> {
  @private 
    /** 
     * Reference to the graph this node belongs to
     */
    FTGraphImpl *graph;
    
    
    /**
     * Id of this node
     */
    id <FTId> nodeId;
    
    NSLock *lock;
        
    /**
     * Set of all outgoing references
     */
    id <FTOrderedReferenceSet> outgoingReferences;
    
    
    /**
     * Set of all incoming references
     */
    id <FTOrderedReferenceSet> incomingReferences;
}


/*!
 * @method init
 * @result self
 */
- init;


/*!
 * @method initWithCode
 * @abstract initializer called by deserialization process
 * @param decoder to use
 * @result self
 */
- (id) initWithCoder:(NSCoder *) decoder;


/*!
 * @method initWithNodeId
 * @abstract initializes this instance
 * @param anId id for this node
 * @param aGraph graph to which this node belongs to 
 * @result self
 */
- initWithNodeId: (id <FTId>) anId forGraph: (FTGraphImpl *) aGraph;
- (void) dealloc;


/*!
 * @method addOutgoingReferenceToNodeId
 * @abstract add a reference to the specified node
 * @param aSourceNodeId id of source node
 * @param anEdgeId id of underlying edge
 * @throws ECIllegalArgumentException if the given reference already exists in
 *   this set.
 * @result self
 */
- addIncomingReferenceFromNodeId: (id <FTId>) aSourceNodeId 
  withEdgeId: (id <FTId>) anEdgeId;


/*!
 * @method addOutgoingReferenceToNodeId
 * @abstract add a reference to the specified node
 * @param aTargetNodeId id of target node
 * @param anEdgeId id of underlying edge
 * @throws ECIllegalArgumentException if the given reference already exists in
 *   this set.
 * @result self
 */
- addOutgoingReferenceToNodeId: (id <FTId>) aTargetNodeId 
  withEdgeId: (id <FTId>) anEdgeId;


/*!
 * @method countIncomingReferences
 * @result returns the number of incoming edges
 */
- (unsigned) countIncomingReferences;


/*!
 * @method countOutgoingReferences
 * @result returns the number of outgoing edges
 */

- (unsigned) countOutgoingReferences;


/*!
 * @method createAndAppendEdgeWithId
 * @abstract creates an edge which refers to the given node
 * @param edgeId id of the edge
 * @param targetNode node to which the edge refers
 * @result created edge instance
 */
- (id <FTEdge>) createAndAppendEdgeWithId: (id <FTId>) edgeId
  withTargetNode: (id <FTNode>) targetNode;


/*!
 * @method description
 * @result information about the state of this object
 */
- (NSString *) description;


/*!
 * @method encodeWithCoder
 * @abstract Is being called in order to serialize this instance
 * @param encoder object to be used for serialization
 */
- (void) encodeWithCoder: (NSCoder *) encoder;


/*!
 * @method incomingEdges
 * @abstract Get all edges pointing to self
 * @result iterator of instances of type FTEdge or an empty iterator
 */
- (id <ECIterator>) incomingEdges;


/*! 
 * @method incomingNodes
 * @abstract access to all nodes refering the receiver of this message
 * @result iterator over instances which implement the protocol FTNode
 */
- (id <ECIterator>) incomingNodes;


/*!
 * @method internalStateChanged
 * @abstract Informs graph instance to update the persistent data
 *   representing this node
 * @result self
 */
- internalStateChanged;


/*!
 * @method nodeId
 * @result return the id of this node.
 */
- (id <FTId>) nodeId;


/*!
 * @method outgoingEdges
 * @abstract Get all outgoing edges
 * @result iterator of instances of type FTEdge or an empty iterator
 */
- (id <ECIterator>) outgoingEdges;


/*! 
 * @method outgoingNodes
 * @abstract access to all nodes referred by the receiver of this message
 * @result iterator over instances which implement the protocol FTNode
 */
- (id <ECIterator>) outgoingNodes;


/*!
 * @method referencesToNodeIterator
 * @abstract returns an iterator over all targets nodes of the references
 *   which are contained in the given reference set
 * @param references containing all references to examine
 * @result iterator over all targets nodes of the references
 */
- (id <ECIterator>) referencesToNodeIterator: 
  (id <FTOrderedReferenceSet>) references;
  
  
/*!
 * @method removeIncomingReferencePointingFrom
 * @abstract removes the specified reference from the set of incoming
 *   references
 * @param sourceNode node pointing to the receiver
 * @param edgeId id for the underlying edge
 * @result self
 */
- removeIncomingReferencePointingFrom: (FTNodeImpl *) sourceNode 
  withEdgeId: (id <FTId>) edgeId;
  
  
/*!
 * @method removeOutgoingReferencePointingTo
 * @abstract removes the specified reference from the set of outgoing
 *   references
 * @param targetNode node pointing the receiver is pointing to
 * @param edgeId id for the underlying edge
 * @result self
 */
- removeIncomingReferencePointingFrom: (FTNodeImpl *) targetNode
  withEdgeId: (id <FTId>) edgeId;
  
/*!
 * @method removeOutgoingNodeWithId
 * @abstract removes all outgoing nodes with the specified id
 * @param nodeId identifier for the node
 * @result self
 * @throws ECIllegalArgumentException if the node with the given id could
 *   not be found
 */
- removeAllOutgoingNodesWithId: (id <FTId>) aNodeId;


/*!
 * @method removeFromReferenceSet
 * @abstract removes the specified reference from the set
 * @param references set to be modified
 * @param aNode node of the reference to be removed
 * @param anEdgeId edgeId of the reference to be removed
 * @result self
 */
- removeFromReferenceSet: (id <FTOrderedReferenceSet>) references
  node: (FTNodeImpl *) aNode withEdgeId: (id <FTId>) anEdgeId;
  

/*!
 * @method serviceWithId
 * @abstract retrieve the specified service
 * @param aServiceId identifier for the service
 * @result related service or nil, if not found or version cannot be found
 */
- (id <FTService>) serviceWithId: (NSString *) aServiceId;
  
  
/*!
 * @method writeLock
 * @abstract Set / unset write lock
 * @param enable YES enables the lock
 * @result self
 */
- writeLock: (BOOL) enable;
@end
#endif
