[Rsiena-commits] r236 - pkg/RSienaTest/src/network

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Aug 5 08:24:20 CEST 2013


Author: ortmann
Date: 2013-08-05 08:24:20 +0200 (Mon, 05 Aug 2013)
New Revision: 236

Added:
   pkg/RSienaTest/src/network/INetworkChangeListener.h
Modified:
   pkg/RSienaTest/src/network/Network.cpp
   pkg/RSienaTest/src/network/Network.h
   pkg/RSienaTest/src/network/OneModeNetwork.cpp
   pkg/RSienaTest/src/network/OneModeNetwork.h
Log:
add:
	(INetworkChangeListener)
changes:
	added functionality to add listeners to network
	isOneMode() is now const

Added: pkg/RSienaTest/src/network/INetworkChangeListener.h
===================================================================
--- pkg/RSienaTest/src/network/INetworkChangeListener.h	                        (rev 0)
+++ pkg/RSienaTest/src/network/INetworkChangeListener.h	2013-08-05 06:24:20 UTC (rev 236)
@@ -0,0 +1,39 @@
+/******************************************************************************
+ * SIENA: Simulation Investigation for Empirical Network Analysis
+ *
+ * Web: http://www.stats.ox.ac.uk/~snijders/siena/
+ *
+ * File: NetworkChangeListener.h
+ *
+ * Description: This module defines the interface INetworkChangeListener.
+ * Any class implementing this interface can be added to a network and gets
+ * informed once an edge is introduced/withdrawn from the network.
+ *****************************************************************************/
+
+#ifndef INETWORKCHANGELISTENER_H_
+#define INETWORKCHANGELISTENER_H_
+
+#include "Network.h"
+
+namespace siena {
+
+class INetworkChangeListener {
+public:
+	virtual ~INetworkChangeListener() {
+	}
+	virtual void onTieIntroductionEvent(const Network& rNetwork, const int ego,
+			const int alter) = 0;
+	virtual void onTieWithdrawalEvent(const Network& rNetwork, const int ego,
+			const int alter) = 0;
+	virtual void onNetworkClearEvent(const Network& rNetwork) = 0;
+protected:
+	INetworkChangeListener() {
+	}
+private:
+	// disable copy constructor and copy assignment
+	INetworkChangeListener(const INetworkChangeListener& rhs);
+	INetworkChangeListener& operator=(const INetworkChangeListener& rhs);
+};
+
+}
+#endif /* INETWORKCHANGELISTENER_H_ */

Modified: pkg/RSienaTest/src/network/Network.cpp
===================================================================
--- pkg/RSienaTest/src/network/Network.cpp	2013-08-02 09:29:40 UTC (rev 235)
+++ pkg/RSienaTest/src/network/Network.cpp	2013-08-05 06:24:20 UTC (rev 236)
@@ -10,15 +10,18 @@
  *****************************************************************************/
 
 #include "Network.h"
-#include "TieIterator.h"
-#include "IncidentTieIterator.h"
-#include "NetworkUtils.h"
-#include "../utils/Utils.h"
+
 #include <map>
 #include <stdexcept>
 #include <limits>
 #include <algorithm>
 
+#include "TieIterator.h"
+#include "IncidentTieIterator.h"
+#include "NetworkUtils.h"
+#include "INetworkChangeListener.h"
+#include "../utils/Utils.h"
+
 namespace siena {
 
 // ----------------------------------------------------------------------------
@@ -62,12 +65,12 @@
 
 	// Copy everything from rNetwork
 
-	for (int i = 0; i < this->ln; ++i) {
+	for (int i = 0; i < this->ln; i++) {
 		this->lpOutTies[i].insert(rNetwork.lpOutTies[i].begin(),
 				rNetwork.lpOutTies[i].end());
 	}
 
-	for (int i = 0; i < this->lm; ++i) {
+	for (int i = 0; i < this->lm; i++) {
 		this->lpInTies[i].insert(rNetwork.lpInTies[i].begin(),
 				rNetwork.lpInTies[i].end());
 	}
@@ -83,11 +86,11 @@
 	if (this != &rNetwork) {
 		// Empty the current network structure.
 
-		for (int i = 0; i < this->ln; ++i) {
+		for (int i = 0; i < this->ln; i++) {
 			this->lpOutTies[i].clear();
 		}
 
-		for (int i = 0; i < this->lm; ++i) {
+		for (int i = 0; i < this->lm; i++) {
 			this->lpInTies[i].clear();
 		}
 
@@ -103,18 +106,18 @@
 
 		// Copy everything from rNetwork
 
-		for (int i = 0; i < this->ln; ++i) {
+		for (int i = 0; i < this->ln; i++) {
 			this->lpOutTies[i].insert(rNetwork.lpOutTies[i].begin(),
 					rNetwork.lpOutTies[i].end());
 		}
 
-		for (int i = 0; i < this->lm; ++i) {
+		for (int i = 0; i < this->lm; i++) {
 			this->lpInTies[i].insert(rNetwork.lpInTies[i].begin(),
 					rNetwork.lpInTies[i].end());
 		}
 
 		this->ltieCount = rNetwork.ltieCount;
-		++this->lmodificationCount;
+		this->lmodificationCount++;
 	}
 
 	return *this;
@@ -248,7 +251,7 @@
 		lpInTies[j].insert(std::map<int, int>::value_type(i, v));
 	}
 	// Remember that the network has changed
-	++this->lmodificationCount;
+	this->lmodificationCount++;
 
 	// Act on tie withdrawal or introduction.
 	if (oldValue && !v) {
@@ -266,7 +269,9 @@
  * from actor <i>i</i> to actor <i>j</i>.
  */
 void Network::onTieWithdrawal(int i, int j) {
-	--this->ltieCount;
+	this->ltieCount--;
+	// fire the withdrawal event
+	fireWithdrawalEvent(i, j);
 }
 
 /**
@@ -274,7 +279,9 @@
  * from actor <i>i</i> to actor <i>j</i>.
  */
 void Network::onTieIntroduction(int i, int j) {
-	++this->ltieCount;
+	this->ltieCount++;
+	// fire the introduction even
+	fireIntroductionEvent(i, j);
 }
 
 /**
@@ -304,11 +311,11 @@
 void Network::clear() {
 	// Clear the maps and reset the various degree counters.
 
-	for (int i = 0; i < this->ln; ++i) {
+	for (int i = 0; i < this->ln; i++) {
 		this->lpOutTies[i].clear();
 	}
 
-	for (int i = 0; i < this->lm; ++i) {
+	for (int i = 0; i < this->lm; i++) {
 		this->lpInTies[i].clear();
 	}
 
@@ -316,7 +323,10 @@
 	this->ltieCount = 0;
 
 	// The network has changed
-	++this->lmodificationCount;
+	this->lmodificationCount++;
+
+	// fire network clear event
+	fireNetworkClearEvent();
 }
 
 /**
@@ -533,4 +543,67 @@
 						+ ") of actors acting as receivers of ties");
 	}
 }
+
+/**
+ * Adds the given <i>listener</i> from the network, if it is not yet attached.
+ */
+void siena::Network::addNetworkChangeListener(
+		INetworkChangeListener* const listener) {
+	// ensure that the list is a set (no duplicates)
+	std::list<INetworkChangeListener*>::iterator tmp = std::find(
+			lNetworkChangeListener.begin(), lNetworkChangeListener.end(),
+			listener);
+	if (tmp == lNetworkChangeListener.end()) {
+		lNetworkChangeListener.push_back(listener);
+	}
 }
+
+/**
+ * Removes the given <i>listener</i> from the network.
+ */
+void siena::Network::removeNetworkChangeListener(
+		INetworkChangeListener* const listener) {
+	std::list<INetworkChangeListener*>::iterator tmp = std::find(
+			lNetworkChangeListener.begin(), lNetworkChangeListener.end(),
+			listener);
+	if (tmp != lNetworkChangeListener.end()) {
+		// no need for erase remove since add ensures that the element is unique
+		lNetworkChangeListener.erase(tmp);
+	}
+}
+
+void Network::fireNetworkClearEvent() const {
+	for (std::list<INetworkChangeListener*>::const_iterator iter =
+			lNetworkChangeListener.begin();
+			iter != lNetworkChangeListener.end(); ++iter) {
+		(*iter)->onNetworkClearEvent(*this);
+	}
+}
+
+/**
+ * Inform all listeners that edge (ego,alter) has been inserted to the network.
+ */
+void Network::fireIntroductionEvent(int ego, int alter) const {
+	for (std::list<INetworkChangeListener*>::const_iterator iter =
+			lNetworkChangeListener.begin();
+			iter != lNetworkChangeListener.end(); ++iter) {
+		(*iter)->onTieIntroductionEvent(*this, ego, alter);
+	}
+}
+
+bool Network::isOneMode() const {
+	return false;
+}
+
+/**
+ * Inform all listeners that edge (ego,alter) has been removed to the network.
+ */
+void Network::fireWithdrawalEvent(int ego, int alter) const {
+	for (std::list<INetworkChangeListener*>::const_iterator iter =
+			lNetworkChangeListener.begin();
+			iter != lNetworkChangeListener.end(); ++iter) {
+		(*iter)->onTieWithdrawalEvent(*this, ego, alter);
+	}
+}
+
+}

Modified: pkg/RSienaTest/src/network/Network.h
===================================================================
--- pkg/RSienaTest/src/network/Network.h	2013-08-02 09:29:40 UTC (rev 235)
+++ pkg/RSienaTest/src/network/Network.h	2013-08-05 06:24:20 UTC (rev 236)
@@ -13,6 +13,7 @@
 #define NETWORK_H_
 
 #include <map>
+#include <list>
 
 namespace siena {
 
@@ -22,6 +23,7 @@
 
 class TieIterator;
 class IncidentTieIterator;
+class INetworkChangeListener;
 
 // ----------------------------------------------------------------------------
 // Section: Enums
@@ -75,11 +77,15 @@
 
 	bool complete() const;
 	bool hasEdge(int ego, int alter) const;
-	virtual bool isOneMode();
+	virtual bool isOneMode() const;
 
 	int outTwoStarCount(int i, int j) const;
 	int inTwoStarCount(int i, int j) const;
 
+	void addNetworkChangeListener(INetworkChangeListener* const listener);
+
+	void removeNetworkChangeListener(INetworkChangeListener* const listener);
+
 	inline int modificationCount() const;
 
 protected:
@@ -93,6 +99,9 @@
 private:
 	void allocateArrays();
 	void deleteArrays();
+	void fireNetworkClearEvent() const;
+	void fireIntroductionEvent(int ego, int alter) const;
+	void fireWithdrawalEvent(int ego, int alter) const;
 
 	// The number of senders
 	int ln;
@@ -117,6 +126,9 @@
 	// is changed.
 
 	int lmodificationCount;
+
+	// set of network change listener
+	std::list<INetworkChangeListener*> lNetworkChangeListener;
 };
 
 // ----------------------------------------------------------------------------

Modified: pkg/RSienaTest/src/network/OneModeNetwork.cpp
===================================================================
--- pkg/RSienaTest/src/network/OneModeNetwork.cpp	2013-08-02 09:29:40 UTC (rev 235)
+++ pkg/RSienaTest/src/network/OneModeNetwork.cpp	2013-08-05 06:24:20 UTC (rev 236)
@@ -442,7 +442,7 @@
 	}
 }
 
-bool OneModeNetwork::isOneMode() {
+bool OneModeNetwork::isOneMode() const {
 	return true;
 }
 

Modified: pkg/RSienaTest/src/network/OneModeNetwork.h
===================================================================
--- pkg/RSienaTest/src/network/OneModeNetwork.h	2013-08-02 09:29:40 UTC (rev 235)
+++ pkg/RSienaTest/src/network/OneModeNetwork.h	2013-08-05 06:24:20 UTC (rev 236)
@@ -45,7 +45,7 @@
 	int reciprocalDegree(int i) const;
 	bool symmetric() const;
 	virtual void clear();
-	virtual bool isOneMode();
+	virtual bool isOneMode() const;
 
 	CommonNeighborIterator reciprocatedTies(int i) const;
 	CommonNeighborIterator reciprocatedTies(int i,



More information about the Rsiena-commits mailing list