private void ClusterizatorCb_SelectionChanged(object sender, SelectionChangedEventArgs e)
 {
     if (ClusterizatorCb.SelectedItem != null)
     {
         _clusterizator = (IClusterizator)ClusterizatorCb.SelectedItem;
         if (_distanceCalculator != null)
         {
             _clusterizator.ClusterDistanceCalculator = _distanceCalculator;
         }
     }
 }
        public ActionResult Index(
            long[] matterIds,
            short[] characteristicLinkIds,
            Notation[] notations,
            Language[] languages,
            int clustersCount,
            ClusterizationType clusterizationType,
            double equipotencyWeight        = 1,
            double normalizedDistanceWeight = 1,
            double distanceWeight           = 1,
            double bandwidth    = 0,
            int maximumClusters = 2)
        {
            return(CreateTask(() =>
            {
                var characteristicNames = new string[characteristicLinkIds.Length];
                var mattersCharacteristics = new object[matterIds.Length];
                var characteristics = new double[matterIds.Length][];
                matterIds = matterIds.OrderBy(m => m).ToArray();
                Dictionary <long, string> matters = db.Matter.Where(m => matterIds.Contains(m.Id)).ToDictionary(m => m.Id, m => m.Name);

                for (int j = 0; j < matterIds.Length; j++)
                {
                    long matterId = matterIds[j];
                    characteristics[j] = new double[characteristicLinkIds.Length];
                    for (int i = 0; i < characteristicLinkIds.Length; i++)
                    {
                        Notation notation = notations[i];
                        long sequenceId = db.Matter.Single(m => m.Id == matterId).Sequence.Single(c => c.Notation == notation).Id;

                        int characteristicLinkId = characteristicLinkIds[i];
                        if (db.CharacteristicValue.Any(c => c.SequenceId == sequenceId && c.CharacteristicLinkId == characteristicLinkId))
                        {
                            characteristics[j][i] = db.CharacteristicValue.Single(c => c.SequenceId == sequenceId && c.CharacteristicLinkId == characteristicLinkId).Value;
                        }
                        else
                        {
                            Chain tempChain = commonSequenceRepository.GetLibiadaChain(sequenceId);

                            Link link = characteristicTypeLinkRepository.GetLinkForCharacteristic(characteristicLinkId);
                            FullCharacteristic characteristic = characteristicTypeLinkRepository.GetCharacteristic(characteristicLinkId);
                            IFullCalculator calculator = FullCalculatorsFactory.CreateCalculator(characteristic);
                            characteristics[j][i] = calculator.Calculate(tempChain, link);
                        }
                    }
                }

                for (int k = 0; k < characteristicLinkIds.Length; k++)
                {
                    characteristicNames[k] = characteristicTypeLinkRepository.GetCharacteristicName(characteristicLinkIds[k], notations[k]);
                }

                var clusterizationParams = new Dictionary <string, double>
                {
                    { "clustersCount", clustersCount },
                    { "equipotencyWeight", equipotencyWeight },
                    { "normalizedDistanceWeight", normalizedDistanceWeight },
                    { "distanceWeight", distanceWeight },
                    { "bandwidth", bandwidth },
                    { "maximumClusters", maximumClusters }
                };

                IClusterizator clusterizator = ClusterizatorsFactory.CreateClusterizator(clusterizationType, clusterizationParams);
                int[] clusterizationResult = clusterizator.Cluster(clustersCount, characteristics);
                for (int i = 0; i < clusterizationResult.Length; i++)
                {
                    mattersCharacteristics[i] = new
                    {
                        MatterName = matters[matterIds[i]],
                        cluster = clusterizationResult[i] + 1,
                        Characteristics = characteristics[i]
                    };
                }

                var characteristicsList = new SelectListItem[characteristicLinkIds.Length];
                for (int i = 0; i < characteristicNames.Length; i++)
                {
                    characteristicsList[i] = new SelectListItem
                    {
                        Value = i.ToString(),
                        Text = characteristicNames[i],
                        Selected = false
                    };
                }

                var result = new Dictionary <string, object>
                {
                    { "characteristicNames", characteristicNames },
                    { "characteristics", mattersCharacteristics },
                    { "characteristicsList", characteristicsList },
                    { "clustersCount", clusterizationResult.Distinct().Count() }
                };

                return new Dictionary <string, object>
                {
                    { "data", JsonConvert.SerializeObject(result) }
                };
            }));
        }
        public ActionResult Index(
            long[] matterIds,
            short[] characteristicLinkIds,
            Notation[] notations,
            Language?[] languages,
            Translator?[] translators,
            PauseTreatment?[] pauseTreatments,
            bool?[] sequentialTransfers,
            ImageOrderExtractor?[] trajectories,
            int clustersCount,
            ClusterizationType clusterizationType,
            double equipotencyWeight        = 1,
            double normalizedDistanceWeight = 1,
            double distanceWeight           = 1,
            double bandwidth    = 0,
            int maximumClusters = 2)
        {
            return(CreateTask(() =>
            {
                Dictionary <long, string> mattersNames;
                Dictionary <long, string> matters = Cache.GetInstance()
                                                    .Matters
                                                    .Where(m => matterIds.Contains(m.Id))
                                                    .ToDictionary(m => m.Id, m => m.Name);

                long[][] sequenceIds;
                using (var db = new LibiadaWebEntities())
                {
                    var commonSequenceRepository = new CommonSequenceRepository(db);
                    sequenceIds = commonSequenceRepository.GetSequenceIds(matterIds,
                                                                          notations,
                                                                          languages,
                                                                          translators,
                                                                          pauseTreatments,
                                                                          sequentialTransfers,
                                                                          trajectories);
                    mattersNames = db.Matter.Where(m => matterIds.Contains(m.Id)).ToDictionary(m => m.Id, m => m.Name);
                }

                double[][] characteristics;

                characteristics = SequencesCharacteristicsCalculator.Calculate(sequenceIds, characteristicLinkIds);

                var clusterizationParams = new Dictionary <string, double>
                {
                    { "clustersCount", clustersCount },
                    { "equipotencyWeight", equipotencyWeight },
                    { "normalizedDistanceWeight", normalizedDistanceWeight },
                    { "distanceWeight", distanceWeight },
                    { "bandwidth", bandwidth },
                    { "maximumClusters", maximumClusters }
                };

                IClusterizator clusterizator = ClusterizatorsFactory.CreateClusterizator(clusterizationType, clusterizationParams);
                int[] clusterizationResult = clusterizator.Cluster(clustersCount, characteristics);
                var mattersCharacteristics = new object[matterIds.Length];
                for (int i = 0; i < clusterizationResult.Length; i++)
                {
                    mattersCharacteristics[i] = new
                    {
                        MatterName = mattersNames[matterIds[i]],
                        cluster = clusterizationResult[i] + 1,
                        Characteristics = characteristics[i]
                    };
                }

                var characteristicNames = new string[characteristicLinkIds.Length];
                var characteristicsList = new SelectListItem[characteristicLinkIds.Length];
                var characteristicTypeLinkRepository = FullCharacteristicRepository.Instance;
                for (int k = 0; k < characteristicLinkIds.Length; k++)
                {
                    characteristicNames[k] = characteristicTypeLinkRepository.GetCharacteristicName(characteristicLinkIds[k], notations[k]);
                    characteristicsList[k] = new SelectListItem
                    {
                        Value = k.ToString(),
                        Text = characteristicNames[k],
                        Selected = false
                    };
                }

                var result = new Dictionary <string, object>
                {
                    { "characteristicNames", characteristicNames },
                    { "characteristics", mattersCharacteristics },
                    { "characteristicsList", characteristicsList },
                    { "clustersCount", clusterizationResult.Distinct().Count() }
                };

                return new Dictionary <string, string> {
                    { "data", JsonConvert.SerializeObject(result) }
                };
            }));
        }