private LAEAnswer CalculateKramerMethodAsync(out List <LAEVariable> lAEVariables) { bool systemCompatible = this.CheckLinearAlgebraicEquationSystemCompatibility(); if (!systemCompatible) { lAEVariables = null; return(LAEAnswer.NoSolutions); } double matrixDeterminant = MatrixT <double> .GetMatrixDeterminant(this.Matrix); ConcurrentBag <LAEVariable> result = new ConcurrentBag <LAEVariable>(); Parallel.For(0, this.Matrix.Columns, (i) => { MatrixT <double> currentMatrix = MatrixT <double> .SubstituteMatrixColumn(this.Matrix, i, this.RightPartEquations); double currentDeterminant = MatrixT <double> .GetMatrixDeterminant(currentMatrix); result.Add(new LAEVariable(this.Variables[i].Name, currentDeterminant / matrixDeterminant)); }); lAEVariables = result.Cast <LAEVariable>().ToList(); return(LAEAnswer.OneSolution); }
public static IEnumerable <T> Resolve <T>(string name, DnsType type, DnsClass @class, bool recursionDesired = true) { var req = new Request() { RecursionDesired = recursionDesired }; req.AddQuestion(new Question(name, type, @class)); var bag = new ConcurrentBag <RecordBase>(); Parallel.ForEach(IP4, server => { try { var res = Resolver.Lookup(req, server); if (res.ReturnCode.Equals(ReturnCode.Success)) { Parallel.ForEach(res.Answers, answer => bag.Add(answer.Record)); } } catch (Exception) { //sink it, server might be dead or down } }); return(bag.Cast <T>().Distinct().ToList()); }
public override async ValueTask <bool> MoveNextAsync() { _items.TryTake(out var _); while (true) { if (_items.Count > 0) { return(true); } if (_complete && _items.Count == 0) { return(false); } var waits = _items.Cast <Task>().AsEnumerable(); if (!_complete) { var mni = _owner.MoveNextInternal().AsTask(); waits = waits.Append(mni.ContinueWith(final => { if (!final.Result) { _complete = true; } })); await Task.WhenAny(waits); } else { await Task.WhenAny(waits); } } }
public void ShouldForwardNonTargetedMessagesToDeadLetterProcessing() { var deadLetters = new ConcurrentBag <object>(); Action <object> deadLetterMethod = msg => { deadLetters.Add(msg); }; _transport = new InMemoryMessageTransport(deadLetterMethod); var sendMessage = _transport.CreateSenderMethod(_source.Token) .WithOutboundMessageHandler(msg => { // Ignore the responses send to the outbox; we only care about the // messages sent to the dead letter queue }); var unhandledMessage = "Hello, World!"; sendMessage(unhandledMessage); Thread.Sleep(500); Assert.True(deadLetters.Count(msg => msg is string) == 1); var actualMessage = deadLetters.Cast <string>().First(); Assert.Equal(unhandledMessage, actualMessage); }
public async Task <IEnumerable <IHandle <T> > > GetSubscriptions <T>(IDomainEvent e) where T : IDomainEvent { var consumers = new ConcurrentBag <Providers.Integration>(); Parallel.ForEach(e.IntegrationTypes, (integration, state) => { IntegrationStore store; try { //using the abstract factory pattern (as well as dependency injection), I'm able to implement this //statement instead of having a switch statement to determine what provider it is and creating concrete //implementations. store = _stores.SingleOrDefault(f => f.HandlesProvider == integration.IntegrationType); } catch (InvalidOperationException) { throw new InvalidOperationException( $"There are more than one IntegrationStores for integration {integration.IntegrationType}"); } if (store == null) { throw new NotImplementedException( $"IntegrationStore for integration type {integration.IntegrationType} has not implemented."); } var eld = store.CreateIntegration(integration); if (eld.Handles(e)) { consumers.Add(eld); } }); var result = consumers.Cast <IHandle <T> >(); return(await Task.FromResult(result)); }
private ConcurrentBag <Task> CreateConcurrentTaskList <T>(T eventDataTask, ConcurrentBag <object> subscribers) { return(new ConcurrentBag <Task>(new ConcurrentBag <Subscription <T> >(subscribers.Cast <Subscription <T> >()).Select(p => CurrentTask(eventDataTask, p)))); }
private void nearestNeighboursButton_Click(object sender, EventArgs e) { nNDataGridView.AutoGenerateColumns = true; bool KSpecified = true; int K; List <iKNNResult> AllResults = new List <iKNNResult>(); ConcurrentBag <iKNNResult> ResultBag = new ConcurrentBag <iKNNResult>(); try { K = Convert.ToInt32(kTextBox.Text); } catch { KSpecified = false; kTextBox.Text = "All"; Application.DoEvents(); K = -1; } if (thisFormResultType == typeof(UserQueryResult)) { UserQueryResult UQR = (UserQueryResult)thisFormResult; List <UserKNNResult> UKRList = new List <UserKNNResult>(); Parallel.ForEach <UserQueryResult>(parentReference.UserQueryResults, CurrentUQR => { if (CurrentUQR.AccountName != UQR.AccountName) { UserKNNResult UKR = new UserKNNResult(CurrentUQR); double CurrentDistance = HelperFunctions.GetEuclideanDistance(UQR.ReturnAccessVector(), CurrentUQR.ReturnAccessVector()); UKR.AssignKNNDistanceFromX(CurrentDistance); ResultBag.Add(UKR); } }); UKRList = ResultBag.Cast <UserKNNResult>().ToList <UserKNNResult>(); AllResults = UKRList.OrderBy(o => o.Distance).ToList().Cast <iKNNResult>().ToList(); thisQueryReport = new UserKNNReport(AllResults.Cast <UserKNNResult>().ToList(), Ordering.Ascending); } else if (thisFormResultType == typeof(GroupingQueryResult)) { GroupingQueryResult GQR = (GroupingQueryResult)thisFormResult; List <GroupingKNNResult> GKRList = new List <GroupingKNNResult>(); Parallel.ForEach <GroupingQueryResult>(parentReference.GroupingQueryResults, CurrentGQR => { if (CurrentGQR.GroupingName != GQR.GroupingName) { GroupingKNNResult GKR = new GroupingKNNResult(CurrentGQR); double CurrentDistance; if (parentReference.ClusterByRelativeCount) { CurrentDistance = HelperFunctions.GetEuclideanDistance(GQR.ReturnAccessVector(), CurrentGQR.ReturnAccessVector()); } else { CurrentDistance = HelperFunctions.GetEuclideanDistance(GQR.ReturnTF_IDFVector(), CurrentGQR.ReturnTF_IDFVector()); } GKR.AssignKNNDistanceFromX(CurrentDistance); ResultBag.Add(GKR); } }); GKRList = ResultBag.Cast <GroupingKNNResult>().ToList(); AllResults = GKRList.OrderBy(o => o.Distance).ToList().Cast <iKNNResult>().ToList(); thisQueryReport = new GroupingKNNReport(AllResults.Cast <GroupingKNNResult>().ToList(), Ordering.Ascending); } else { } if (KSpecified && K <= AllResults.Count) { List <iKNNResult> Outlist = new List <iKNNResult>(); for (int i = 0; i < K; i++) { Outlist.Add(AllResults[i]); } //AllKNNResults = Outlist; if (thisFormResultType == typeof(UserQueryResult)) { thisQueryReport = new UserKNNReport(Outlist.Cast <UserKNNResult>().ToList(), Ordering.Ascending); } else if (thisFormResultType == typeof(GroupingQueryResult)) { thisQueryReport = new GroupingKNNReport(Outlist.Cast <GroupingKNNResult>().ToList(), Ordering.Ascending); } else { } } else { AllKNNResults = AllResults; } if (thisFormResultType == typeof(UserQueryResult)) { UserKNNReport ReportPointer = (UserKNNReport)thisQueryReport; thisBindingSource.DataSource = ReportPointer.QRList; //thisBindingSource.DataSource = (UserKNNReport)thisQueryReport..Cast<UserKNNResult>().ToList(); nNDataGridView.DataSource = thisBindingSource; } else if (thisFormResultType == typeof(GroupingQueryResult)) { GroupingKNNReport ReportPointer = (GroupingKNNReport)thisQueryReport; thisBindingSource.DataSource = ReportPointer.QRList; nNDataGridView.DataSource = thisBindingSource; } else { } }
public ConcurrentBag <T> ConvertBagToType <T>(T sampleForTValue, ConcurrentBag <dynamic> collectionToConvert) { return(new ConcurrentBag <T>(collectionToConvert.Cast <T>())); }
public ClusteringOutput(List <QueryResult> QRList, RBAC RBACRef) { thisFormResultType = QRList[0].GetType(); parentReference = RBACRef; ConcurrentBag <Tuple <string, DenseVector> > InputList = new ConcurrentBag <Tuple <string, DenseVector> >(); if (QRList[0].GetType() == typeof(UserQueryResult)) { Parallel.ForEach <QueryResult>(QRList, QR => { UserQueryResult UQR = (UserQueryResult)QR; Tuple <string, DenseVector> TupleIn = new Tuple <string, DenseVector>(UQR.AccountName, (DenseVector)UQR.ReturnAccessVector()); InputList.Add(TupleIn); }); } else if (QRList[0].GetType() == typeof(GroupingQueryResult)) { Parallel.ForEach <QueryResult>(QRList, QR => { GroupingQueryResult GQR = (GroupingQueryResult)QR; DenseVector VectorA; if (parentReference.ClusterByRelativeCount) { VectorA = (DenseVector)GQR.ReturnAccessVector(); } else { VectorA = (DenseVector)GQR.ReturnTF_IDFVector(); } Tuple <string, DenseVector> TupleIn = new Tuple <string, DenseVector>(GQR.GroupingName, VectorA); InputList.Add(TupleIn); }); } else { } //add options on algo configtab on main form later if (parentReference.ClusteringAlgoType == typeof(HACAlgo)) { thisAlgo = new HACAlgo(InputList.OrderBy(o => o.Item1).ToList(), parentReference.PreferredDistanceStyle, parentReference.HACStoppingMetric, parentReference.PreferredStoppingStyle); } else { thisAlgo = new KMeansPlusPlus(InputList.OrderBy(o => o.Item1).ToList(), parentReference.KMeansValue, parentReference.PreferredKMeansStoppingStyle, parentReference.KMeansMaxIter); } parentReference.statusLabelChanger($"Initialising {thisAlgo.GetType().ToString().Split('.')[1]}, please be patient"); thisAlgo.InitialiseClusters(); while (!thisAlgo.Stopped) { thisAlgo.IterateOnce(); if (!thisAlgo.Stopped) { parentReference.statusLabelChanger($"Running {thisAlgo.GetType().ToString().Split('.')[1]}, iteration {thisAlgo.Iterator}"); } } //set all centroids as means in case mapping of clusters is required further //down the line: if (QRList[0].ReturnAccessVector().Count > 500) { thisAlgo.SetCentroidsAsMeansHighDimensionality(); } else { thisAlgo.SetCentroidsAsMeans(); } parentReference.statusLabelChanger("Creating Data View"); ConcurrentBag <QueryResult> ResultsBag = new ConcurrentBag <QueryResult>(); if (thisFormResultType == typeof(UserQueryResult)) { foreach (Cluster Clust in thisAlgo.Clusters) { Parallel.ForEach <Tuple <string, DenseVector> >(Clust.MemberList, Member => { UserQueryResult Target = (UserQueryResult)(from UQR in QRList.Cast <UserQueryResult>() where UQR.AccountName == Member.Item1 select UQR).ToList()[0]; ResultsBag.Add(new UserClusteringResult(Target, Clust.ClusterID, Clust.ListPosition)); }); } clusteringResultList = ResultsBag.Cast <UserClusteringResult>().OrderBy(o => o.ClusterIndex).ToList <QueryResult>(); } else if (thisFormResultType == typeof(GroupingQueryResult)) { foreach (Cluster Clust in thisAlgo.Clusters) { Parallel.ForEach <Tuple <string, DenseVector> >(Clust.MemberList, Member => { GroupingQueryResult Target = (GroupingQueryResult)(from GQR in QRList.Cast <GroupingQueryResult>().ToList() where GQR.GroupingName == Member.Item1 select GQR).ToList <GroupingQueryResult>()[0]; ResultsBag.Add(new GroupingClusteringResult(Target, Clust.ClusterID, Clust.ListPosition)); }); } clusteringResultList = ResultsBag.Cast <GroupingClusteringResult>().OrderBy(o => o.ClusterIndex).ToList <QueryResult>(); } else { } parentReference.statusLabelChanger("Idle"); InitializeComponent(); this.Text = $"Clustering Results from {thisAlgo.Iterator} Iterations, using {thisAlgo.GetType().ToString().Split('.')[1]}, {thisAlgo.Clusters.Count} Clusters"; thisBindingSource = new BindingSource(); if (thisFormResultType == typeof(UserQueryResult)) { //needs a bit of casting to allow datagridview to access type-specific public properties thisQR = new UserClusteringReport(clusteringResultList.Cast <UserClusteringResult>().ToList(), Ordering.Ascending); UserClusteringReport ReportReference = (UserClusteringReport)thisQR; thisBindingSource.DataSource = ReportReference.QRList; clustersDataGridView.DataSource = thisBindingSource; } else { thisQR = new GroupingClusteringReport(clusteringResultList.Cast <GroupingClusteringResult>().ToList(), Ordering.Ascending); GroupingClusteringReport ReportReference = (GroupingClusteringReport)thisQR; thisBindingSource.DataSource = ReportReference.QRList; clustersDataGridView.DataSource = thisBindingSource; } }