Esempio n. 1
0
        public void GetConfigByNameAndVersion()
        {
            var config = new WorkflowsConfig();

            config.AddDocument(new WorkflowConfig()
            {
                Name    = "wrk1",
                Label   = "wrk1 config",
                Version = 1,
            }
                               .AddFilter("Country", "France")
                               );

            config.AddDocument(new WorkflowConfig()
            {
                Name    = "wrk1",
                Label   = "wrk1 config",
                Version = 2,
            }
                               .AddFilter("Country", "France")
                               );

            Assert.AreEqual(config.Get("wrk1", 2).Version, 2);
            Assert.AreEqual(config.Get("wrk1", 1).Version, 1);
        }
Esempio n. 2
0
        public void GetConfigEventIncoming()
        {
            var config = new WorkflowsConfig();

            config.AddDocument(new WorkflowConfig()
            {
                Name    = "wrk1",
                Label   = "wrk1 config",
                Version = 1,
            }
                               .AddFilter("Country", "France")
                               );

            config.AddDocument(new WorkflowConfig()
            {
                Name    = "wrk2",
                Label   = "wrk2 config",
                Version = 1,
            }
                               .AddFilter("Country", "Germany")
                               );

            var ev = new IncomingEvent()
            {
                Name         = "evnt1",
                Uuid         = Guid.NewGuid(),
                ExternalId   = Guid.NewGuid().ToString(),
                EventDate    = DateTimeOffset.Now,
                CreationDate = DateTimeOffset.Now,
            }
            .AddExtendedDatas("Country", "Germany");

            var item = config.Get(ev).First();

            Assert.AreEqual(item.Name, "wrk2");
        }
Esempio n. 3
0
        public void GetConfigByName()
        {
            var config = new WorkflowsConfig();

            config.AddDocument(new WorkflowConfig()
            {
                Name    = "wrk1",
                Label   = "wrk1 config",
                Version = 1,
            }
                               .AddFilter("Country", "France")
                               );

            var c = config.Get("wrk1").Single();

            Assert.AreEqual(c.Name, "wrk1");
        }
Esempio n. 4
0
        public static void Load(WorkflowsConfig config, Func <WorkflowConfigVisitor> visitorCreator, string path, string searchPattern, TextWriter output = null, TextWriter outputError = null)
        {
            if (visitorCreator == null)
            {
                throw new NullReferenceException(nameof(visitorCreator));
            }

            var items = ParseDirectory(path, searchPattern, output, outputError)
                        .ToList();

            var fragments = items.Where(c => c.IsFragment).ToDictionary(c => c.File);

            foreach (var parser in items.Where(c => !c.IsFragment))
            {
                var visitor = visitorCreator();

                if (parser.Includes.Any())
                {
                    foreach (var include in parser.Includes)
                    {
                        if (!fragments.TryGetValue(include, out WorkflowConfigParser p2))
                        {
                            throw new System.IO.FileNotFoundException($"missing file {include}");
                        }
                        else
                        {
                            visitor.Filename = p2.File;
                            visitor.Visit(p2.Tree);
                        }
                    }
                }

                visitor.Filename = parser.File;
                var document = (WorkflowConfig)visitor.Visit(parser.Tree);

                config.AddDocument(document);
            }
        }
Esempio n. 5
0
        public void InitializeWorkflowWithRule()
        {
            var config  = new WorkflowsConfig();
            var storage = new MemoryStorage();

            config.AddDocument(
                new WorkflowConfig()
            {
                Name = "wrk1", Label = "wrk1 config", Version = 1,
            }
                .AddInitializer(new InitializationOnEventConfig()
            {
                EventName = "evnt1",
            }.AddSwitch("State1"))
                .AddState(new StateConfig()
            {
                Name = "State1", Label = "State1",
            }
                          .AddEvent(new IncomingEventConfig()
            {
                Name = "evnt2"
            }
                                    .AddTransition(new TransitionConfig()
            {
                TargetStateName = "State2", WhenRule = (c) => c.IncomingEvent.Name == "evnt2"
            })
                                    )
                          )
                .AddState(new StateConfig()
            {
                Name = "State2", Label = "State2"
            }
                          )
                );

            var template = new TemplateRepository(typeof(TemplateModels))
            {
                DefaultAction = TemplateModels.DefaultAction,
            };
            var metadatas = new MetadatRepository(typeof(MetadataModels))
            {
                DefaultAction = MetadataModels.DefaultAction.ToDictionary(c => c.Key, c => c.Value),
            };

            var factory = new WorkflowFactory <RunContext>(null, null)
            {
                Serializer = new PartialJsonWorkflowSerializer()
            };

            var processor = new WorkflowProcessor <RunContext>(config, factory)
            {
                LoadExistingWorkflowsByExternalId = (key) => storage.GetBy <Workflow, string>(key, c => c.ExternalId).ToList(),
                OutputActions = () => CreateOutput(new PartialJsonWorkflowSerializer(), storage),
                Templates     = template,
                Metadatas     = metadatas,
            };


            var ev = new IncomingEvent()
            {
                Name         = "evnt1",
                Uuid         = Guid.NewGuid(),
                ExternalId   = Guid.NewGuid().ToString(),
                EventDate    = WorkflowClock.Now(),
                CreationDate = WorkflowClock.Now(),
            };

            processor.EvaluateEvent(ev);
            var wrk = storage.GetAll <Workflow>().FirstOrDefault(c => c.ExternalId == ev.ExternalId);

            Assert.AreEqual(wrk.CurrentState, "State1");


            ev = new IncomingEvent()
            {
                Name         = "evnt2",
                Uuid         = Guid.NewGuid(),
                ExternalId   = Guid.NewGuid().ToString(),
                EventDate    = WorkflowClock.Now(),
                CreationDate = WorkflowClock.Now(),
            };
            processor.EvaluateEvent(ev);
            wrk = storage.GetAll <Workflow>().FirstOrDefault(c => c.ExternalId == ev.ExternalId);
            Assert.AreEqual(wrk, null);
        }
Esempio n. 6
0
        public void InitializeWorkflowWithPushedAction()
        {
            var config  = new WorkflowsConfig();
            var storage = new MemoryStorage();

            config.AddDocument(
                new WorkflowConfig()
            {
                Name = "wrk1", Label = "wrk1 config", Version = 1,
            }

                .AddInitializer(new InitializationOnEventConfig()
            {
                EventName = "evnt1", Recursive = true
            }.AddSwitch("State1"))

                .AddState(new StateConfig()
            {
                Name = "State1", Label = "State1",
            }
                          .AddIncomingActions(null, new ResultActionConfig()
            {
                Name = "act_on_state_in_1"
            })
                          .AddOutcomingActions(null, new ResultActionConfig()
            {
                Name = "act_on_state_out"
            })
                          .AddEvent(new IncomingEventConfig()
            {
                Name = "evnt1"
            }
                                    .AddAction(null, new ResultActionConfig()
            {
                Name = "act_on_event"
            })
                                    .AddTransition(new TransitionConfig()
            {
                TargetStateName = "State2"
            }
                                                   .AddAction(null, new ResultActionConfig()
            {
                Name = "act_on_transition"
            })
                                                   )

                                    )
                          )
                .AddState(new StateConfig()
            {
                Name = "State2", Label = "State2"
            }
                          .AddIncomingActions(null, new ResultActionConfig()
            {
                Name = "act_on_state_in_2"
            }
                                              .AddArgument("name", "@Event.Name")
                                              )

                          )
                );

            var template = new TemplateRepository(typeof(TemplateModels))
            {
                DefaultAction = TemplateModels.DefaultAction,
            };
            var metadatas = new MetadatRepository(typeof(MetadataModels))
            {
                DefaultAction = MetadataModels.DefaultAction.ToDictionary(c => c.Key, c => c.Value),
            };
            var factory = new WorkflowFactory <RunContext>(null, null)
            {
                Serializer = new PartialJsonWorkflowSerializer()
            };
            var processor = new WorkflowProcessor <RunContext>(config, factory)
            {
                LoadExistingWorkflowsByExternalId = (key) => storage.GetBy <Workflow, string>(key, c => c.ExternalId).ToList(),
                OutputActions = () => CreateOutput(new PartialJsonWorkflowSerializer(), storage),
                Templates     = template,
                Metadatas     = metadatas,
            };

            var ev = new IncomingEvent()
            {
                Name         = "evnt1",
                Uuid         = Guid.NewGuid(),
                ExternalId   = Guid.NewGuid().ToString(),
                EventDate    = WorkflowClock.Now(),
                CreationDate = WorkflowClock.Now(),
            };

            processor.EvaluateEvent(ev);
            var wrk = storage.GetAll <Workflow>().FirstOrDefault(c => c.ExternalId == ev.ExternalId);

            var act = wrk.LastEvent.Actions;

            act.First(c => c.Name == "act_on_state_in_1");
            act.First(c => c.Name == "act_on_state_out");
            act.First(c => c.Name == "act_on_event");
            act.First(c => c.Name == "act_on_transition");
        }