/// <summary> /// Deserialize given XML list of evolution rules. /// </summary> /// <param name="evolutionRules">XML list of evolution rules.</param> /// <exception cref="MissingXmlAttribute">If some attribute of floating object is missing.</exception> /// <exception cref="MissingXmlElement">If some element of floating object is missing.</exception> private void DeserializeEvolutionRules(List <XElement> evolutionRules) { EvolutionRules = new List <EvolutionRule>(); foreach (XElement evolutionRule in evolutionRules) { string type = Xmlizer.GetAttributeValueWithException(evolutionRule, "evoRule", "type"); int priority = Convert.ToInt32(evolutionRule.Attribute("priority")?.Value); int delay = Convert.ToInt32(evolutionRule.Attribute("delay")?.Value); XElement leftSideObject = evolutionRule.Element("leftside"); if (leftSideObject == null) { throw new MissingXmlElement("Element leftside of evoRule is missing"); } char[] delimiter = { ',' }; var leftSideObjectNames = Xmlizer.GetAttributeValueWithException(leftSideObject, "leftside", "value") .Split(delimiter, StringSplitOptions.RemoveEmptyEntries); XElement rightSideObject = evolutionRule.Element("rightside"); if (rightSideObject == null) { throw new MissingXmlElement("Element rightside of evoRule is missing"); } var rightSideObjectNames = Xmlizer.GetAttributeValueWithException(rightSideObject, "rightside", "value") .Split(delimiter, StringSplitOptions.RemoveEmptyEntries); var leftSideObjects = leftSideObjectNames.Select(GetSimulationObject).ToList(); var rightSideObjects = rightSideObjectNames.Select(GetSimulationObject).ToList(); EvolutionRules.Add(EvolutionRule.NewRule(type, priority, leftSideObjects, rightSideObjects, delay)); } }
/// <summary> /// Deserialize given XML list of glues. /// </summary> /// <param name="glues">XML element list of glues.</param> /// <returns>Dictionary of glues.</returns> /// <exception cref="MissingXmlAttribute">If some attribute of glue is missing.</exception> /// <exception cref="MissingXmlElement">If some element of glue is missing.</exception> private void DeserializeGlues(List <XElement> glues) { foreach (XElement glue in glues) { string glueName = Xmlizer.GetAttributeValueWithException(glue, "glue", "name"); Glues[glueName] = new Glue(glueName); } }
/// <summary> /// Deserialize given XML list of proteins. /// </summary> /// <param name="proteins">XML element list of proteins.</param> /// <exception cref="MissingXmlAttribute">If some attribute of protein is missing.</exception> /// <exception cref="MissingXmlElement">If some element of protein is missing.</exception> private void DeserializeProteins(List <XElement> proteins) { foreach (XElement protein in proteins) { string name = Xmlizer.GetAttributeValueWithException(protein, "protein", "name"); AmbiguityTest(name); Proteins[name] = new Protein(name); } }
/// <summary> /// Deserialize given XML list of tiles. /// </summary> /// <param name="tiles">XML element list of tiles.</param> /// <exception cref="MissingXmlAttribute">If some attribute of tile is missing.</exception> /// <exception cref="MissingXmlElement">If some element of tile is missing.</exception> private void DeserializeTiles(List <XElement> tiles) { foreach (XElement tile in tiles) { string name = Xmlizer.GetAttributeValueWithException(tile, "tile", "name"); AmbiguityTest(name); Tiles[name] = DeserializeTile(name, tile); } }
/// <summary> /// Deserialize given XML polygon node of fixed objecs. /// </summary> /// <param name="polygonElement">XML polygon element.</param> /// <returns>Tile vertices.</returns> /// <exception cref="MissingXmlAttribute">If some attribute of polygon is missing.</exception> /// <exception cref="MissingXmlElement">If some element of polygon is missing.</exception> private NamedVertices DeserializePolygon(XElement polygonElement) { XElement sidesElement = polygonElement.Element("sides"); int sides = Convert.ToInt32(Xmlizer.GetAttributeValueWithException(sidesElement, "sides", "value")); XElement radiusElement = polygonElement.Element("radius"); double radius = Convert.ToDouble(Xmlizer.GetAttributeValueWithException(radiusElement, "radius", "value"), CultureInfo.InvariantCulture); return(new NamedVertices(sides, radius)); }
/// <summary> /// Deserialize given XML list of named positions. /// </summary> /// <param name="positions">XML element list of positions.</param> /// <returns>List of deserialized positions.</returns> /// <exception cref="MissingXmlAttribute">If some attribute of position is missing.</exception> /// <exception cref="MissingXmlElement">If some element of position is missing.</exception> private List <NamedPosition> DeserializeNamedPositions(List <XElement> positions) { List <NamedPosition> namedPositions = new List <NamedPosition>(); foreach (XElement position in positions) { string name = Xmlizer.GetAttributeValueWithException(position, "position", "name"); namedPositions.Add(new NamedPosition(name, DeserializePosition(position))); } return(namedPositions); }
/// Returns value of a deserialized optional XML element of type double /// </summary> /// <param name="parentElement">parent element in which the "name" is searched for</param> /// <param name="name">Name of the element.</param> /// <param name="defaultValue">return value if the element does not exist</param> /// <exception cref="InvalidOperationException">The name is already in use.</exception> private bool GetBool(XElement parentElement, string name, bool defaultValue) { XElement element = parentElement.Element(name); if (element == null) { return(defaultValue); } return(Convert.ToBoolean(Xmlizer.GetAttributeValueWithException(element, name, "value"), CultureInfo.InvariantCulture)); }
/// <summary> /// Returns value of a deserialized optional XML element of type double /// </summary> /// <param name="parentElement">parent element in which the "name" is searched for</param> /// <param name="name">Name of the element.</param> /// <param name="defaultValue">return value if the element does not exist</param> /// <exception cref="InvalidOperationException">The name is already in use.</exception> private double GetDouble(XElement parentElement, string name, double defaultValue) { XElement element = parentElement.Element(name); if (element == null) { return(defaultValue); } return(Convert.ToDouble(Xmlizer.GetAttributeValueWithException(element, name, "value"), CultureInfo.InvariantCulture)); }
/// <summary> /// Deserialize given XML list of floating objects. /// </summary> /// <param name="floatingObjects">XML element list of floating objects.</param> /// <exception cref="MissingXmlAttribute">If some attribute of floating object is missing.</exception> /// <exception cref="MissingXmlElement">If some element of floating object is missing.</exception> private void DeserializeFloatingObjects(List <XElement> floatingObjects) { foreach (XElement floatingObject in floatingObjects) { string name = Xmlizer.GetAttributeValueWithException(floatingObject, "floatingObject", "name"); AmbiguityTest(name); XElement mobilityElement = floatingObject.Element("mobility"); double mobility = Convert.ToDouble(Xmlizer.GetAttributeValueWithException(mobilityElement, "mobility", "value"), CultureInfo.InvariantCulture); double concentration = GetDouble(floatingObject, "concentration", 0); FloatingObjects[name] = new FloatingObject(name, mobility, concentration); } }
/// <summary> /// Returns angle for given parent element /// </summary> /// <param name="angle">Angle element</param> /// <param name="defaultAngle">Default value if the XML node is missing.</param> /// <returns></returns> private Angle GetAngle(XElement angle, Angle defaultAngle) { if (angle == null) { return(defaultAngle); } double angleValue = Convert.ToDouble(Xmlizer.GetAttributeValueWithException(angle, angle.Name.ToString(), "value"), CultureInfo.InvariantCulture); if (angle.Attribute("unit")?.Value == "rad") { return(Angle.FromRadians(angleValue)); } return(Angle.FromDegrees(angleValue)); }
/// <summary> /// Deserialize given XML list of initial objects. /// </summary> /// <param name="seedTiles">XML element list of tiles.</param> /// <exception cref="MissingXmlAttribute">If some attribute of seedTile is missing.</exception> /// <exception cref="MissingXmlElement">If some element of seedTile is missing.</exception> private void DeserializeSeedTiles(List <XElement> seedTiles) { SeedTiles = new List <TileInSpace>(); foreach (XElement seedTile in seedTiles) { string name = Xmlizer.GetAttributeValueWithException(seedTile, "initialObject", "name"); if (!Tiles.ContainsKey(name)) { throw new InvalidOperationException($"Seed tile named '{name}' is not defined in input xml file."); } List <Angle> angles = DeserializeAngles(seedTile); Point3D point = DeserializePosition(seedTile); SeedTiles.Add(new TileInSpace(Tiles[name], point, new EulerAngles(angles[0], angles[1], angles[2]).ToQuaternion())); } }
/// <summary> /// Deserialize given XML list of proteins. /// </summary> /// <param name="tiles">XML element list of tiles wit proteins.</param> /// <exception cref="MissingXmlAttribute">If some attribute of protein is missing.</exception> /// <exception cref="MissingXmlElement">If some element of protein is missing.</exception> private void DeserializeProteinsOnTiles(List <XElement> tiles) { foreach (XElement tile in tiles) { string tileName = Xmlizer.GetAttributeValueWithException(tile, "tile", "name"); List <ProteinOnTile> proteins = new List <ProteinOnTile>(); foreach (XElement protein in tile.Elements("protein")) { string name = Xmlizer.GetAttributeValueWithException(protein, "protein", "name"); Point3D position = DeserializePosition(protein); proteins.Add(new ProteinOnTile(name, position)); } v_ProteinsOnTile[tileName] = proteins; } }
/// <summary> /// Deserialize color of a P system's object. /// </summary> /// <param name="colorElement">XML element of the color.</param> /// <param name="color">If the color node is null, the Black color is returned.</param> /// <param name="alpha">If no 'alpha' attribute is specified, color is fully opaque.</param> /// <exception cref="MissingXmlAttribute">If the color name is missing.</exception> /// <exception cref="InvalidOperationException">If the color name is not valid.</exception> private void DeserializeColor(XElement colorElement, out Color color, out int alpha) { color = Color.Black; alpha = 255; if (colorElement != null) { string colorName = Xmlizer.GetAttributeValueWithException(colorElement, "color", "name"); color = Color.FromName(colorName); if (!color.IsNamedColor) { throw new InvalidOperationException($"'{colorName}' is not a valid color name."); } XAttribute alphaAttribute = colorElement.Attribute("alpha"); if (alphaAttribute != null) { alpha = Convert.ToInt32(alphaAttribute.Value); } } }
/// <summary> /// Deserialize given XML list of signal objects. /// </summary> /// <param name="glueTuples">XML element list of glue tuples.</param> /// <exception cref="MissingXmlAttribute">If some attribute of floating object is missing.</exception> /// <exception cref="MissingXmlElement">If some element of floating object is missing.</exception> private void DeserializeSignalObjects(List <XElement> glueTuples) { foreach (XElement glueTuple in glueTuples) { string nameOfTheGlue1 = Xmlizer.GetAttributeValueWithException(glueTuple, "glueTuple", "glue1"); string nameOfTheGlue2 = Xmlizer.GetAttributeValueWithException(glueTuple, "glueTuple", "glue2"); NamedMultiset signalObjects = new NamedMultiset(); XElement objectElement = glueTuple.Element("objects"); List <string> floatingObjectNames = Xmlizer.GetAttributeValueWithException(objectElement, "objects", "value").Split(',').ToList(); foreach (string floatingObjectName in floatingObjectNames) { if (!FloatingObjects.ContainsKey(floatingObjectName)) { throw new InvalidOperationException(string.Format("Floating object named '{0}' is not defined in input xml file.", floatingObjectName)); } signalObjects.Add(floatingObjectName); } //TODO check if it find correct value GluePRelation[new Tuple <Glue, Glue>(Glues[nameOfTheGlue1], Glues[nameOfTheGlue2])] = signalObjects; } }
/// <summary> /// Deserialize given XML list of vertices of a tile. /// </summary> /// <param name="verticesElement">XML element list of vertices.</param> /// <returns>List of deserialized vertices.</returns> /// <exception cref="MissingXmlAttribute">If some attribute of protein is missing.</exception> /// <exception cref="MissingXmlElement">If some element of protein is missing.</exception> private NamedVertices DeserializeVertices(XElement verticesElement) { List <XElement> vertexElements = verticesElement.Elements("vertex").ToList(); if (vertexElements.Count == 0) { throw new MissingXmlElement("Element vertex of vertices of tile is missing or empty."); } List <NamedPosition> vertices = new List <NamedPosition>(); foreach (XElement vertexElement in vertexElements) { string name = Xmlizer.GetAttributeValueWithException(vertexElement, "vertex", "name"); Point3D position = DeserializePosition(vertexElement); if (position.Z != 0) { throw new InvalidOperationException("3D tiles with 'Z' coordinate not supported in this version."); } NamedPosition vertex = new NamedPosition(name, position); vertices.Add(vertex); } return(new NamedVertices(vertices)); }
/// <summary> /// Deserialize given XML list of glue realations. /// </summary> /// <param name="glueRelations">XML element list of glue realations.</param> /// <exception cref="MissingXmlAttribute">If some attribute of floating object is missing.</exception> /// <exception cref="MissingXmlElement">If some element of floating object is missing.</exception> private void DeserializeGlueRelations(List <XElement> glueRelations) { GluePRelation = new GlueRelation(); foreach (XElement glueRelation in glueRelations) { string nameOfTheGlue1 = Xmlizer.GetAttributeValueWithException(glueRelation, "glueTuple", "glue1"); string nameOfTheGlue2 = Xmlizer.GetAttributeValueWithException(glueRelation, "glueTuple", "glue2"); if (!Glues.ContainsKey(nameOfTheGlue1)) { throw new InvalidOperationException(string.Format("Glue named '{0}' is not defined in input xml file.", nameOfTheGlue1)); } if (!Glues.ContainsKey(nameOfTheGlue2)) { throw new InvalidOperationException(string.Format("Glue named '{0}' is not defined in input xml file.", nameOfTheGlue2)); } Glue glue1 = Glues[nameOfTheGlue1]; Glue glue2 = Glues[nameOfTheGlue2]; GluePRelation[Tuple.Create(glue1, glue2)] = new NamedMultiset(); } }
/// <summary> /// Deserialize tile from a given XML node. /// </summary> /// <param name="name">Tile name .</param> /// <param name="tileElement">XML element of tile.</param> /// <exception cref="MissingXmlAttribute">If some attribute of a tile is missing.</exception> /// <exception cref="MissingXmlElement">If some element of tile is missing.</exception> /// <returns>Deserialized tile.</returns> private Tile DeserializeTile(string name, XElement tileElement) { NamedVertices vertices; XElement verticesElement = tileElement.Element("vertices"); if (verticesElement != null) { vertices = DeserializeVertices(verticesElement); } else { XElement polygonElement = tileElement.Element("polygon"); if (polygonElement == null) { throw new MissingXmlElement("Element 'vertices' or 'polygon' are missing. One of them is required."); } vertices = DeserializePolygon(polygonElement); } XElement positionsElement = tileElement.Element("positions"); List <NamedPosition> namedPositions = new List <NamedPosition>(); if (positionsElement != null) { List <XElement> positionElements = positionsElement.Elements("position").ToList(); if (positionElements.Any()) { namedPositions = DeserializeNamedPositions(positionElements); } } Angle defaultAngle = GetAngle(tileElement.Element("connectingAngle"), default(Angle)); List <Connector> connectors = new List <Connector>(); List <XElement> connectorElements = tileElement.GetElements("connectors/connector"); foreach (XElement connectorElement in connectorElements) { string connectorName = Xmlizer.GetAttributeValueWithException(connectorElement, "connector", "name"); List <XElement> connectorsPositionElements = connectorElement.GetElements("positions/position"); List <Point3D> positions = new List <Point3D>(); foreach (XElement connectorsPositionElement in connectorsPositionElements) { string positionName = Xmlizer.GetAttributeValueWithException(connectorsPositionElement, "position", "name"); NamedPosition vertex = vertices.FirstOrDefault(x => x.Name == positionName); if (vertex.Name != null) { positions.Add(vertex.Position); } else { NamedPosition position = namedPositions.Find(x => x.Name == positionName); if (position.Name != null) { positions.Add(position.Position); } else { throw new MissingXmlElement( $"Vertex/Position '{positionName}' is not defined in input xml file for connector '{connectorName}'."); } } } XElement glueElement = connectorElement.Element("glue"); string glueName = Xmlizer.GetAttributeValueWithException(glueElement, "glue", "name"); if (!Glues.ContainsKey(glueName)) { throw new InvalidOperationException($"Glue named '{glueName}' is not defined in input xml file."); } Glue glue = Glues[glueName]; Angle angle = GetAngle(connectorElement.Element("angle"), defaultAngle); double resistance = GetDouble(connectorElement, "resistance", 0); Connector connector = new Connector(connectorName, positions, glue, angle, resistance); connectors.Add(connector); } XElement surfaceGlueElement = tileElement.Element("surfaceGlue"); string surfaceGlueName = Xmlizer.GetAttributeValueWithException(surfaceGlueElement, "surfaceGlue", "name"); if (!Glues.ContainsKey(surfaceGlueName)) { throw new InvalidOperationException($"Surface glue named '{surfaceGlueName}' is not defined in input xml file."); } Color color; int alpha; DeserializeColor(tileElement.Element("color"), out color, out alpha); List <ProteinOnTile> proteinsOnTile = null; if (v_ProteinsOnTile.ContainsKey(name)) { proteinsOnTile = v_ProteinsOnTile[name]; } double alphaRatio = GetDouble(tileElement, "alphaRatio", 1); if (alphaRatio <= 0) { throw new InvalidOperationException($"Alpha resistance ratio of the tile '{name}' must be positive."); } return(new Tile(name, vertices.Select(vertex => vertex.Position).ToList(), connectors, Glues[surfaceGlueName], proteinsOnTile, color, alpha, alphaRatio)); }