GetRelationIntervalsManager() public method

The get relation intervals manager.
public GetRelationIntervalsManager ( IBaseObject first, IBaseObject second ) : BinaryIntervalsManager
first IBaseObject /// The first. ///
second IBaseObject /// The second. ///
return BinaryIntervalsManager
        public void GetBinaryIntervalIncompleteChainTest()
        {
            // A C A _ C C _ A A A C C A
            // A _ A _ _ _ _ A A A _ _ A
            // _ C _ _ C C _ _ _ _ C C _
            var chain = new Chain(13);

            chain.Set(elements["A"], 0);
            chain.Set(elements["C"], 1);
            chain.Set(elements["A"], 2);

            chain.Set(elements["C"], 4);
            chain.Set(elements["C"], 5);

            chain.Set(elements["A"], 7);
            chain.Set(elements["A"], 8);
            chain.Set(elements["A"], 9);
            chain.Set(elements["C"], 10);
            chain.Set(elements["C"], 11);
            chain.Set(elements["A"], 12);

            var intervalManager = chain.GetRelationIntervalsManager(elements["A"], elements["C"]);
            Assert.AreEqual(1, intervalManager.GetBinaryInterval(1));
            Assert.AreEqual(2, intervalManager.GetBinaryInterval(2));
            Assert.AreEqual(-1, intervalManager.GetBinaryInterval(3));
            Assert.AreEqual(-1, intervalManager.GetBinaryInterval(4));
            Assert.AreEqual(1, intervalManager.GetBinaryInterval(5));
            Assert.AreEqual(-1, intervalManager.GetBinaryInterval(6));
            Assert.AreEqual(-1, intervalManager.GetBinaryInterval(7));

            intervalManager = chain.GetRelationIntervalsManager(elements["C"], elements["A"]);
            Assert.AreEqual(1, intervalManager.GetBinaryInterval(1));
            Assert.AreEqual(-1, intervalManager.GetBinaryInterval(2));
            Assert.AreEqual(2, intervalManager.GetBinaryInterval(3));
            Assert.AreEqual(-1, intervalManager.GetBinaryInterval(4));
            Assert.AreEqual(1, intervalManager.GetBinaryInterval(5));
            Assert.AreEqual(-1, intervalManager.GetBinaryInterval(6));
        }
        /// <summary>
        /// The not frequency characteristic.
        /// </summary>
        /// <param name="characteristicTypeLinkId">
        /// The characteristic type and link id.
        /// </param>
        /// <param name="sequenceId">
        /// The sequence id.
        /// </param>
        /// <param name="chain">
        /// The chain.
        /// </param>
        /// <param name="calculator">
        /// The calculator.
        /// </param>
        /// <param name="link">
        /// The link.
        /// </param>
        private void NotFrequencyCharacteristic(
            int characteristicTypeLinkId,
            long sequenceId,
            Chain chain,
            IBinaryCalculator calculator,
            Link link)
        {
            var newCharacteristics = new List<BinaryCharacteristic>();
            var databaseCharacteristics = db.BinaryCharacteristic.Where(b => b.SequenceId == sequenceId
                                                                    && b.CharacteristicTypeLinkId == characteristicTypeLinkId)
                                                           .ToArray();
            int calculatedCount = databaseCharacteristics.Length;
            if (calculatedCount < chain.Alphabet.Cardinality * chain.Alphabet.Cardinality)
            {
                List<long> sequenceElements = DbHelper.GetElementIds(db, sequenceId);
                for (int i = 0; i < chain.Alphabet.Cardinality; i++)
                {
                    for (int j = 0; j < chain.Alphabet.Cardinality; j++)
                    {
                        long firstElementId = sequenceElements[i];
                        long secondElementId = sequenceElements[i];
                        if (!databaseCharacteristics.Any(b => b.FirstElementId == firstElementId && b.SecondElementId == secondElementId))
                        {
                            double result = calculator.Calculate(chain.GetRelationIntervalsManager(i + 1, j + 1), link);

                            newCharacteristics.Add(binaryCharacteristicRepository.CreateBinaryCharacteristic(sequenceId, characteristicTypeLinkId, firstElementId, secondElementId, result));
                        }
                    }
                }
            }

            db.BinaryCharacteristic.AddRange(newCharacteristics);
            db.SaveChanges();
        }
        /// <summary>
        /// The frequency characteristic.
        /// </summary>
        /// <param name="characteristicTypeLinkId">
        /// The characteristic type and link id.
        /// </param>
        /// <param name="frequencyCount">
        /// The frequency count.
        /// </param>
        /// <param name="chain">
        /// The chain.
        /// </param>
        /// <param name="sequenceId">
        /// The sequence id.
        /// </param>
        /// <param name="calculator">
        /// The calculator.
        /// </param>
        /// <param name="link">
        /// The link.
        /// </param>
        private void FrequencyCharacteristic(int characteristicTypeLinkId, int frequencyCount, Chain chain, long sequenceId, IBinaryCalculator calculator, Link link)
        {
            List<long> sequenceElements = DbHelper.GetElementIds(db, sequenceId);
            var newCharacteristics = new List<BinaryCharacteristic>();
            var databaseCharacteristics = db.BinaryCharacteristic.Where(b => b.SequenceId == sequenceId
                                                                    && b.CharacteristicTypeLinkId == characteristicTypeLinkId).ToArray();

            // calculating frequencies of elements in alphabet
            var frequencies = new List<KeyValuePair<IBaseObject, double>>();
            for (int f = 0; f < chain.Alphabet.Cardinality; f++)
            {
                var probabilityCalculator = new Probability();
                var result = probabilityCalculator.Calculate(chain.CongenericChain(f), Link.NotApplied);
                frequencies.Add(new KeyValuePair<IBaseObject, double>(chain.Alphabet[f], result));
            }

            // ordering alphabet by frequencies
            SortKeyValuePairList(frequencies);

            // calculating relation characteristic only for elements with maximum frequency
            for (int i = 0; i < frequencyCount; i++)
            {
                for (int j = 0; j < frequencyCount; j++)
                {
                    int firstElementNumber = chain.Alphabet.IndexOf(frequencies[i].Key) + 1;
                    int secondElementNumber = chain.Alphabet.IndexOf(frequencies[j].Key) + 1;
                    long firstElementId = sequenceElements[firstElementNumber];
                    long secondElementId = sequenceElements[secondElementNumber];

                    // searching characteristic in database
                    if (!databaseCharacteristics.Any(b => b.FirstElementId == firstElementId && b.SecondElementId == secondElementId))
                    {
                        double result = calculator.Calculate(chain.GetRelationIntervalsManager(i + 1, j + 1), link);

                        newCharacteristics.Add(binaryCharacteristicRepository.CreateBinaryCharacteristic(sequenceId, characteristicTypeLinkId, firstElementId, secondElementId, result));
                    }
                }
            }

            db.BinaryCharacteristic.AddRange(newCharacteristics);
            db.SaveChanges();
        }