/
docNemo
/
clothes-graph
Обзор
Документация
Войти
/
docNemo
/
clothes-graph
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
src/ClothesGraph.Model/Storage/ExpressionMapper.cs
108 строк
4 KB
docNemo
Фундамент продукта: параметрическое построение выкроек, печать, установщик
08 авг 2026, 22:17
08 авг 2026, 22:17
cd47cce
Код
Авторство
О чём код?
using System; using System.Collections.Generic; using System.Linq; using ClothesGraph.Model.Drafting; using ClothesGraph.Model.Formulas; using ClothesGraph.Model.Measurements; namespace ClothesGraph.Model.Storage; /// <summary>Перевод формул между моделью и файлом.</summary> internal static class ExpressionMapper { public static ExpressionDto ToDto(Formula formula) => ToDto(formula.Root); private static ExpressionDto ToDto(Expression expression) => expression switch { NumberExpression number => new ExpressionDto { Kind = "number", Value = number.Value }, MeasurementExpression measurement => new ExpressionDto { Kind = "measurement", Reference = measurement.Measurement.Value }, PropertyExpression property => new ExpressionDto { Kind = "property", Reference = property.Node.Value, Property = property.Property.ToString() }, NegateExpression negate => new ExpressionDto { Kind = "negate", Operands = [ToDto(negate.Operand)] }, BinaryExpression binary => new ExpressionDto { Kind = "binary", Operator = binary.Operator.ToString(), Operands = [ToDto(binary.Left), ToDto(binary.Right)] }, CallExpression call => new ExpressionDto { Kind = "call", Function = call.Function, Operands = call.Arguments.Select(ToDto).ToList() }, _ => throw new InvalidPatternFileException( $"Неизвестный узел формулы {expression.GetType().Name}") }; public static Formula ToFormula(ExpressionDto? dto) => dto is null ? Formula.Constant(0) : Formula.FromExpression(ToExpression(dto)); private static Expression ToExpression(ExpressionDto dto) => dto.Kind switch { "number" => new NumberExpression(dto.Value ?? 0), "measurement" => new MeasurementExpression( MeasurementId.FromGuid(Required(dto.Reference, "ссылка на мерку"))), "property" => new PropertyExpression( NodeId.FromGuid(Required(dto.Reference, "ссылка на построение")), Enum.TryParse<NodeProperty>(dto.Property, out var property) ? property : throw new InvalidPatternFileException( $"Неизвестное свойство построения «{dto.Property}»")), "negate" => new NegateExpression(ToExpression(Single(dto))), "binary" => new BinaryExpression( Enum.TryParse<BinaryOperator>(dto.Operator, out var op) ? op : throw new InvalidPatternFileException( $"Неизвестная операция «{dto.Operator}»"), ToExpression(Operand(dto, 0)), ToExpression(Operand(dto, 1))), "call" => new CallExpression( dto.Function ?? throw new InvalidPatternFileException("Не указана функция"), (dto.Operands ?? []).Select(ToExpression).ToList()), _ => throw new InvalidPatternFileException($"Неизвестный вид формулы «{dto.Kind}»") }; private static Guid Required(Guid? value, string what) => value ?? throw new InvalidPatternFileException($"В формуле отсутствует {what}"); private static ExpressionDto Single(ExpressionDto dto) => dto.Operands is { Count: 1 } ? dto.Operands[0] : throw new InvalidPatternFileException("Ожидался один операнд"); private static ExpressionDto Operand(ExpressionDto dto, int index) => dto.Operands is { } operands && operands.Count > index ? operands[index] : throw new InvalidPatternFileException($"Отсутствует операнд {index + 1}"); }