#ifndef _BIP_Engine_Connector_HPP_
#define _BIP_Engine_Connector_HPP_

// inherited classes
#include <ConnectorItf.hpp>
#include "ConnectorExportPort.hpp"
#include "QuotedPortReference.hpp"

class Connector : public ConnectorItf {
 public:
  // constructors
  Connector(const string &name);

  // destructor
  virtual ~Connector();

  // specific
  bool isTopLevel() const;
  const vector<Connector *> &subConnectors() const;
  const vector<Connector *> &allSubConnectors() const;
  void updatePortValues();
  const vector<InteractionValue *> &enabledInteractions() const;
  const vector<InteractionValue *> &maximalInteractions() const;
  const vector<InteractionValue *> &locallyMaximalInteractions() const;
  const vector<const Interaction *> &interactions() const;
  void release(const vector<Interaction *> &interactions) const;
  void release(const vector<InteractionValue *> &interactions) const;

  const Interaction &triggers() const;
  const Interaction &allPorts() const;

  const vector<Priority *> &dominatingPriorities() const;
  const vector<Priority *> &dominatedPriorities() const;
  
  void reset() const;
  void resetMaximal() const;

  void setUpdated(bool b) { mUpdated = b; }
  bool updated() const { return mUpdated; }

 protected:
  // protected setters for references
  void addPort(QuotedPortReference &port) {
    mPorts.push_back(&port);
    port.setHolder(*this);
    port.port().addConnectedConnector(*this);
  }

  void setExportedPort(ConnectorExportPort &exportedPort) {
    mExportedPort = &exportedPort;
    exportedPort.setHolder(*this);
  }

  // specific
  void enumerateInteractions(vector<const Interaction *> &allInteractions, vector<QuotedPortReference *> &partialInteraction, unsigned int nextPortIndex) const;
  void enumerateInteractionValues(vector<InteractionValue *> &allInteractions, const Interaction &interaction, vector<PortValue *> &partialValues, unsigned int nextPortIndex) const;
  void enumerateLocallyMaximalInteractionValues(vector<InteractionValue *> &allInteractions, vector<Port *> &ports, unsigned int mandatoryIndex) const;

  void initializeTriggers();
  void initializeAllPorts();

  bool resetEnabledInteractions() const { return mResetEnabledInteractions; }
  bool resetMaximalInteractions() const { return mResetMaximalInteractions; }
  bool resetLocallyMaximalInteractions() const { return mResetLocallyMaximalInteractions; }

  bool mInitializedDependentConnectors;
  vector<Connector *> mDependentConnectors;

  bool mInitializedDependentPriorities;
  vector<Priority *> mDependentPriorities;

  bool mUpdated;

  mutable Interaction *mTriggers;
  mutable Interaction *mAllPorts;

  mutable bool mInitializedInteractions;
  mutable vector<const Interaction *> mInteractions;

  mutable bool mResetEnabledInteractions;
  mutable vector<InteractionValue *> mEnabledInteractions;

  mutable bool mResetMaximalInteractions;
  mutable vector<InteractionValue *> mMaximalInteractions;

  mutable bool mResetLocallyMaximalInteractions;
  mutable vector<InteractionValue *> mLocallyMaximalInteractions;

  mutable bool mInitializedDominatingPriorities;
  mutable vector<Priority *> mDominatingPriorities;

  mutable bool mInitializedDominatedPriorities;
  mutable vector<Priority *> mDominatedPriorities;

  mutable bool mInitializedIsTopLevel;
  mutable bool mIsTopLevel;

  mutable bool mInitializedSubConnectors;
  mutable vector<Connector *> mSubConnectors;

  mutable bool mInitializedAllSubConnectors;
  mutable vector<Connector *> mAllSubConnectors;
};

#endif // _BIP_Engine_Connector_HPP_
