ClassDiagram.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Linq;
  3. using System.Xml.Linq;
  4. using System.Collections.Generic;
  5. namespace csharp_sccd_compiler
  6. {
  7. public class ClassDiagram : Visitable
  8. {
  9. public string model_name { get; private set; }
  10. public string model_author { get; private set; }
  11. public string model_description { get; private set; }
  12. public List<string> class_names { get; private set; }
  13. public List<string> inports { get; private set; }
  14. public List<string> outports { get; private set; }
  15. public string top_section { get; private set; }
  16. public List<Class> classes { get; private set; }
  17. public Class default_class { get; private set; }
  18. public ClassDiagram (string input_file_path)
  19. {
  20. XElement root = XDocument.Load(input_file_path).Root;
  21. XAttribute name_attribute = root.Attribute("name");
  22. if (name_attribute != null && name_attribute.Value.Trim() != "")
  23. this.model_name = name_attribute.Value.Trim();
  24. XAttribute author_attribute = root.Attribute("author");
  25. if (author_attribute != null && author_attribute.Value.Trim() != "")
  26. this.model_author = author_attribute.Value.Trim();
  27. XElement description_element = root.Element("description");
  28. if (description_element != null && description_element.Value.Trim() != "")
  29. model_description = description_element.Value;
  30. this.class_names = new List<string>();
  31. foreach (XElement class_xml in root.Elements("class"))
  32. {
  33. XAttribute class_name_attribute = class_xml.Attribute("name");
  34. if (class_name_attribute == null)
  35. throw new CompilerException("Missing class name.");
  36. string class_name = class_name_attribute.Value.Trim();
  37. if (class_name == "")
  38. throw new CompilerException("Empty class name.");
  39. if (this.class_names.Contains(class_name))
  40. throw new CompilerException(string.Format("Found 2 classes with the same name '{0}'.", class_name));
  41. this.class_names.Add(class_name);
  42. }
  43. if (this.class_names.Count == 0)
  44. throw new CompilerException("Found no classes to compile.");
  45. this.inports = new List<string> ();
  46. foreach (XElement inport_xml in root.Elements("inport"))
  47. {
  48. XAttribute inport_name_attribute = inport_xml.Attribute("name");
  49. if (inport_name_attribute == null)
  50. throw new CompilerException("Missing inport name.");
  51. string inport_name = inport_name_attribute.Value.Trim();
  52. if (inport_name == "")
  53. throw new CompilerException("Empty inport name.");
  54. if (this.inports.Contains(inport_name))
  55. throw new CompilerException(string.Format("Found 2 inports with the same name '{0}'.", inport_name));
  56. this.inports.Add(inport_name);
  57. }
  58. this.outports = new List<string> ();
  59. foreach (XElement outport_xml in root.Elements("outport"))
  60. {
  61. XAttribute outport_name_attribute = outport_xml.Attribute("name");
  62. if (outport_name_attribute == null)
  63. throw new CompilerException("Missing outport name.");
  64. string outport_name = outport_name_attribute.Value.Trim();
  65. if (outport_name == "")
  66. throw new CompilerException("Empty outport name.");
  67. if (this.outports.Contains(outport_name))
  68. throw new CompilerException(string.Format("Found 2 outports with the same name '{0}'.", outport_name));
  69. this.outports.Add(outport_name);
  70. }
  71. List<XElement> top_elements = root.Elements("top").ToList();
  72. if (top_elements.Count == 1 && top_elements[0].Value.Trim() != "")
  73. this.top_section = top_elements[0].Value;
  74. else if (top_elements.Count > 1)
  75. throw new CompilerException("Class diagram can only have one <top> element.");
  76. this.classes = new List<Class>();
  77. List<Class> default_classes = new List<Class>();
  78. foreach (XElement class_xml in root.Elements("class"))
  79. {
  80. Class processed_class = null;
  81. try
  82. {
  83. processed_class = new Class(class_xml);
  84. }
  85. catch(CompilerException e)
  86. {
  87. throw new CompilerException(string.Format("Class <{0}> failed compilation.", class_xml.Attribute("name").Value), e);
  88. }
  89. Logger.displayInfo(string.Format("Class <{0}> has been successfully loaded.", processed_class.name));
  90. this.classes.Add(processed_class);
  91. if (processed_class.is_default)
  92. default_classes.Add(processed_class);
  93. }
  94. if (default_classes.Count != 1)
  95. {
  96. if (this.classes.Count == 1)
  97. {
  98. Logger.displayInfo(string.Format("Only one class given. Using <{0}> as the default class.", this.classes[0].name));
  99. this.default_class = this.classes[0];
  100. }
  101. else
  102. throw new CompilerException("Provide one and only one default class to instantiate on start up.");
  103. }
  104. else
  105. this.default_class = default_classes[0];
  106. }
  107. public override void accept(Visitor visitor)
  108. {
  109. visitor.visit (this);
  110. }
  111. }
  112. }