private static ExternalConnection ParseExternalConnection(string line) { if (string.IsNullOrEmpty(line) || !line.EndsWith("*)")) { return(null); } string name = string.Empty; string dataType = string.Empty; ExternalConnection variable = null; int nameStart; int typeStart; int typeEnd; if (line.StartsWith(sInvalidGlobVarPrefix)) { //非法 nameStart = sInvalidGlobVarPrefix.Length; typeStart = line.IndexOf(':', nameStart) + 1; if (typeStart <= nameStart) { return(null); } name = line.Substring(nameStart, typeStart - nameStart - 1); typeEnd = line.IndexOf(';', typeStart); if (typeEnd <= typeStart) { return(null); } dataType = line.Substring(typeStart, typeEnd - typeStart); variable = new ExternalConnection(name, ViDataType.GetDataType(dataType)); variable.ValidState = false; } else { // 合法 nameStart = 0; typeStart = line.IndexOf(':') + 1; if (typeStart <= nameStart) { return(null); } name = line.Substring(nameStart, typeStart - nameStart - 1); typeEnd = line.IndexOf(';', typeStart); if (typeEnd <= typeStart) { return(null); } dataType = line.Substring(typeStart, typeEnd - typeStart); variable = new ExternalConnection(name, ViDataType.GetDataType(dataType)); variable.ValidState = true; } return(variable); }
/// <summary> /// 将文本行描述的变量定义,解析为管脚类型对象。 /// </summary> /// <param name="decl">管脚的文本行描述</param> /// <param name="typeCreation">自动创建管脚数据类型的方式</param> /// <param name="creator">ViConnType 的创建函数</param> /// <returns>管脚类型对象,null 表示解析失败</returns> public static ViConnType Parse(string decl, ViTypeCreation typeCreation, Func <string, ViDataType, ViConnType> creator) { string name = null, type = null, comment = null; string[] addiInfo = null; if (!SeperateNameType(decl.Trim(), ref name, ref type, ref addiInfo, ref comment)) { return(null); } ViDataType dataType = null; if (!string.IsNullOrEmpty(type)) { dataType = ViDataType.GetDataType(type, typeCreation); if (dataType == null) { return(null); } } ViConnType connType = creator(name, dataType); if (connType == null) { return(null); } // 备注 connType.Comment = comment; if (addiInfo != null) { // 特性 foreach (string info in addiInfo) { if (info.Equals("F_EDGE", StringComparison.OrdinalIgnoreCase)) { connType.Attributes |= ViConnAttributes.F_EDGE; } else if (info.Equals("R_EDGE", StringComparison.OrdinalIgnoreCase)) { connType.Attributes |= ViConnAttributes.R_EDGE; } } // 缺省值 if (addiInfo.Count() >= 2 && addiInfo[addiInfo.Count() - 2] == ":=") { connType.DefaultValue = addiInfo[addiInfo.Count() - 1]; } } return(connType); }
/// <summary> /// 创建一个变量。变量名称和UUID会自动生成。 /// </summary> /// <param name="dataType">变量类型</param> /// <returns>创建的变量。</returns> /// @infor 创建的变量已经被加入到 Children 数组中。 public ExternalConnection CreateVariable(ViDataType dataType) { // 生成变量名称 string name = this.NextVariableName(); ExternalConnection variable = new ExternalConnection(name, dataType); // 生成变量 UUID variable.UUID = ViGuid.NewGuid(); // 将变量加入 Children 数组 this.AddChild(variable); // 大功告成 return(variable); }
/// <summary> /// 得到指定名称的数据类型对象。如果该名称的数据类型不存在,则会按照指定的创建方式进行创建。 /// </summary> /// <param name="name">类型名称</param> /// <param name="creation">自动创建数据类型的方式</param> /// <returns>数据类型对象</returns> public static ViDataType GetDataType(string name, ViTypeCreation creation) { if (string.IsNullOrEmpty(name)) { return(null); } // 确保数据被加载了 ViObservableCollection <ViDataType> allDataTypes = AllDataTypes; // 便于使用,去掉括号后面的注释信息 int pos = name.IndexOf('('); if (pos > 0) { name = name.Substring(0, pos); } // 关键字都是大写 string key = name.ToUpper().Trim(); // 从字典中取得指定名称的对象 if (mapAllDTypes.ContainsKey(key)) { return(mapAllDTypes[key]); } // 自动创建? if (creation != ViTypeCreation.None && ViIECStandard.IsValidName(name)) { ViDataType dataType = new ViDataType(name, string.Empty); dataType.AutoCreated = true; if (creation == ViTypeCreation.CreateGlobal) { allDataTypes.Add(dataType); mapAllDTypes[key] = dataType; } // return(dataType); } // 没有找到 return(null); }
public ExternalConnection(string name, ViDataType dataType) : base(name, dataType) { this.UUID = Guid.NewGuid().ToString(); }
public bool DeserializeFrom(ViBlur blur, System.Xml.XmlReader reader) { this.Name = reader.GetAttribute(blur, "name"); string category = reader.GetAttribute("category"); if (!string.IsNullOrEmpty(category)) { this.Category = category; } string mode = reader.GetAttribute("mode"); if (!string.IsNullOrEmpty(mode)) { this.ModesDecl = mode; } if (reader.IsEmptyElement) { return(true); } int stage = 0x00; // 0x00: 未开始 // 0x01: 开始 INPUT // 0x02: 开始 OUTPUT // 0x03: 开始 IN_OUT while (!reader.EOF) { if (!reader.Read()) { break; } if (reader.NodeType == System.Xml.XmlNodeType.Whitespace) { continue; } if (reader.NodeType == System.Xml.XmlNodeType.Element) { if (string.Compare(reader.Name, "dataType", true) == 0) { string typeName = reader.GetAttribute("typeName"); string typeShortName = reader.GetAttribute("typeShortName"); ViDataType dataType = new ViDataType(typeName, typeShortName); this.Type = dataType; } else if (string.Compare(reader.Name, "inputVars", true) == 0) { this.InputConnectors.Clear(); stage = 0x01; } else if (string.Compare(reader.Name, "outputVars", true) == 0) { this.OutputConnectors.Clear(); stage = 0x02; } else if (string.Compare(reader.Name, "inOutVars", true) == 0) { string count = reader.GetAttribute("count"); if (!string.IsNullOrEmpty(count)) { this.InOutConnectorCount = int.Parse(count); } } else if (string.Compare(reader.Name, ViConnType.TAG, true) == 0) { ViConnType connType = new ViConnType(null, null); if (!connType.DeserializeFrom(blur, reader)) { return(false); } if (stage == 0x01) { this.InputConnectors.Add(connType); } else if (stage == 0x02) { this.OutputConnectors.Add(connType); } } } else if (reader.NodeType == System.Xml.XmlNodeType.EndElement) { if (string.Compare(reader.Name, ViFirmBlockType.TAG, true) == 0) { return(true); } } } return(false); }
/// <summary> /// 构建对象。 /// </summary> /// <param name="name">功能块名称</param> /// <param name="dataType">功能块返回的类型,为 null 表示是 FUNCTION BLOCK。</param> public ViFirmBlockType(string name, ViDataType dataType) : base(name, dataType) { this.InputConnectors = new ViObservableCollection <ViConnType>(); this.OutputConnectors = new ViObservableCollection <ViConnType>(); }
public bool DeserializeFrom(ViBlur blur, System.Xml.XmlReader reader) { string name = reader.GetAttribute(blur, "name"); string connectable = reader.GetAttribute("connectable"); string fast = reader.GetAttribute("fast"); string attributes = reader.GetAttribute("attributes"); string defaultValue = reader.GetAttribute("defaultValue"); string connected = reader.GetAttribute("connected"); //Name if (!string.IsNullOrEmpty(name)) { this.Name = name; } //Connectable if (!string.IsNullOrEmpty(connectable)) { this.Connectable = bool.Parse(connectable); } //Fast if (!string.IsNullOrEmpty(fast)) { this.Fast = bool.Parse(fast); } //Attributes if (!string.IsNullOrEmpty(attributes)) { if (string.Compare(attributes, ViConnAttributes.F_EDGE.ToString(), true) == 0) { this.Attributes = ViConnAttributes.F_EDGE; } else if (string.Compare(attributes, ViConnAttributes.R_EDGE.ToString(), true) == 0) { this.Attributes = ViConnAttributes.R_EDGE; } else { this.Attributes = ViConnAttributes.None; } } if (!string.IsNullOrEmpty(defaultValue)) { this.DefaultValue = defaultValue; } while (!reader.EOF) { if (!reader.Read()) { break; } if (reader.NodeType == System.Xml.XmlNodeType.Whitespace) { continue; } if (reader.NodeType == System.Xml.XmlNodeType.Element) { if (string.Compare(reader.Name, "dataType", true) == 0) { string typeName = reader.GetAttribute("typeName"); //读取Value if (!reader.IsEmptyElement && string.IsNullOrEmpty(typeName)) { if (!reader.Read()) { break; } typeName = reader.Value; } ViDataType dataType = ViDataType.GetDataType(typeName); this.Type = dataType; } } else if (reader.NodeType == System.Xml.XmlNodeType.EndElement) { if (string.Compare(reader.Name, ViConnType.TAG, true) == 0) { return(true); } } } return(false); }
/// <summary> /// 构建对象。 /// </summary> /// <param name="name">管脚名称</param> /// <param name="dataType">管脚数据类型</param> public ViConnType(string name, ViDataType dataType) : base(name, dataType) { }
/// <summary> /// 是否是有效的初始值? /// </summary> /// <param name="dataType">数据类型</param> /// <param name="value">初始值</param> /// <returns>是否是有效的初始值?是则返回 true</returns> public static bool IsValidValue(ViDataType dataType, string value) { return(IsValidValue(dataType.Name, value)); }
public bool AreTypesCompatible(ViDataType type1, ViDataType type2) { return(this.AreTypesCompatible(type1.Name, type2.Name)); }
/// <summary> /// 构建对象。 /// </summary> /// <param name="name">管脚名称</param> /// <param name="dataType">管脚数据类型</param> public ViGlobalVariable(string name, ViDataType dataType) : base(name, dataType) { }
/// <summary> /// 构建对象。 /// </summary> /// <param name="name">功能块名称</param> /// <param name="dataType">功能块返回的类型,为 null 表示是 FUNCTION BLOCK。</param> public ViPOEBlockType(string name, ViDataType dataType) : base(name, dataType) { }