//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; } }
public LoadedPlan(PlanNodeDO root, bool isNew = false) { IsNew = isNew; Root = root; if (isNew) { Snapshot = new PlanSnapshot(); } else { RebuildSnapshot(); } }
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; } }
/**********************************************************************************/ 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()); } }
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); }
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]); } } }
/**********************************************************************************/ 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); } }
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); }
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); } }
public bool HasChildren(PlanNodeDO currentActivity) { return(currentActivity.ChildNodes.Count > 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); }
public PlanNodeDO GetNextActivity(PlanNodeDO currentActivity, PlanNodeDO root) { return(GetNextActivity(currentActivity, true, root)); }
public bool HasChildren(PlanNodeDO currentActivity) { return(PlanNodes[currentActivity.Id].StartingSubPlanId == currentActivity.Id); }
public PlanNodeDO GetNextActivity(PlanNodeDO currentActivity, PlanNodeDO root) { return(null); }
/**********************************************************************************/ public static void Visit(PlanNodeDO root, Func <PlanNodeDO, PlanNodeDO, bool> visitor) { Visit(root, null, visitor); }
/**********************************************************************************/ 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()); }
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); }
/**********************************************************************************/ 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); }
public List <PlanNodeDO> GetDownstreamActivities(IUnitOfWork uow, PlanNodeDO curActivityDO) { throw new NotImplementedException(); }
public ChangedObject(PlanNodeDO node, List <PropertyInfo> changedProperties) { Node = node; ChangedProperties = changedProperties; }
public PlanNodeDO GetNextSibling(PlanNodeDO currentActivity) { return(null); }
public CachedPlan(PlanNodeDO root, IExpirationToken expiration) { Root = root; Expiration = expiration; }
public PlanNodeDO GetFirstChild(PlanNodeDO currentActivity) { return(PlanNodes[currentActivity.Id].ChildNodes.First().ChildNodes.First()); }
public CacheItem(PlanNodeDO node, CachedPlan plan) { Node = node; Plan = plan; }
public void Delete(IUnitOfWork uow, PlanNodeDO activity) { throw new NotImplementedException(); }
public PlanNodeDO GetParent(PlanNodeDO currentActivity) { return(currentActivity.ParentPlanNode); }