private static ICollection <KeyValuePair> GetCharacteristicsValues(IRow row, IReadOnlyList <Characteristic> characteristics) { if (row == null) { return(null); } var characteristicsValues = new List <KeyValuePair>(); for (var i = 1; i < row.LastCellNum; i++) { var newchar = new CharacteristicValue(row.GetCell(i).StringCellValue); characteristics[i - 1].PossibleValues = new List <CharacteristicValue> { newchar }; characteristicsValues.Add(new KeyValuePair { Key = characteristics[i - 1], Value = characteristics[i - 1].PossibleValues.FirstOrDefault() }); } return(characteristicsValues); }
private bool CompareCharacteristicValues(CharacteristicValue valueA, CharacteristicValue valueB) { if (!valueA.Characteristic.SameIdentityAs(valueB.Characteristic)) { return(false); } return(_characteristicHandlers.Any(x => x.CompareValues(valueA, valueB))); }
/// <summary> /// Calculation method. /// </summary> /// <param name="chainCharacteristicsIds"> /// Dictionary with chain ids as a key /// and characteristicLink ids array as value. /// </param> /// <returns> /// The <see cref="T:double[][]"/>. /// </returns> public static Dictionary <long, Dictionary <short, double> > Calculate(Dictionary <long, short[]> chainCharacteristicsIds) { var newCharacteristics = new List <CharacteristicValue>(); var allCharacteristics = new Dictionary <long, Dictionary <short, double> >(); using (var db = new LibiadaWebEntities()) { short[] characteristicLinkIds = chainCharacteristicsIds.SelectMany(c => c.Value).Distinct().ToArray(); var characteristicTypeLinkRepository = FullCharacteristicRepository.Instance; var calculators = new Dictionary <short, LinkedFullCalculator>(); foreach (short characteristicLinkId in characteristicLinkIds) { Link link = characteristicTypeLinkRepository.GetLinkForCharacteristic(characteristicLinkId); FullCharacteristic characteristic = characteristicTypeLinkRepository.GetCharacteristic(characteristicLinkId); calculators.Add(characteristicLinkId, new LinkedFullCalculator(characteristic, link)); } var commonSequenceRepository = new CommonSequenceRepository(db); long[] sequenceIds = chainCharacteristicsIds.Keys.ToArray(); foreach (long sequenceId in sequenceIds) { short[] sequenceCharacteristicLinkIds = chainCharacteristicsIds[sequenceId]; Dictionary <short, double> characteristics = db.CharacteristicValue .Where(c => sequenceId == c.SequenceId && sequenceCharacteristicLinkIds.Contains(c.CharacteristicLinkId)) .ToDictionary(ct => ct.CharacteristicLinkId, ct => ct.Value); allCharacteristics.Add(sequenceId, characteristics); if (characteristics.Count < sequenceCharacteristicLinkIds.Length) { Chain sequence = commonSequenceRepository.GetLibiadaChain(sequenceId); foreach (short sequenceCharacteristicLinkId in sequenceCharacteristicLinkIds) { if (!characteristics.ContainsKey(sequenceCharacteristicLinkId)) { LinkedFullCalculator calculator = calculators[sequenceCharacteristicLinkId]; double characteristicValue = calculator.Calculate(sequence); var characteristic = new CharacteristicValue { SequenceId = sequenceId, CharacteristicLinkId = sequenceCharacteristicLinkId, Value = characteristicValue }; characteristics.Add(sequenceCharacteristicLinkId, characteristicValue); newCharacteristics.Add(characteristic); } } } } var characteristicRepository = new CharacteristicRepository(db); characteristicRepository.TrySaveCharacteristicsToDatabase(newCharacteristics); return(allCharacteristics); } }
public bool CompareValues(CharacteristicValue valueA, CharacteristicValue valueB) { if (CharacteristicValueType.IsInstanceOfType(valueA) && CharacteristicValueType.IsInstanceOfType(valueB)) { return(DoCompareValues(valueA, valueB)); } return(false); }
public IActionResult Card(CharacteristicValue model, TableItemEvent e) { if (ModelState.IsValid || e == TableItemEvent.Delete) { db.CharacteristicValues.Change(model, e); db.SaveChanges(); return(RedirectToAction("Tables", "Home", new { name = "CharacteristicValues" })); } else { return(Content($"Поля не удовлетворяют условиям")); } }
/// <summary> /// The calculate characteristic. /// </summary> /// <param name="characteristicLinkId"> /// The characteristic type and link id. /// </param> /// <param name="subsequences"> /// The subsequences. /// </param> /// <returns> /// The <see cref="List{Subsequence}"/>. /// </returns> private List <double> CalculateCharacteristic(short characteristicLinkId, Subsequence[] subsequences) { var characteristics = new List <double>(); var newCharacteristics = new List <CharacteristicValue>(); FullCharacteristic fullCharacteristic = characteristicTypeLinkRepository.GetCharacteristic(characteristicLinkId); IFullCalculator calculator = FullCalculatorsFactory.CreateCalculator(fullCharacteristic); Link link = characteristicTypeLinkRepository.GetLinkForCharacteristic(characteristicLinkId); var subsequenceIds = subsequences.Select(s => s.Id); var existingCharacteristics = db.CharacteristicValue .Where(cv => cv.CharacteristicLinkId == characteristicLinkId && subsequenceIds.Contains(cv.SequenceId)) .ToDictionary(cv => cv.SequenceId); Dictionary <long, Chain> sequences = subsequenceExtractor.GetSubsequencesSequences(subsequences); for (int i = 0; i < subsequences.Length; i++) { if (existingCharacteristics.ContainsKey(subsequences[i].Id)) { characteristics.Add(existingCharacteristics[subsequences[i].Id].Value); } else { double value = calculator.Calculate(sequences[subsequences[i].Id], link); var currentCharacteristic = new CharacteristicValue { SequenceId = subsequences[i].Id, CharacteristicLinkId = characteristicLinkId, Value = value }; newCharacteristics.Add(currentCharacteristic); characteristics.Add(value); } } db.CharacteristicValue.AddRange(newCharacteristics); db.SaveChanges(); return(characteristics); }
protected override bool DoCompareValues(CharacteristicValue valueA, CharacteristicValue valueB) { return(((StringCharacteristicValue)valueA).Value == ((StringCharacteristicValue)valueB).Value); }
protected abstract bool DoCompareValues(CharacteristicValue valueA, CharacteristicValue valueB);
/// <summary> /// Calculates subsequences characteristics. /// </summary> /// <param name="characteristicIds"> /// The ids of characteristic types, arrangement types and links as <see cref="FullCharacteristicLink"/>. /// </param> /// <param name="features"> /// The features ids of subsequences to extract. /// </param> /// <param name="parentSequenceId"> /// The parent sequence id. /// </param> /// <param name="filters"> /// Textual search filters for subsequences products. /// </param> /// <returns> /// The <see cref="T:SubsequenceData[]"/> . /// </returns> public static SubsequenceData[] CalculateSubsequencesCharacteristics( short[] characteristicIds, Feature[] features, long parentSequenceId, string[] filters = null) { Dictionary <long, Chain> sequences; long[] subsequenceIds; SubsequenceData[] subsequenceData; Dictionary <long, Dictionary <short, double> > characteristics; var calculators = new IFullCalculator[characteristicIds.Length]; var links = new Link[characteristicIds.Length]; var newCharacteristics = new List <CharacteristicValue>(); // creating local context to avoid memory overflow due to possibly big cache of characteristics using (var db = new LibiadaWebEntities()) { var subsequenceExtractor = new SubsequenceExtractor(db); Subsequence[] subsequences = filters == null? subsequenceExtractor.GetSubsequences(parentSequenceId, features) : subsequenceExtractor.GetSubsequences(parentSequenceId, features, filters); subsequenceData = subsequences.Select(s => new SubsequenceData(s)).ToArray(); // converting to libiada sequences subsequenceIds = subsequences.Select(s => s.Id).ToArray(); characteristics = db.CharacteristicValue .Where(c => characteristicIds.Contains(c.CharacteristicLinkId) && subsequenceIds.Contains(c.SequenceId)) .ToArray() .GroupBy(c => c.SequenceId) .ToDictionary(c => c.Key, c => c.ToDictionary(ct => ct.CharacteristicLinkId, ct => ct.Value)); if (characteristics.Count == subsequences.Length && characteristics.All(c => c.Value.Count == characteristicIds.Length)) { sequences = new Dictionary <long, Chain>(); } else { sequences = subsequenceExtractor.GetSubsequencesSequences(subsequences); } } var characteristicTypeLinkRepository = FullCharacteristicRepository.Instance; for (int k = 0; k < characteristicIds.Length; k++) { short characteristicLinkId = characteristicIds[k]; FullCharacteristic characteristic = characteristicTypeLinkRepository.GetCharacteristic(characteristicLinkId); calculators[k] = FullCalculatorsFactory.CreateCalculator(characteristic); links[k] = characteristicTypeLinkRepository.GetLinkForCharacteristic(characteristicLinkId); } // cycle through subsequences for (int i = 0; i < subsequenceIds.Length; i++) { characteristics.TryGetValue(subsequenceIds[i], out Dictionary <short, double> sequenceDbCharacteristics); sequenceDbCharacteristics = sequenceDbCharacteristics ?? new Dictionary <short, double>(); var values = new double[calculators.Length]; // cycle through characteristics and notations for (int j = 0; j < calculators.Length; j++) { short characteristicLinkId = characteristicIds[j]; if (!sequenceDbCharacteristics.TryGetValue(characteristicLinkId, out values[j])) { values[j] = calculators[j].Calculate(sequences[subsequenceIds[i]], links[j]); var currentCharacteristic = new CharacteristicValue { SequenceId = subsequenceIds[i], CharacteristicLinkId = characteristicLinkId, Value = values[j] }; newCharacteristics.Add(currentCharacteristic); } } subsequenceData[i].CharacteristicsValues = values; } using (var db = new LibiadaWebEntities()) { // trying to save calculated characteristics to database var characteristicRepository = new CharacteristicRepository(db); characteristicRepository.TrySaveCharacteristicsToDatabase(newCharacteristics); } return(subsequenceData); }
private void A2LSymbolsWrite(ref System.IO.StreamWriter fileA2L) { Excel.Application xlApp; Excel.Workbook xlWorkBook; Excel.Worksheet xlWorkSheet_Parameters; Excel.Worksheet xlWorkSheet_Signals; Excel.Worksheet xlWorkSheet_Defines; Excel.Worksheet xlWorkSheet_States; string Line; int LineNum; int RetVal = 0; int index; bool ConvExists = false; bool GroupExists = false; XLSECTParameter XLSECTParameter1 = new XLSECTParameter(); XLSECTSignal XLSECTSignal1 = new XLSECTSignal(); Measurement Measurement1 = new Measurement(); CharacteristicValue CharacteristicValue1 = new CharacteristicValue(); Axis_Pts Axis_Pts1 = new Axis_Pts(); CharacteristicCurve CharacteristicCurve1 = new CharacteristicCurve(); CharacteristicMap CharacteristicMap1 = new CharacteristicMap(); // CONV Conversion ConversionNew = new Conversion(); // GROUP A2LGroup GroupNew = new A2LGroup(); A2LGroup GPCurrent = null; A2LGroupList = new List <A2LGroup>(); ConversionList = new List <Conversion>(); object misValue = System.Reflection.Missing.Value; try { xlApp = new Excel.Application(); xlWorkBook = xlApp.Workbooks.Open(filePath, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); xlWorkSheet_Parameters = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); xlWorkSheet_Signals = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2); xlWorkSheet_Defines = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(3); xlWorkSheet_States = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(4); // Generate the signals and write them in the new XML file Line = "2"; LineNum = 2; // While there are symbols in the Excel sheet while (-2 != RetVal) { // Elaborate the signals sheet RetVal = XLSECTSignal1.upload(ref xlWorkSheet_Signals, Line); #if zorro // Build the Scaling IDs list // Create the new COMPU_METHOD ConversionNew.upload(ref XLSECTSignal1); ConvExists = false; // Check if it exists already if (CScalingList != null) { foreach (CalibrationScaling CS in CScalingList) { if (CS.ID == ConversionNew.ID) { ConvExists = true; } } } // If it doesn't exist, add it to the list if (ConvExists == false) { ConversionList.Add(ConversionNew); ConversionNew = new Conversion(); } #endif // Build the Group IDs list // Create the new group GroupNew.upload(ref XLSECTSignal1); GroupExists = false; // Check if it exists already foreach (A2LGroup GP in A2LGroupList) { if (GP.ID == GroupNew.ID) { GroupExists = true; GPCurrent = GP; break; } } // If it doesn't exist, add it to the list if (GroupExists == false) { // GroupNew.Add(ref XLSECTSignal1); A2LGroupList.Add(GroupNew); GPCurrent = GroupNew; /* Create the new group for next group */ GroupNew = new A2LGroup(); } // If it is a Signal (MEASUREMENT in A2L nomenclature) if ((RetVal == 0) || (RetVal == 123456789)) { Measurement1.upload(ref XLSECTSignal1, false, 0, ref Containr); Measurement1.AppendToFile(ref fileA2L); GPCurrent.Add(ref XLSECTSignal1); // SignalValue1.Show(); } else { if (RetVal > 1) { if (RetVal > 123456789) { RetVal -= 123456789; } for (index = 0; index < RetVal; index++) { Measurement1.upload(ref XLSECTSignal1, true, index, ref Containr); Measurement1.AppendToFile(ref fileA2L); GPCurrent.Add(ref XLSECTSignal1, index); } } } LineNum++; Line = Convert.ToString(LineNum); } // Generate the calibrations and write them in the new XML file Line = "2"; LineNum = 2; RetVal = 0; while (-2 != RetVal) { // Elaborate the parameters sheet RetVal = XLSECTParameter1.upload(ref xlWorkSheet_Parameters, Line); #if NOT_NECESSARY CScalingNew.upload(ref XLSECTParameter1); ScalingExists = false; foreach (CalibrationScaling CS in CScalingList) { if (CS.ID == CScalingNew.ID) { ScalingExists = true; } } if (ScalingExists == false) { CScalingList.Add(CScalingNew); CScalingNew = new CalibrationScaling(); } #endif // Create the new group GroupNew.upload(ref XLSECTParameter1); GroupExists = false; // Check if it exists already foreach (A2LGroup GP in A2LGroupList) { if (GP.ID == GroupNew.ID) { GroupExists = true; GPCurrent = GP; break; } } // If it doesn't exist, add it to the list if (GroupExists == false) { // GroupNew.Add(ref XLSECTParameter1); A2LGroupList.Add(GroupNew); GPCurrent = GroupNew; /* Create the new group for next group */ GroupNew = new A2LGroup(); } // If it is a CalibrationValue if (RetVal == 0) { CharacteristicValue1.upload(ref XLSECTParameter1, ref Containr); CharacteristicValue1.AppendToFile(ref fileA2L); GPCurrent.Add(ref XLSECTParameter1); // CalibrationValue1.Show(); } // If it is a CalibrationSharedAxis if (RetVal == 1) { Axis_Pts1.upload(ref XLSECTParameter1, ref Containr); Axis_Pts1.AppendToFile(ref fileA2L); GPCurrent.Add(ref XLSECTParameter1, true); // CalibrationSharedAxis1.Show(); } // If it is a CalibrationCurve if (RetVal == 2) { CharacteristicCurve1.upload(ref XLSECTParameter1, ref Containr); CharacteristicCurve1.AppendToFile(ref fileA2L); GPCurrent.Add(ref XLSECTParameter1, true); // CalibrationCurve1.Show(); } // If it is a CalibrationMap if (RetVal == 3) { CharacteristicMap1.upload(ref XLSECTParameter1, ref Containr); CharacteristicMap1.AppendToFile(ref fileA2L); GPCurrent.Add(ref XLSECTParameter1, true); // CalibrationMap1.Show(); } // Copy record layouts LineNum++; Line = Convert.ToString(LineNum); } if (A2LGroupList != null) { fileA2L.WriteLine(" /begin GROUP Calibration \"All the calibration parameters and table\""); fileA2L.WriteLine(" ROOT"); fileA2L.WriteLine(" /begin SUB_GROUP"); foreach (A2LGroup GP in A2LGroupList) { fileA2L.WriteLine(" " + GP.ID); } fileA2L.WriteLine(" /end SUB_GROUP"); fileA2L.WriteLine(" /end GROUP"); foreach (A2LGroup GP in A2LGroupList) { GP.AppendToFile(ref fileA2L); } } #if NOT_NECESSARY foreach (CalibrationScaling CS in CScalingList) { CS.AppendToFile(ref fileXML); } fileXML.WriteLine("</LIE00V12PARTIAL>"); fileXML.Close(); #endif xlWorkBook.Close(true, misValue, misValue); xlApp.Quit(); releaseObject(xlWorkSheet_Parameters); releaseObject(xlWorkSheet_Signals); releaseObject(xlWorkSheet_Defines); releaseObject(xlWorkSheet_States); releaseObject(xlWorkBook); releaseObject(xlApp); } catch { MessageBox.Show("File " + filePath + " doesn't exist"); } }