Exemple #1
0
        public static PipeOutputPackage Infer(PipeOutputPackage basedOffPackage, Type inputType, Type outputType,
                                              PipeCallback processCallbackFunc)
        {
            int weight = basedOffPackage.NextChainingWeight();

            return(new PipeOutputPackage(weight, inputType, outputType, processCallbackFunc));
        }
        public IEnumerable <PipeOutputPackage> PipePackageInstalled(PipeOutputPackage package)
        {
            if (package.InputType.IsEnumerable() || package.OutputType.IsEnumerable())
            {
                yield break;
            }

            MethodInfo castingMethodInfo = GenericCastingMethodInfo.MakeGenericMethod(package.OutputType);

            Type         inputType           = typeof(IEnumerable <>).MakeGenericType(package.InputType);
            Type         outputType          = typeof(IEnumerable <>).MakeGenericType(package.OutputType);
            PipeCallback processCallbackFunc = (rawInputStream, broker) =>
            {
                var inputEnumerable = (IEnumerable)rawInputStream;
                var pipe            =
                    from input in inputEnumerable.Cast <object>()
                    select package.ProcessInput(input, broker);

                return(Task.Factory.ContinueWhenAll(pipe.ToArray(), tasks => tasks.Select(t => t.Result))
                       .ContinueWith(resultTask =>
                {
                    object results = resultTask.Result;
                    return castingMethodInfo.Invoke(results, new[] { results });
                }));
            };

            yield return(PipeOutputPackage.Infer(package, inputType, outputType, processCallbackFunc));
        }
        public void GivenCallbackWhichReturnsNull_WhenLegallyCalled_ItShouldThrowUnexpectedPipePackageOperationException
            ()
        {
            PipeCallback      processCallbackFunc = (input, broker) => null;
            PipeOutputPackage pipePackageOption   = PipeOutputPackage.Direct(typeof(string), typeof(string),
                                                                             processCallbackFunc);

            Assert.Throws <UnexpectedPipePackageOperationException>(
                async() => await pipePackageOption.ProcessInput("abc", null));
        }
        public async Task GivenEchoCallback_WhenItIsCalledWithInput_ItShouldReturnTheInput()
        {
            PipeCallback      processCallbackFunc = (input, broker) => Task.FromResult(input);
            PipeOutputPackage pipePackageOption   = PipeOutputPackage.Direct(typeof(string), typeof(string),
                                                                             processCallbackFunc);

            object output = await pipePackageOption.ProcessInput("abc", null);

            Assert.AreEqual("abc", output);
        }
        GivenProcessorWhichReturnsString_WhenProcesCallbackReturnsInteger_ItShouldThrowUnexpectedPipePackageOperationException
            ()
        {
            PipeCallback      processCallbackFunc = (input, broker) => Task.FromResult((object)2);
            PipeOutputPackage pipePackageOption   = PipeOutputPackage.Direct(typeof(string), typeof(string),
                                                                             processCallbackFunc);

            Assert.Throws <UnexpectedPipePackageOperationException>(
                async() => await pipePackageOption.ProcessInput("abc", null));
        }
        public void GivenPackageInstance_WhenNullPassedIntoInputParameter_ItShouldThrowArgumentNullException()
        {
            PipeCallback      processCallbackFunc = (input, broker) => null;
            PipeOutputPackage pipePackageOption   = PipeOutputPackage.Direct(typeof(string), typeof(string),
                                                                             processCallbackFunc);

            var argumentNullException =
                Assert.Throws <ArgumentNullException>(async() => await pipePackageOption.ProcessInput(null, null));

            Assert.AreEqual("input", argumentNullException.ParamName);
        }
        public void GivenPackageWhichProcessesIntegers_WhenInputParameterIsStringType_ItShouldThrowNotArgumentException()
        {
            PipeCallback      processCallbackFunc = (input, broker) => Task.FromResult((object)"abc");
            PipeOutputPackage pipePackageOption   = PipeOutputPackage.Direct(typeof(int), typeof(string),
                                                                             processCallbackFunc);

            var argumentException =
                Assert.Throws <ArgumentException>(async() => await pipePackageOption.ProcessInput("abc", null));

            Assert.AreEqual("input", argumentException.ParamName);
        }
        private static PipeOutputPackage CreatePipeOutputPackage <TSource, TDestination>(
            Func <TSource, ISemanticBroker, TDestination> processCallback)
        {
            PipeCallback wrappedProcessCallback = (input, broker) =>
            {
                var    castedInput = (TSource)input;
                object result      = processCallback(castedInput, broker);
                return(result.IntoTaskResult());
            };

            return(PipeOutputPackage.Direct(typeof(TSource), typeof(TDestination), wrappedProcessCallback));
        }
Exemple #9
0
        private PipeOutputPackage ConvertToDataType(Type inputType, PipeOutputPackage basedOffPackage)
        {
            PipeCallback processCallbackFunc = (input, broker) =>
            {
                Array array = Array.CreateInstance(inputType, 1);
                array.SetValue(input, 0);
                return(((object)array).IntoTaskResult());
            };

            Type outputType = typeof(IEnumerable <>).MakeGenericType(inputType);

            return(PipeOutputPackage.Infer(basedOffPackage, inputType, outputType, processCallbackFunc));
        }
        public void Ctor_WhenNullToOutputTypeParameter_ItShouldThrowArgumentNullExcpetion()
        {
            var argumentNullException = Assert.Throws <ArgumentNullException>(() =>
            {
                PipeCallback processCallbackFunc    = (input, broker) => null;
                PipeOutputPackage pipePackageOption = PipeOutputPackage.Direct(typeof(string), null,
                                                                               processCallbackFunc);

                Assert.Fail();
            });

            Assert.AreEqual("outputType", argumentNullException.ParamName);
        }
        private static PipeOutputPackage CreateAsyncPipeOutputPackage <TSource, TDestination>(
            Func <TSource, ISemanticBroker, Task <TDestination> > processCallback)
        {
            PipeCallback wrappedProcessCallback = (input, broker) =>
            {
                var castedInput = (TSource)input;

                return
                    (processCallback(castedInput, broker)
                     .ContinueWith(task => (object)task.Result));
            };

            return(PipeOutputPackage.Direct(typeof(TSource), typeof(TDestination), wrappedProcessCallback));
        }
        private static PipeOutputPackage SimplifyInput(PipeOutputPackage package)
        {
            Type newInputType = DetermineNewInterestType(package.InputType);

            if (newInputType == package.InputType)
            {
                return(package);
            }

            PipeCallback processCallbackFunc = (input, broker) =>
            {
                Func <object, object> transformFunc =
                    TransformerFactory.ConvertFor(newInputType, package.InputType);
                object revisedInput = transformFunc(input);
                return(package.ProcessInput(revisedInput, broker));
            };

            return(PipeOutputPackage.Direct(newInputType, package.OutputType, processCallbackFunc));
        }
Exemple #13
0
        private PipeOutputPackage(int weight, Type inputType, Type outputType, PipeCallback processCallbackFunc)
        {
            if (inputType == null)
            {
                throw new ArgumentNullException("inputType");
            }
            if (outputType == null)
            {
                throw new ArgumentNullException("outputType");
            }
            if (processCallbackFunc == null)
            {
                throw new ArgumentNullException("processCallbackFunc");
            }

            _processCallbackFunc = processCallbackFunc;
            InputType            = inputType;
            OutputType           = outputType;
            Weight = weight;
        }
        private PipeOutputPackage(int weight, Type inputType, Type outputType, PipeCallback processCallbackFunc)
        {
            if (inputType == null)
            {
                throw new ArgumentNullException("inputType");
            }
            if (outputType == null)
            {
                throw new ArgumentNullException("outputType");
            }
            if (processCallbackFunc == null)
            {
                throw new ArgumentNullException("processCallbackFunc");
            }

            _processCallbackFunc = processCallbackFunc;
            InputType = inputType;
            OutputType = outputType;
            Weight = weight;
        }
Exemple #15
0
        public static PipeOutputPackage Bridge(PipeOutputPackage startPackage, PipeOutputPackage endPackage)
        {
            if (startPackage.OutputType != endPackage.InputType)
            {
                throw new NotSupportedException();
            }

            Type sourceType      = startPackage.InputType;
            Type destinationType = endPackage.OutputType;
            int  weight          = startPackage.Weight + endPackage.Weight;

            PipeCallback processCallbackFunc =
                (input, broker) =>
                startPackage.ProcessInput(input, broker)
                .ContinueWith(startTask =>
            {
                object intermediate = startTask.Result;
                return(endPackage.ProcessInput(intermediate, broker));
            }).Unwrap();

            return(new PipeOutputPackage(weight, sourceType, destinationType, processCallbackFunc));
        }
 public static PipeOutputPackage Infer(PipeOutputPackage basedOffPackage, Type inputType, Type outputType,
     PipeCallback processCallbackFunc)
 {
     int weight = basedOffPackage.NextChainingWeight();
     return new PipeOutputPackage(weight, inputType, outputType, processCallbackFunc);
 }
Exemple #17
0
        public static PipeOutputPackage Direct(Type inputType, Type outputType, PipeCallback processCallbackFunc)
        {
            const int weight = 1;

            return(new PipeOutputPackage(weight, inputType, outputType, processCallbackFunc));
        }
 public static PipeOutputPackage Direct(Type inputType, Type outputType, PipeCallback processCallbackFunc)
 {
     const int weight = 1;
     return new PipeOutputPackage(weight, inputType, outputType, processCallbackFunc);
 }