void AddFlowControls() { foreach (Activity a in mBusinessFlow.Activities) { foreach (Act act in a.Acts) { foreach (FlowControl FC in act.FlowControls) { FlowElement FlowControlFE = new FlowElement(FlowElement.eElementType.FlowControl, act.Description + Environment.NewLine + FC.Condition + "?", 200, 300); //TODO: fix me cause problem when status = 0 !!!!!!!!!!!!!!!! // FlowControlFE.BindStatusLabel(FC, FlowControl.Fields.Status); mFlowDiagram.AddFlowElem(FlowControlFE); // Create Line connector to the activity this FC is in FlowLink FL2 = new FlowLink(GetFlowElemOfActivity(a), FlowControlFE); FL2.LinkStyle = FlowLink.eLinkStyle.Line; FL2.SourcePosition = FlowLink.eFlowElementPosition.Right; FL2.DestinationPosition = FlowLink.eFlowElementPosition.Left; mFlowDiagram.AddConnector(FL2); //TODO: update the FlowControlFE to be on same y of the Activity and x + 150 if (FC.FlowControlAction == eFlowControlAction.GoToActivity) { // Add Link from FC to target Activity if condition met FlowElement TargetActivityFE = (from x in mFlowDiagram.GetAllFlowElem() where x.Object is Activity && ((Activity)x.Object).Guid == FC.GetGuidFromValue() select x).FirstOrDefault(); FlowLink FL3 = new FlowLink(FlowControlFE, TargetActivityFE); FL3.LinkStyle = FlowLink.eLinkStyle.DottedArrow; FL3.SourcePosition = FlowLink.eFlowElementPosition.Top; // TODO: find best connector - if we go up or down FL3.DestinationPosition = FlowLink.eFlowElementPosition.Right; // TODO: find best connector- if we go up or down mFlowDiagram.AddConnector(FL3); } if (FC.FlowControlAction == eFlowControlAction.StopBusinessFlow || FC.FlowControlAction == eFlowControlAction.StopRun) { FlowLink FL3 = new FlowLink(FlowControlFE, EndFlowElement); FL3.LinkStyle = FlowLink.eLinkStyle.DottedArrow; FL3.SourcePosition = FlowLink.eFlowElementPosition.Top; // TODO: find best connector FL3.DestinationPosition = FlowLink.eFlowElementPosition.Right; // TODO: find best connector mFlowDiagram.AddConnector(FL3); } //TODO: handle all other FC actions //if (FC.FlowControlAction == eFlowControlAction.SetVariableValue ) //{ // FlowLink FL3 = new FlowLink(FlowControlFE, EndFlowElement); // FL3.LinkStyle = FlowLink.eLinkStyle.DottedArrow; // FL3.SourcePosition = FlowLink.eFlowElementPosition.Top; // TODO: find best connector // FL3.DestinationPosition = FlowLink.eFlowElementPosition.Right; // TODO: find best connector // mFlowDiagram.AddConnector(FL3); //} } } } }
private void CreateBFFlowDiagram() { mFlowDiagram = new FlowDiagramPage(); double x = 50; double y = 50; // Show start elem StartFlowElement = new FlowElement(FlowElement.eElementType.Start, "Start", x, y); x += StartFlowElement.Width + 50; mFlowDiagram.AddFlowElem(StartFlowElement); y += 100; FlowElement LastActivity = StartFlowElement; foreach (Activity activity in mBusinessFlow.Activities) { FlowElement ActivityFE = new FlowElement(FlowElement.eElementType.Activity, activity, nameof(Activity.ActivityName), x, y); y += ActivityFE.Height + 50; //TODO: fixme to see image //ActivityFE.SetImage(activity.Image); ActivityFE.Object = activity; // keep ref to the Activity for later use ActivityFE.BindStatusLabel(activity, nameof(Activity.Status)); mFlowDiagram.AddFlowElem(ActivityFE); //Add connector to last activity FlowLink FL = new FlowLink(LastActivity, ActivityFE); FL.LinkStyle = FlowLink.eLinkStyle.Arrow; FL.SourcePosition = FlowLink.eFlowElementPosition.bottom; FL.DestinationPosition = FlowLink.eFlowElementPosition.Top; mFlowDiagram.AddConnector(FL); LastActivity = ActivityFE; } // Add 'End' Flow elem x += LastActivity.Width + 50; EndFlowElement = new FlowElement(FlowElement.eElementType.End, "End", x, y); mFlowDiagram.AddFlowElem(EndFlowElement); // connect last Activity to 'End' Flow Elem FlowLink FLEnd = new FlowLink(LastActivity, EndFlowElement); FLEnd.LinkStyle = FlowLink.eLinkStyle.Arrow; FLEnd.SourcePosition = FlowLink.eFlowElementPosition.bottom; FLEnd.DestinationPosition = FlowLink.eFlowElementPosition.Top; mFlowDiagram.AddConnector(FLEnd); AddFlowControls(); MainFrame.Content = mFlowDiagram; }
public override void MouseUpHandler(object sender, MouseButtonEventArgs e) { if (e.ChangedButton == MouseButton.Left) { if (this.ClickCount >= 2) { if (this.StartNode != null && this.EndNode != null) { if (!MainWindow.Current.Arrows.ContainsKeys(this.StartNode.NodeID, this.EndNode.NodeID)) { var link = new FlowLink { From = this.StartNode.NodeID, To = this.EndNode.NodeID, Label = string.Empty }; var arrow = new BezierLink { StartPoint = this.StartNode.Position, EndPoint = this.EndNode.Position, StartOffset = DataManager.NodeSize / 2, EndOffset = DataManager.NodeSize / 2, Tag = (link.From, link.To) }; Canvas.SetZIndex(arrow, 100); arrow.ReadyControl(); MainWindow.Current.MyCanvas.Children.Add(arrow); MainWindow.Current.Arrows.Add(link.From, link.To, arrow); DataManager.CurrentDocument.LinksStore.Add(link.From, link.To, link); } this.ClickCount = 0; this.StartNode = null; this.EndNode = null; MainWindow.Current.MyCanvas.Children.Remove(this.PhantomArrow); } } } }
public void CalculateFlow() { // Truncate old data from table _context.FlowNodes.RemoveRange(_context.FlowNodes); _context.FlowLinks.RemoveRange(_context.FlowLinks); _context.SaveChanges(); // Initialize Buckets per Exchange var allNodes = new Dictionary <Guid, IEnumerable <FlowNode> >(); var allLinks = new Dictionary <Guid, IEnumerable <FlowLink> >(); foreach (var exchange in _context.Exchanges) { var links = new List <FlowLink>(); // Add all Inputs var inputs = _context.Transactions.Where(t => t.ExchangeId == exchange.Id && t.Type == TransactionType.In).OrderBy(t => t.DateTime); var nodes = inputs.Select(input => new FlowNode(input.DateTime, input.InAmount, input.InCurrency, exchange.Id, input.Id, "In")).ToList(); // Add all Outputs var outputs = _context.Transactions.Where(t => t.ExchangeId == exchange.Id && t.Type == TransactionType.Out).OrderBy(t => t.DateTime); nodes.AddRange(outputs.Select(output => new FlowNode(output.DateTime, output.OutAmount, output.OutCurrency, exchange.Id, output.Id, "Out"))); var transactions = _context.Transactions.Where(t => t.ExchangeId == exchange.Id).OrderBy(t => t.DateTime); var lastBuckets = new Dictionary <string, Guid>(); // Currency/FlowNode Id foreach (var transaction in transactions) { switch (transaction.Type) { case TransactionType.In: { if (lastBuckets.ContainsKey(transaction.InCurrency)) { // Add Amount to new Bucket var sourceNode = nodes.Single(n => n.DateTime == transaction.DateTime && n.Amount == transaction.InAmount); var oldNode = nodes.Single(n => n.Id == lastBuckets[transaction.InCurrency]); var newNode = new FlowNode(transaction.DateTime, transaction.InAmount + oldNode.Amount, transaction.InCurrency, exchange.Id, Guid.Empty); nodes.Add(newNode); var link1 = new FlowLink(transaction.DateTime, oldNode.Amount, transaction.InCurrency, oldNode.Id, newNode.Id, exchange.Id); var link2 = new FlowLink(transaction.DateTime, transaction.InAmount, transaction.InCurrency, sourceNode.Id, newNode.Id, exchange.Id, "In"); links.Add(link1); links.Add(link2); lastBuckets[transaction.InCurrency] = newNode.Id; } else { var sourceNode = nodes.Single(n => n.TransactionId == transaction.Id); var node = new FlowNode(transaction.DateTime, transaction.InAmount, transaction.InCurrency, exchange.Id, transaction.Id); nodes.Add(node); var link = new FlowLink(transaction.DateTime, transaction.InAmount, transaction.InCurrency, sourceNode.Id, node.Id, exchange.Id, "In"); links.Add(link); lastBuckets.Add(transaction.InCurrency, node.Id); } } break; case TransactionType.Trade: if (lastBuckets.ContainsKey(transaction.BuyCurrency)) { // Buy Bucket already exists var buyBucketNode = nodes.Single(n => n.Id == lastBuckets[transaction.BuyCurrency]); var prevNode = nodes.Single(n => n.Id == lastBuckets[transaction.SellCurrency]); var restNode = new FlowNode(transaction.DateTime, prevNode.Amount - transaction.SellAmount, transaction.SellCurrency, exchange.Id, transaction.Id); var newBuyBucketNode = new FlowNode(transaction.DateTime, buyBucketNode.Amount + transaction.BuyAmount, buyBucketNode.Currency, exchange.Id, transaction.Id); // Calculate fee if (transaction.FeeCurrency == prevNode.Currency) { restNode.Amount -= transaction.FeeAmount; } else if (transaction.FeeCurrency == newBuyBucketNode.Currency) { newBuyBucketNode.Amount -= transaction.FeeAmount; } else { throw new NotImplementedException("Transaction Fee could not be added"); } var linkToNewBuyBucket = new FlowLink(transaction.DateTime, transaction.BuyAmount, transaction.BuyCurrency, prevNode.Id, newBuyBucketNode.Id, exchange.Id, $"Trade {Math.Round(transaction.BuyAmount, 2)} {transaction.BuyCurrency} for {Math.Round(transaction.SellAmount, 2)} {transaction.SellCurrency}"); var linkToNewBucket = new FlowLink(transaction.DateTime, buyBucketNode.Amount, buyBucketNode.Currency, buyBucketNode.Id, newBuyBucketNode.Id, exchange.Id); var linkToNewRestBucket = new FlowLink(transaction.DateTime, prevNode.Amount - transaction.SellAmount, transaction.SellCurrency, prevNode.Id, restNode.Id, exchange.Id); // Add data nodes.Add(restNode); nodes.Add(newBuyBucketNode); links.Add(linkToNewBuyBucket); links.Add(linkToNewBucket); links.Add(linkToNewRestBucket); // Set new Buckets lastBuckets[transaction.BuyCurrency] = newBuyBucketNode.Id; lastBuckets[transaction.SellCurrency] = restNode.Id; } else { // Create new Buy Bucket var prevNode = nodes.Single(n => n.Id == lastBuckets[transaction.SellCurrency]); var buyNode = new FlowNode(transaction.DateTime, transaction.BuyAmount, transaction.BuyCurrency, exchange.Id, transaction.Id); var sellAndRestNode = new FlowNode(transaction.DateTime, prevNode.Amount - transaction.SellAmount, transaction.SellCurrency, exchange.Id, transaction.Id); // Calculate fee if (transaction.FeeCurrency == prevNode.Currency) { sellAndRestNode.Amount -= transaction.FeeAmount; } else if (transaction.FeeCurrency == buyNode.Currency) { buyNode.Amount -= transaction.FeeAmount; } else { throw new NotImplementedException("Transaction Fee could not be added"); } // Add Links var buyLink = new FlowLink(transaction.DateTime, transaction.BuyAmount, transaction.BuyCurrency, prevNode.Id, buyNode.Id, exchange.Id, $"Trade {Math.Round(transaction.BuyAmount, 2)} {transaction.BuyCurrency} for {Math.Round(transaction.SellAmount, 2)} {transaction.SellCurrency}"); var sellAndRestLink = new FlowLink(transaction.DateTime, prevNode.Amount - transaction.SellAmount, transaction.SellCurrency, prevNode.Id, sellAndRestNode.Id, exchange.Id); // Add to Dictionaries links.Add(buyLink); links.Add(sellAndRestLink); nodes.Add(buyNode); nodes.Add(sellAndRestNode); // Set new Buckets lastBuckets[transaction.BuyCurrency] = buyNode.Id; lastBuckets[transaction.SellCurrency] = sellAndRestNode.Id; } break; case TransactionType.Out: var previousNode = nodes.Single(n => n.Id == lastBuckets[transaction.OutCurrency]); var outNode = nodes.Single(n => n.TransactionId == transaction.Id); var nextNode = new FlowNode(transaction.DateTime, previousNode.Amount - outNode.Amount, transaction.OutCurrency, exchange.Id, Guid.Empty); nodes.Add(nextNode); var linkPreviousOut = new FlowLink(transaction.DateTime, transaction.OutAmount, transaction.OutCurrency, previousNode.Id, outNode.Id, exchange.Id, "Out"); var linkPreviousNext = new FlowLink(transaction.DateTime, nextNode.Amount, transaction.OutCurrency, previousNode.Id, nextNode.Id, exchange.Id); links.Add(linkPreviousOut); links.Add(linkPreviousNext); lastBuckets[transaction.OutCurrency] = nextNode.Id; break; default: throw new ArgumentException($"Transaction Type {transaction.Type} is unknown"); } } allNodes.Add(exchange.Id, nodes); allLinks.Add(exchange.Id, links); } // Save Data foreach (var allNode in allNodes) { foreach (var nodes in allNode.Value) { _context.FlowNodes.Add(nodes); } } foreach (var allLink in allLinks) { foreach (var link in allLink.Value) { _context.FlowLinks.Add(link); } } _context.SaveChanges(); }