Ejemplo 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);
        }
Ejemplo n.º 2
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");
        }
Ejemplo n.º 3
0
        private WorkflowsConfig GetConfigurations(DirectoryInfo d, string extension)
        {
            if (!extension.StartsWith("*."))
            {
                if (extension.StartsWith("."))
                {
                    extension = "*" + extension;
                }
                else
                {
                    extension = "*." + extension;
                }
            }

            WorkflowsConfig configs = new WorkflowsConfig();

            Func <WorkflowConfigVisitor> f = () =>
            {
                WorkflowConfigVisitor visitor = new WorkflowConfigVisitor();

                foreach (var method in this._methods)
                {
                    visitor.AddRule(method.Key, method.Value);
                }

                return(visitor);
            };

            var files = d.GetFiles(extension, SearchOption.AllDirectories).ToList();
            HashSet <string> _paths = new HashSet <string>();

            foreach (var item in files)
            {
                if (_paths.Add(item.Directory.FullName))
                {
                    WorkflowsConfigLoader.Load(configs, f, item.Directory.FullName, extension, null, null);
                }
            }

            return(configs);
        }
Ejemplo 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);
            }
        }
Ejemplo n.º 5
0
        private WorkflowEngine CreateEngine(MemoryStorage storage, string configText)
        {
            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 serializer = new PartialJsonWorkflowSerializer();

            WorkflowsConfig configs = new WorkflowsConfig()
                                      .AddDocument(GetConfig(configText))
            ;

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

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

            WorkflowEngine engine = new WorkflowEngine()
            {
                Processor = processor,
            };

            return(engine);
        }
Ejemplo n.º 6
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");
        }
Ejemplo n.º 7
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);
        }
Ejemplo n.º 8
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");
        }