コード例 #1
0
ファイル: UnitTest1.cs プロジェクト: Sergyu/WMS
        private TreeModel buildModel(Man man)
        {
            var growNode          = new ActionNode <Man>("GrowAgeAndHealth");
            var goToWork          = new DecisionNode <Man>("DecideIFCanWork");
            var startDying        = new DecisionNode <Man>("DecideIFDye");
            var workHard          = new ActionNode <Man>("Work");
            var deadNode          = new ActionNode <Man>("Dead");
            var isSportish        = new DecisionNode <Man>("IfSportishContext");
            var fromManToSportish = new TransformationNode <Man, Sport>("FromManToSport");
            var practiceSport     = new ActionNode <Sport>("DoSport");
            var sportToMan        = new TransformationNode <Sport, Man>("SportToMan");

            growNode.AddChild(goToWork);
            growNode.SetAction(m => { m.Age++; m.Health++; });

            goToWork.SetCondition(m => m.Age > 18);
            goToWork.SetYesChild(workHard);
            goToWork.SetNoChild(growNode);

            workHard.AddChild(isSportish);
            workHard.SetAction(m => { m.Health--; m.Age++; });

            isSportish.SetNoChild(startDying);
            isSportish.SetYesChild(fromManToSportish);
            isSportish.SetCondition(m => m.Age < 60);

            fromManToSportish.AddChild(practiceSport);
            fromManToSportish.SetTranslator(m => new Sport(m.Age));


            practiceSport.SetAction(s => s.Do());
            practiceSport.AddChild(sportToMan);

            sportToMan.SetTranslator(sport =>
            {
                man.Health += sport.GainedHealth;
                return(man);
            });
            sportToMan.AddChild(startDying);

            startDying.SetCondition(m => m.Health < 5);
            startDying.SetNoChild(workHard);
            startDying.SetYesChild(deadNode);

            var model = new TreeModel(100);

            model.SetStartNode(growNode);
            model.SetEndNode(deadNode);
            Console.WriteLine("Done");
            return(model);
        }
コード例 #2
0
        private TreeModel newModel(Document document)
        {
            var model            = new TreeModel(100);
            var documentSplit    = new SplitterNode <Document, Part>("FromDocToPart");
            var part1Signing     = new ActionNode <Part>("Part0");
            var part1IsValid     = new DecisionNode <Part>("DecisionPar0");
            var part2Signing     = new ActionNode <Part>("Part1");
            var part2IsValid     = new DecisionNode <Part>("DecisionPar1");
            var part3Signing     = new ActionNode <Part>("Part2");
            var part3IsValid     = new DecisionNode <Part>("DecisionPar2");
            var partAggregation  = new AggregatorNode <Part, Document>("AssembleDocument");
            var isDocumentSigned = new DecisionNode <Document>("IsDocSigned");
            var stop             = new ActionNode <Document>("Stop");

            documentSplit.AddChild(part1Signing);
            documentSplit.AddChild(part2Signing);
            documentSplit.AddChild(part3Signing);

            part1Signing.AddChild(part1IsValid);
            part1IsValid.SetYesChild(partAggregation);
            part1IsValid.SetNoChild(part1Signing);

            part2Signing.AddChild(part2IsValid);
            part2IsValid.SetYesChild(partAggregation);
            part2IsValid.SetNoChild(part2Signing);

            part3Signing.AddChild(part3IsValid);
            part3IsValid.SetYesChild(partAggregation);
            part3IsValid.SetNoChild(part3Signing);

            partAggregation.AddChild(isDocumentSigned);

            isDocumentSigned.SetNoChild(documentSplit);
            isDocumentSigned.SetYesChild(stop);


            documentSplit.SetSplitter((doc, node) =>
            {
                for (int i = 0; i < doc.Parts.Count; i++)
                {
                    if (node.Name.Contains(i.ToString()))
                    {
                        return(doc.Parts[i]);
                    }
                }
                throw null;
            });

            part1Signing.SetAction(part => part.Signer.Signe(part));
            part2Signing.SetAction(part => part.Signer.Signe(part));
            part3Signing.SetAction(part => part.Signer.Signe(part));

            part1IsValid.SetCondition(part => part.Signed);
            part2IsValid.SetCondition(part => part.Signed);
            part3IsValid.SetCondition(part => part.Signed);

            partAggregation.SetAggregator((part, doc) => { });

            isDocumentSigned.SetCondition(doc => {
                return(doc.IsSigned);
            });

            model.SetStartNode(documentSplit);
            model.SetEndNode(stop);

            return(model);
        }