PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/FlowEditor/HAB/Brain/BrainHelper.cs

https://gitlab.com/Tiger66639/neural_network_designer
C# | 259 lines | 150 code | 17 blank | 92 comment | 24 complexity | d252258adf295e0b7ddbad6f705fbfc3 MD5 | raw file
  1. using JaStDev.HAB.Brain.Thread_syncing;
  2. using JaStDev.HAB.Sins;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Xml.Serialization;
  7. namespace JaStDev.HAB.Brain
  8. {
  9. /// <summary>
  10. /// Provides some general routines that work on the <see cref="Brain"/>.
  11. /// </summary>
  12. public class BrainHelper
  13. {
  14. /// <summary>
  15. /// Creates a NeuronCluster and assigns it the specified value as attached value through a link with
  16. /// the specified meaning. The cluster doesn't have a meaning.
  17. /// </summary>
  18. /// <param name="synsetid">The value to assign to the cluster.</param>
  19. /// <returns>A cluster with a link to an intneuron, both registered.</returns>
  20. static public NeuronCluster CreateClusterWithAttachedVal(int val, ulong meaning)
  21. {
  22. NeuronCluster iRes = new NeuronCluster();
  23. Brain.Current.Add(iRes);
  24. IntNeuron iId = new IntNeuron();
  25. iId.Value = val;
  26. Brain.Current.Add(iId);
  27. Link iLink = new Link(iId, iRes, meaning);
  28. return iRes;
  29. }
  30. public static NeuronCluster CreateFlow()
  31. {
  32. NeuronCluster iCluster = new NeuronCluster();
  33. iCluster.Meaning = (ulong)PredefinedNeurons.Flow;
  34. Brain.Current.Add(iCluster);
  35. return iCluster;
  36. }
  37. /// <summary>
  38. /// Creates a new frame and it's evokers cluster and returns both.
  39. /// </summary>
  40. /// <param name="evokers">The evokers.</param>
  41. /// <returns></returns>
  42. public static NeuronCluster CreateFrame(out NeuronCluster evokers)
  43. {
  44. NeuronCluster iRes = new NeuronCluster();
  45. iRes.Meaning = (ulong)PredefinedNeurons.Frame;
  46. evokers = new NeuronCluster();
  47. evokers.Meaning = (ulong)PredefinedNeurons.FrameEvokers;
  48. Brain.Current.Add(iRes);
  49. Brain.Current.Add(evokers);
  50. Link iNew = new Link(evokers, iRes, (ulong)PredefinedNeurons.FrameEvokers);
  51. return iRes;
  52. }
  53. /// <summary>
  54. /// Creates a neuron cluster in the form of an object containing a textneuron and a 'meaning' neuron.
  55. /// </summary>
  56. /// <remarks>
  57. /// Will always convert the string value to lowercase.
  58. /// </remarks>
  59. /// <param name="value">The value for the textneuron. If there is already a textneuron registered with the <see cref="TextSin"/>
  60. /// for the same value (case insesitive), this neuron is reused.</param>
  61. /// <param name="includeMeaning">if set to <c>true</c> a neuron that can be used as the meaning for a link or cluster is also created.</param>
  62. /// <returns>The neuronCluster that was newly created.</returns>
  63. public static NeuronCluster CreateObject(string value, out Neuron meaning)
  64. {
  65. value = value.ToLower();
  66. NeuronCluster iCluster = new NeuronCluster();
  67. iCluster.Meaning = (ulong)PredefinedNeurons.Object;
  68. Brain.Current.Add(iCluster);
  69. meaning = new Neuron();
  70. Brain.Current.Add(meaning);
  71. using (ChildrenAccessor iList = iCluster.ChildrenW)
  72. iList.Add(meaning);
  73. ulong iTextId;
  74. TextNeuron iText;
  75. if (TextSin.Words.TryGetValue(value, out iTextId) == true) //could be that this word already exists, if so, we reuse it, cause an item can only be once in the dict.
  76. iText = Brain.Current[iTextId] as TextNeuron;
  77. else
  78. {
  79. iText = new TextNeuron();
  80. iText.Text = value;
  81. Brain.Current.Add(iText);
  82. TextSin.Words[value] = iText.ID;
  83. }
  84. using (ChildrenAccessor iList = iCluster.ChildrenW)
  85. iList.Add(iText);
  86. return iCluster;
  87. }
  88. /// <summary>
  89. /// Creates a new cluster with meaning 'same' and adds a new text neuron to it.
  90. /// </summary>
  91. /// <remarks>
  92. /// Doesn't directly add to textsin dict, but is done indirectly.
  93. /// </remarks>
  94. /// <param name="text">The text that should be assigned to the text neuron that is created.</param>
  95. /// <param name="synsetid">The synsetid of the sense. This is also stored in the object so that we can find it again. It also
  96. /// tags it as comming from wordnet.</param>
  97. /// <returns></returns>
  98. static public NeuronCluster CreateObject(string text, ulong meaningID, int attachedInt)
  99. {
  100. TextNeuron iText = new TextNeuron();
  101. iText.Text = text;
  102. Brain.Current.Add(iText);
  103. return CreateObject(iText, meaningID, attachedInt);
  104. }
  105. /// <summary>
  106. /// Creates a new cluster with meaning 'Object' and adds the textneuron to it. It also attached the specified int value with
  107. /// a link using the specified meaning.
  108. /// </summary>
  109. /// <param name="text">The neuron to add to the cluster.</param>
  110. /// <param name="synsetid">The value to attach. </param>
  111. /// <returns>A neuron cluster with a child an a link to an int neuron, all registered.</returns>
  112. /// <remarks>
  113. /// Also makes certain that the textneuron is stored in the dictionary of the textsin so that it can be used.
  114. /// </remarks>
  115. static public NeuronCluster CreateObject(TextNeuron text, ulong meaningID, int attachedInt)
  116. {
  117. NeuronCluster iRes = CreateClusterWithAttachedVal(attachedInt, meaningID);
  118. iRes.Meaning = (ulong)PredefinedNeurons.Object;
  119. using (ChildrenAccessor iList = iRes.ChildrenW)
  120. iList.Add(text);
  121. TextSin.Words[text.Text.ToLower()] = text.ID; //always store the data in lowercase.
  122. return iRes;
  123. }
  124. /// <summary>
  125. /// Deletes the object and all the children of the object.
  126. /// </summary>
  127. /// <param name="toDelete">To delete.</param>
  128. static public void DeleteObject(NeuronCluster toDelete)
  129. {
  130. IList<Neuron> iChildren;
  131. using (ChildrenAccessor iList = toDelete.Children) //don't wrap foreach, this will delete, but for as long as iList is active, the list is locked for reading.
  132. iChildren = iList.ConvertTo<Neuron>();
  133. Brain.Current.Delete(toDelete);
  134. foreach (Neuron i in iChildren)
  135. {
  136. TextNeuron iText = i as TextNeuron;
  137. if (iText != null)
  138. TextSin.Words.Remove(iText.Text); //need to make certain that any textneurons are also removed from the dict.
  139. Brain.Current.Delete(i);
  140. }
  141. }
  142. /// Tries to find the neuron cluster that represents the object for the specified text neuron and synsetid.
  143. /// If it doesn't find something, it returns null.
  144. /// </summary>
  145. /// <param name="textId">The id of the textneuron to search the object for.</param>
  146. /// <param name="synsedId">The synsed id.</param>
  147. /// <returns>A neuroncluster that represents the object, or null if nothing is found.</returns>
  148. static public NeuronCluster FindObject(TextNeuron text, ulong meaningID, int attachedInt)
  149. {
  150. if (text != null)
  151. {
  152. using (NeuronsAccessor iClusteredBy = text.ClusteredBy)
  153. {
  154. List<NeuronCluster> iClusters = (from i in iClusteredBy.Items
  155. where ((NeuronCluster)Brain.Current[i]).Meaning == (ulong)PredefinedNeurons.Object
  156. select ((NeuronCluster)Brain.Current[i])).ToList();
  157. foreach (NeuronCluster i in iClusters)
  158. {
  159. IntNeuron iFound = i.FindFirstOut(meaningID) as IntNeuron;
  160. if (iFound != null && iFound.Value == attachedInt)
  161. return i;
  162. }
  163. }
  164. }
  165. return null;
  166. }
  167. /// <summary>
  168. /// Gets the cluster with the specified text value and a link with the specified meaning, pointing to the specified value. If
  169. /// none exists, one is created.
  170. /// </summary>
  171. /// <remarks>
  172. /// This function allows you to search for an 'object' cluster that points to a specific neuron, like a SynSetID of FrameElementID.
  173. /// </remarks>
  174. /// <param name="args">The args.</param>
  175. /// <returns>
  176. /// A neuroncluster that represents the object
  177. /// </returns>
  178. static public NeuronCluster GetObject(GetObjectArgs args)
  179. {
  180. NeuronCluster iRes = null;
  181. ulong iTextId;
  182. if (args.TextNeuron == null && args.Text != null) //check if the args are ok
  183. {
  184. if (TextSin.Words.TryGetValue(args.Text, out iTextId) == true)
  185. args.TextNeuron = Brain.Current[iTextId] as TextNeuron;
  186. }
  187. else if (args.TextNeuron == null)
  188. throw new ArgumentNullException("Either TextNeuron or Text must be provided to search for an object.");
  189. if (args.TextNeuron != null)
  190. {
  191. iRes = FindObject(args.TextNeuron, args.MeaningID, args.AttachedInt);
  192. if (iRes == null)
  193. {
  194. iRes = CreateObject(args.TextNeuron, args.MeaningID, args.AttachedInt);
  195. args.IsNew = true;
  196. }
  197. }
  198. else
  199. {
  200. iRes = CreateObject(args.Text, args.MeaningID, args.AttachedInt);
  201. args.IsNew = true;
  202. }
  203. return iRes;
  204. }
  205. }
  206. /// <summary>
  207. /// Contains all the arguments for the <see cref="BrainHelper.GetObject"/> function.
  208. /// </summary>
  209. public class GetObjectArgs
  210. {
  211. /// <summary>
  212. /// Gets or sets the integer value to which the result object must point to using the meaning specified
  213. /// in <see cref="GetObjectArgs.MeaningID"/>. The actual value will be stored in an IntNeuron.
  214. /// </summary>
  215. /// <value>The attached value.</value>
  216. public int AttachedInt { get; set; }
  217. /// <summary>
  218. /// Gets wether the object was created or already existed.
  219. /// </summary>
  220. /// <value><c>true</c> if this instance is new; otherwise, <c>false</c>.</value>
  221. [XmlIgnore]
  222. public bool IsNew { get; internal set; }
  223. /// <summary>
  224. /// Gets or sets the ID of the meaning to search for on the object. The value to where the link points to will be compared with
  225. /// <see cref="GetObjectArgs.AttachedValue"/>
  226. /// </summary>
  227. /// <value>The meaning ID.</value>
  228. public ulong MeaningID { get; set; }
  229. /// <summary>
  230. /// Gets or sets the text that the object need to encapsulate. Use this property or <see cref="GetObjectArgs.TextNeuron"/>
  231. /// to provide the text to search for.
  232. /// </summary>
  233. /// <value>The text.</value>
  234. public string Text { get; set; }
  235. /// <summary>
  236. /// Gets or sets the text neuron that the object cluster needs to contain. Use this property or <see cref="GetObjectArgs.Text"/>
  237. /// to provide the text to search for.
  238. /// </summary>
  239. /// <value>The text neuron.</value>
  240. public TextNeuron TextNeuron { get; set; }
  241. }
  242. }