public static IConditionTreeItem SimplifyConditions(IConditionTreeItem item) { switch (item) { case ConditionTreeLeaf leaf: return(leaf); case ConditionTree tree: { var left = SimplifyConditions(tree.Left); var right = SimplifyConditions(tree.Right); if (left == ConditionTree.Empty && right == ConditionTree.Empty) { return(ConditionTree.Empty); } if (left == ConditionTree.Empty) { return(right); } if (right == ConditionTree.Empty) { return(left); } return(tree); } default: throw new NotSupportedException(); } }
private Conditional <FlagOptions> ReadFlagOptionNode(XElement flagElement, LoadContext lc) { IConditionTreeItem conditions = ConditionTree.Empty; var conditionsAttribute = flagElement.Attribute("conditions"); if (conditionsAttribute != null) { IXmlLineInfo line = conditionsAttribute as IXmlLineInfo; try { conditions = lc.ConditionParser.Parse(conditionsAttribute.Value, lc.ParseContext); } catch (ConditionFormatException ex) { lc.Errors.Add(new LoadError(lc.FileName, line.LineNumber, line.LinePosition + conditionsAttribute.Name.LocalName.Length + 2 + ex.PositionStart, LoadErrorCategory.Error, ex.Message)); return(null); } } FlagOptions theOptions = FlagOptions.None; foreach (var node in flagElement.Descendants(lc.NS + "option")) { theOptions |= (FlagOptions)Enum.Parse(typeof(FlagOptions), node.Value, true); } return(new Conditional <FlagOptions>(theOptions, conditions)); }
protected virtual IEnumerable <XmlConnectionGroup> ReadConnectionGroup(ComponentDescription description, XElement connectionGroupElement, XmlConnectionGroup parentGroup) { IConditionTreeItem conditionCollection = ConditionTree.Empty; var conditionsAttribute = connectionGroupElement.Attribute("conditions"); if (conditionsAttribute != null) { if (!conditionParser.Parse(conditionsAttribute, description, logger, out conditionCollection)) { yield break; } } var connectionGroup = new XmlConnectionGroup(new ConditionTree(ConditionTree.ConditionOperator.AND, parentGroup.Conditions, conditionCollection)); autoRotateOptionsReader.TrySetAutoRotateOptions(connectionGroupElement, parentGroup, connectionGroup); var childGroups = connectionGroupElement.Elements().SelectMany(x => ReadElement(x, description, connectionGroup)); yield return(connectionGroup); foreach (var child in childGroups) { yield return(child); } }
private IEnumerable <RenderDescription> ReadLineCommand(XElement element, IConditionTreeItem baseConditions, IReadOnlyList <string> usedDefinitions) { if (!element.GetAttribute("start", logger, out var start) || !element.GetAttribute("end", logger, out var end)) { yield break; } Console.WriteLine(start.Value); var startPoints = EnumerateComponentPoint(start, baseConditions, usedDefinitions).ToList(); var endPoints = EnumerateComponentPoint(end, baseConditions, usedDefinitions).ToList(); foreach (var startPoint in startPoints) { foreach (var endPoint in endPoints) { var command = new Line(); command.Start = startPoint.Value; command.End = endPoint.Value; if (element.Attribute("thickness") != null) { command.Thickness = double.Parse(element.Attribute("thickness").Value); } var conditions = new ConditionTree(ConditionTree.ConditionOperator.AND, baseConditions, new ConditionTree(ConditionTree.ConditionOperator.AND, startPoint.Conditions, endPoint.Conditions)); yield return(new RenderDescription(conditions, new IRenderCommand[] { command })); } } }
private IEnumerable <RenderDescription> ReadRectCommand(XElement element, IConditionTreeItem baseConditions, IReadOnlyList <string> usedDefinitions) { if (!element.GetAttribute("x", logger, out var x) || !element.GetAttribute("y", logger, out var y)) { yield break; } var locationPoints = EnumerateComponentPoint(x, y, baseConditions, usedDefinitions).ToList(); foreach (var locationPoint in locationPoints) { var command = new Rectangle(); if (element.Attribute("thickness") != null) { command.StrokeThickness = double.Parse(element.Attribute("thickness").Value); } var fill = element.Attribute("fill"); if (fill != null && fill.Value.ToLowerInvariant() != "false") { command.Fill = true; } command.Width = double.Parse(element.Attribute("width").Value); command.Height = double.Parse(element.Attribute("height").Value); command.Location = locationPoint.Value; var conditions = new ConditionTree(ConditionTree.ConditionOperator.AND, baseConditions, locationPoint.Conditions); yield return(new RenderDescription(conditions, new IRenderCommand[] { command })); } }
public static void Write(this System.IO.BinaryWriter writer, IConditionTreeItem value) { if (value == ConditionTree.Empty) { writer.Write((byte)0); // 0 for empty } else if (value is ConditionTree) { writer.Write((byte)1); // 1 for tree var tree = value as ConditionTree; writer.Write((ushort)tree.Operator); writer.Write(tree.Left); writer.Write(tree.Right); } else if (value is ConditionTreeLeaf) { var condition = value as ConditionTreeLeaf; writer.Write((byte)2); // 0 for condition writer.Write((int)condition.Type); writer.Write((int)condition.Comparison); writer.Write(condition.VariableName); writer.WriteType(condition.CompareTo); } }
public static IConditionTreeItem ReadConditionTree(this System.IO.BinaryReader reader) { byte type = reader.ReadByte(); if (type == 0) { // Empty return(ConditionTree.Empty); } else if (type == 1) { // Tree ConditionTree.ConditionOperator op = (ConditionTree.ConditionOperator)reader.ReadUInt16(); IConditionTreeItem left = reader.ReadConditionTree(); IConditionTreeItem right = reader.ReadConditionTree(); return(new ConditionTree(op, left, right)); } else if (type == 2) { ConditionType conditionType = (ConditionType)reader.ReadInt32(); ConditionComparison comparison = (ConditionComparison)reader.ReadInt32(); string variableName = reader.ReadString(); BinaryType binType; object compareTo = reader.ReadType(out binType); return(new ConditionTreeLeaf(conditionType, variableName, comparison, binType.ToPropertyUnion(compareTo))); } else { throw new System.IO.InvalidDataException(); } }
private void ReadConnectionsSection(XElement declaration, LoadContext lc, ComponentDescription description) { List <ConnectionGroup> parsedConnectionGroups = new List <ConnectionGroup>(); var connectionGroupNodes = declaration.XPathSelectElements("/cd:connections/cd:group", lc.NamespaceManager); foreach (var connectionGroupNode in connectionGroupNodes) { IConditionTreeItem conditionCollection = ConditionTree.Empty; List <ConnectionDescription> connections = new List <ConnectionDescription>(); var conditionsAttribute = connectionGroupNode.Attribute("conditions"); if (conditionsAttribute != null) { IXmlLineInfo line = conditionsAttribute as IXmlLineInfo; try { conditionCollection = lc.ConditionParser.Parse(connectionGroupNode.Attribute("conditions").Value, lc.ParseContext); } catch (ConditionFormatException ex) { lc.Errors.Add(new LoadError(lc.FileName, line.LineNumber, line.LinePosition + conditionsAttribute.Name.LocalName.Length + 2 + ex.PositionStart, LoadErrorCategory.Error, ex.Message)); continue; } } foreach (var connectionNode in connectionGroupNode.Elements(ns + "connection")) { ConnectionEdge edge = ConnectionEdge.None; if (connectionNode.Attribute("edge") != null) { string edgeText = connectionNode.Attribute("edge").Value.ToLowerInvariant(); if (edgeText == "start") { edge = ConnectionEdge.Start; } else if (edgeText == "end") { edge = ConnectionEdge.End; } else if (edgeText == "both") { edge = ConnectionEdge.Both; } } string connectionName = "#"; if (connectionNode.Attribute("name") != null) { connectionName = connectionNode.Attribute("name").Value; } connections.Add(new ConnectionDescription(new ComponentPoint(connectionNode.Attribute("start").Value), new ComponentPoint(connectionNode.Attribute("end").Value), edge, connectionName)); } parsedConnectionGroups.Add(new ConnectionGroup(conditionCollection, connections.ToArray())); } description.Connections = parsedConnectionGroups.ToArray(); }
public void ReadSection(XElement connectionsElement, ComponentDescription description) { List <ConnectionGroup> parsedConnectionGroups = new List <ConnectionGroup>(); var connectionGroupNodes = connectionsElement.Elements(XmlLoader.ComponentNamespace + "group"); foreach (var connectionGroupNode in connectionGroupNodes) { IConditionTreeItem conditionCollection = ConditionTree.Empty; List <ConnectionDescription> connections = new List <ConnectionDescription>(); var conditionsAttribute = connectionGroupNode.Attribute("conditions"); if (conditionsAttribute != null) { if (!conditionParser.Parse(conditionsAttribute, description, logger, out conditionCollection)) { continue; } } foreach (var connectionNode in connectionGroupNode.Elements(XmlLoader.ComponentNamespace + "connection")) { ConnectionEdge edge = ConnectionEdge.None; if (connectionNode.Attribute("edge") != null) { string edgeText = connectionNode.Attribute("edge").Value.ToLowerInvariant(); if (edgeText == "start") { edge = ConnectionEdge.Start; } else if (edgeText == "end") { edge = ConnectionEdge.End; } else if (edgeText == "both") { edge = ConnectionEdge.Both; } } string connectionName = "#"; if (connectionNode.Attribute("name") != null) { connectionName = connectionNode.Attribute("name").Value; } if (!componentPointParser.TryParse(connectionNode.Attribute("start"), out var start) || !componentPointParser.TryParse(connectionNode.Attribute("end"), out var end)) { continue; } connections.Add(new ConnectionDescription(start, end, edge, connectionName)); } parsedConnectionGroups.Add(new ConnectionGroup(conditionCollection, connections.ToArray())); } description.Connections = parsedConnectionGroups.ToArray(); }
protected virtual RenderDescription ReadRenderGroup(ComponentDescription description, XElement renderNode) { IConditionTreeItem conditionCollection = ConditionTree.Empty; var conditionsAttribute = renderNode.Attribute("conditions"); if (conditionsAttribute != null) { if (!conditionParser.Parse(conditionsAttribute, description, logger, out conditionCollection)) { return(null); } } var commands = new List <IRenderCommand>(); foreach (var renderCommandNode in renderNode.Descendants()) { string commandType = renderCommandNode.Name.LocalName; if (commandType == "line") { if (ReadLineCommand(renderCommandNode, out var command)) { commands.Add(command); } } else if (commandType == "rect") { if (ReadRectCommand(renderCommandNode, out var command)) { commands.Add(command); } } else if (commandType == "ellipse") { if (ReadEllipseCommand(description.Metadata.FormatVersion, renderCommandNode, out var command)) { commands.Add(command); } } else if (commandType == "text") { if (ReadTextCommand(renderCommandNode, description, out var command)) { commands.Add(command); } } else if (commandType == "path") { if (ReadPathCommand(renderCommandNode, out var command)) { commands.Add(command); } } } return(new RenderDescription(conditionCollection, commands.ToArray())); }
private Conditional <FlagOptions> ReadFlagOptionNode(ComponentDescription description, XElement flagElement) { IConditionTreeItem conditions = ConditionTree.Empty; var conditionsAttribute = flagElement.Attribute("conditions"); if (conditionsAttribute != null) { conditionParser.Parse(conditionsAttribute, description, logger, out conditions); } FlagOptions theOptions = FlagOptions.None; foreach (var node in flagElement.Elements(flagElement.GetDefaultNamespace() + "option")) { theOptions |= (FlagOptions)Enum.Parse(typeof(FlagOptions), node.Value, true); } return(new Conditional <FlagOptions>(theOptions, conditions)); }
private IConditionTreeItem ParseToken(Queue <ConditionToken> r, ComponentDescription description) { if (r.Count == 0) { throw new ConditionFormatException("Invalid condition", 0, 0); } ConditionToken t = r.Dequeue(); if (t.Type == ConditionToken.TokenType.Symbol) { return(ParseLeaf(t, description)); } else if (t.Type == ConditionToken.TokenType.Operator && t.Operator == ConditionToken.OperatorType.AND) { IConditionTreeItem right = ParseToken(r, description); IConditionTreeItem left = ParseToken(r, description); return(new ConditionTree( ConditionTree.ConditionOperator.AND, left, right)); } else if (t.Type == ConditionToken.TokenType.Operator && t.Operator == ConditionToken.OperatorType.OR) { IConditionTreeItem right = ParseToken(r, description); IConditionTreeItem left = ParseToken(r, description); return(new ConditionTree( ConditionTree.ConditionOperator.OR, left, right)); } else { throw new ArgumentException("Invalid queue.", "r"); } }
private IEnumerable <RenderDescription> ReadTextCommand(XElement element, ComponentDescription description, IConditionTreeItem baseConditions, IReadOnlyList <string> usedDefinitions) { if (!element.GetAttribute("x", logger, out var x) || !element.GetAttribute("y", logger, out var y)) { yield break; } var locationPoints = EnumerateComponentPoint(x, y, baseConditions, usedDefinitions).ToList(); foreach (var locationPoint in locationPoints) { if (!ReadTextCommand(element, description, out var command)) { yield break; } command.Location = locationPoint.Value; foreach (var resolvedCommand in EnumerateRenderText(element, command, locationPoint.Conditions, usedDefinitions)) { yield return(new RenderDescription(resolvedCommand.Conditions, new IRenderCommand[] { resolvedCommand.Value })); } } }
private IConditionTreeItem TransformConditionTreeItem(ComponentDescription description, IConditionTreeItem conditions) { switch (conditions) { case ConditionTreeLeaf leaf: if (IsBooleanVariable(description, leaf)) { switch (leaf.Comparison) { case ConditionComparison.Truthy: _logger.LogInformation("Converting 'truthy' comparison on boolean variable to an 'equals true' comparison"); return(new ConditionTreeLeaf( leaf.Type, leaf.VariableName, ConditionComparison.Equal, new Circuit.PropertyValue(true))); case ConditionComparison.Falsy: _logger.LogInformation("Converting 'falsy' comparison on boolean variable to an 'equals false' comparison"); return(new ConditionTreeLeaf( leaf.Type, leaf.VariableName, ConditionComparison.Equal, new Circuit.PropertyValue(false))); } } break; case ConditionTree tree: return(new ConditionTree( tree.Operator, TransformConditionTreeItem(description, tree.Left), TransformConditionTreeItem(description, tree.Right) )); } // Unchanged return(conditions); }
public ConditionTree(ConditionOperator op, IConditionTreeItem left, IConditionTreeItem right) { this.Operator = op; this.Left = left; this.Right = right; }
public ComponentPropertyFormat(string value, IConditionTreeItem conditions) { Value = value; Conditions = conditions; }
public Conditional(T value, IConditionTreeItem conditions) { Value = value; Conditions = conditions; }
private ComponentProperty ReadPropertyNode(XElement propertyElement, LoadContext lc) { IXmlLineInfo elementLine = propertyElement as IXmlLineInfo; string propertyName = propertyElement.Attribute("name").Value; string type = propertyElement.Attribute("type").Value; string defaultValue = propertyElement.Attribute("default").Value; string serializeAs = propertyElement.Attribute("serialize").Value; string display = propertyElement.Attribute("display").Value; PropertyType propertyType; switch (type.ToLowerInvariant()) { case "double": propertyType = PropertyType.Decimal; if (lc.FormatVersion >= new Version(1, 2)) { lc.Errors.Add(new LoadError(lc.FileName, elementLine.LineNumber, elementLine.LinePosition, LoadErrorCategory.Warning, "Property type 'double' is deprecated, use 'decimal' instead")); } break; case "decimal": propertyType = PropertyType.Decimal; break; case "int": propertyType = PropertyType.Integer; break; case "bool": propertyType = PropertyType.Boolean; break; default: propertyType = PropertyType.String; break; } PropertyUnion propertyDefaultValue = new PropertyUnion(defaultValue, propertyType); List <string> propertyOptions = null; if (type == "enum") { propertyOptions = new List <string>(); var optionNodes = propertyElement.Elements(ns + "option"); foreach (var optionNode in optionNodes) { propertyOptions.Add(optionNode.Value); } } List <ComponentPropertyFormat> formatRules = new List <ComponentPropertyFormat>(); if (propertyElement.Attribute("format") != null) { formatRules.Add(new ComponentPropertyFormat(propertyElement.Attribute("format").Value, ConditionTree.Empty)); } else { var formatRuleNodes = propertyElement.XPathSelectElements("cd:formatting/cd:format", lc.NamespaceManager); foreach (var formatNode in formatRuleNodes) { IXmlLineInfo line = formatNode as IXmlLineInfo; IConditionTreeItem conditionCollection = ConditionTree.Empty; if (formatNode.Attribute("conditions") != null) { try { conditionCollection = lc.ConditionParser.Parse(formatNode.Attribute("conditions").Value, lc.ParseContext); } catch (ConditionFormatException ex) { lc.Errors.Add(new LoadError(lc.FileName, line.LineNumber, line.LinePosition + formatNode.Name.LocalName.Length + 2 + ex.PositionStart, LoadErrorCategory.Error, ex.Message)); return(null); } } formatRules.Add(new ComponentPropertyFormat(formatNode.Attribute("value").Value, conditionCollection)); } } Dictionary <PropertyOtherConditionType, IConditionTreeItem> otherConditions = new Dictionary <PropertyOtherConditionType, IConditionTreeItem>(); var otherConditionsNodes = propertyElement.XPathSelectElements("cd:other/cd:conditions", lc.NamespaceManager); foreach (var otherConditionNode in otherConditionsNodes) { if (otherConditionNode != null && otherConditionNode.Attribute("for") != null && otherConditionNode.Attribute("value") != null) { IXmlLineInfo line = otherConditionNode as IXmlLineInfo; string conditionsFor = otherConditionNode.Attribute("for").Value; string conditionsString = otherConditionNode.Attribute("value").Value; IConditionTreeItem conditionCollection; try { conditionCollection = lc.ConditionParser.Parse(conditionsString, lc.ParseContext); } catch (ConditionFormatException ex) { lc.Errors.Add(new LoadError(lc.FileName, line.LineNumber, line.LinePosition + otherConditionNode.Name.LocalName.Length + 2 + ex.PositionStart, LoadErrorCategory.Error, ex.Message)); return(null); } if (Enum.IsDefined(typeof(PropertyOtherConditionType), conditionsFor)) { otherConditions.Add((PropertyOtherConditionType)Enum.Parse(typeof(PropertyOtherConditionType), conditionsFor, true), conditionCollection); } } } ComponentProperty property = new ComponentProperty(propertyName, serializeAs, display, propertyType, propertyDefaultValue, formatRules.ToArray(), otherConditions, (propertyOptions == null ? null : propertyOptions.ToArray())); return(property); }
private void ReadRenderSection(XElement declaration, LoadContext lc, ComponentDescription description) { List <RenderDescription> parsedRenderDescriptions = new List <RenderDescription>(); var renderDescriptions = declaration.XPathSelectElements("/cd:render/cd:group", lc.NamespaceManager); foreach (var renderNode in renderDescriptions) { IConditionTreeItem conditionCollection = ConditionTree.Empty; List <IRenderCommand> commands = new List <IRenderCommand>(); var conditionsAttribute = renderNode.Attribute("conditions"); if (conditionsAttribute != null) { IXmlLineInfo line = conditionsAttribute as IXmlLineInfo; try { conditionCollection = lc.ConditionParser.Parse(conditionsAttribute.Value, lc.ParseContext); } catch (ConditionFormatException ex) { lc.Errors.Add(new LoadError(lc.FileName, line.LineNumber, line.LinePosition + conditionsAttribute.Name.LocalName.Length + 2 + ex.PositionStart, LoadErrorCategory.Error, ex.Message)); continue; } } foreach (var renderCommandNode in renderNode.Descendants()) { string commandType = renderCommandNode.Name.LocalName; if (commandType == "line") { var command = new Line(); command.LoadFromXml(renderCommandNode, lc); commands.Add(command); } else if (commandType == "rect") { var command = new Rectangle(); command.LoadFromXml(renderCommandNode); commands.Add(command); } else if (commandType == "ellipse") { var command = new Ellipse(); command.LoadFromXml(renderCommandNode, lc); commands.Add(command); } else if (commandType == "text") { var command = new Text(); command.LoadFromXml(renderCommandNode, lc); commands.Add(command); } else if (commandType == "path") { var command = new RenderPath(); command.LoadFromXml(renderCommandNode, lc); commands.Add(command); } } parsedRenderDescriptions.Add(new RenderDescription(conditionCollection, commands.ToArray())); } description.RenderDescriptions = parsedRenderDescriptions.ToArray(); }
public ConnectionGroup(IConditionTreeItem conditions, ConnectionDescription[] connections) : base(connections, conditions) { }
public XmlRenderGroup(IConditionTreeItem conditions) : base(new List <IXmlRenderCommand>(), conditions) { }
private IEnumerable <Conditional <RenderText> > EnumerateRenderText(XElement commandElement, RenderText command, IConditionTreeItem baseConditions, IReadOnlyList <string> usedDefinitions) { // Only one text run can use a definition variable if (command.TextRuns.Count(r => r.Text.StartsWith("$") && availableDefinitions.Contains(r.Text.Substring(1))) > 1) { logger.LogError(commandElement, "Only one definition per text element is permitted"); yield break; } var replaceRun = command.TextRuns.FirstOrDefault(r => r.Text.StartsWith("$") && availableDefinitions.Contains(r.Text.Substring(1))); var replaceRunIndex = command.TextRuns.IndexOf(replaceRun); if (replaceRun == null) { // No definitions used yield return(new Conditional <RenderText>(command, baseConditions)); yield break; } if (!ReportUndeclared(commandElement.GetFileRange(), new HashSet <string>(new [] { replaceRun.Text.Substring(1) }).Except(usedDefinitions).ToHashSet())) { yield break; } foreach (var definitionValue in definitionsSection.Definitions[replaceRun.Text.Substring(1)]) { var conditions = new ConditionTree(ConditionTree.ConditionOperator.AND, baseConditions, definitionValue.Conditions); var replacementRun = new TextRun(definitionValue.Value, replaceRun.Formatting); var replacementCommand = (RenderText)command.Clone(); replacementCommand.TextRuns.RemoveAt(replaceRunIndex); replacementCommand.TextRuns.Insert(replaceRunIndex, replacementRun); yield return(new Conditional <RenderText>(replacementCommand, conditions)); } }
private IEnumerable <Conditional <ComponentPoint> > EnumerateComponentPoint(XAttribute location, IConditionTreeItem baseConditions, IReadOnlyList <string> usedDefinitions) { if (!componentPointTemplateParser.TryParse(location, out var templatePoint)) { yield break; } if (!templatePoint.Variables.Any()) { if (componentPointParser.TryParse(location, out var componentPoint)) { yield return(new Conditional <ComponentPoint>(componentPoint, baseConditions)); } yield break; } var undeclaredUsages = templatePoint.Variables.Except(usedDefinitions).ToHashSet(); if (!ReportUndeclared(location.GetFileRange(), undeclaredUsages)) { yield break; } foreach (var result in EnumerateComponentPoint(templatePoint, baseConditions)) { yield return(result); } }
private ComponentProperty ReadPropertyNode(ComponentDescription description, XElement propertyElement) { string propertyName = propertyElement.Attribute("name").Value; string type = propertyElement.Attribute("type").Value; string defaultValue = propertyElement.Attribute("default").Value; string serializeAs = propertyElement.Attribute("serialize").Value; string display = propertyElement.Attribute("display").Value; PropertyType propertyType; switch (type.ToLowerInvariant()) { case "double": // TODO: Add warning case "decimal": propertyType = PropertyType.Decimal; break; case "int": propertyType = PropertyType.Integer; break; case "bool": propertyType = PropertyType.Boolean; break; default: propertyType = PropertyType.String; break; } var propertyDefaultValue = PropertyValue.Parse(defaultValue, propertyType.ToPropertyType()); List <string> propertyOptions = null; if (type == "enum") { propertyOptions = new List <string>(); var optionNodes = propertyElement.Elements(propertyElement.GetDefaultNamespace() + "option"); foreach (var optionNode in optionNodes) { propertyOptions.Add(optionNode.Value); } } List <ComponentPropertyFormat> formatRules = new List <ComponentPropertyFormat>(); if (propertyElement.Attribute("format") != null) { formatRules.Add(new ComponentPropertyFormat(propertyElement.Attribute("format").Value, ConditionTree.Empty)); } else { var formatRuleNodes = propertyElement.Elements(propertyElement.GetDefaultNamespace() + "formatting") .SelectMany(x => x.Elements(propertyElement.GetDefaultNamespace() + "format")); foreach (var formatNode in formatRuleNodes) { IConditionTreeItem conditionCollection = ConditionTree.Empty; var conditionsAttribute = formatNode.Attribute("conditions"); if (conditionsAttribute != null) { conditionParser.Parse(conditionsAttribute, description, logger, out conditionCollection); } formatRules.Add(new ComponentPropertyFormat(formatNode.Attribute("value").Value, conditionCollection)); } } Dictionary <PropertyOtherConditionType, IConditionTreeItem> otherConditions = new Dictionary <PropertyOtherConditionType, IConditionTreeItem>(); var otherConditionsNodes = propertyElement.Elements(propertyElement.GetDefaultNamespace() + "other") .SelectMany(x => x.Elements(propertyElement.GetDefaultNamespace() + "conditions")); foreach (var otherConditionNode in otherConditionsNodes) { if (otherConditionNode?.Attribute("for") != null && otherConditionNode.Attribute("value") != null) { string conditionsFor = otherConditionNode.Attribute("for").Value; if (!conditionParser.Parse(otherConditionNode.Attribute("value"), description, logger, out var conditionCollection)) { continue; } if (Enum.IsDefined(typeof(PropertyOtherConditionType), conditionsFor)) { otherConditions.Add((PropertyOtherConditionType)Enum.Parse(typeof(PropertyOtherConditionType), conditionsFor, true), conditionCollection); } } } ComponentProperty property = new ComponentProperty(propertyName, serializeAs, display, propertyType, propertyDefaultValue, formatRules.ToArray(), otherConditions, (propertyOptions == null ? null : propertyOptions.ToArray())); return(property); }
private IEnumerable <Conditional <ComponentPoint> > EnumerateComponentPoint(XAttribute x, XAttribute y, IConditionTreeItem baseConditions, IReadOnlyList <string> usedDefinitions) { if (!componentPointTemplateParser.TryParse(x, y, out var templatePoint)) { yield break; } if (!templatePoint.Variables.Any()) { if (componentPointParser.TryParse(x, y, out var componentPoint)) { yield return(new Conditional <ComponentPoint>(componentPoint, baseConditions)); } yield break; } var undeclaredXUsages = templatePoint.XVariables.Except(usedDefinitions).ToHashSet(); if (!ReportUndeclared(x.GetFileRange(), undeclaredXUsages)) { yield break; } var undeclaredYUsages = templatePoint.YVariables.Except(usedDefinitions).ToHashSet(); if (!ReportUndeclared(y.GetFileRange(), undeclaredYUsages)) { yield break; } var variable = templatePoint.Variables.First(); foreach (var condition in definitionsSection.Definitions[variable]) { var cp = templatePoint.Construct(new Dictionary <string, double> { [variable] = double.Parse(condition.Value), }); var conditions = new ConditionTree(ConditionTree.ConditionOperator.AND, baseConditions, condition.Conditions); yield return(new Conditional <ComponentPoint>(cp, conditions)); } }
private IEnumerable <Conditional <ComponentPoint> > EnumerateComponentPoint(ComponentPointTemplate templatePoint, IConditionTreeItem baseConditions) { var variable = templatePoint.Variables.First(); foreach (var condition in definitionsSection.Definitions[variable]) { var cp = templatePoint.Construct(new Dictionary <string, double> { [variable] = double.Parse(condition.Value), }); var conditions = new ConditionTree(ConditionTree.ConditionOperator.AND, baseConditions, condition.Conditions); yield return(new Conditional <ComponentPoint>(cp, conditions)); } }
public RenderDescription(IConditionTreeItem conditions, IRenderCommand[] commands) : base(commands, conditions) { }
protected IEnumerable <RenderDescription> ReadRenderDescriptions(ComponentDescription description, XElement renderNode) { IConditionTreeItem conditionCollection = ConditionTree.Empty; var conditionsAttribute = renderNode.Attribute("conditions"); if (conditionsAttribute != null) { if (!conditionParser.Parse(conditionsAttribute, description, logger, out conditionCollection)) { yield break; } } var declaredDefinitions = new List <string>(); var whenDefinedAttribute = renderNode.Attribute("whenDefined"); if (whenDefinedAttribute != null) { var usedVariables = whenDefinedAttribute.Value.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); declaredDefinitions.AddRange(usedVariables.Select(x => x.Substring(1))); } var allUndefined = declaredDefinitions.Except(availableDefinitions).ToHashSet(); if (allUndefined.Any()) { foreach (var undefined in allUndefined) { logger.LogError(whenDefinedAttribute, $"Usage of undefined variable: ${undefined}"); } yield break; } var commands = new List <IRenderCommand>(); foreach (var renderCommandNode in renderNode.Descendants()) { string commandType = renderCommandNode.Name.LocalName; if (commandType == "line") { foreach (var renderLineDescription in ReadLineCommand(renderCommandNode, conditionCollection, declaredDefinitions)) { yield return(renderLineDescription); } } else if (commandType == "rect") { foreach (var renderRectDescription in ReadRectCommand(renderCommandNode, conditionCollection, declaredDefinitions)) { yield return(renderRectDescription); } } else if (commandType == "ellipse") { if (ReadEllipseCommand(description.Metadata.Version, renderCommandNode, out var command)) { commands.Add(command); } } else if (commandType == "text") { foreach (var renderTextDescription in ReadTextCommand(renderCommandNode, description, conditionCollection, declaredDefinitions)) { yield return(renderTextDescription); } } else if (commandType == "path") { if (ReadPathCommand(renderCommandNode, out var command)) { commands.Add(command); } } } yield return(new RenderDescription(conditionCollection, commands.ToArray())); }
public static bool Parse(this IConditionParser parser, XAttribute conditionsAttribute, ComponentDescription description, IXmlLoadLogger logger, out IConditionTreeItem value) { try { value = parser.Parse(description, conditionsAttribute.Value); return(true); } catch (ConditionFormatException ex) { IXmlLineInfo line = conditionsAttribute; int startCol = line.LinePosition + conditionsAttribute.Name.LocalName.Length + 2 + ex.PositionStart; var position = new FileRange(line.LineNumber, startCol, line.LineNumber, startCol + ex.Length); logger.Log(LogLevel.Error, position, ex.Message, null); value = null; return(false); } }
public XmlConnectionGroup(IConditionTreeItem conditions) : base(new List <XmlConnectionDescription>(), conditions) { }