Search code examples
moryx

How do I create a WorkplanStep that checks a condition based on the previous process?


I need to create a WorkplanStep in a MORYX application that evaluates a condition based on the outcome of the previous activity. Depending on this evaluation, it should produce either “Success” or “Failure”.

I created a new WorkplanStep but don't know how to implement this dependency in the code.


Solution

  • Here are the steps to create such a WorkplanStep:

    Create a Custom WorkplanStep Class:

    You’ll need to create a custom class for your WorkplanStep. This class will represent the step in which you want to make the decision.

    Here’s a general version of such a class:

    public class CustomConditionStep : WorkplanStepBase
    {
        public CustomConditionStep()
        {
            // Initialize your custom step here
            // ...
    
            // Set the outputs for example
            Name = "Custom Condition Step";
                Outputs = new IConnector[2];
                OutputDescriptions = new[]
                {
                    new OutputDescription{Name = "Success", OutputType = OutputType.Success},
                    new OutputDescription{Name = "Failure", OutputType = OutputType.Alternative},
                };
    
        }
    
        protected override TransitionBase Instantiate(IWorkplanContext context)
        {
            // Create an instance of your custom transition class
            // ...
        }
    }
    

    Create a Custom Transition Class:

    The transition class determines how the step reacts to specific events. Here’s a more detailed version of such a transition class:

    public class CustomConditionTransition : TransitionBase
    {
        private IProcess _process;
    
        public CustomConditionTransition(IProcess process)
        {
            _process = process;
        }
    
        protected override void InputTokenAdded(object sender, IToken token)
        {
            // Implement your logic here
            // For example, check if a certain condition is met
            // ...
    
            if (/* Your condition here */)
                PlaceToken(Outputs[0], token); // Success
            else
                PlaceToken(Outputs[1], token); // Failure
        }
    }
    

    By the way, you need the part with the constructor and the process only if you want to make a decision based on the process.

    If you want to add a condition, you can modify the logic in the CustomConditionTransition class. For example, you can check with an if/else-statement a specific condition and decide whether the process was successful or not.