-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #161 from SainsburyWellcomeCentre/aeon-foraging
Split dispenser state from dispenser UI controller
- Loading branch information
Showing
13 changed files
with
299 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System; | ||
using System.ComponentModel; | ||
using System.Linq; | ||
using System.Reactive.Linq; | ||
using Bonsai; | ||
|
||
namespace Aeon.Foraging | ||
{ | ||
[Description("Create a sequence of dispenser event commands.")] | ||
public class CreateDispenserEvent : Source<DispenserEventArgs> | ||
{ | ||
[Description("The number of dispenser units associated with the event command.")] | ||
public int Value { get; set; } | ||
|
||
[Description("Specifies the type of dispenser event command to create.")] | ||
public DispenserEventType EventType { get; set; } | ||
|
||
public override IObservable<DispenserEventArgs> Generate() | ||
{ | ||
return Observable.Return(new DispenserEventArgs(Value, EventType)); | ||
} | ||
|
||
public IObservable<DispenserEventArgs> Generate<TSource>(IObservable<TSource> source) | ||
{ | ||
return source.Select(value => new DispenserEventArgs(Value, EventType)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System; | ||
using System.ComponentModel; | ||
using System.Linq; | ||
using System.Reactive.Linq; | ||
using Bonsai; | ||
|
||
namespace Aeon.Foraging | ||
{ | ||
[Combinator] | ||
[Description("Generates a sequence of the estimated number of units in the specified dispenser.")] | ||
public class DispenserAccumulate | ||
{ | ||
public IObservable<DispenserState> Process(IObservable<DispenserEventArgs> source) | ||
{ | ||
return source.Scan(new DispenserState(), Accumulate); | ||
} | ||
|
||
public IObservable<DispenserState> Process(IObservable<DispenserEventArgs> source, IObservable<DispenserState> seed) | ||
{ | ||
return seed.Take(1).SelectMany(state => source.Scan(state, Accumulate)); | ||
} | ||
|
||
static DispenserState Accumulate(DispenserState state, DispenserEventArgs evt) | ||
{ | ||
return evt.EventType switch | ||
{ | ||
DispenserEventType.Discount => new DispenserState { Count = state.Count - evt.Value }, | ||
DispenserEventType.Refill => new DispenserState { Count = state.Count + evt.Value }, | ||
DispenserEventType.Reset => new DispenserState { Count = evt.Value }, | ||
_ => throw new InvalidOperationException("Invalid dispenser event type."), | ||
}; | ||
} | ||
} | ||
|
||
public class DispenserState | ||
{ | ||
public int Count { get; set; } | ||
|
||
public override string ToString() | ||
{ | ||
return $"DispenserState(Total: {Count})"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using Aeon.Acquisition; | ||
using Bonsai; | ||
using Bonsai.Harp; | ||
using System; | ||
using System.ComponentModel; | ||
using System.Reactive.Linq; | ||
using System.Reactive.Subjects; | ||
|
||
namespace Aeon.Foraging | ||
{ | ||
[DefaultProperty(nameof(Name))] | ||
[TypeVisualizer(typeof(DispenserEventVisualizer))] | ||
[Description("Generates a sequence of event commands for the specified dispenser.")] | ||
public class DispenserController : MetadataSource<DispenserEventArgs>, INamedElement | ||
{ | ||
[Description("The name of the dispenser.")] | ||
public string Name { get; set; } | ||
|
||
string INamedElement.Name => $"{Name}{nameof(DispenserController)}"; | ||
|
||
internal BehaviorSubject<DispenserState> State { get; } = new(value: default); | ||
|
||
public IObservable<DispenserEventArgs> Process(IObservable<DispenserState> source) | ||
{ | ||
return Process().Merge(source | ||
.Do(State).IgnoreElements() | ||
.Cast<DispenserEventArgs>()); | ||
} | ||
|
||
public IObservable<Timestamped<DispenserEventArgs>> Process(IObservable<DispenserState> source, IObservable<HarpMessage> clockSource) | ||
{ | ||
return Process(clockSource).Merge(source | ||
.Do(State).IgnoreElements() | ||
.Cast<Timestamped<DispenserEventArgs>>()); | ||
} | ||
} | ||
} |
9 changes: 3 additions & 6 deletions
9
src/Aeon.Foraging/DispenserStateMetadata.cs → src/Aeon.Foraging/DispenserEventArgs.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 43 additions & 30 deletions
73
...oraging/DispenserStateControl.Designer.cs → ...oraging/DispenserEventControl.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.