예제 #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 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);
        }
        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));
        }
        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 Ctor_WhenNullToProcessCallbackFuncParameter_ItShouldThrowArgumentNullExcpetion()
        {
            var argumentNullException = Assert.Throws <ArgumentNullException>(() =>
            {
                PipeOutputPackage pipePackageOption = PipeOutputPackage.Direct(typeof(string), typeof(string), null);
                Assert.Fail();
            });

            Assert.AreEqual("processCallbackFunc", 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);
        }
        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);
        }
        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));
        }
        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));
        }
예제 #11
0
            public Task <TDestination> Output <TDestination>()
            {
                PipeOutputPackage     solvedPipePackage = _solver.SolveAsPipePackage(typeof(TSource), typeof(TDestination));
                Func <object, object> transformInput    =
                    TransformerFactory.ConvertFor(typeof(TSource), solvedPipePackage.InputType);
                Func <object, object> transformOutput =
                    TransformerFactory.ConvertFor(solvedPipePackage.OutputType, typeof(TDestination));

                var input = transformInput(_source);

                return
                    (solvedPipePackage.ProcessInput(input, _broker)
                     .ContinueWith(task => (TDestination)transformOutput(task.Result)));
            }
        public SemanticBuilder InstallPipe <TSource, TDestination>(
            Func <TSource, ISemanticBroker, Task <TDestination> > processCallback)
        {
            if (processCallback == null)
            {
                throw new ArgumentNullException("processCallback");
            }

            PipeOutputPackage package = CreateAsyncPipeOutputPackage(processCallback);

            _registryMediator.AppendPackage(package);

            return(this);
        }
예제 #13
0
        public void UpdateShortestPath(PipeOutputPackage package)
        {
            bool isShorterPath = RegisterShortestTransistion(package);

            if (!isShorterPath)
            {
                return;
            }

            RegisterEdge(_outgoingEdges, package.InputType, package.OutputType);
            RegisterEdge(_incomingEdges, package.OutputType, package.InputType);

            RecursivelySplayOutgoingTransistions(package);
            RecursivelyBackFillIncomingTransitions(package);
        }
예제 #14
0
        private void RecursivelySplayOutgoingTransistions(PipeOutputPackage package)
        {
            List <Type> nextOutgoingNodes;

            if (!_outgoingEdges.TryGetValue(package.OutputType, out nextOutgoingNodes))
            {
                return;
            }

            IEnumerable <PipeOutputPackage> bridgingPackages =
                from descendantOutgoingNode in nextOutgoingNodes
                where descendantOutgoingNode != package.OutputType
                let transitionKey = new Tuple <Type, Type>(package.OutputType, descendantOutgoingNode)
                                    let outgoingPackage = _shortestTransistions[transitionKey]
                                                          select PipeOutputPackage.Bridge(package, outgoingPackage);

            RecursivelyUpdateShortestPath(bridgingPackages);
        }
예제 #15
0
        private void RecursivelyBackFillIncomingTransitions(PipeOutputPackage package)
        {
            List <Type> nextIncomingNodes;

            if (!_incomingEdges.TryGetValue(package.InputType, out nextIncomingNodes))
            {
                return;
            }

            IEnumerable <PipeOutputPackage> bridgingPackages =
                from ancestorIncomingType in nextIncomingNodes
                where ancestorIncomingType != package.InputType
                let transitionKey = new Tuple <Type, Type>(ancestorIncomingType, package.InputType)
                                    let incomingPackage = _shortestTransistions[transitionKey]
                                                          select PipeOutputPackage.Bridge(incomingPackage, package);

            RecursivelyUpdateShortestPath(bridgingPackages);
        }
예제 #16
0
        private bool RegisterShortestTransistion(PipeOutputPackage package)
        {
            var keyToLookup = new Tuple <Type, Type>(package.InputType, package.OutputType);
            PipeOutputPackage currentlyKnownPath;

            if (_shortestTransistions.TryGetValue(keyToLookup, out currentlyKnownPath))
            {
                if (package.Weight < currentlyKnownPath.Weight)
                {
                    _shortestTransistions[keyToLookup] = package;
                    return(true);
                }
            }
            else
            {
                _shortestTransistions.Add(keyToLookup, package);
                return(true);
            }

            return(false);
        }
예제 #17
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 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);
 }
 public IEnumerable <PipeOutputPackage> PipePackageInstalled(PipeOutputPackage package)
 {
     return(_callbackFunc(package));
 }
 public IEnumerable<PipeOutputPackage> PipePackageInstalled(PipeOutputPackage package)
 {
     return _callbackFunc(package);
 }