//we do this not to accidentatlly add duplicates
        protected void ClearNavigationProperties(PlanNodeDO entity)
        {
            entity.Fr8Account     = null;
            entity.ParentPlanNode = null;

            var activity = entity as ActivityDO;

            if (activity != null)
            {
                activity.AuthorizationToken = null;
                activity.ActivityTemplate   = null;
            }
        }
Esempio n. 2
0
        public LoadedPlan(PlanNodeDO root, bool isNew = false)
        {
            IsNew = isNew;
            Root  = root;

            if (isNew)
            {
                Snapshot = new PlanSnapshot();
            }
            else
            {
                RebuildSnapshot();
            }
        }
Esempio n. 3
0
        private static void FixParentActivityReferences(PlanNodeDO root)
        {
            var activitiesIndex = new Dictionary <Guid, PlanNodeDO>();

            TraverseActivityTree(root, activitiesIndex);

            foreach (var activityDo in activitiesIndex.Values)
            {
                PlanNodeDO temp = null;

                if (activityDo.ParentPlanNodeId != null)
                {
                    activitiesIndex.TryGetValue(activityDo.ParentPlanNodeId.Value, out temp);
                }

                activityDo.ParentPlanNode = temp;
            }
        }
Esempio n. 4
0
        /**********************************************************************************/

        public static PlanNodeDO CloneWithStructure(PlanNodeDO source, Action <PlanNodeDO> nodeCallback = null)
        {
            var clone = source.Clone();

            if (nodeCallback != null)
            {
                nodeCallback(clone);
            }

            foreach (var child in source.ChildNodes)
            {
                var clonedChild = CloneWithStructure(child, nodeCallback);

                clonedChild.ParentPlanNode = clone;
                clone.ChildNodes.Add(clonedChild);
            }

            return(clone);
        }
            /**********************************************************************************/
            // Executes node is passed if it is an activity
            private async Task ExecuteNode(PlanNodeDO currentNode, IUpdatableCrateStorage payloadStorage, ActivityExecutionMode mode)
            {
                var currentActivity = currentNode as ActivityDO;

                if (currentActivity == null)
                {
                    return;
                }

                _utilizationMonitoringService.TrackActivityExecution(currentActivity, _container);

                var payload = await _activity.Run(_uow, currentActivity, mode, _container);

                if (payload != null)
                {
                    var activityPayloadStroage = _crate.FromDto(payload.CrateStorage);

                    SyncPayload(activityPayloadStroage, payloadStorage, currentActivity.Id.ToString());
                }
            }
Esempio n. 6
0
        private PlanNodeDO GetNextActivity(PlanNodeDO currentActivity, bool depthFirst, PlanNodeDO root)
        {
            // Move to the first child if current activity has nested ones
            if (depthFirst && currentActivity.ChildNodes.Count != 0)
            {
                return(currentActivity.ChildNodes.OrderBy(x => x.Ordering).FirstOrDefault());
            }

            // Move to the next activity of the current activity's parent
            if (currentActivity.ParentPlanNode == null)
            {
                // We are at the root of activity tree. Next activity can be only among children.
                return(null);
            }

            var prev          = currentActivity;
            var nextCandidate = currentActivity.ParentPlanNode.ChildNodes
                                .OrderBy(x => x.Ordering)
                                .FirstOrDefault(x => x.Ordering > currentActivity.Ordering);

            /* No more activities in the current branch
             *          a
             *       b     c
             *     d   E  f  g
             *
             * We are at E. Get next activity as if current activity is b. (move to c)
             */

            if (nextCandidate == null)
            {
                // Someone doesn't want us to go higher this node
                if (prev == root)
                {
                    return(null);
                }
                nextCandidate = GetNextActivity(prev.ParentPlanNode, false, root);
            }

            return(nextCandidate);
        }
Esempio n. 7
0
        public PlanNodeDO Find(Guid nodeId)
        {
            if (IsDeleted)
            {
                return(null);
            }

            PlanNodeDO result = null;

            PlanTreeHelper.Visit(Root, x =>
            {
                if (x.Id == nodeId)
                {
                    result = x;
                    return(false);
                }

                return(true);
            });

            return(result);
        }
        public void Activity_CheckGetNextActivities()
        {
            // If allis working right than iterative execution of GetNextActivity would return
            // the sequence of activities equals to the sequence of activity tree depth-first traversal with
            // children visited according to their ordering

            using (var uow = ObjectFactory.GetInstance <IUnitOfWork>())
            {
                var root = FixtureData.TestActivityTree();

                uow.PlanRepository.Add(new PlanDO()
                {
                    Name       = "name",
                    PlanState  = PlanState.Executing,
                    ChildNodes = { root }
                });

                uow.SaveChanges();

                PlanNodeDO        curActivity = root;
                List <PlanNodeDO> seqToTest   = new List <PlanNodeDO>();
                List <PlanNodeDO> refSeq      = new List <PlanNodeDO>();

                TraverseActivities(root, refSeq);

                do
                {
                    seqToTest.Add(curActivity);
                    curActivity = _planNode.GetNextActivity(curActivity, null);
                } while (curActivity != null);

                Assert.AreEqual(refSeq.Count, seqToTest.Count);

                for (int i = 0; i < refSeq.Count; i++)
                {
                    Assert.AreEqual(refSeq[i], seqToTest[i]);
                }
            }
        }
Esempio n. 9
0
        /**********************************************************************************/

        private static void VisitOrdered(PlanNodeDO root, PlanNodeDO parent, Func <PlanNodeDO, PlanNodeDO, bool> vistor)
        {
            if (root == null)
            {
                return;
            }

            if (!vistor(root, parent))
            {
                return;
            }

            if (root.ChildNodes == null)
            {
                return;
            }

            foreach (var planNodeDo in root.ChildNodes.OrderBy(x => x.Ordering))
            {
                VisitOrdered(planNodeDo, root, vistor);
            }
        }
Esempio n. 10
0
        public List <PlanNodeDO> GetUpstreamActivities(IUnitOfWork uow, PlanNodeDO curActivityDO)
        {
            if (curActivityDO == null)
            {
                throw new ArgumentNullException("curActivityDO");
            }
            if (curActivityDO.ParentPlanNodeId == null)
            {
                return(new List <PlanNodeDO>());
            }

            List <PlanNodeDO> planNodes = new List <PlanNodeDO>();
            var node = curActivityDO;

            do
            {
                var currentNode = node;

                if (node.ParentPlanNode != null)
                {
                    foreach (var predcessors in node.ParentPlanNode.ChildNodes.Where(x => x.Ordering < currentNode.Ordering && x != currentNode).OrderByDescending(x => x.Ordering))
                    {
                        GetDownstreamRecusive(predcessors, planNodes);
                    }
                }

                node = node.ParentPlanNode;

                if (node != null)
                {
                    planNodes.Add(node);
                }
            } while (node != null);

            return(planNodes);
        }
Esempio n. 11
0
        private void CollectActivitiesByLevels(PlanNodeDO root, List <List <ActivityDO> > levels, int level)
        {
            List <ActivityDO> activities;

            if (levels.Count == level)
            {
                activities = new List <ActivityDO>();
                levels.Add(activities);
            }
            else
            {
                activities = levels[level];
            }

            if (root is ActivityDO)
            {
                activities.Add((ActivityDO)root);
            }

            foreach (var child in root.ChildNodes.OfType <ActivityDO>().OrderBy(x => x.Ordering))
            {
                CollectActivitiesByLevels(child, levels, level + 1);
            }
        }
Esempio n. 12
0
 public bool HasChildren(PlanNodeDO currentActivity)
 {
     return(currentActivity.ChildNodes.Count > 0);
 }
Esempio n. 13
0
        public static PlanNodeDO TestActivityTree()
        {
            var tree = new PlanNodeDO
            {
                Id         = GetTestGuidById(1),
                Ordering   = 1,
                ChildNodes = new List <PlanNodeDO>
                {
                    new PlanNodeDO
                    {
                        Id               = GetTestGuidById(23),
                        Ordering         = 1,
                        ParentPlanNodeId = GetTestGuidById(1)
                    },
                    new PlanNodeDO
                    {
                        Id = GetTestGuidById(43),
                        ParentPlanNodeId = GetTestGuidById(1),
                        Ordering         = 2,
                        ChildNodes       = new List <PlanNodeDO>
                        {
                            new PlanNodeDO
                            {
                                Id               = GetTestGuidById(44),
                                Ordering         = 1,
                                ParentPlanNodeId = GetTestGuidById(43)
                            },
                            new PlanNodeDO
                            {
                                Id               = GetTestGuidById(46),
                                Ordering         = 2,
                                ParentPlanNodeId = GetTestGuidById(43)
                            },
                            new PlanNodeDO
                            {
                                Id               = GetTestGuidById(48),
                                Ordering         = 3,
                                ParentPlanNodeId = GetTestGuidById(43)
                            },
                        }
                    },
                    new PlanNodeDO
                    {
                        Id               = GetTestGuidById(52),
                        Ordering         = 3,
                        ParentPlanNodeId = GetTestGuidById(1),
                        ChildNodes       = new List <PlanNodeDO>
                        {
                            new PlanNodeDO
                            {
                                Id               = GetTestGuidById(53),
                                Ordering         = 1,
                                ParentPlanNodeId = GetTestGuidById(52)
                            },
                            new PlanNodeDO
                            {
                                Id = GetTestGuidById(54),
                                ParentPlanNodeId = GetTestGuidById(52),
                                Ordering         = 2,

                                ChildNodes = new List <PlanNodeDO>
                                {
                                    new PlanNodeDO
                                    {
                                        Id = GetTestGuidById(56),
                                        ParentPlanNodeId = GetTestGuidById(54),
                                        Ordering         = 1
                                    },
                                    new PlanNodeDO
                                    {
                                        Id = GetTestGuidById(57),
                                        ParentPlanNodeId = GetTestGuidById(54),
                                        Ordering         = 2
                                    },
                                    new PlanNodeDO
                                    {
                                        Id = GetTestGuidById(58),
                                        ParentPlanNodeId = GetTestGuidById(54),
                                        Ordering         = 3
                                    },
                                }
                            },
                            new PlanNodeDO
                            {
                                Id = GetTestGuidById(55),
                                ParentPlanNodeId = GetTestGuidById(52),
                                Ordering         = 3
                            },
                        }
                    },
                    new PlanNodeDO
                    {
                        Id               = GetTestGuidById(59),
                        Ordering         = 4,
                        ParentPlanNodeId = GetTestGuidById(1),
                        ChildNodes       = new List <PlanNodeDO>
                        {
                            new PlanNodeDO
                            {
                                Id = GetTestGuidById(60),
                                ParentPlanNodeId = GetTestGuidById(59),
                                Ordering         = 1
                            },
                            new PlanNodeDO
                            {
                                Id = GetTestGuidById(61),
                                ParentPlanNodeId = GetTestGuidById(59),
                                Ordering         = 2,

                                ChildNodes = new List <PlanNodeDO>
                                {
                                    new PlanNodeDO
                                    {
                                        Id = GetTestGuidById(63),
                                        ParentPlanNodeId = GetTestGuidById(61),
                                        Ordering         = 1
                                    },
                                    new PlanNodeDO
                                    {
                                        Id = GetTestGuidById(64),
                                        ParentPlanNodeId = GetTestGuidById(61),
                                        Ordering         = 2
                                    },
                                    new PlanNodeDO
                                    {
                                        Id = GetTestGuidById(65),
                                        ParentPlanNodeId = GetTestGuidById(61),
                                        Ordering         = 3
                                    },
                                }
                            },

                            new PlanNodeDO
                            {
                                Id = GetTestGuidById(62),
                                ParentPlanNodeId = GetTestGuidById(59),
                                Ordering         = 3
                            },
                        },
                    }
                }
            };

            FixParentActivityReferences(tree);

            return(tree);
        }
Esempio n. 14
0
 public PlanNodeDO GetNextActivity(PlanNodeDO currentActivity, PlanNodeDO root)
 {
     return(GetNextActivity(currentActivity, true, root));
 }
Esempio n. 15
0
 public bool HasChildren(PlanNodeDO currentActivity)
 {
     return(PlanNodes[currentActivity.Id].StartingSubPlanId == currentActivity.Id);
 }
Esempio n. 16
0
 public PlanNodeDO GetNextActivity(PlanNodeDO currentActivity, PlanNodeDO root)
 {
     return(null);
 }
Esempio n. 17
0
        /**********************************************************************************/

        public static void Visit(PlanNodeDO root, Func <PlanNodeDO, PlanNodeDO, bool> visitor)
        {
            Visit(root, null, visitor);
        }
Esempio n. 18
0
        /**********************************************************************************/

        public static void Visit(PlanNodeDO root, Func <PlanNodeDO, bool> vistor)
        {
            Visit(root, null, (x, y) => vistor(x));
        }
            /**********************************************************************************/

            private PlanNodeDO GetNextChildActivity(OperationalStateCM.StackFrame topFrame, PlanNodeDO currentNode)
            {
                // get the currently processing child
                var currentChild = topFrame.CurrentChildId != null?_uow.PlanRepository.GetById <PlanNodeDO>(topFrame.CurrentChildId.Value) : null;

                if (currentNode.ChildNodes == null)
                {
                    throw new NullReferenceException($"ChildNodes is null for node: {currentNode.Id}.");
                }

                // If we are already processing children of the currentNode, selecte the next one
                if (currentChild != null)
                {
                    return(currentNode.ChildNodes.OrderBy(x => x.Ordering).FirstOrDefault(x => x.Ordering > currentChild.Ordering));
                }

                // or, if we have not processed any child yet - select the first one if any
                return(currentNode.ChildNodes.OrderBy(x => x.Ordering).FirstOrDefault());
            }
Esempio n. 20
0
 public PlanNodeDO GetParent(PlanNodeDO currentActivity)
 {
     return(null);
 }
        private PlanNodeDO LoadPlanByPlanId(Guid planId)
        {
            var lookup = new Dictionary <Guid, PlanNodeDO>();

            var plans = Plans.GetQuery().Where(x => x.Id == planId).Include(x => x.Fr8Account).AsEnumerable().Select(x => x.Clone()).ToArray();

            if (plans.Length == 0)
            {
                return(null);
                //throw new KeyNotFoundException("Unable to find plan not with id = " + planId);
            }

            var actions  = ActivityRepository.GetQuery().Where(x => x.RootPlanNodeId == planId).Include(x => x.Fr8Account).AsEnumerable().Select(CloneActivity).ToArray();
            var subPlans = SubPlans.GetQuery().Where(x => x.RootPlanNodeId == planId).Include(x => x.Fr8Account).AsEnumerable().Select(x => x.Clone()).ToArray();

            if (actions.Length == 0 && subPlans.Length == 0)
            {
                var nodes = PlanNodes.GetQuery().Where(x => x.RootPlanNodeId == planId).Include(x => x.Fr8Account).AsEnumerable().Select(x => x.Clone()).ToArray();

                if (nodes.Length == 0)
                {
                    throw new NotSupportedException($"Completely empty plans like {planId} are not supported");
                }

                foreach (var planNodeDo in nodes)
                {
                    lookup[planNodeDo.Id] = planNodeDo;
                }
            }
            else
            {
                foreach (var planNodeDo in actions)
                {
                    lookup[planNodeDo.Id] = planNodeDo;
                }

                foreach (var planNodeDo in subPlans)
                {
                    lookup[planNodeDo.Id] = planNodeDo;
                }
            }

            foreach (var planNodeDo in plans)
            {
                lookup[planNodeDo.Id] = planNodeDo;
            }

            PlanNodeDO root         = null;
            var        pendingNodes = new Stack <KeyValuePair <Guid, PlanNodeDO> >();

            foreach (var planNodeDo in lookup)
            {
                pendingNodes.Push(planNodeDo);
            }

            while (pendingNodes.Count > 0)
            {
                var planNodeDo = pendingNodes.Pop();

                PlanNodeDO parent;

                if (planNodeDo.Value.ParentPlanNodeId == null)
                {
                    root = planNodeDo.Value;
                    continue;
                }

                //We are...
                //We are...
                //We are loading the broken plan
                if (!lookup.TryGetValue(planNodeDo.Value.ParentPlanNodeId.Value, out parent))
                {
                    var node = PlanNodes.GetQuery().Include(x => x.Fr8Account).FirstOrDefault(x => x.Id == planNodeDo.Value.ParentPlanNodeId.Value);

                    //This plan...
                    //This plan...
                    //Was broken from the start
                    if (node == null)
                    {
                        throw new Exception($"Plan {planId} is completely broken. It has node {planNodeDo.Key} that references non existing parent {planNodeDo.Value.ParentPlanNodeId.Value}");
                    }

                    node = node.Clone();

                    lookup[node.Id] = node;
                    parent          = node;
                    pendingNodes.Push(new KeyValuePair <Guid, PlanNodeDO>(node.Id, node));
                }

                planNodeDo.Value.ParentPlanNode = parent;
                parent.ChildNodes.Add(planNodeDo.Value);
            }

            return(root);
        }
Esempio n. 22
0
        /**********************************************************************************/

        private PlanNodeDO GetNewNodeOrGetExising(Dictionary <Guid, PlanNodeDO> exisingNodes, PlanNodeDO newNode)
        {
            if (newNode == null)
            {
                return(null);
            }

            PlanNodeDO existingNode;

            if (exisingNodes.TryGetValue(newNode.Id, out existingNode))
            {
                return(existingNode);
            }

            return(newNode);
        }
Esempio n. 23
0
 public List <PlanNodeDO> GetDownstreamActivities(IUnitOfWork uow, PlanNodeDO curActivityDO)
 {
     throw new NotImplementedException();
 }
Esempio n. 24
0
 public ChangedObject(PlanNodeDO node, List <PropertyInfo> changedProperties)
 {
     Node = node;
     ChangedProperties = changedProperties;
 }
Esempio n. 25
0
 public PlanNodeDO GetNextSibling(PlanNodeDO currentActivity)
 {
     return(null);
 }
Esempio n. 26
0
 public CachedPlan(PlanNodeDO root, IExpirationToken expiration)
 {
     Root       = root;
     Expiration = expiration;
 }
Esempio n. 27
0
 public PlanNodeDO GetFirstChild(PlanNodeDO currentActivity)
 {
     return(PlanNodes[currentActivity.Id].ChildNodes.First().ChildNodes.First());
 }
Esempio n. 28
0
 public CacheItem(PlanNodeDO node, CachedPlan plan)
 {
     Node = node;
     Plan = plan;
 }
Esempio n. 29
0
 public void Delete(IUnitOfWork uow, PlanNodeDO activity)
 {
     throw new NotImplementedException();
 }
Esempio n. 30
0
 public PlanNodeDO GetParent(PlanNodeDO currentActivity)
 {
     return(currentActivity.ParentPlanNode);
 }