public Catching( ITransducer <TInput, TResult> successTransducer, ITransducer <ExceptionalInput <TInput, TException>, TResult> exceptionalTransducer) { Success = successTransducer; Exceptional = exceptionalTransducer; }
// from ISender public ISender <B> Add <A, B> (ITransducer <A, B> tr) where A : T { IReceiver <T> receiver = (IReceiver <T>)tr; Add(receiver); return(tr); }
private static string GetNonFinalRuleInfo <S>(int k, IAutomaton <S> aut, ITransducer <S> trans, int source, int target, List <S> rules, Func <S, string> describeS = null) { if (describeS == null) { describeS = aut.DescribeLabel; } string lab = ""; string info = ""; for (int i = 0; i < rules.Count; i++) { lab += (lab == "" ? "" : ", ") + describeS(rules[i]); if (trans != null) { info += string.Format("Rule{0}.Guard = \"{1}\" Rule{0}.Update = \"{2}\" Rule{0}.Yields = \"{3}\" ", i + 1, EncodeChars(trans.DescribeGuard(rules[i])), EncodeChars(trans.DescribeUpdate(rules[i])), EncodeChars(trans.DescribeYields(rules[i]))); } } if (k >= 0 && lab.Length > k) { lab = lab.Substring(0, k) + "..."; } lab = EncodeChars(lab); if (lab.Length > 50) { info += string.Format(" HiddenLabel = \"{0}\"", lab); lab = "..."; } return(string.Format("<Link Source=\"{0}\" Target=\"{1}\" Label=\"{2}\" Category=\"NonepsilonTransition\" {3}/>", source, target, lab, info)); }
public Composing( ITransducer <TInput, TMedium> left, ITransducer <TMedium, TResult> right) { Sync = left.SyncCompose(right); Async = left.AsyncCompose(right); }
/// <summary> /// Takes the input enumerable and produces a new enumerable by passing /// each value through the supplied transducer one-by-one and yielding /// the results. /// </summary> /// <typeparam name="TInput">The type of the input.</typeparam> /// <typeparam name="TResult">The type of the result.</typeparam> /// <param name="input">The input.</param> /// <param name="transducer">The transducer.</param> /// <returns> /// An enumerable that is a sequence of values after the input is applied to /// the supplied transducer. /// </returns> public static IEnumerable <TResult> Transduce <TInput, TResult>( this IEnumerable <TInput> input, ITransducer <TInput, TResult> transducer) { var reducer = transducer.Apply(Enumerating.ListReducer <TResult>()); var list = new List <TResult>(); foreach (var value in input) { var reduction = reducer.Invoke(list, value); foreach (var result in reduction.Value) { yield return(result); } if (reduction.IsTerminated) { yield break; } list.Clear(); } var completionReduction = reducer.Complete(list); foreach (var result in completionReduction.Value) { yield return(result); } }
/// <summary> /// Composes two transducers together by attaching the output of the first to the input of the second. /// </summary> /// <typeparam name="A">The input.</typeparam> /// <typeparam name="B">b</typeparam> /// <typeparam name="C">c</typeparam> /// <typeparam name="D">d</typeparam> /// <typeparam name="E">e</typeparam> /// <typeparam name="F">f</typeparam> /// <typeparam name="G">The result.</typeparam> /// <param name="first">The first.</param> /// <param name="second">The second.</param> /// <param name="third">The third.</param> /// <param name="fourth">The fourth.</param> /// <param name="fifth">The fifth.</param> /// <param name="sixth">The sixth.</param> /// <returns>A transducer composed from the input transducers.</returns> public static ITransducer <A, G> Compose <A, B, C, D, E, F, G>( ITransducer <A, B> first, ITransducer <B, C> second, ITransducer <C, D> third, ITransducer <D, E> fourth, ITransducer <E, F> fifth, ITransducer <F, G> sixth) => Compose(first, second, third, fourth, fifth).Compose(sixth);
public TransducerChannel(ITransducer parent, string name, string unit, enuPrefix prefix, enuChannelType channelType, enuTChannelStatus status, int averaging = 1) { this.parent = parent; Name = name; Unit = unit; Prefix = prefix; ChannelType = channelType; Status = status; Averaging = averaging; }
private static string GetFinalRuleInfo <S>(int k, ITransducer <S> trans, int state, List <S> rules) { if (trans == null) { throw new AutomataException(AutomataExceptionKind.InternalError); } string lab = ""; string info = ""; for (int i = 0; i < rules.Count; i++) { lab += (lab == "" ? "" : ", ") + trans.DescribeLabel(rules[i]); info += string.Format("Rule{0}.Guard = \"{1}\" Rule{0}.Yields = \"{2}\" ", i + 1, EncodeChars(trans.DescribeGuard(rules[i])), EncodeChars(trans.DescribeYields(rules[i]))); } if (k >= 0 && lab.Length > k) { lab = lab.Substring(0, k) + "..."; } lab = EncodeChars(lab); return(string.Format("<Link Source=\"{0}\" Target=\"f{0}\" Label=\"{1}\" Category=\"FinalLabel\" {2}/>", state, lab, info)); }
/// <summary> /// Creates the default switch for a Switching transducer. Any input that isn't captured by a /// previous switch in the Switching transducer will fall into this transducer. /// </summary> /// <typeparam name="TInput">The type of the input.</typeparam> /// <typeparam name="TResult">The type of the result.</typeparam> /// <param name="transducer">The transducer.</param> /// <returns>A default switch for a Switching transducer.</returns> public static TransducerSwitch <TInput, TResult> Default <TInput, TResult>(ITransducer <TInput, TResult> transducer) => Create(_ => true, transducer);
/// <summary> /// Produces a feedback transducer composed with this transducer. /// </summary> /// <typeparam name="TInput">The input to the new transducer</typeparam> /// <typeparam name="TResult">The result and feedback type</typeparam> /// <param name="transducer">The original transducer</param> /// <param name="operation">The feedback operation.</param> /// <returns>A feedback transducer</returns> public static ITransducer <TInput, TResult> Feedback <TInput, TResult>( this ITransducer <TInput, TResult> transducer, ITransducer <TResult, TResult> operation) => transducer.Compose(Feedback(operation));
public static IReducer <TextWriter, TInput> WriteReducer <TInput, TResult>( this ITransducer <TInput, TResult> transducer) => transducer.Apply(WriteReducer <TResult>());
/// <summary> /// Creates a switch for a Switching transducer that pushes results into the supplied transducer if /// an unmatched input matches the supplied test. /// </summary> /// <typeparam name="TInput">The type of the input.</typeparam> /// <typeparam name="TResult">The type of the result.</typeparam> /// <param name="test">The test.</param> /// <param name="transducer">The transducer.</param> /// <returns>A switch for a Switching transducer.</returns> public static TransducerSwitch <TInput, TResult> Create <TInput, TResult>(Predicate <TInput> test, ITransducer <TInput, TResult> transducer) => new TransducerSwitch <TInput, TResult>(test, transducer);
/// <summary> /// Produces a transducer that composes the input transducer and restricts the type of the results from it. /// </summary> /// <typeparam name="TInput">The input to the new transducer</typeparam> /// <typeparam name="TBase">The original type of the transducer</typeparam> /// <typeparam name="TDerived">The restricted type of the transducer</typeparam> /// <param name="transducer">The original transducer</param> /// <returns>A new transducer that restricts the produced type</returns> public static ITransducer <TInput, TDerived> Casting <TInput, TBase, TDerived>( this ITransducer <TInput, TBase> transducer) where TDerived : TBase => transducer.Compose(Casting <TBase, TDerived>());
/// <summary> /// Produces a transducer that parses the results of the supplied transducer. /// </summary> /// <typeparam name="TInput">The type of the input.</typeparam> /// <typeparam name="TResult">The type of the result.</typeparam> /// <param name="transducer">The transducer.</param> /// <returns>A transducer whose results are parsed from the results of the supplied transducer.</returns> public static ITransducer <TInput, TResult> Parsing <TInput, TResult>( this ITransducer <TInput, string> transducer) where TResult : struct => transducer.Compose(Parsing <TResult>());
private static bool IsSameState(ITransducer <NpcState, GameEvent, NpcAction> tRunner, ITransducer sRunner) { return(tRunner.State.ToString() == sRunner.State); }
/// <summary> /// Produces a transducer that takes multiple values from this transducer at a time. /// Each input pushes previous values out of the window of values supplied. Does not /// produce a window until it's filled. /// </summary> /// <typeparam name="TInput">The input to the original and new transducer</typeparam> /// <typeparam name="TResult">The result of the original transducer</typeparam> /// <param name="transducer">The original transducer</param> /// <param name="windowSize">The size of the produced window</param> /// <returns>A sliding transducer</returns> public static ITransducer <TInput, IList <TResult> > Sliding <TInput, TResult>( this ITransducer <TInput, TResult> transducer, int windowSize) => transducer.Compose(Sliding <TResult>(windowSize));
public AsyncPrimary( ITransducer <TInput, TInput> operation, IAsyncReducer <TReduction, TInput> next) { Next = operation.Apply(new AsyncTail <TReduction>(this, next)); }
/// <summary> /// Instantiates a transducer that passes values to a supplied 'success' transducer and catches exceptions in subsequent /// reductions and passes the result to an exception handling transducer. /// </summary> /// <typeparam name="TInput">The input type</typeparam> /// <typeparam name="TResult">The result of types produced by both the success and exceptional transducers</typeparam> /// <typeparam name="TException">The type of exceptions to catch.</typeparam> /// <param name="success">The primary transducer that input is passed to.</param> /// <param name="exceptional">The transducer that handles exceptional cases.</param> /// <returns></returns> public static ITransducer <TInput, TResult> Catching <TInput, TResult, TException>( ITransducer <TInput, TResult> success, ITransducer <ExceptionalInput <TInput, TException>, TResult> exceptional) where TException : Exception => new Catching <TInput, TResult, TException>(success, exceptional);
/// <summary> /// Write the FSA in dot format. /// </summary> /// <param name="fa">the FSA to write</param> /// <param name="faName">the name of the FSA</param> /// <param name="tw">text writer for the output</param> /// <param name="rankdir">the main direction of the arrows</param> /// <param name="fontsize">the size of the font in labels</param> /// <param name="descr">function that describes the labels as strings</param> public static void AutomatonToDot <S>(Func <S, string> descr, IAutomaton <S> fa, string faName, System.IO.TextWriter tw, bool showName, Config config) { // TODO: find why is this here ITransducer <S> faf = fa as ITransducer <S>; // TODO: this is ineffective, hoist the faf == null check before the delegate assignment Func <S, bool> isfinal = lab => { return(faf == null ? false : faf.IsFinalRule(lab)); }; // TODO: this block is mostly unused! clean up! // only finalLabels are used List <Move <S> > epsilonmoves = new List <Move <S> >(); Dictionary <Tuple <int, int>, string> nonepsilonMoves = new Dictionary <Tuple <int, int>, string>(); Dictionary <int, string> finalLabels = new Dictionary <int, string>(); foreach (var move in fa.GetMoves()) { if (move.IsEpsilon) { epsilonmoves.Add(move); } else if (isfinal(move.Label)) { string conLab = descr(move.Label); if (!string.IsNullOrEmpty(conLab)) { string lab; if (finalLabels.TryGetValue(move.SourceState, out lab)) { lab = lab + ", " + conLab; } else { lab = conLab; } finalLabels[move.SourceState] = lab; } } else { var key = new Tuple <int, int>(move.SourceState, move.TargetState); string lab; if (nonepsilonMoves.TryGetValue(key, out lab)) { lab = lab + ", " + descr(move.Label); } else { lab = descr(move.Label); } nonepsilonMoves[key] = lab; } } string sanitize(string input) { return (input .Replace("\\", "\\\\") .Replace("\"", "\\\"") //.Replace("<", "\\<") //.Replace(">", "\\>") ); } string openLabelTag = config.htmlLabels ? "<" : "\""; string closeLabelTag = config.htmlLabels ? ">" : "\""; tw.WriteLine("digraph \"" + faName + "\" {"); tw.WriteLine(string.Format("rankdir={0}; fontsize={1};", config.rankdir.ToString(), config.fontsize)); tw.WriteLine(); tw.WriteLine("//Defaults"); tw.WriteLine(string.Format($"node [style = filled, shape = " + config.shape.ToString() + ", fillcolor = white, fontsize = {0}, margin = 0.01]", config.fontsize)); tw.WriteLine(string.Format("edge [ fontsize = {0} ]", config.fontsize)); tw.WriteLine(); tw.WriteLine("//Initial state format"); { showName = showName && !string.IsNullOrEmpty(faName); tw.WriteLine(string.Format("preInit [shape = plaintext, color = {1}, label = \"{0}\"]", (showName ? faName + ": " : ""), (showName ? "black" : "white"))); } tw.WriteLine(); tw.WriteLine("// All other states format"); foreach (int state in fa.GetStates()) { if (fa.IsFinalState(state) && !finalLabels.ContainsKey(state)) { tw.WriteLine(string.Format("{0} [peripheries=2]", state)); } if (fa.IsFinalState(state) && finalLabels.ContainsKey(state)) { tw.WriteLine(string.Format("{0} []", state)); tw.WriteLine(string.Format("f{0} [shape = box, label=\"\", peripheries=2]", state)); } tw.WriteLine(string.Format("{0} [label = " + openLabelTag + "{1}" + closeLabelTag + "]", state, sanitize(fa.DescribeState(state)))); } tw.WriteLine(); tw.WriteLine("//Transitions"); tw.WriteLine(string.Format("preInit -> {0}", fa.InitialState)); foreach (Move <S> t in fa.GetMoves()) { if (!isfinal(t.Label)) { tw.WriteLine(string.Format("{0} -> {1} [label = " + openLabelTag + "{2}" + closeLabelTag + (t.IsEpsilon ? ", style=dashed" : "") + "];", t.SourceState, t.TargetState, t.IsEpsilon ? "ε" : sanitize(descr(t.Label)) )); } else if (finalLabels.ContainsKey(t.SourceState)) { tw.WriteLine(string.Format("{0} -> {1} [label = " + openLabelTag + "{2}" + closeLabelTag + "];", t.SourceState, "f" + t.TargetState, sanitize(finalLabels[t.SourceState]) )); } } tw.WriteLine("}"); }
public static ITransducer <TInput, TResult> UncheckedSumming <TInput, TResult>( this ITransducer <TInput, IList <TResult> > transducer) where TResult : struct => transducer.Compose(UncheckedSumming <TResult>());
/// <summary> /// Passes the values from the enumerable through the supplied transducer and /// collects the results into a list. /// </summary> /// <typeparam name="TInput">The type of the input.</typeparam> /// <typeparam name="TResult">The type of the result.</typeparam> /// <param name="input">The input.</param> /// <param name="transducer">The transducer.</param> /// <returns>A list of the computed results.</returns> public static IList <TResult> Collect <TInput, TResult>( this IEnumerable <TInput> input, ITransducer <TInput, TResult> transducer) => input.Reduce(new List <TResult>(), transducer.Apply(ListReducer <TResult>())).Value;
/// <summary> /// Any value applied to this transducer will be passed through the supplied transducer. The resulting /// value will first be applied to the given reducing function, then will be re-applied to the internal /// reducing function. /// </summary> /// <typeparam name="T">The type of the input and result.</typeparam> /// <param name="operation">The feedback operation</param> /// <returns>A feedback transducer</returns> public static ITransducer <T, T> Feedback <T>(ITransducer <T, T> operation) => new Feedback <T>(operation);
public ComposeTransducer(ITransducer <TIn, TMid> transducer1, ITransducer <TMid, TOut> transducer2) { _transducer1 = transducer1; _transducer2 = transducer2; }
/// <summary> /// Write the automaton in dgml format. /// </summary> /// <param name="fa">the automaton to write</param> /// <param name="tw">text writer for the output</param> public static void AutomatonToDgml <S>(int k, IAutomaton <S> fa, string name, System.IO.TextWriter tw, Func <S, string> describeS = null) { ITransducer <S> faf = fa as ITransducer <S>; bool isTransducer = (faf != null); Func <S, bool> isfinal = lab => { return(isTransducer ? faf.IsFinalRule(lab) : false); }; var finalMoves = new Dictionary <int, List <S> >(); var nonFinalMoves = new Dictionary <Tuple <int, int>, List <S> >(); var epsilonmoves = new List <Move <S> >(); var nonEpsilonStates = new HashSet <int>(); Func <int, bool> IsEpsilonState = (s => !nonEpsilonStates.Contains(s)); foreach (var move in fa.GetMoves()) { if (move.IsEpsilon) { epsilonmoves.Add(move); } else if (isfinal(move.Label) && !(faf.IsGuardTrue(move.Label) && faf.GetYieldsLength(move.Label) == 0)) { List <S> rules; if (!finalMoves.TryGetValue(move.SourceState, out rules)) { rules = new List <S>(); finalMoves[move.SourceState] = rules; } rules.Add(move.Label); } else if (!isfinal(move.Label)) { nonEpsilonStates.Add(move.SourceState); List <S> rules; var p = new Tuple <int, int>(move.SourceState, move.TargetState); if (!nonFinalMoves.TryGetValue(p, out rules)) { rules = new List <S>(); nonFinalMoves[p] = rules; } rules.Add(move.Label); } } tw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); tw.WriteLine("<DirectedGraph xmlns=\"http://schemas.microsoft.com/vs/2009/dgml\" ZoomLevel=\"1.5\" GraphDirection=\"TopToBottom\" >"); tw.WriteLine("<Nodes>"); tw.WriteLine("<Node Id=\"init\" Label=\"{0}\" Stroke=\"white\" Background=\"white\"/>", name); foreach (int state in fa.GetStates()) { if (state == fa.InitialState && fa.IsFinalState(state)) { tw.WriteLine("<Node Id=\"{0}\" Label=\"{1}\" Category=\"State\" >", state, fa.DescribeState(state)); if (!finalMoves.ContainsKey(state)) { //if (IsEpsilonState(state)) // tw.WriteLine("<Category Ref=\"EpsilonState\" />"); //else tw.WriteLine("<Category Ref=\"FinalState\" />"); } //tw.WriteLine("<Category Ref=\"InitialState\" />"); tw.WriteLine("</Node>"); if (finalMoves.ContainsKey(state)) { tw.WriteLine("<Node Id=\"f{0}\" Label=\" \" Category=\"State\" >", state); tw.WriteLine("<Category Ref=\"FinalState\" />"); tw.WriteLine("<Category Ref=\"SinkState\" />"); tw.WriteLine("</Node>"); } } else if (state == fa.InitialState) { tw.WriteLine("<Node Id=\"{0}\" Label=\"{1}\" Category=\"State\" >", state, fa.DescribeState(state)); tw.WriteLine("<Category Ref=\"InitialState\" />"); tw.WriteLine("</Node>"); } else if (fa.IsFinalState(state)) { tw.WriteLine("<Node Id=\"{0}\" Label=\"{1}\" Category=\"State\" >", state, fa.DescribeState(state)); if (!finalMoves.ContainsKey(state)) { //if (IsEpsilonState(state)) // tw.WriteLine("<Category Ref=\"EpsilonState\" />"); //else tw.WriteLine("<Category Ref=\"FinalState\" />"); } tw.WriteLine("</Node>"); if (finalMoves.ContainsKey(state)) { tw.WriteLine("<Node Id=\"f{0}\" Label=\" \" Category=\"State\" >", state); tw.WriteLine("<Category Ref=\"FinalState\" />"); tw.WriteLine("<Category Ref=\"SinkState\" />"); tw.WriteLine("</Node>"); } } else { tw.WriteLine("<Node Id=\"{0}\" Label=\"{1}\" Category=\"State\" />", state, fa.DescribeState(state)); } } tw.WriteLine("</Nodes>"); tw.WriteLine("<Links>"); tw.WriteLine("<Link Source=\"init\" Target=\"{0}\" Label=\"{1}\" Category=\"StartTransition\" />", fa.InitialState, fa.DescribeStartLabel()); foreach (var move in epsilonmoves) { tw.WriteLine("<Link Source=\"{0}\" Target=\"{1}\" Category=\"EpsilonTransition\" />", move.SourceState, move.TargetState); } foreach (var move in nonFinalMoves) { tw.WriteLine(GetNonFinalRuleInfo(k, fa, faf, move.Key.Item1, move.Key.Item2, move.Value, describeS)); } foreach (var move in finalMoves) { tw.WriteLine(GetFinalRuleInfo(k, faf, move.Key, move.Value)); } tw.WriteLine("</Links>"); WriteCategoriesAndStyles(tw); tw.WriteLine("</DirectedGraph>"); }
/// <summary> /// Passes results from some TextReader through the supplied transducer to some TextWriter. /// </summary> /// <typeparam name="TResult">The type the transducer produces.</typeparam> /// <param name="input">The input.</param> /// <param name="output">The output.</param> /// <param name="transducer">The transducer.</param> public static void Transduce <TResult>( this TextReader input, TextWriter output, ITransducer <string, TResult> transducer) => input.Reduce(output, transducer.Apply(WriteReducer <TResult>()));
public Feedback(ITransducer <TInput, TInput> operation) { Operation = operation; }
/// <summary> /// Creates a transducer that passes all input through all of the supplied transducers. /// </summary> /// <typeparam name="TInput">The input to the original transducer</typeparam> /// <typeparam name="TMultiInput">The input to the multi-transducers</typeparam> /// <typeparam name="TResult">The result of each transducer</typeparam> /// <param name="transducer">The original transducer</param> /// <param name="transducers">The multiplexed transducers</param> /// <returns>A transducer that multiplexes the original transducers' results</returns> public static ITransducer <TInput, TResult> Multiplexing <TInput, TMultiInput, TResult>( this ITransducer <TInput, TMultiInput> transducer, params ITransducer <TMultiInput, TResult>[] transducers) => transducer.Compose(Multiplexing(transducers));