Exemplo n.º 1
0
        public ActionResult Index(
            long[] matterIds,
            int characteristicLinkId,
            Notation notation,
            Language?language,
            Translator?translator,
            PauseTreatment?pauseTreatment,
            bool?sequentialTransfer,
            ImageOrderExtractor?trajectory,
            string calculationType)
        {
            return(CreateTask(() =>
            {
                if (matterIds.Length != 2)
                {
                    throw new ArgumentException("Number of selected matters must be 2.", nameof(matterIds));
                }

                var characteristics = new Dictionary <int, Dictionary <int, double> >();
                string characteristicName = characteristicTypeLinkRepository.GetCharacteristicName(characteristicLinkId, notation);
                var result = new Dictionary <string, object>
                {
                    { "characteristics", characteristics },
                    { "matterNames", Cache.GetInstance().Matters.Where(m => matterIds.Contains(m.Id)).Select(m => m.Name).ToList() },
                    { "characteristicName", characteristicName },
                    { "calculationType", calculationType }
                };

                var sequenceIds = commonSequenceRepository.GetSequenceIds(matterIds,
                                                                          notation,
                                                                          language,
                                                                          translator,
                                                                          pauseTreatment,
                                                                          sequentialTransfer,
                                                                          trajectory);

                Chain firstChain = commonSequenceRepository.GetLibiadaChain(sequenceIds[0]);
                Chain secondChain = commonSequenceRepository.GetLibiadaChain(sequenceIds[1]);

                AccordanceCharacteristic accordanceCharacteristic = characteristicTypeLinkRepository.GetCharacteristic(characteristicLinkId);
                IAccordanceCalculator calculator = AccordanceCalculatorsFactory.CreateCalculator(accordanceCharacteristic);
                Link link = characteristicTypeLinkRepository.GetLinkForCharacteristic(characteristicLinkId);
                Alphabet firstChainAlphabet = firstChain.Alphabet;
                Alphabet secondChainAlphabet = secondChain.Alphabet;

                switch (calculationType)
                {
                case "Equality":
                    if (!firstChainAlphabet.SetEquals(secondChainAlphabet))
                    {
                        throw new Exception("Alphabets of sequences are not equal.");
                    }

                    characteristics.Add(0, new Dictionary <int, double>());
                    characteristics.Add(1, new Dictionary <int, double>());
                    var alphabet = new List <string>();

                    for (int i = 0; i < firstChainAlphabet.Cardinality; i++)
                    {
                        IBaseObject element = firstChainAlphabet[i];
                        alphabet.Add(element.ToString());

                        CongenericChain firstCongenericChain = firstChain.CongenericChain(element);
                        CongenericChain secondCongenericChain = secondChain.CongenericChain(element);

                        double characteristicValue = calculator.Calculate(firstCongenericChain, secondCongenericChain, link);
                        characteristics[0].Add(i, characteristicValue);

                        characteristicValue = calculator.Calculate(secondCongenericChain, firstCongenericChain, link);
                        characteristics[1].Add(i, characteristicValue);
                    }

                    result.Add("alphabet", alphabet);
                    break;

                case "All":
                    var firstAlphabet = new List <string>();
                    for (int i = 0; i < firstChain.Alphabet.Cardinality; i++)
                    {
                        characteristics.Add(i, new Dictionary <int, double>());
                        IBaseObject firstElement = firstChainAlphabet[i];
                        firstAlphabet.Add(firstElement.ToString());
                        for (int j = 0; j < secondChainAlphabet.Cardinality; j++)
                        {
                            var secondElement = secondChainAlphabet[j];

                            var firstCongenericChain = firstChain.CongenericChain(firstElement);
                            var secondCongenericChain = secondChain.CongenericChain(secondElement);

                            var characteristicValue = calculator.Calculate(firstCongenericChain, secondCongenericChain, link);
                            characteristics[i].Add(j, characteristicValue);
                        }
                    }

                    var secondAlphabet = new List <string>();
                    for (int j = 0; j < secondChainAlphabet.Cardinality; j++)
                    {
                        secondAlphabet.Add(secondChainAlphabet[j].ToString());
                    }

                    result.Add("firstAlphabet", firstAlphabet);
                    result.Add("secondAlphabet", secondAlphabet);
                    break;

                case "Specified":
                    throw new NotImplementedException();

                default:
                    throw new ArgumentException("Calculation type is not implemented", nameof(calculationType));
                }

                return new Dictionary <string, string> {
                    { "data", JsonConvert.SerializeObject(result) }
                };
            }));
        }
        public ActionResult Index(
            long[] matterIds,
            int characteristicLinkId,
            Notation notation,
            Language?language,
            Translator?translator,
            PauseTreatment?pauseTreatment,
            bool?sequentialTransfer,
            string calculationType)
        {
            return(CreateTask(() =>
            {
                if (matterIds.Length != 2)
                {
                    throw new ArgumentException("Number of selected matters must be 2.", nameof(matterIds));
                }

                var characteristics = new List <List <double> >();
                string characteristicName = characteristicTypeLinkRepository.GetCharacteristicName(characteristicLinkId, notation);
                var result = new Dictionary <string, object>
                {
                    { "characteristics", characteristics },
                    { "matterNames", db.Matter.Where(m => matterIds.Contains(m.Id)).Select(m => m.Name).ToList() },
                    { "characteristicName", characteristicName },
                    { "calculationType", calculationType }
                };

                long firstMatterId = matterIds[0];
                long secondMatterId = matterIds[1];
                long firstSequenceId;
                long secondSequenceId;
                switch (notation.GetNature())
                {
                case Nature.Literature:
                    firstSequenceId = db.LiteratureSequence.Single(l => l.MatterId == firstMatterId &&
                                                                   l.Notation == notation &&
                                                                   l.Language == language &&
                                                                   l.Translator == translator).Id;
                    secondSequenceId = db.LiteratureSequence.Single(l => l.MatterId == secondMatterId &&
                                                                    l.Notation == notation &&
                                                                    l.Language == language &&
                                                                    l.Translator == translator).Id;
                    break;

                case Nature.Music:
                    firstSequenceId = db.MusicSequence.Single(m => m.MatterId == firstMatterId &&
                                                              m.Notation == notation &&
                                                              m.PauseTreatment == pauseTreatment &&
                                                              m.SequentialTransfer == sequentialTransfer).Id;
                    secondSequenceId = db.MusicSequence.Single(m => m.MatterId == secondMatterId &&
                                                               m.Notation == notation &&
                                                               m.PauseTreatment == pauseTreatment &&
                                                               m.SequentialTransfer == sequentialTransfer).Id;
                    break;

                default:
                    firstSequenceId = db.CommonSequence.Single(c => c.MatterId == firstMatterId && c.Notation == notation).Id;
                    secondSequenceId = db.CommonSequence.Single(c => c.MatterId == secondMatterId && c.Notation == notation).Id;
                    break;
                }

                Chain firstChain = commonSequenceRepository.GetLibiadaChain(firstSequenceId);
                Chain secondChain = commonSequenceRepository.GetLibiadaChain(secondSequenceId);

                AccordanceCharacteristic accordanceCharacteristic = characteristicTypeLinkRepository.GetCharacteristic(characteristicLinkId);
                IAccordanceCalculator calculator = AccordanceCalculatorsFactory.CreateCalculator(accordanceCharacteristic);
                Link link = characteristicTypeLinkRepository.GetLinkForCharacteristic(characteristicLinkId);
                Alphabet firstChainAlphabet = firstChain.Alphabet;
                Alphabet secondChainAlphabet = secondChain.Alphabet;

                switch (calculationType)
                {
                case "Equality":
                    if (!firstChainAlphabet.SetEquals(secondChainAlphabet))
                    {
                        throw new Exception("Alphabets of sequences are not equal.");
                    }

                    characteristics.Add(new List <double>());
                    characteristics.Add(new List <double>());
                    var alphabet = new List <string>();

                    for (int i = 0; i < firstChainAlphabet.Cardinality; i++)
                    {
                        IBaseObject element = firstChainAlphabet[i];
                        alphabet.Add(element.ToString());

                        CongenericChain firstCongenericChain = firstChain.CongenericChain(element);
                        CongenericChain secondCongenericChain = secondChain.CongenericChain(element);

                        double characteristicValue = calculator.Calculate(firstCongenericChain, secondCongenericChain, link);
                        characteristics[0].Add(characteristicValue);

                        characteristicValue = calculator.Calculate(secondCongenericChain, firstCongenericChain, link);
                        characteristics[1].Add(characteristicValue);
                    }

                    result.Add("alphabet", alphabet);
                    break;

                case "All":
                    var firstAlphabet = new List <string>();
                    for (int i = 0; i < firstChain.Alphabet.Cardinality; i++)
                    {
                        characteristics.Add(new List <double>());
                        IBaseObject firstElement = firstChainAlphabet[i];
                        firstAlphabet.Add(firstElement.ToString());
                        for (int j = 0; j < secondChainAlphabet.Cardinality; j++)
                        {
                            var secondElement = secondChainAlphabet[j];

                            var firstCongenericChain = firstChain.CongenericChain(firstElement);
                            var secondCongenericChain = secondChain.CongenericChain(secondElement);

                            var characteristicValue = calculator.Calculate(firstCongenericChain, secondCongenericChain, link);
                            characteristics[i].Add(characteristicValue);
                        }
                    }

                    var secondAlphabet = new List <string>();
                    for (int j = 0; j < secondChainAlphabet.Cardinality; j++)
                    {
                        secondAlphabet.Add(secondChainAlphabet[j].ToString());
                    }

                    result.Add("firstAlphabet", firstAlphabet);
                    result.Add("secondAlphabet", secondAlphabet);
                    break;

                case "Specified":
                    throw new NotImplementedException();

                default:
                    throw new ArgumentException("Calculation type is not implemented", nameof(calculationType));
                }

                return result;
            }));
        }