private static void UpdateStoredMetric( [NotNull] XElement targetNode, [CanBeNull] CompetitionMetricValue metricValue, bool forceUpdate) { if (metricValue == null || metricValue.ValuesRange.IsEmpty) { targetNode.SetAttribute(MinAttribute, null); targetNode.SetAttribute(MaxAttribute, null); targetNode.SetAttribute(UnitAttribute, null); } else if (forceUpdate || metricValue.HasUnsavedChanges || !targetNode.HasAttributes) { var metricRange = metricValue.ValuesRange; metricValue.ValuesRange.GetMinMaxString(metricValue.DisplayMetricUnit, out var minValueText, out var maxValueText); if (double.IsInfinity(metricRange.Min)) { minValueText = XmlConvert.ToString(metricRange.Min); } if (double.IsInfinity(metricRange.Max)) { maxValueText = XmlConvert.ToString(metricRange.Max); } var unit = metricValue.DisplayMetricUnit; targetNode.SetAttribute(MinAttribute, minValueText); targetNode.SetAttribute(MaxAttribute, maxValueText); targetNode.SetAttribute(UnitAttribute, unit.IsEmpty ? null : unit.DisplayName); } }
private static bool CheckMetricValue( Target target, CompetitionMetricValue metricValue, IMessageLogger messageLogger) { var metric = metricValue.Metric; if (metric.MetricUnits.IsEmpty) { if (!metricValue.DisplayMetricUnit.IsEmpty) { messageLogger.WriteSetupErrorMessage( target, $"{metric} metric value was parsed incorrectly. Unit value should be null as metric has empty units scale.", "Ensure that annotation does not include metric unit."); return(false); } } else if (metricValue.DisplayMetricUnit.IsEmpty) { messageLogger.WriteSetupErrorMessage( target, $"{metric} metric value was parsed incorrectly. Unit value should be not null as metric has empty units scale.", "Ensure that annotation does include metric unit."); return(false); } return(true); }
private static string FixAttributeContent(Match m, CompetitionMetricValue metricValue, out bool hasMatch) { var attributeStartText = m.Groups[RegexPrefixPart].Value; var attributeEndText = m.Groups[RegexSuffixPart].Value; var optionalArgsText = GetOptionalAttributeArgs(m.Groups[RegexArgsPart]?.Value); var result = new StringBuilder(m.Length + 10); result .Append(attributeStartText) .Append("("); AppendFirstAttributeArgs(metricValue, result); if (optionalArgsText.NotNullNorEmpty()) { result .Append(",") .Append(optionalArgsText); } result .Append(")") .Append(attributeEndText); hasMatch = true; return(result.ToString()); }
private static string GetInplaceAnnotationText(CompetitionMetricValue metricValue) { var result = new StringBuilder(); result.Append(", "); result.Append(metricValue.Metric.AttributeType.GetShortAttributeName()); result.Append("("); AppendFirstAttributeArgs(metricValue, result); result.Append(")"); return(result.ToString()); }
private static string GetNewAnnotationLine(string whitespacePrefix, CompetitionMetricValue metricValue) { var result = new StringBuilder(); result.Append(whitespacePrefix); result.Append("["); result.Append(metricValue.Metric.AttributeType.GetShortAttributeName()); result.Append("("); AppendFirstAttributeArgs(metricValue, result); result.Append(")]"); return(result.ToString()); }
private static bool TryUpdateLineCore(string line, CompetitionMetricValue metricValue, out string newLine) { var hasMatch = false; var regex = _regexCache(metricValue.Metric.AttributeType); newLine = regex.Replace( line, m => FixAttributeContent(m, metricValue, out hasMatch), 1); return(hasMatch); }
public static bool TryUpdateLineWithAttribute( SourceAnnotationFile sourceFile, int attributeLineNumber, CompetitionMetricValue metricValue) { var line = sourceFile[attributeLineNumber]; if (!TryUpdateLineCore(line, metricValue, out var newLine)) { return(false); } if (newLine != line) { sourceFile.ReplaceLine(attributeLineNumber, newLine); } return(true); }
private static void AppendFirstAttributeArgs(CompetitionMetricValue metricValue, StringBuilder result) { var metric = metricValue.Metric; var metricRange = metricValue.ValuesRange; var metricUnit = metricValue.DisplayMetricUnit; if (metricRange.Min.Equals(0) && metricRange.Max.Equals(0)) { result.Append("0"); } else if (metricRange.IsNotEmpty) { metricRange.GetMinMaxString(metricUnit, out var minValueText, out var maxValueText); if (double.IsInfinity(metricRange.Min)) { minValueText = $"double.{nameof(double.NegativeInfinity)}"; } if (double.IsInfinity(metricRange.Max)) { maxValueText = $"double.{nameof(double.PositiveInfinity)}"; } if (metricRange.ShouldStoreMinMetricValue(metricUnit, metric)) { result.Append(minValueText).Append(", "); } result.Append(maxValueText); if (!metric.MetricUnits.IsEmpty) { var metricEnumType = metric.MetricUnits.MetricEnumType; Code.BugIf( metricEnumType == null || metricUnit.EnumValue == null || !Enum.IsDefined(metricEnumType, metricUnit.EnumValue), "Bad enum value."); result .Append(", ") .Append(metricEnumType.Name) .Append(".") .Append(metricUnit.EnumValue); } } }
public static int InsertLineWithAttribute( SourceAnnotationFile sourceCodeFile, int insertLineNumber, TargetSourceLines benchmarkMethod, CompetitionMetricValue metricValue) { var whitespacePrefix = GetWhitespacePrefix(sourceCodeFile[insertLineNumber]); var newLine = GetNewAnnotationLine(whitespacePrefix, metricValue); var newLineNumber = insertLineNumber + 1; sourceCodeFile.InsertLine(newLineNumber, newLine); var attributeTypeHandle = metricValue.Metric.AttributeType.TypeHandle; benchmarkMethod.AddAttribute(attributeTypeHandle, newLineNumber); return(newLineNumber); }
public static bool TryInsertAttributeInplace( SourceAnnotationFile sourceCodeFile, int inplaceLineNumber, TargetSourceLines benchmarkMethod, CompetitionMetricValue metricValue) { var line = sourceCodeFile[inplaceLineNumber]; var inplacePosition = line.LastIndexOf(']'); if (inplacePosition < 0) { return(false); } var appendText = GetInplaceAnnotationText(metricValue); line = line.Insert(inplacePosition, appendText); sourceCodeFile.ReplaceLine(inplaceLineNumber, line); var attributeTypeHandle = metricValue.Metric.AttributeType.TypeHandle; benchmarkMethod.AddAttribute(attributeTypeHandle, inplaceLineNumber); return(true); }