Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Factory.cs
1using System;
2using System.Collections.Generic;
3using System.Reflection;
4using System.Threading.Tasks;
5using System.Xml;
7
8namespace TAG.Simulator
9{
13 public static class Factory
14 {
15 private static readonly Dictionary<string, ISimulationNode> nodeTypes = new Dictionary<string, ISimulationNode>();
16 private static readonly Dictionary<string, KeyValuePair<string, Assembly>> schemas = new Dictionary<string, KeyValuePair<string, Assembly>>();
17 private static bool initialized = false;
18
22 public static void Initialize()
23 {
24 if (!initialized)
25 {
26 object[] Arguments = new object[] { null, null };
27
28 foreach (Type T in Types.GetTypesImplementingInterface(typeof(ISimulationNode)))
29 {
30 TypeInfo TI = T.GetTypeInfo();
31 if (TI.IsAbstract)
32 continue;
33
34 ISimulationNode Node = (ISimulationNode)Activator.CreateInstance(T, Arguments);
35 string Key = Node.Namespace + "#" + Node.LocalName;
36
37 if (nodeTypes.ContainsKey(Key))
38 throw new Exception("A class handling " + Key + " has already been registered.");
39 else
40 nodeTypes[Key] = Node;
41
42 string s = Node.Namespace;
43 if (!schemas.ContainsKey(s))
44 schemas[s] = new KeyValuePair<string, Assembly>(Node.SchemaResource, TI.Assembly);
45 }
46
47 initialized = true;
48 }
49 }
50
57 public static bool TryGetSchemaResource(string Namespace, out KeyValuePair<string, Assembly> Result)
58 {
59 return schemas.TryGetValue(Namespace, out Result);
60 }
61
69 public static async Task<ISimulationNode> Create(XmlElement Definition, ISimulationNode Parent, Model Model)
70 {
71 string Key;
72
73 if (!initialized)
74 throw new NotSupportedException("Factory not initialized.");
75
76 Key = Definition.NamespaceURI + "#" + Definition.LocalName;
77 if (!nodeTypes.TryGetValue(Key, out ISimulationNode Result))
78 throw new Exception("Unable to instantiate objects of type " + Definition.NamespaceURI + "#" + Definition.LocalName);
79
80 Result = Result.Create(Parent, Model);
81 await Result.FromXml(Definition);
82
83 return Result;
84 }
85 }
86}
Factory of simulation objects.
Definition: Factory.cs:14
static void Initialize()
Initializes the factory
Definition: Factory.cs:22
static async Task< ISimulationNode > Create(XmlElement Definition, ISimulationNode Parent, Model Model)
Creates a simulation objected, based on its XML definition.
Definition: Factory.cs:69
static bool TryGetSchemaResource(string Namespace, out KeyValuePair< string, Assembly > Result)
Tries to get the embedded resource name of the schema defining a namespace, and the corresponding ass...
Definition: Factory.cs:57
Root node of a simulation model
Definition: Model.cs:49
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:14
static Type[] GetTypesImplementingInterface(string InterfaceFullName)
Gets all types implementing a given interface.
Definition: Types.cs:84
Basic interface for simulator nodes. Implementing this interface allows classes with default contruct...
string Namespace
XML Namespace where the element is defined.
string LocalName
Local name of XML element defining contents of class.
string SchemaResource
Points to the embedded XML Schema resource defining the semantics of the XML namespace.