F# の判別共用体型を C# で複製するにはどうすればよいですか?
-
22-09-2019 - |
質問
渡されたメッセージを処理する Actor という新しいクラスを作成しました。私が直面している問題は、関連するが異なるメッセージをアクターに渡す最もエレガントな方法は何かを理解することです。私の最初のアイデアは継承を使用することですが、非常に肥大化しているように見えますが、明確な要件である強い型です。
何かアイデアはありますか?
例
private abstract class QueueMessage { }
private class ClearMessage : QueueMessage
{
public static readonly ClearMessage Instance = new ClearMessage();
private ClearMessage() { }
}
private class TryDequeueMessage : QueueMessage
{
public static readonly TryDequeueMessage Instance = new TryDequeueMessage();
private TryDequeueMessage() { }
}
private class EnqueueMessage : QueueMessage
{
public TValue Item { get; private set; }
private EnqueueMessage(TValue item)
{
Item = item;
}
}
アクタークラス
/// <summary>Represents a callback method to be executed by an Actor.</summary>
/// <typeparam name="TReply">The type of reply.</typeparam>
/// <param name="reply">The reply made by the actor.</param>
public delegate void ActorReplyCallback<TReply>(TReply reply);
/// <summary>Represents an Actor which receives and processes messages in concurrent applications.</summary>
/// <typeparam name="TMessage">The type of message this actor accepts.</typeparam>
/// <typeparam name="TReply">The type of reply made by this actor.</typeparam>
public abstract class Actor<TMessage, TReply> : IDisposable
{
/// <summary>The default total number of threads to process messages.</summary>
private const Int32 DefaultThreadCount = 1;
/// <summary>Used to serialize access to the message queue.</summary>
private readonly Locker Locker;
/// <summary>Stores the messages until they can be processed.</summary>
private readonly System.Collections.Generic.Queue<Message> MessageQueue;
/// <summary>Signals the actor thread to process a new message.</summary>
private readonly ManualResetEvent PostEvent;
/// <summary>This tells the actor thread to stop reading from the queue.</summary>
private readonly ManualResetEvent DisposeEvent;
/// <summary>Processes the messages posted to the actor.</summary>
private readonly List<Thread> ActorThreads;
/// <summary>Initializes a new instance of the Genex.Concurrency<TRequest, TResponse> class.</summary>
public Actor() : this(DefaultThreadCount) { }
/// <summary>Initializes a new instance of the Genex.Concurrency<TRequest, TResponse> class.</summary>
/// <param name="thread_count"></param>
public Actor(Int32 thread_count)
{
if (thread_count < 1) throw new ArgumentOutOfRangeException("thread_count", thread_count, "Must be 1 or greater.");
Locker = new Locker();
MessageQueue = new System.Collections.Generic.Queue<Message>();
EnqueueEvent = new ManualResetEvent(true);
PostEvent = new ManualResetEvent(false);
DisposeEvent = new ManualResetEvent(true);
ActorThreads = new List<Thread>();
for (Int32 i = 0; i < thread_count; i++)
{
var thread = new Thread(ProcessMessages);
thread.IsBackground = true;
thread.Start();
ActorThreads.Add(thread);
}
}
/// <summary>Posts a message and waits for the reply.</summary>
/// <param name="value">The message to post to the actor.</param>
/// <returns>The reply from the actor.</returns>
public TReply PostWithReply(TMessage message)
{
using (var wrapper = new Message(message))
{
lock (Locker) MessageQueue.Enqueue(wrapper);
PostEvent.Set();
wrapper.Channel.CompleteEvent.WaitOne();
return wrapper.Channel.Value;
}
}
/// <summary>Posts a message to the actor and executes the callback when the reply is received.</summary>
/// <param name="value">The message to post to the actor.</param>
/// <param name="callback">The callback that will be invoked once the replay is received.</param>
public void PostWithAsyncReply(TMessage value, ActorReplyCallback<TReply> callback)
{
if (callback == null) throw new ArgumentNullException("callback");
ThreadPool.QueueUserWorkItem(state => callback(PostWithReply(value)));
}
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
public void Dispose()
{
if (DisposeEvent.WaitOne(10))
{
DisposeEvent.Reset();
PostEvent.Set();
foreach (var thread in ActorThreads)
{
thread.Join();
}
((IDisposable)PostEvent).Dispose();
((IDisposable)DisposeEvent).Dispose();
}
}
/// <summary>Processes a message posted to the actor.</summary>
/// <param name="message">The message to be processed.</param>
protected abstract void ProcessMessage(Message message);
/// <summary>Dequeues the messages passes them to ProcessMessage.</summary>
private void ProcessMessages()
{
while (PostEvent.WaitOne() && DisposeEvent.WaitOne(10))
{
var message = (Message)null;
while (true)
{
lock (Locker)
{
message = MessageQueue.Count > 0 ?
MessageQueue.Dequeue() :
null;
if (message == null)
{
PostEvent.Reset();
break;
}
}
try
{
ProcessMessage(message);
}
catch
{
}
}
}
}
/// <summary>Represents a message that is passed to an actor.</summary>
protected class Message : IDisposable
{
/// <summary>The actual value of this message.</summary>
public TMessage Value { get; private set; }
/// <summary>The channel used to give a reply to this message.</summary>
public Channel Channel { get; private set; }
/// <summary>Initializes a new instance of Genex.Concurrency.Message class.</summary>
/// <param name="value">The actual value of the message.</param>
public Message(TMessage value)
{
Value = value;
Channel = new Channel();
}
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
public void Dispose()
{
Channel.Dispose();
}
}
/// <summary>Represents a channel used by an actor to reply to a message.</summary>
protected class Channel : IDisposable
{
/// <summary>The value of the reply.</summary>
public TReply Value { get; private set; }
/// <summary>Signifies that the message has been replied to.</summary>
public ManualResetEvent CompleteEvent { get; private set; }
/// <summary>Initializes a new instance of Genex.Concurrency.Channel class.</summary>
public Channel()
{
CompleteEvent = new ManualResetEvent(false);
}
/// <summary>Reply to the message received.</summary>
/// <param name="value">The value of the reply.</param>
public void Reply(TReply value)
{
Value = value;
CompleteEvent.Set();
}
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
public void Dispose()
{
((IDisposable)CompleteEvent).Dispose();
}
}
}
解決
サンプルコードでは、次のように実装します。 PostWithAsyncReply
に関しては PostWithReply
. 。これは理想的ではありません。なぜなら、電話をかけたときに PostWithAsyncReply
アクターがそれを処理するのに時間がかかりますが、実際には 2 つのスレッドが結びついています。アクターを実行するものと、アクターの終了を待つものです。1 つのスレッドでアクターを実行し、非同期の場合はコールバックを呼び出す方がよいでしょう。(明らかに、同期の場合は 2 つのスレッドの結合を避けることはできません)。
アップデート:
上記についてさらに詳しく:実行するスレッドの数を指示する引数を指定してアクターを構築します。簡単にするために、すべてのアクターが 1 つのスレッドで実行されると仮定します (実際には、1 つのスレッドのみが直接アクセスするため、アクターはロックなしで内部状態を持つことができるため、非常に良い状況です)。
アクター A はアクター B に電話をかけ、応答を待っています。リクエストを処理するには、アクター B はアクター C を呼び出す必要があります。したがって、現在 A と B のスレッドだけが待機しており、実際に CPU に作業を与えているのは C のスレッドだけです。マルチスレッドについてはこれで終わりです。しかし、答えをずっと待っていると、こういう結果になります。
各アクターで開始するスレッドの数を増やすことができます。しかし、あなたは彼らが何もせずに座っていることができるようにそれらを開始するでしょう。スタックは大量のメモリを使用し、コンテキストの切り替えにコストがかかる可能性があります。
したがって、最終結果を受け取ることができるように、コールバック メカニズムを使用してメッセージを非同期に送信することをお勧めします。実装の問題は、純粋に座って待つために、スレッド プールから別のスレッドを取得することです。したがって、基本的にはスレッド数を増やすという回避策を適用します。スレッドを次のタスクに割り当てます。 決して走らない.
実装したほうがいいよ PostWithReply
に関しては PostWithAsyncReply
, 、つまり逆に。非同期バージョンは低レベルです。私のデリゲートベースの例を基にして構築します (コードの入力が少なくて済むため):
private bool InsertCoinImpl(int value)
{
// only accept dimes/10p/whatever it is in euros
return (value == 10);
}
public void InsertCoin(int value, Action<bool> accepted)
{
Submit(() => accepted(InsertCoinImpl(value)));
}
したがって、プライベート実装はブール値を返します。パブリック非同期メソッドは、戻り値を受け取るアクションを受け入れます。プライベート実装とコールバック アクションは両方とも同じスレッドで実行されます。
同期的に待機する必要があるケースが少数派になることを願っています。ただし、必要な場合は、特定のアクターやメッセージ タイプに関連付けられない、完全に汎用的なヘルパー メソッドによって提供できます。
public static T Wait<T>(Action<Action<T>> activity)
{
T result = default(T);
var finished = new EventWaitHandle(false, EventResetMode.AutoReset);
activity(r =>
{
result = r;
finished.Set();
});
finished.WaitOne();
return result;
}
したがって、他の俳優では次のように言えます。
bool accepted = Helpers.Wait<bool>(r => chocMachine.InsertCoin(5, r));
型引数 Wait
不要かもしれませんが、コンパイルは試していません。しかし Wait
基本的にはコールバックを魔法のように作成するので、それを非同期メソッドに渡すことができ、外側ではコールバックに渡されたものを戻り値として返すだけです。渡すラムダに注意してください Wait
実際には、呼び出したスレッドと同じスレッドで実行されます。 Wait
.
通常のプログラムに戻ります...
あなたが尋ねた実際の問題に関しては、アクターにメッセージを送信して、アクターに何かを実行させます。ここでは代表者が役に立ちます。これらを使用すると、コンパイラにデータを含むクラス、明示的に呼び出す必要のないコンストラクター、およびメソッドを効果的に生成させることができます。多数の小さなクラスを作成する必要がある場合は、デリゲートに切り替えてください。
abstract class Actor
{
Queue<Action> _messages = new Queue<Action>();
protected void Submit(Action action)
{
// take out a lock of course
_messages.Enqueue(action);
}
// also a "run" that reads and executes the
// message delegates on background threads
}
現在、特定の派生アクターは次のパターンに従います。
class ChocolateMachineActor : Actor
{
private void InsertCoinImpl(int value)
{
// whatever...
}
public void InsertCoin(int value)
{
Submit(() => InsertCoinImpl(value));
}
}
したがって、アクターにメッセージを送信するには、パブリック メソッドを呼び出すだけです。プライベート Impl
メソッドが実際の作業を行います。大量のメッセージ クラスを手動で記述する必要はありません。
明らかに、返信に関する部分は省略していますが、パラメータを追加すればすべて実行できます。(上記のアップデートを参照してください)。
他のヒント
スティーブGilhamは、コンパイラが実際にハンドルが労働組合を判別する方法をまとめました。独自のコードのために、あなたはその簡易版を検討することができます。次のF#を考える:
type QueueMessage<T> = ClearMessage | TryDequeueMessage | EnqueueMessage of T
ここではC#で、それをエミュレートする一つの方法だ。
public enum MessageType { ClearMessage, TryDequeueMessage, EnqueueMessage }
public abstract class QueueMessage<T>
{
// prevents unwanted subclassing
private QueueMessage() { }
public abstract MessageType MessageType { get; }
/// <summary>
/// Only applies to EnqueueMessages
/// </summary>
public abstract T Item { get; }
public static QueueMessage<T> MakeClearMessage() { return new ClearMessage(); }
public static QueueMessage<T> MakeTryDequeueMessage() { return new TryDequeueMessage(); }
public static QueueMessage<T> MakeEnqueueMessage(T item) { return new EnqueueMessage(item); }
private sealed class ClearMessage : QueueMessage<T>
{
public ClearMessage() { }
public override MessageType MessageType
{
get { return MessageType.ClearMessage; }
}
/// <summary>
/// Not implemented by this subclass
/// </summary>
public override T Item
{
get { throw new NotImplementedException(); }
}
}
private sealed class TryDequeueMessage : QueueMessage<T>
{
public TryDequeueMessage() { }
public override MessageType MessageType
{
get { return MessageType.TryDequeueMessage; }
}
/// <summary>
/// Not implemented by this subclass
/// </summary>
public override T Item
{
get { throw new NotImplementedException(); }
}
}
private sealed class EnqueueMessage : QueueMessage<T>
{
private T item;
public EnqueueMessage(T item) { this.item = item; }
public override MessageType MessageType
{
get { return MessageType.EnqueueMessage; }
}
/// <summary>
/// Gets the item to be enqueued
/// </summary>
public override T Item { get { return item; } }
}
}
さて、QueueMessage
を与えられたコードでは、あなたはパターンマッチングの代わりにMessageType
プロパティをオンにし、あなただけのItem
sにEnqueueMessage
プロパティにアクセスすることを確認することができます。
編集
ここでジュリエットのコードに基づいて、別の代替です。それはC#からより使いやすいインタフェースを持っているように、私はかかわらず、流線のものにしようとしました。あなたはMethodNotImplemented
例外を得ることができないという点で、これは以前のバージョンよりも好ましいます。
public abstract class QueueMessage<T>
{
// prevents unwanted subclassing
private QueueMessage() { }
public abstract TReturn Match<TReturn>(Func<TReturn> clearCase, Func<TReturn> tryDequeueCase, Func<T, TReturn> enqueueCase);
public static QueueMessage<T> MakeClearMessage() { return new ClearMessage(); }
public static QueueMessage<T> MakeTryDequeueMessage() { return new TryDequeueMessage(); }
public static QueueMessage<T> MakeEnqueueMessage(T item) { return new EnqueueMessage(item); }
private sealed class ClearMessage : QueueMessage<T>
{
public ClearMessage() { }
public override TReturn Match<TReturn>(Func<TReturn> clearCase, Func<TReturn> tryDequeueCase, Func<T, TReturn> enqueueCase)
{
return clearCase();
}
}
private sealed class TryDequeueMessage : QueueMessage<T>
{
public TryDequeueMessage() { }
public override TReturn Match<TReturn>(Func<TReturn> clearCase, Func<TReturn> tryDequeueCase, Func<T, TReturn> enqueueCase)
{
return tryDequeueCase();
}
}
private sealed class EnqueueMessage : QueueMessage<T>
{
private T item;
public EnqueueMessage(T item) { this.item = item; }
public override TReturn Match<TReturn>(Func<TReturn> clearCase, Func<TReturn> tryDequeueCase, Func<T, TReturn> enqueueCase)
{
return enqueueCase(item);
}
}
}
あなたはこのようにこのコードを使用すると思います:
public class MessageUserTest
{
public void Use()
{
// your code to get a message here...
QueueMessage<string> msg = null;
// emulate pattern matching, but without constructor names
int i =
msg.Match(
clearCase: () => -1,
tryDequeueCase: () => -2,
enqueueCase: s => s.Length);
}
}
ユニオン タイプとパターン マッチングは、訪問者のパターンにかなり直接的にマップされます。これについては、以前に何度か投稿しました。
- 関数型プログラミング スタイルで実行するのに最適なタスクは何ですか?
- https://stackoverflow.com/questions/1883246/none-pure-function-code-smells/1884256#1884256
したがって、さまざまなタイプのメッセージを渡したい場合は、訪問者パターンを実装する必要があります。
(警告、テストされていないコードが先にありますが、そのコードがどのように行われるかについては理解できるはずです)
次のようなものがあるとします。
type msg =
| Add of int
| Sub of int
| Query of ReplyChannel<int>
let rec counts = function
| [] -> (0, 0, 0)
| Add(_)::xs -> let (a, b, c) = counts xs in (a + 1, b, c)
| Sub(_)::xs -> let (a, b, c) = counts xs in (a, b + 1, c)
| Query(_)::xs -> let (a, b, c) = counts xs in (a, b, c + 1)
最終的には、次のようなかさばる C# コードが完成します。
interface IMsgVisitor<T>
{
T Visit(Add msg);
T Visit(Sub msg);
T Visit(Query msg);
}
abstract class Msg
{
public abstract T Accept<T>(IMsgVistor<T> visitor)
}
class Add : Msg
{
public readonly int Value;
public Add(int value) { this.Value = value; }
public override T Accept<T>(IMsgVisitor<T> visitor) { return visitor.Visit(this); }
}
class Sub : Msg
{
public readonly int Value;
public Add(int value) { this.Value = value; }
public override T Accept<T>(IMsgVisitor<T> visitor) { return visitor.Visit(this); }
}
class Query : Msg
{
public readonly ReplyChannel<int> Value;
public Add(ReplyChannel<int> value) { this.Value = value; }
public override T Accept<T>(IMsgVisitor<T> visitor) { return visitor.Visit(this); }
}
メッセージに対して何かをしたい場合は、ビジターを実装する必要があります。
class MsgTypeCounter : IMsgVisitor<MsgTypeCounter>
{
public readonly Tuple<int, int, int> State;
public MsgTypeCounter(Tuple<int, int, int> state) { this.State = state; }
public MsgTypeCounter Visit(Add msg)
{
Console.WriteLine("got Add of " + msg.Value);
return new MsgTypeCounter(Tuple.Create(1 + State.Item1, State.Item2, State.Item3));
}
public MsgTypeCounter Visit(Sub msg)
{
Console.WriteLine("got Sub of " + msg.Value);
return new MsgTypeCounter(Tuple.Create(State.Item1, 1 + State.Item2, State.Item3));
}
public MsgTypeCounter Visit(Query msg)
{
Console.WriteLine("got Query of " + msg.Value);
return new MsgTypeCounter(Tuple.Create(State.Item1, 1 + State.Item2, State.Item3));
}
}
そして最終的には次のように使用できます。
var msgs = new Msg[] { new Add(1), new Add(3), new Sub(4), new ReplyChannel(null) };
var counts = msgs.Aggregate(new MsgTypeVisitor(Tuple.Create(0, 0, 0)),
(acc, x) => x.Accept(acc)).State;
はい、一見わかりにくいですが、これがタイプセーフな方法で複数のメッセージをクラスに渡す方法であり、それが C# で共用体を実装しない理由でもあります ;)
とにかくロングショットが、..
私はその区別組合を想定していますと、ADT(抽象データ型)のためのF#です。どちらのタイプは、いくつかのものの一つかもしれないことを意味します。
2がある場合は、あなたがしようとすると2種類のパラメータを持つシンプルな汎用的なクラスにそれを置くことができます:
public struct DiscriminatedUnion<T1,T2>
{
public DiscriminatedUnion(T1 t1) { value = t1; }
public DiscriminatedUnion(T2 t1) { value = t2; }
public static implicit operator T1(DiscriminatedUnion<T1,T2> du) {return (T1)du.value; }
public static implicit operator T2(DiscriminatedUnion<T1,T2> du) {return (T2)du.value; }
object value;
}
それが3以上のために働くようにするには、我々は、このクラスを複数回複製する必要があります。 いずれは、実行時の型に応じて、オーバーロードの機能のためのソリューションを持っている?
あなたはこれを持っている場合は、
type internal Either<'a, 'b> =
| Left of 'a
| Right of 'b
はF#で、その後クラスEither<'a, 'b>
に対して生成CLRのC#の同等のようなインナータイプを有する
internal class _Left : Either<a, b>
{
internal readonly a left1;
internal _Left(a left1);
}
タグ、ゲッターとファクトリメソッドと、各
internal const int tag_Left = 0;
internal static Either<a, b> Left(a Left1);
internal a Left1 { get; }
プラス弁別
internal int Tag { get; }
を実装する方法のラフトインターフェースIStructuralEquatable, IComparable, IStructuralComparable
コンパイル時があります。
private class ClearMessage
{
public static readonly ClearMessage Instance = new ClearMessage();
private ClearMessage() { }
}
private class TryDequeueMessage
{
public static readonly TryDequeueMessage Instance = new TryDequeueMessage();
private TryDequeueMessage() { }
}
private class EnqueueMessage
{
public TValue Item { get; private set; }
private EnqueueMessage(TValue item) { Item = item; }
}
次のように判別組合を使用してを行うことができます:
// New file
// Create an alias
using Message = Union<ClearMessage, TryDequeueMessage, EnqueMessage>;
int ProcessMessage(Message msg)
{
return Message.Match(
clear => 1,
dequeue => 2,
enqueue => 3);
}