From 35f120922d072d3c7ee34ebff9756aee2913c7a0 Mon Sep 17 00:00:00 2001 From: guille <guillermopolito@gmail.com> Date: Thu, 27 Oct 2016 17:36:43 +0200 Subject: [PATCH] Moved basic event implementation to support --- .../emeraude/n2s3/core/InputLayer.scala | 7 +- .../cristal/emeraude/n2s3/core/Neuron.scala | 9 +- .../emeraude/n2s3/core/Synchronizer.scala | 9 +- .../cristal/emeraude/n2s3/core/Unit.scala | 6 +- .../emeraude/n2s3/core/event/Event.scala | 22 --- .../n2s3/core/event/EventHolder.scala | 133 ---------------- .../n2s3/core/event/EventHolderMessage.scala | 8 - .../n2s3/core/event/EventResponse.scala | 11 -- .../n2s3/core/event/EventTriggered.scala | 6 - .../n2s3/core/event/InputRestartEvent.scala | 6 - .../core/event/InputRestartResponse.scala | 8 - .../n2s3/core/event/LabelChangeEvent.scala | 4 +- .../n2s3/core/event/LabelChangeResponse.scala | 1 + .../n2s3/core/event/NeuronFireEvent.scala | 2 + .../n2s3/core/event/NeuronFireResponse.scala | 1 + .../n2s3/core/event/NeuronUpdateEvent.scala | 2 + .../emeraude/n2s3/core/event/Subscribe.scala | 6 - .../core/event/SubscribeSynchronized.scala | 6 - .../n2s3/core/event/SynapseUpdateEvent.scala | 2 + .../n2s3/core/event/SynchronizedEvent.scala | 11 -- .../emeraude/n2s3/core/event/TimedEvent.scala | 6 - .../n2s3/core/event/TimedEventResponse.scala | 10 -- .../n2s3/core/event/Unsubscribe.scala | 6 - .../exceptions/EventNotFoundException.scala | 2 +- .../features/io/report/BenchmarkMonitor.scala | 1 + .../io/report/BenchmarkMonitorRef.scala | 9 +- .../features/learning/BackPropagation.scala | 3 +- .../n2s3/features/learning/SpikeCounter.scala | 3 +- .../learning/SpikingSquareError.scala | 3 +- .../n2s3/features/learning/SquareError.scala | 13 +- .../learning/SupervisedSTDPLearning.scala | 3 +- .../n2s3/features/logging/LabelMonitor.scala | 3 +- .../n2s3/features/logging/NeuronLog.scala | 5 +- .../n2s3/features/logging/SpikeMonitor.scala | 9 +- .../n2s3/features/logging/SynapseLog.scala | 10 +- .../emeraude/n2s3/models/qbg/Event.scala | 2 +- .../emeraude/n2s3/support/event/Event.scala | 31 ++++ .../n2s3/support/event/Observable.scala | 149 ++++++++++++++++++ .../emeraude/n2s3/apps/ExampleFreeWay.scala | 3 +- .../emeraude/n2s3/apps/bio/Masquelier.scala | 3 +- .../n2s3/apps/bio/SmallReservoir.scala | 19 ++- .../n2s3/apps/ternary/BitStream.scala | 3 +- .../n2s3/apps/ternary/PatternStart.scala | 3 +- 43 files changed, 260 insertions(+), 299 deletions(-) delete mode 100644 n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/Event.scala delete mode 100644 n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/EventHolder.scala delete mode 100644 n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/EventHolderMessage.scala delete mode 100644 n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/EventResponse.scala delete mode 100644 n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/EventTriggered.scala delete mode 100644 n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/InputRestartEvent.scala delete mode 100644 n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/InputRestartResponse.scala delete mode 100644 n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/Subscribe.scala delete mode 100644 n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/SubscribeSynchronized.scala delete mode 100644 n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/SynchronizedEvent.scala delete mode 100644 n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/TimedEvent.scala delete mode 100644 n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/TimedEventResponse.scala delete mode 100644 n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/Unsubscribe.scala create mode 100644 n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/support/event/Event.scala create mode 100644 n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/support/event/Observable.scala diff --git a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/InputLayer.scala b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/InputLayer.scala index 3f78b399..195d015c 100644 --- a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/InputLayer.scala +++ b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/InputLayer.scala @@ -4,9 +4,10 @@ import fr.univ_lille.cristal.emeraude.n2s3.core.GlobalTypesAlias.Timestamp import fr.univ_lille.cristal.emeraude.n2s3.core.Neuron.NeuronMessage import fr.univ_lille.cristal.emeraude.n2s3.core.Synchronizer._ import fr.univ_lille.cristal.emeraude.n2s3.core.actors.WrapMessage -import fr.univ_lille.cristal.emeraude.n2s3.core.event.{EventHolder, EventHolderMessage, LabelChangeEvent, LabelChangeResponse} +import fr.univ_lille.cristal.emeraude.n2s3.core.event.{LabelChangeEvent, LabelChangeResponse} import fr.univ_lille.cristal.emeraude.n2s3.features.io.input.StreamSupport import fr.univ_lille.cristal.emeraude.n2s3.support.actors.Message +import fr.univ_lille.cristal.emeraude.n2s3.support.event.{Observable, ObservableMessage} import fr.univ_lille.cristal.emeraude.n2s3.support.io._ import scala.annotation.tailrec @@ -19,7 +20,7 @@ object AskRemainInput extends Message /** * Specialization of the NetworkContainer for the input layer */ -class InputLayer(stream: StreamSupport[_, InputSeq[N2S3Input]]) extends NetworkContainer with EventHolder { +class InputLayer(stream: StreamSupport[_, InputSeq[N2S3Input]]) extends NetworkContainer with Observable { /******************************************************************************************************************** * Declaration of events @@ -66,7 +67,7 @@ class InputLayer(stream: StreamSupport[_, InputSeq[N2S3Input]]) extends NetworkC } case AskRemainInput => sender.send(WrapMessage(!stream.atEnd())) - case m: EventHolderMessage => + case m: ObservableMessage => processEventHolderMessage(m) case _ => super.receiveMessage(message, sender) } diff --git a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/Neuron.scala b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/Neuron.scala index c5f68456..5c6924f5 100644 --- a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/Neuron.scala +++ b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/Neuron.scala @@ -9,8 +9,9 @@ import fr.univ_lille.cristal.emeraude.n2s3.core.NetworkEntity.AskReference import fr.univ_lille.cristal.emeraude.n2s3.core.NeuronConnection.ConnectionEnds import fr.univ_lille.cristal.emeraude.n2s3.core.Synchronizer.{Done, SynchronizedMessage} import fr.univ_lille.cristal.emeraude.n2s3.core.actors.WrapMessage -import fr.univ_lille.cristal.emeraude.n2s3.core.event.{EventHolder, EventHolderMessage, NeuronFireEvent} +import fr.univ_lille.cristal.emeraude.n2s3.core.event.NeuronFireEvent import fr.univ_lille.cristal.emeraude.n2s3.support.actors.Message +import fr.univ_lille.cristal.emeraude.n2s3.support.event.{Observable, ObservableMessage} import scala.collection.mutable import scala.language.postfixOps @@ -173,7 +174,7 @@ trait NeuronModel { * The connection object is always stored in the output neuron. * All connection are managed by a synchronizer */ -trait Neuron extends NetworkEntity with EventHolder with PropertyHolder with Serializable { +trait Neuron extends NetworkEntity with Observable with PropertyHolder with Serializable { import Neuron._ @@ -320,7 +321,7 @@ trait Neuron extends NetworkEntity with EventHolder with PropertyHolder with Ser * *****************************************************************************************************************/ /** - * Redirect EventHolderMessage and PropertyMessage to their process method + * Redirect ObservableMessage and PropertyMessage to their process method * Handle CreateNeuronConnectionWith and CreateNeuronInputConnectionWith messages for the connection creation * manage NeuronMessage by redirect content to the processSomaMessage method * @@ -328,7 +329,7 @@ trait Neuron extends NetworkEntity with EventHolder with PropertyHolder with Ser * @param sender is a reference of the sender, which can be used to send response */ def receiveMessage(message : Message, sender : NetworkEntityReference) : Unit = message match { - case m : EventHolderMessage => processEventHolderMessage(m) + case m : ObservableMessage => processEventHolderMessage(m) case m : PropertyMessage => processPropertyMessage(m, sender) /******************************************************************************************************************** diff --git a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/Synchronizer.scala b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/Synchronizer.scala index dfdac32f..81cb3777 100644 --- a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/Synchronizer.scala +++ b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/Synchronizer.scala @@ -4,12 +4,11 @@ ***********************************************************************************************************/ package fr.univ_lille.cristal.emeraude.n2s3.core -import akka.actor.{Identify, Props} -import fr.univ_lille.cristal.emeraude.n2s3.core +import akka.actor.Props import fr.univ_lille.cristal.emeraude.n2s3.core.GlobalTypesAlias._ import fr.univ_lille.cristal.emeraude.n2s3.core.actors._ -import fr.univ_lille.cristal.emeraude.n2s3.core.event.{Event, EventHolder, EventTriggered} import fr.univ_lille.cristal.emeraude.n2s3.support.actors.{ActorCompanion, Message, PropsBuilder, UnhandledMessageException} +import fr.univ_lille.cristal.emeraude.n2s3.support.event.{Event, EventTriggered, Observable} /** * Synchronizer companion object @@ -102,7 +101,7 @@ object Synchronizer { * Furthermore, synchronizer is responsible to ask input for new events when the number of remain messages * falls below a threshold */ -class Synchronizer extends NetworkEntity with EventHolder { +class Synchronizer extends NetworkEntity with Observable { import Synchronizer._ @@ -265,7 +264,7 @@ class Synchronizer extends NetworkEntity with EventHolder { case WaitEndOfActivity => if (this.isActive){ sender.disableAutoResponse() - subscribeTo(sender, WaitComputationEvent) } + addSubscription(sender, WaitComputationEvent) } else { sender.send(WaitComputationEvent.defaultResponse) } diff --git a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/Unit.scala b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/Unit.scala index 0b42aeca..522c225f 100644 --- a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/Unit.scala +++ b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/Unit.scala @@ -41,9 +41,9 @@ case class Time(timestamp : Timestamp) { } class TimeUnit(val value : Double) { - def Second = new Time((value*1e6).toInt) - def MilliSecond = new Time((value*1e3).toInt) - def MicroSecond = new Time(value.toInt) + def Second = Time((value*1e6).toInt) + def MilliSecond = Time((value*1e3).toInt) + def MicroSecond = Time(value.toInt) } object UnitCast { diff --git a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/Event.scala b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/Event.scala deleted file mode 100644 index 389d592b..00000000 --- a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/Event.scala +++ /dev/null @@ -1,22 +0,0 @@ -package fr.univ_lille.cristal.emeraude.n2s3.core.event - -/** - * Basic class for all events. - * - * @tparam Response is the type of the Response of the event - */ -abstract class Event[Response <: EventResponse] extends Serializable { - - /** - * When this parameter return true, all event observers will be removed after that event is trigger - */ - def isSingleUsage : Boolean = false - - /** - * Return a default response if none is given to the trigger method. - * - * @throws UnsupportedOperationException by default if subclasses don't override this method - */ - def defaultResponse : Response = throw new UnsupportedOperationException - -} diff --git a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/EventHolder.scala b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/EventHolder.scala deleted file mode 100644 index 21a1227d..00000000 --- a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/EventHolder.scala +++ /dev/null @@ -1,133 +0,0 @@ -package fr.univ_lille.cristal.emeraude.n2s3.core.event - -import fr.univ_lille.cristal.emeraude.n2s3.core.{NetworkEntityPath, NetworkEntityReference} -import fr.univ_lille.cristal.emeraude.n2s3.core.exceptions.EventNotFoundException - -import scala.collection.mutable -import scala.collection.mutable.ArrayBuffer - -/** - * Class which have a behavior close of Observable design pattern - * Output entities can subscribe to available events of the subclass - */ -trait EventHolder { - - /** - * Contains the list of declared event, associated with all observers - */ - private val events = mutable.HashMap[Event[_ <: EventResponse], mutable.ArrayBuffer[NetworkEntityReference]]() - private val synchronizedEvents = mutable.HashMap[TimedEvent[_ <: TimedEventResponse], mutable.ArrayBuffer[(NetworkEntityReference, NetworkEntityPath, NetworkEntityReference)]]() - - /** - * This method allow to declare an event which can be handled - * - * @param event is the type of the event - */ - def addEvent(event : Event[_ <: EventResponse]): Unit = { - events += event -> ArrayBuffer[NetworkEntityReference]() - - if(event.isInstanceOf[TimedEvent[_]]) - synchronizedEvents += event.asInstanceOf[TimedEvent[TimedEventResponse]] -> ArrayBuffer[(NetworkEntityReference, NetworkEntityPath, NetworkEntityReference)]() - } - - /** - * Subscribe an observer to the specified event - * - * @param entity is the entity which will receive response when event is triggered - * @param event is the event to be observed - * @throws EventNotFoundException is the event is not declared in this class - */ - def subscribeTo(entity : NetworkEntityReference, event : Event[_ <: EventResponse]) : Unit = { - events.get(event) match { - case Some(list) => list += entity - case None => throw new EventNotFoundException(event, this.getClass) - } - } - - /** - * Subscribe an observer to the specified event. Triggered message will pass through the synchronizer - * - * @param entity is the entity which will receive response when event is triggered - * @param event is the event to be observed - * @throws EventNotFoundException is the event is not declared in this class - */ - def subscribeSynchronizedTo(entity : NetworkEntityReference, event : TimedEvent[_ <: TimedEventResponse], synchronizer : NetworkEntityPath) : Unit = { - synchronizedEvents.get(event) match { - case Some(list) => list += ((referenceToSynchronizer(synchronizer), synchronizer, entity)) - case None => throw new EventNotFoundException(event, this.getClass) - } - } - - /** - * Unsubscribe an observer to the specified event - * - * @param entity is the entity which will receive response when event is triggered - * @param event is the event to be observed - * @throws EventNotFoundException is the event is not declared in this class - */ - def unsubscribeTo(entity : NetworkEntityReference, event : Event[_ <: EventResponse]) : Unit = { - events.get(event) match { - case Some(list) => list -= entity - case None => throw new EventNotFoundException(event, this.getClass) - } - events.getOrElseUpdate(event, ArrayBuffer[NetworkEntityReference]()) - } - - /** - * Trigger an event by sent default response of this event to all observers. - * if this event is single usage, all observers will be removed - * - * @param event is the event to trigger - * @tparam Response is the response type of the event - */ - def triggerEvent[Response <: EventResponse](event : Event[Response]) : Unit = { - triggerEventWith(event, event.defaultResponse) - } - - /** - * Trigger an event by sent specified response to all observers. - * if this event is single usage, all observers will be removed - * - * @param event is the event to trigger - * @param response is the response which will be sent to observers - * @tparam Response is the response type of the event - */ - def triggerEventWith[Response <: EventResponse](event : Event[Response], response : Response) : Unit = { - events.get(event) match { - case Some(subscribers) => - subscribers.foreach(subscriber => subscriber.send(response)) - if(event.isSingleUsage) - subscribers.clear() - case None => - } - - response match { - case message : TimedEventResponse => - synchronizedEvents.get(event.asInstanceOf[TimedEvent[TimedEventResponse]]) match { - case Some(subscribers) => - subscribers.foreach{ case (synchronizer, path, subscriber) => synchronizer.send(SynchronizedEvent(path, subscriber, message, message.timestamp)) } - if(event.isSingleUsage) - subscribers.clear() - case None => - } - case _ => - } - } - - /** - * method which allow to process all EventHolderMessage - * - * @param message is the content - */ - def processEventHolderMessage(message : EventHolderMessage) : Unit = { - message match { - case Subscribe(event, ref) => subscribeTo(ref, event) - case SubscribeSynchronized(event, ref, synchronizer) => - subscribeSynchronizedTo(ref, event, synchronizer) - case Unsubscribe(event, ref) => unsubscribeTo(ref, event) - } - } - - def referenceToSynchronizer(synchronizer : NetworkEntityPath) : NetworkEntityReference - -} diff --git a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/EventHolderMessage.scala b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/EventHolderMessage.scala deleted file mode 100644 index 60b650d8..00000000 --- a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/EventHolderMessage.scala +++ /dev/null @@ -1,8 +0,0 @@ -package fr.univ_lille.cristal.emeraude.n2s3.core.event - -import fr.univ_lille.cristal.emeraude.n2s3.support.actors.Message - -/*********************************************************************************************** - * SUBSCRIBE AND UNSUBSCRIBE MESSAGES - **********************************************************************************************/ -abstract class EventHolderMessage extends Message \ No newline at end of file diff --git a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/EventResponse.scala b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/EventResponse.scala deleted file mode 100644 index 2a1d0800..00000000 --- a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/EventResponse.scala +++ /dev/null @@ -1,11 +0,0 @@ -package fr.univ_lille.cristal.emeraude.n2s3.core.event - -import fr.univ_lille.cristal.emeraude.n2s3.support.actors.Message - -/********************************************************************************************************* - * List of event which can be trigger by neuron - * Event can be added by an entity which inherited from Observable trait. - * Each event carry a message which it send when the event is triggered by the observable entity - ********************************************************************************************************/ - -abstract class EventResponse extends Message diff --git a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/EventTriggered.scala b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/EventTriggered.scala deleted file mode 100644 index 1756a9a3..00000000 --- a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/EventTriggered.scala +++ /dev/null @@ -1,6 +0,0 @@ -package fr.univ_lille.cristal.emeraude.n2s3.core.event - -/** - * Default response when a event don't need specific response - */ -object EventTriggered extends EventResponse diff --git a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/InputRestartEvent.scala b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/InputRestartEvent.scala deleted file mode 100644 index 734bd988..00000000 --- a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/InputRestartEvent.scala +++ /dev/null @@ -1,6 +0,0 @@ -package fr.univ_lille.cristal.emeraude.n2s3.core.event - -/** - * Created by guille on 10/14/16. - */ -object InputRestartEvent extends Event[InputRestartResponse] diff --git a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/InputRestartResponse.scala b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/InputRestartResponse.scala deleted file mode 100644 index 4db3c0a0..00000000 --- a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/InputRestartResponse.scala +++ /dev/null @@ -1,8 +0,0 @@ -package fr.univ_lille.cristal.emeraude.n2s3.core.event - -import akka.actor.ActorRef - -/** - * Created by guille on 10/14/16. - */ -case class InputRestartResponse(source : ActorRef) extends EventResponse diff --git a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/LabelChangeEvent.scala b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/LabelChangeEvent.scala index b779511d..fa2719c6 100644 --- a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/LabelChangeEvent.scala +++ b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/LabelChangeEvent.scala @@ -1,6 +1,8 @@ package fr.univ_lille.cristal.emeraude.n2s3.core.event +import fr.univ_lille.cristal.emeraude.n2s3.support.event.TimedEvent + /** * Created by guille on 10/14/16. */ -object LabelChangeEvent extends TimedEvent[LabelChangeResponse] +object LabelChangeEvent extends TimedEvent[LabelChangeResponse] diff --git a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/LabelChangeResponse.scala b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/LabelChangeResponse.scala index 5ded130b..8ae97513 100644 --- a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/LabelChangeResponse.scala +++ b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/LabelChangeResponse.scala @@ -1,6 +1,7 @@ package fr.univ_lille.cristal.emeraude.n2s3.core.event import fr.univ_lille.cristal.emeraude.n2s3.core.GlobalTypesAlias._ +import fr.univ_lille.cristal.emeraude.n2s3.support.event.TimedEventResponse /******************************************************************************************************** * Event triggered when an input has a new label diff --git a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/NeuronFireEvent.scala b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/NeuronFireEvent.scala index 8e79913e..40d4c5fd 100644 --- a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/NeuronFireEvent.scala +++ b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/NeuronFireEvent.scala @@ -1,5 +1,7 @@ package fr.univ_lille.cristal.emeraude.n2s3.core.event +import fr.univ_lille.cristal.emeraude.n2s3.support.event.Event + /** * Created by guille on 10/14/16. */ diff --git a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/NeuronFireResponse.scala b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/NeuronFireResponse.scala index d3084edb..e66d9b50 100644 --- a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/NeuronFireResponse.scala +++ b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/NeuronFireResponse.scala @@ -2,6 +2,7 @@ package fr.univ_lille.cristal.emeraude.n2s3.core.event import fr.univ_lille.cristal.emeraude.n2s3.core.GlobalTypesAlias._ import fr.univ_lille.cristal.emeraude.n2s3.core.NetworkEntityPath +import fr.univ_lille.cristal.emeraude.n2s3.support.event.EventResponse /******************************************************************************************************** * Event triggered when a neuron fire diff --git a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/NeuronUpdateEvent.scala b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/NeuronUpdateEvent.scala index fd1d34fe..06439a34 100644 --- a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/NeuronUpdateEvent.scala +++ b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/NeuronUpdateEvent.scala @@ -1,5 +1,7 @@ package fr.univ_lille.cristal.emeraude.n2s3.core.event +import fr.univ_lille.cristal.emeraude.n2s3.support.event.{Event, EventTriggered} + /*************************************************************************************************** * Event triggered when update arise in neuron **************************************************************************************************/ diff --git a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/Subscribe.scala b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/Subscribe.scala deleted file mode 100644 index 0ef3b74f..00000000 --- a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/Subscribe.scala +++ /dev/null @@ -1,6 +0,0 @@ -package fr.univ_lille.cristal.emeraude.n2s3.core.event - -import fr.univ_lille.cristal.emeraude.n2s3.core.NetworkEntityReference - -/** Subscribe an actor to an event */ -case class Subscribe(event : Event[_ <: EventResponse], ref : NetworkEntityReference) extends EventHolderMessage \ No newline at end of file diff --git a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/SubscribeSynchronized.scala b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/SubscribeSynchronized.scala deleted file mode 100644 index a41397cc..00000000 --- a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/SubscribeSynchronized.scala +++ /dev/null @@ -1,6 +0,0 @@ -package fr.univ_lille.cristal.emeraude.n2s3.core.event - -import fr.univ_lille.cristal.emeraude.n2s3.core.{NetworkEntityPath, NetworkEntityReference} - -/** Subscribe an actor to an event and send the triggered event though a synchronizer */ -case class SubscribeSynchronized(event : TimedEvent[_ <: TimedEventResponse], ref : NetworkEntityReference, synchronizer: NetworkEntityPath) extends EventHolderMessage diff --git a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/SynapseUpdateEvent.scala b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/SynapseUpdateEvent.scala index b522c6f0..4b4a3236 100644 --- a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/SynapseUpdateEvent.scala +++ b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/SynapseUpdateEvent.scala @@ -1,5 +1,7 @@ package fr.univ_lille.cristal.emeraude.n2s3.core.event +import fr.univ_lille.cristal.emeraude.n2s3.support.event.{Event, EventTriggered} + /*************************************************************************************************** * Event triggered when update arise in synapse **************************************************************************************************/ diff --git a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/SynchronizedEvent.scala b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/SynchronizedEvent.scala deleted file mode 100644 index 0415dffe..00000000 --- a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/SynchronizedEvent.scala +++ /dev/null @@ -1,11 +0,0 @@ -package fr.univ_lille.cristal.emeraude.n2s3.core.event - -import fr.univ_lille.cristal.emeraude.n2s3.core.GlobalTypesAlias.Timestamp -import fr.univ_lille.cristal.emeraude.n2s3.core.Synchronizer.SynchronizedMessage -import fr.univ_lille.cristal.emeraude.n2s3.core.{NetworkEntityPath, NetworkEntityReference} -import fr.univ_lille.cristal.emeraude.n2s3.support.actors.Message - -/** - * Created by guille on 10/20/16. - */ -case class SynchronizedEvent(synchronizer : NetworkEntityPath, override val postSync : NetworkEntityReference, override val message : Message, override val timestamp : Timestamp) extends SynchronizedMessage \ No newline at end of file diff --git a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/TimedEvent.scala b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/TimedEvent.scala deleted file mode 100644 index 1676e4fa..00000000 --- a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/TimedEvent.scala +++ /dev/null @@ -1,6 +0,0 @@ -package fr.univ_lille.cristal.emeraude.n2s3.core.event - -/** - * Created by guille on 10/20/16. - */ -abstract class TimedEvent[Response <: TimedEventResponse] extends Event[Response] diff --git a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/TimedEventResponse.scala b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/TimedEventResponse.scala deleted file mode 100644 index bddd3b2b..00000000 --- a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/TimedEventResponse.scala +++ /dev/null @@ -1,10 +0,0 @@ -package fr.univ_lille.cristal.emeraude.n2s3.core.event - -import fr.univ_lille.cristal.emeraude.n2s3.core.GlobalTypesAlias._ - -/** - * Created by guille on 10/20/16. - */ -abstract class TimedEventResponse extends EventResponse { - val timestamp : Timestamp -} diff --git a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/Unsubscribe.scala b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/Unsubscribe.scala deleted file mode 100644 index fef6cc7d..00000000 --- a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/event/Unsubscribe.scala +++ /dev/null @@ -1,6 +0,0 @@ -package fr.univ_lille.cristal.emeraude.n2s3.core.event - -import fr.univ_lille.cristal.emeraude.n2s3.core.NetworkEntityReference - -/** Unsubscribe an actor to an event */ -case class Unsubscribe(event : Event[_ <: EventResponse], ref : NetworkEntityReference) extends EventHolderMessage \ No newline at end of file diff --git a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/exceptions/EventNotFoundException.scala b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/exceptions/EventNotFoundException.scala index d60a38a2..dadb80bc 100644 --- a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/exceptions/EventNotFoundException.scala +++ b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/core/exceptions/EventNotFoundException.scala @@ -1,6 +1,6 @@ package fr.univ_lille.cristal.emeraude.n2s3.core.exceptions -import fr.univ_lille.cristal.emeraude.n2s3.core.event.Event +import fr.univ_lille.cristal.emeraude.n2s3.support.event.Event /** * Created by falezp on 06/06/16. diff --git a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/io/report/BenchmarkMonitor.scala b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/io/report/BenchmarkMonitor.scala index 42e6a9d7..b035870d 100644 --- a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/io/report/BenchmarkMonitor.scala +++ b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/io/report/BenchmarkMonitor.scala @@ -12,6 +12,7 @@ import fr.univ_lille.cristal.emeraude.n2s3.core.Synchronizer.Done import fr.univ_lille.cristal.emeraude.n2s3.core._ import fr.univ_lille.cristal.emeraude.n2s3.core.event._ import fr.univ_lille.cristal.emeraude.n2s3.support.actors.Message +import fr.univ_lille.cristal.emeraude.n2s3.support.event.{Subscribe, Unsubscribe} import scala.collection.mutable diff --git a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/io/report/BenchmarkMonitorRef.scala b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/io/report/BenchmarkMonitorRef.scala index 9acdd0c2..edda7309 100644 --- a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/io/report/BenchmarkMonitorRef.scala +++ b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/io/report/BenchmarkMonitorRef.scala @@ -5,15 +5,16 @@ package fr.univ_lille.cristal.emeraude.n2s3.features.io.report import akka.actor.{ActorRef, Props} -import fr.univ_lille.cristal.emeraude.n2s3.core._ - -import scala.concurrent.Await import akka.pattern.ask import fr.univ_lille.cristal.emeraude.n2s3.core.Synchronizer.Done +import fr.univ_lille.cristal.emeraude.n2s3.core._ import fr.univ_lille.cristal.emeraude.n2s3.core.actors.Config -import fr.univ_lille.cristal.emeraude.n2s3.core.event.{LabelChangeEvent, Subscribe} +import fr.univ_lille.cristal.emeraude.n2s3.core.event.LabelChangeEvent import fr.univ_lille.cristal.emeraude.n2s3.features.builder.{N2S3, NeuronGroupObserverRef, NeuronGroupRef} import fr.univ_lille.cristal.emeraude.n2s3.support.actors.LocalActorDeploymentStrategy +import fr.univ_lille.cristal.emeraude.n2s3.support.event.Subscribe + +import scala.concurrent.Await class BenchmarkMonitorRef(neuronGroup: NeuronGroupRef) extends NeuronGroupObserverRef { diff --git a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/learning/BackPropagation.scala b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/learning/BackPropagation.scala index 09f34d3a..3e5e9e5f 100644 --- a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/learning/BackPropagation.scala +++ b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/learning/BackPropagation.scala @@ -9,10 +9,11 @@ import akka.pattern.ask import fr.univ_lille.cristal.emeraude.n2s3.core.Synchronizer.{Next, WaitEndOfActivity} import fr.univ_lille.cristal.emeraude.n2s3.core.{ExternalSender, GetProperty, NetworkEntityPath, PropertyValue, _} import fr.univ_lille.cristal.emeraude.n2s3.core.actors.{Config, WrapMessage} -import fr.univ_lille.cristal.emeraude.n2s3.core.event.{LabelChangeEvent, LabelChangeResponse, Subscribe} +import fr.univ_lille.cristal.emeraude.n2s3.core.event.{LabelChangeEvent, LabelChangeResponse} import fr.univ_lille.cristal.emeraude.n2s3.features.builder.NeuronLayer import fr.univ_lille.cristal.emeraude.n2s3.features.logging.graph.ValuesGraph import fr.univ_lille.cristal.emeraude.n2s3.support.actors.{AbstractActorSystem, LocalActorDeploymentStrategy, PropsBuilder} +import fr.univ_lille.cristal.emeraude.n2s3.support.event.Subscribe import scala.collection.mutable import scala.concurrent.Await diff --git a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/learning/SpikeCounter.scala b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/learning/SpikeCounter.scala index 9a0907c0..b6038040 100644 --- a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/learning/SpikeCounter.scala +++ b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/learning/SpikeCounter.scala @@ -3,8 +3,9 @@ package fr.univ_lille.cristal.emeraude.n2s3.features.learning import akka.actor.Actor import fr.univ_lille.cristal.emeraude.n2s3.core._ import fr.univ_lille.cristal.emeraude.n2s3.core.actors.WrapMessage -import fr.univ_lille.cristal.emeraude.n2s3.core.event.{NeuronFireEvent, NeuronFireResponse, Subscribe} +import fr.univ_lille.cristal.emeraude.n2s3.core.event.{NeuronFireEvent, NeuronFireResponse} import fr.univ_lille.cristal.emeraude.n2s3.support.actors.Message +import fr.univ_lille.cristal.emeraude.n2s3.support.event.Subscribe import scala.collection.mutable /** diff --git a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/learning/SpikingSquareError.scala b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/learning/SpikingSquareError.scala index a50788e3..9bd75df6 100644 --- a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/learning/SpikingSquareError.scala +++ b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/learning/SpikingSquareError.scala @@ -5,11 +5,12 @@ import akka.pattern.ask import fr.univ_lille.cristal.emeraude.n2s3.core.Synchronizer.{Next, WaitEndOfActivity} import fr.univ_lille.cristal.emeraude.n2s3.core._ import fr.univ_lille.cristal.emeraude.n2s3.core.actors.{Config, WrapMessage} -import fr.univ_lille.cristal.emeraude.n2s3.core.event.{LabelChangeEvent, LabelChangeResponse, Subscribe} +import fr.univ_lille.cristal.emeraude.n2s3.core.event.{LabelChangeEvent, LabelChangeResponse} import fr.univ_lille.cristal.emeraude.n2s3.core.models.properties.SynapticWeightFloat import fr.univ_lille.cristal.emeraude.n2s3.features.builder.NeuronLayer import fr.univ_lille.cristal.emeraude.n2s3.features.logging.graph.ValuesGraph import fr.univ_lille.cristal.emeraude.n2s3.support.actors.{AbstractActorSystem, LocalActorDeploymentStrategy, PropsBuilder} +import fr.univ_lille.cristal.emeraude.n2s3.support.event.Subscribe import scala.collection.mutable import scala.concurrent.Await diff --git a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/learning/SquareError.scala b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/learning/SquareError.scala index d919021b..8f66d942 100644 --- a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/learning/SquareError.scala +++ b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/learning/SquareError.scala @@ -1,21 +1,20 @@ package fr.univ_lille.cristal.emeraude.n2s3.features.learning -import akka.actor.Actor.Receive import akka.actor.{Actor, ActorRef, Props} -import fr.univ_lille.cristal.emeraude.n2s3.core.GlobalTypesAlias._ +import akka.pattern.ask import fr.univ_lille.cristal.emeraude.n2s3.core.Synchronizer.{Next, WaitEndOfActivity} import fr.univ_lille.cristal.emeraude.n2s3.core._ +import fr.univ_lille.cristal.emeraude.n2s3.core.actors.{Config, WrapMessage} +import fr.univ_lille.cristal.emeraude.n2s3.core.event.{LabelChangeEvent, LabelChangeResponse} +import fr.univ_lille.cristal.emeraude.n2s3.core.models.properties.SynapticWeightFloat import fr.univ_lille.cristal.emeraude.n2s3.features.builder.NeuronLayer import fr.univ_lille.cristal.emeraude.n2s3.features.logging.graph.ValuesGraph import fr.univ_lille.cristal.emeraude.n2s3.support.actors.{AbstractActorSystem, LocalActorDeploymentStrategy, PropsBuilder} +import fr.univ_lille.cristal.emeraude.n2s3.support.event.Subscribe import scala.collection.mutable -import scala.concurrent.duration._ import scala.concurrent.Await -import akka.pattern.ask -import fr.univ_lille.cristal.emeraude.n2s3.core.actors.{Config, WrapMessage} -import fr.univ_lille.cristal.emeraude.n2s3.core.event.{LabelChangeEvent, LabelChangeResponse, Subscribe} -import fr.univ_lille.cristal.emeraude.n2s3.core.models.properties.SynapticWeightFloat +import scala.concurrent.duration._ /** * Created by falezp on 24/05/16. diff --git a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/learning/SupervisedSTDPLearning.scala b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/learning/SupervisedSTDPLearning.scala index d26ef3ab..71c9f68d 100644 --- a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/learning/SupervisedSTDPLearning.scala +++ b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/learning/SupervisedSTDPLearning.scala @@ -7,9 +7,10 @@ import fr.univ_lille.cristal.emeraude.n2s3.core.Neuron.NeuronMessage import fr.univ_lille.cristal.emeraude.n2s3.core.Synchronizer.Done import fr.univ_lille.cristal.emeraude.n2s3.core._ import fr.univ_lille.cristal.emeraude.n2s3.core.actors.WrapMessage -import fr.univ_lille.cristal.emeraude.n2s3.core.event.{LabelChangeEvent, LabelChangeResponse, SubscribeSynchronized, SynchronizedEvent} +import fr.univ_lille.cristal.emeraude.n2s3.core.event.{LabelChangeEvent, LabelChangeResponse} import fr.univ_lille.cristal.emeraude.n2s3.features.builder.N2S3 import fr.univ_lille.cristal.emeraude.n2s3.support.actors.Message +import fr.univ_lille.cristal.emeraude.n2s3.support.event.{SubscribeSynchronized, SynchronizedEvent} /** * Created by falezp on 14/10/16. diff --git a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/logging/LabelMonitor.scala b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/logging/LabelMonitor.scala index 1ecbb92f..6a639b8a 100644 --- a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/logging/LabelMonitor.scala +++ b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/logging/LabelMonitor.scala @@ -11,7 +11,8 @@ import scala.collection.mutable.ArrayBuffer import scala.concurrent.Await import akka.pattern.ask import fr.univ_lille.cristal.emeraude.n2s3.core.Synchronizer.Done -import fr.univ_lille.cristal.emeraude.n2s3.core.event.{LabelChangeEvent, LabelChangeResponse, Subscribe} +import fr.univ_lille.cristal.emeraude.n2s3.core.event.{LabelChangeEvent, LabelChangeResponse} +import fr.univ_lille.cristal.emeraude.n2s3.support.event.Subscribe /** * Created by falezp on 19/10/16. */ diff --git a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/logging/NeuronLog.scala b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/logging/NeuronLog.scala index ca3e0456..3f9436d6 100644 --- a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/logging/NeuronLog.scala +++ b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/logging/NeuronLog.scala @@ -9,10 +9,11 @@ import fr.univ_lille.cristal.emeraude.n2s3.core.GlobalTypesAlias.Timestamp import fr.univ_lille.cristal.emeraude.n2s3.core.Synchronizer.Done import fr.univ_lille.cristal.emeraude.n2s3.core.UnitTime._ import fr.univ_lille.cristal.emeraude.n2s3.core._ -import fr.univ_lille.cristal.emeraude.n2s3.core.event.{InputRestartResponse, NeuronFireEvent, NeuronFireResponse, Subscribe} +import fr.univ_lille.cristal.emeraude.n2s3.core.event.{NeuronFireEvent, NeuronFireResponse} import fr.univ_lille.cristal.emeraude.n2s3.features.logging.graph.{InputOutputGraph, NeuronsGraph, ValuesGraph} import fr.univ_lille.cristal.emeraude.n2s3.models.qbg.NeuronPotentialResponse import fr.univ_lille.cristal.emeraude.n2s3.support.actors.{ActorCompanion, PropsBuilder, SinglePropsBuilder} +import fr.univ_lille.cristal.emeraude.n2s3.support.event.Subscribe import scala.concurrent.duration._ @@ -32,8 +33,6 @@ class NeuronsFireLogText(filename : String ) extends Actor { def receive = { case NeuronFireResponse(timestamp, path) => fos.println(path + "\t" + timestamp + "\n") - case InputRestartResponse(source) => - fos.println(source.path.name + " restart\n") case Done => fos.close() sender ! Done diff --git a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/logging/SpikeMonitor.scala b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/logging/SpikeMonitor.scala index b65287ec..86bf8156 100644 --- a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/logging/SpikeMonitor.scala +++ b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/logging/SpikeMonitor.scala @@ -1,17 +1,18 @@ package fr.univ_lille.cristal.emeraude.n2s3.features.logging import akka.actor.{Actor, Props} +import akka.pattern.ask import fr.univ_lille.cristal.emeraude.n2s3.core.GlobalTypesAlias.Timestamp -import fr.univ_lille.cristal.emeraude.n2s3.core.actors.Config +import fr.univ_lille.cristal.emeraude.n2s3.core.Synchronizer.Done import fr.univ_lille.cristal.emeraude.n2s3.core._ +import fr.univ_lille.cristal.emeraude.n2s3.core.actors.Config +import fr.univ_lille.cristal.emeraude.n2s3.core.event.{NeuronFireEvent, NeuronFireResponse} import fr.univ_lille.cristal.emeraude.n2s3.features.builder.N2S3 import fr.univ_lille.cristal.emeraude.n2s3.support.actors.LocalActorDeploymentStrategy +import fr.univ_lille.cristal.emeraude.n2s3.support.event.Subscribe import scala.collection.mutable.ArrayBuffer import scala.concurrent.Await -import akka.pattern.ask -import fr.univ_lille.cristal.emeraude.n2s3.core.Synchronizer.Done -import fr.univ_lille.cristal.emeraude.n2s3.core.event.{NeuronFireEvent, NeuronFireResponse, Subscribe} /** * Created by falezp on 19/10/16. diff --git a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/logging/SynapseLog.scala b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/logging/SynapseLog.scala index adc9af4e..86807447 100644 --- a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/logging/SynapseLog.scala +++ b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/logging/SynapseLog.scala @@ -6,10 +6,10 @@ import akka.actor._ import fr.univ_lille.cristal.emeraude.n2s3.core.GlobalTypesAlias._ import fr.univ_lille.cristal.emeraude.n2s3.core.Synchronizer.WaitEndOfActivity import fr.univ_lille.cristal.emeraude.n2s3.core.UnitTime._ -import fr.univ_lille.cristal.emeraude.n2s3.core.event.EventTriggered import fr.univ_lille.cristal.emeraude.n2s3.core.{ExternalSender, NetworkEntityPath, Time} import fr.univ_lille.cristal.emeraude.n2s3.features.logging.graph.{SynapsesConductancesGraph, SynapsesWeightsBarGraph, ValuesGraph} import fr.univ_lille.cristal.emeraude.n2s3.models.qbg.SynapseWeightResponse +import fr.univ_lille.cristal.emeraude.n2s3.support.event.EventTriggered import scala.collection.mutable import scala.collection.mutable.ListBuffer @@ -68,7 +68,7 @@ class SynapsesGraph(step: Int = 50, unit : TimeUnitType = MilliSecond) extends A event.keys.toList.sortBy(x => x).foreach { k => if (k >= beg.timestamp && k <= end.timestamp) for (i <- 0 until event.get(k).size) { - graph.addSerie(event.get(k).get(i)._1, convert(unit,new Time(k)), event.get(k).get(i)._2) + graph.addSerie(event(k)(i)._1, convert(unit,new Time(k)), event(k)(i)._2) } } graph launchOffline() @@ -87,7 +87,7 @@ class SynapsesLogGraph(step: Int = 50, unit : TimeUnitType = MilliSecond) extend def receive = { case SynapseWeightResponse(t, source, id, value) => if (t - lastT > step) { - graph.refreshSerie(source.path.name + ":" + id, convert(unit,new Time(t)), value) + graph.refreshSerie(source.path.name + ":" + id, convert(unit,Time(t)), value) if (!graph.isRunning()) graph.launch() lastT = t @@ -108,8 +108,8 @@ class SynapsesWeightRepartition(sync: NetworkEntityPath) extends Actor { val graph = new SynapsesConductancesGraph val fos: FileOutputStream = new FileOutputStream("output/log/synapseRepart.log") values.keys.foreach { k => - fos.write((k + "\t" + values.get(k).get + "\n").map(_.toByte).toArray) - graph.addSerie(values.get(k).get) + fos.write((k + "\t" + values(k) + "\n").map(_.toByte).toArray) + graph.addSerie(values(k)) } graph.launch() fos.close() diff --git a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/models/qbg/Event.scala b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/models/qbg/Event.scala index da1db9b2..00a65c6a 100644 --- a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/models/qbg/Event.scala +++ b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/models/qbg/Event.scala @@ -6,7 +6,7 @@ package fr.univ_lille.cristal.emeraude.n2s3.models.qbg import akka.actor._ import fr.univ_lille.cristal.emeraude.n2s3.core.GlobalTypesAlias._ -import fr.univ_lille.cristal.emeraude.n2s3.core.event.{Event, EventResponse} +import fr.univ_lille.cristal.emeraude.n2s3.support.event.{Event, EventResponse} /******************************************************************************************************** * Event trigger when potential of neuron change diff --git a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/support/event/Event.scala b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/support/event/Event.scala new file mode 100644 index 00000000..d4d3767c --- /dev/null +++ b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/support/event/Event.scala @@ -0,0 +1,31 @@ +package fr.univ_lille.cristal.emeraude.n2s3.support.event + +import fr.univ_lille.cristal.emeraude.n2s3.core.GlobalTypesAlias._ +import fr.univ_lille.cristal.emeraude.n2s3.support.actors.Message + +/** + * Represents events that are raised by the simulation at runtime. + * When an event happens, the subscriber will receive a message of type [[Response]] + */ +abstract class Event[Response <: EventResponse] extends Serializable { + + /** + * When true, all subscriptions to this event will be removed after the event is risen + */ + def isSingleUsage : Boolean = false + + /** + * Returns a default message to use when this event happens. + * + * @throws UnsupportedOperationException by default if subclasses don't override this method + */ + def defaultResponse : Response = throw new UnsupportedOperationException +} + +abstract class EventResponse extends Message + +abstract class TimedEvent[Response <: TimedEventResponse] extends Event[Response] + +abstract class TimedEventResponse extends EventResponse { + val timestamp : Timestamp +} \ No newline at end of file diff --git a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/support/event/Observable.scala b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/support/event/Observable.scala new file mode 100644 index 00000000..5306e76d --- /dev/null +++ b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/support/event/Observable.scala @@ -0,0 +1,149 @@ +package fr.univ_lille.cristal.emeraude.n2s3.support.event + +import fr.univ_lille.cristal.emeraude.n2s3.core.GlobalTypesAlias._ +import fr.univ_lille.cristal.emeraude.n2s3.core.Synchronizer.SynchronizedMessage +import fr.univ_lille.cristal.emeraude.n2s3.core.{NetworkEntityPath, NetworkEntityReference} +import fr.univ_lille.cristal.emeraude.n2s3.core.exceptions.EventNotFoundException +import fr.univ_lille.cristal.emeraude.n2s3.support.actors.Message + +import scala.collection.mutable +import scala.collection.mutable.ArrayBuffer + +/******************************************************************************************************************** + * Messages + ******************************************************************************************************************/ + +abstract class ObservableMessage extends Message +object EventTriggered extends EventResponse + +case class Subscribe(event : Event[_ <: EventResponse], ref : NetworkEntityReference) extends ObservableMessage +case class SubscribeSynchronized(event : TimedEvent[_ <: TimedEventResponse], ref : NetworkEntityReference, synchronizer: NetworkEntityPath) extends ObservableMessage +case class Unsubscribe(event : Event[_ <: EventResponse], ref : NetworkEntityReference) extends ObservableMessage + +case class SynchronizedEvent(synchronizer : NetworkEntityPath, override val postSync : NetworkEntityReference, override val message : Message, override val timestamp : Timestamp) extends SynchronizedMessage + +/** + * Implementation of the Observer design pattern + * Users of this trait becomes observables. + */ +trait Observable { + /** + * Contains the list of declared event, associated with all observers + */ + private val events = mutable.HashMap[Event[_ <: EventResponse], mutable.ArrayBuffer[NetworkEntityReference]]() + private val synchronizedEvents = mutable.HashMap[TimedEvent[_ <: TimedEventResponse], mutable.ArrayBuffer[(NetworkEntityReference, NetworkEntityPath, NetworkEntityReference)]]() + + /** + * Declares an event to be handled by this observable. + * + * @param event is the type of the event + */ + def addEvent(event : Event[_ <: EventResponse]): Unit = { + events += event -> ArrayBuffer[NetworkEntityReference]() + + if(event.isInstanceOf[TimedEvent[_]]) + synchronizedEvents += event.asInstanceOf[TimedEvent[TimedEventResponse]] -> ArrayBuffer[(NetworkEntityReference, NetworkEntityPath, NetworkEntityReference)]() + } + + /** + * Subscribes an observer to the specified event + * + * @param entity is the entity which will receive response when event is triggered + * @param event is the event to be observed + * @throws EventNotFoundException is the event is not declared in this class + */ + def addSubscription(entity : NetworkEntityReference, event : Event[_ <: EventResponse]) : Unit = { + events.get(event) match { + case Some(list) => list += entity + case None => throw new EventNotFoundException(event, this.getClass) + } + } + + /** + * Subscribes an observer to the specified event. Triggered message will pass through the synchronizer + * + * @param entity is the entity which will receive response when event is triggered + * @param event is the event to be observed + * @throws EventNotFoundException is the event is not declared in this class + */ + def addSynchronizedSubscription(entity : NetworkEntityReference, event : TimedEvent[_ <: TimedEventResponse], synchronizer : NetworkEntityPath) : Unit = { + synchronizedEvents.get(event) match { + case Some(list) => list += ((referenceToSynchronizer(synchronizer), synchronizer, entity)) + case None => throw new EventNotFoundException(event, this.getClass) + } + } + + /** + * Unsubscribes an observer from an event + * + * @param entity is the entity which will receive response when event is triggered + * @param event is the event to be observed + * @throws EventNotFoundException is the event is not declared in this class + */ + def removeSubscription(entity : NetworkEntityReference, event : Event[_ <: EventResponse]) : Unit = { + events.get(event) match { + case Some(list) => list -= entity + case None => throw new EventNotFoundException(event, this.getClass) + } + events.getOrElseUpdate(event, ArrayBuffer[NetworkEntityReference]()) + } + + /** + * Triggers an event. + * Sends the default response of this event to all its observers. + * If this event is single usage, remove all subscriptions to it. + * + * @param event is the event to trigger + * @tparam Response is the response type of the event + */ + def triggerEvent[Response <: EventResponse](event : Event[Response]) : Unit = { + triggerEventWith(event, event.defaultResponse) + } + + /** + * Triggers an event with a specific response message. + * Sends the specified response to all observers. + * If this event is single usage, all observers will be removed + * + * @param event is the event to trigger + * @param response is the response which will be sent to observers + * @tparam Response is the response type of the event + */ + def triggerEventWith[Response <: EventResponse](event : Event[Response], response : Response) : Unit = { + events.get(event) match { + case Some(subscribers) => + subscribers.foreach(subscriber => subscriber.send(response)) + if(event.isSingleUsage) + subscribers.clear() + case None => + } + + response match { + case message : TimedEventResponse => + synchronizedEvents.get(event.asInstanceOf[TimedEvent[TimedEventResponse]]) match { + case Some(subscribers) => + subscribers.foreach{ case (synchronizer, path, subscriber) => synchronizer.send(SynchronizedEvent(path, subscriber, message, message.timestamp)) } + if(event.isSingleUsage) + subscribers.clear() + case None => + } + case _ => + } + } + + /** + * method which allow to process all ObservableMessage + * + * @param message is the content + */ + def processEventHolderMessage(message : ObservableMessage) : Unit = { + message match { + case Subscribe(event, ref) => addSubscription(ref, event) + case SubscribeSynchronized(event, ref, synchronizer) => + addSynchronizedSubscription(ref, event, synchronizer) + case Unsubscribe(event, ref) => removeSubscription(ref, event) + } + } + + def referenceToSynchronizer(synchronizer : NetworkEntityPath) : NetworkEntityReference +} \ No newline at end of file diff --git a/n2s3_examples/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/apps/ExampleFreeWay.scala b/n2s3_examples/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/apps/ExampleFreeWay.scala index 5c2ca034..8e51052d 100644 --- a/n2s3_examples/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/apps/ExampleFreeWay.scala +++ b/n2s3_examples/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/apps/ExampleFreeWay.scala @@ -7,7 +7,7 @@ import fr.univ_lille.cristal.emeraude.n2s3.core.Synchronizer.Done import fr.univ_lille.cristal.emeraude.n2s3.core.UnitCast._ import fr.univ_lille.cristal.emeraude.n2s3.core._ import fr.univ_lille.cristal.emeraude.n2s3.core.actors.Config -import fr.univ_lille.cristal.emeraude.n2s3.core.event.{NeuronFireEvent, Subscribe} +import fr.univ_lille.cristal.emeraude.n2s3.core.event.NeuronFireEvent import fr.univ_lille.cristal.emeraude.n2s3.core.models.properties._ import fr.univ_lille.cristal.emeraude.n2s3.features.builder.connection.types.FullConnection import fr.univ_lille.cristal.emeraude.n2s3.features.builder.{InputOutputGraphNetworkObserver, N2S3} @@ -17,6 +17,7 @@ import fr.univ_lille.cristal.emeraude.n2s3.features.io.report.{ImageSynapsesWeig import fr.univ_lille.cristal.emeraude.n2s3.features.logging.NeuronsFireLogText import fr.univ_lille.cristal.emeraude.n2s3.models.qbg._ import fr.univ_lille.cristal.emeraude.n2s3.support.actors.LocalActorDeploymentStrategy +import fr.univ_lille.cristal.emeraude.n2s3.support.event.Subscribe import squants.electro.ElectricPotentialConversions.ElectricPotentialConversions import scala.concurrent.Await diff --git a/n2s3_examples/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/apps/bio/Masquelier.scala b/n2s3_examples/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/apps/bio/Masquelier.scala index e1953ead..a2f3f97e 100644 --- a/n2s3_examples/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/apps/bio/Masquelier.scala +++ b/n2s3_examples/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/apps/bio/Masquelier.scala @@ -8,7 +8,7 @@ import fr.univ_lille.cristal.emeraude.n2s3.core.GlobalTypesAlias.Timestamp import fr.univ_lille.cristal.emeraude.n2s3.core.Synchronizer.Done import fr.univ_lille.cristal.emeraude.n2s3.core._ import fr.univ_lille.cristal.emeraude.n2s3.core.actors.{Config, ShapelessSpike} -import fr.univ_lille.cristal.emeraude.n2s3.core.event.{NeuronFireEvent, Subscribe} +import fr.univ_lille.cristal.emeraude.n2s3.core.event.NeuronFireEvent import fr.univ_lille.cristal.emeraude.n2s3.core.models.properties.{MembraneThresholdFloat, MembraneThresholdType, MembraneThresholdTypeEnum} import fr.univ_lille.cristal.emeraude.n2s3.features.builder.N2S3 import fr.univ_lille.cristal.emeraude.n2s3.features.builder.connection.types.FullConnection @@ -20,6 +20,7 @@ import fr.univ_lille.cristal.emeraude.n2s3.models.bio.InhibitorConnection import fr.univ_lille.cristal.emeraude.n2s3.models.qbg.NeuronPotentialEvent import fr.univ_lille.cristal.emeraude.n2s3.support.InputDistribution import fr.univ_lille.cristal.emeraude.n2s3.support.actors.LocalActorDeploymentStrategy +import fr.univ_lille.cristal.emeraude.n2s3.support.event.Subscribe import fr.univ_lille.cristal.emeraude.n2s3.support.io.{InputSeq, N2S3Input, N2S3InputEnd, N2S3InputSpike} import scala.concurrent.Await diff --git a/n2s3_examples/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/apps/bio/SmallReservoir.scala b/n2s3_examples/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/apps/bio/SmallReservoir.scala index ea0e1c66..7455516d 100644 --- a/n2s3_examples/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/apps/bio/SmallReservoir.scala +++ b/n2s3_examples/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/apps/bio/SmallReservoir.scala @@ -3,29 +3,28 @@ package fr.univ_lille.cristal.emeraude.n2s3.apps.bio import java.io.{File, PrintWriter} import akka.actor.Props +import akka.pattern.ask import fr.univ_lille.cristal.emeraude.n2s3.core.GlobalTypesAlias._ -import fr.univ_lille.cristal.emeraude.n2s3.core._ +import fr.univ_lille.cristal.emeraude.n2s3.core.Synchronizer.Done import fr.univ_lille.cristal.emeraude.n2s3.core.UnitCast._ +import fr.univ_lille.cristal.emeraude.n2s3.core._ import fr.univ_lille.cristal.emeraude.n2s3.core.actors.{Config, ShapelessSpike} -import fr.univ_lille.cristal.emeraude.n2s3.core.event.{NeuronFireEvent, Subscribe} +import fr.univ_lille.cristal.emeraude.n2s3.core.event.NeuronFireEvent +import fr.univ_lille.cristal.emeraude.n2s3.core.models.properties._ import fr.univ_lille.cristal.emeraude.n2s3.features.builder.N2S3 -import fr.univ_lille.cristal.emeraude.n2s3.features.builder.connection.types.{FullConnection, RandomConnection} +import fr.univ_lille.cristal.emeraude.n2s3.features.builder.connection.types.RandomConnection import fr.univ_lille.cristal.emeraude.n2s3.features.io.input._ -import fr.univ_lille.cristal.emeraude.n2s3.features.learning.{DelayAdaptation, SpikeProp, SupervisedSTDPLearning} +import fr.univ_lille.cristal.emeraude.n2s3.features.learning.DelayAdaptation import fr.univ_lille.cristal.emeraude.n2s3.features.logging.{ExportNetworkTopology, NeuronsFireLogText} import fr.univ_lille.cristal.emeraude.n2s3.models.bio._ -import fr.univ_lille.cristal.emeraude.n2s3.models.qbg.NeuronPotentialEvent import fr.univ_lille.cristal.emeraude.n2s3.support.InputDistribution import fr.univ_lille.cristal.emeraude.n2s3.support.actors.LocalActorDeploymentStrategy +import fr.univ_lille.cristal.emeraude.n2s3.support.event.Subscribe import fr.univ_lille.cristal.emeraude.n2s3.support.io._ import scala.collection.mutable.ListBuffer import scala.concurrent.Await -import scala.sys.Prop import scala.util.Random -import akka.pattern.ask -import fr.univ_lille.cristal.emeraude.n2s3.core.Synchronizer.Done -import fr.univ_lille.cristal.emeraude.n2s3.core.models.properties._ /** * Created by falezp on 14/10/16. */ @@ -180,7 +179,7 @@ object SmallReservoir extends App { println("Start Training ...") DelayAdaptation.execute(n2s3, Map( - "Up" -> classifier.neuronPaths(0), + "Up" -> classifier.neuronPaths.head, "Down" -> classifier.neuronPaths(1) ), 1.0f) diff --git a/n2s3_examples/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/apps/ternary/BitStream.scala b/n2s3_examples/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/apps/ternary/BitStream.scala index 95f441ae..f05cb32c 100644 --- a/n2s3_examples/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/apps/ternary/BitStream.scala +++ b/n2s3_examples/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/apps/ternary/BitStream.scala @@ -21,8 +21,9 @@ import scala.util.Random import akka.pattern.ask import fr.univ_lille.cristal.emeraude.n2s3.core.Synchronizer.Done import fr.univ_lille.cristal.emeraude.n2s3.core.actors.{Config, ShapelessSpike} -import fr.univ_lille.cristal.emeraude.n2s3.core.event.{NeuronFireEvent, Subscribe} +import fr.univ_lille.cristal.emeraude.n2s3.core.event.NeuronFireEvent import fr.univ_lille.cristal.emeraude.n2s3.core.models.properties._ +import fr.univ_lille.cristal.emeraude.n2s3.support.event.Subscribe /** * Created by falezp on 14/10/16. */ diff --git a/n2s3_examples/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/apps/ternary/PatternStart.scala b/n2s3_examples/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/apps/ternary/PatternStart.scala index ce325988..44aeb1d2 100644 --- a/n2s3_examples/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/apps/ternary/PatternStart.scala +++ b/n2s3_examples/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/apps/ternary/PatternStart.scala @@ -9,7 +9,7 @@ import fr.univ_lille.cristal.emeraude.n2s3.core.Synchronizer.Done import fr.univ_lille.cristal.emeraude.n2s3.core.UnitCast._ import fr.univ_lille.cristal.emeraude.n2s3.core._ import fr.univ_lille.cristal.emeraude.n2s3.core.actors.{Config, ShapelessSpike} -import fr.univ_lille.cristal.emeraude.n2s3.core.event.{NeuronFireEvent, Subscribe} +import fr.univ_lille.cristal.emeraude.n2s3.core.event.NeuronFireEvent import fr.univ_lille.cristal.emeraude.n2s3.core.models.properties.MembraneThresholdFloat import fr.univ_lille.cristal.emeraude.n2s3.features.builder.N2S3 import fr.univ_lille.cristal.emeraude.n2s3.features.builder.connection.types.FullConnection @@ -20,6 +20,7 @@ import fr.univ_lille.cristal.emeraude.n2s3.models.ternary_synapse import fr.univ_lille.cristal.emeraude.n2s3.models.ternary_synapse.InhibitorConnection import fr.univ_lille.cristal.emeraude.n2s3.support.InputDistribution import fr.univ_lille.cristal.emeraude.n2s3.support.actors.LocalActorDeploymentStrategy +import fr.univ_lille.cristal.emeraude.n2s3.support.event.Subscribe import fr.univ_lille.cristal.emeraude.n2s3.support.io.{InputSeq, N2S3Input, N2S3InputEnd, N2S3InputSpike} import scala.concurrent.Await -- GitLab