PageRenderTime 1718ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/GraphQL/Implementations/SonesGraphQueryLanguage/Structure/Helper/ExpressionGraph/ExpressionLevel.cs

http://github.com/sones/sones
C# | 274 lines | 191 code | 43 blank | 40 comment | 12 complexity | 1007348527143ac9bef5b86afdabd718 MD5 | raw file
Possible License(s): AGPL-3.0, Apache-2.0, LGPL-3.0
  1. /*
  2. * sones GraphDB - Community Edition - http://www.sones.com
  3. * Copyright (C) 2007-2011 sones GmbH
  4. *
  5. * This file is part of sones GraphDB Community Edition.
  6. *
  7. * sones GraphDB is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as published by
  9. * the Free Software Foundation, version 3 of the License.
  10. *
  11. * sones GraphDB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with sones GraphDB. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. using System;
  21. using System.Collections.Generic;
  22. using System.Linq;
  23. using System.Text;
  24. using sones.GraphQL.GQL.Structure.Helper.ExpressionGraph.Helper;
  25. using sones.Library.PropertyHyperGraph;
  26. using sones.GraphQL.GQL.ErrorHandling;
  27. using sones.Library.Commons.VertexStore.Definitions;
  28. namespace sones.GraphQL.GQL.Structure.Helper.ExpressionGraph
  29. {
  30. /// <summary>
  31. /// This class implements the levels of the expression graph.
  32. /// </summary>
  33. public sealed class ExpressionLevel : IExpressionLevel
  34. {
  35. #region Properties
  36. /// <summary>
  37. /// The content of the level
  38. /// </summary>
  39. private Dictionary<LevelKey, IExpressionLevelEntry> _Content;
  40. public Dictionary<LevelKey, IExpressionLevelEntry> ExpressionLevels
  41. {
  42. get
  43. {
  44. lock (_Content)
  45. {
  46. return _Content;
  47. }
  48. }
  49. }
  50. #endregion
  51. #region Constructor
  52. /// <summary>
  53. /// Constructor
  54. /// </summary>
  55. public ExpressionLevel()
  56. {
  57. _Content = new Dictionary<LevelKey, IExpressionLevelEntry>();
  58. }
  59. #endregion
  60. #region IExpressionLevel Members
  61. public void AddNodeAndBackwardEdge(LevelKey myPath, IVertex aVertex, EdgeKey backwardDirection, VertexInformation backwardDestination, IComparable EdgeWeight, IComparable NodeWeight)
  62. {
  63. lock (_Content)
  64. {
  65. var node = GenerateVertexInfoFromLevelKeyAndVertexID(aVertex.VertexTypeID, aVertex.VertexID);
  66. if (_Content.ContainsKey(myPath))
  67. {
  68. //the level exists
  69. if (_Content[myPath].Nodes.ContainsKey(node))
  70. {
  71. //Node exists
  72. _Content[myPath].Nodes[node].AddBackwardEdge(backwardDirection, backwardDestination, EdgeWeight);
  73. }
  74. else
  75. {
  76. //Node does not exist
  77. _Content[myPath].Nodes.Add(node, new ExpressionNode(aVertex, NodeWeight, node));
  78. _Content[myPath].Nodes[node].AddBackwardEdge(backwardDirection, backwardDestination, EdgeWeight);
  79. }
  80. }
  81. else
  82. {
  83. HashSet<IExpressionEdge> backwardEdges = new HashSet<IExpressionEdge>() { new ExpressionEdge(backwardDestination, EdgeWeight, backwardDirection) };
  84. _Content.Add(myPath, new ExpressionLevelEntry(myPath));
  85. _Content[myPath].Nodes.Add(node, new ExpressionNode(aVertex, NodeWeight, node));
  86. _Content[myPath].Nodes[node].AddBackwardEdge(backwardDirection, backwardDestination, EdgeWeight);
  87. }
  88. }
  89. }
  90. public void AddBackwardEdgesToNode(LevelKey myPath, VertexInformation myVertexInformation, EdgeKey backwardDestination, Dictionary<VertexInformation, IComparable> validUUIDs)
  91. {
  92. lock (_Content)
  93. {
  94. if (_Content.ContainsKey(myPath))
  95. {
  96. //the level exists
  97. if (_Content[myPath].Nodes.ContainsKey(myVertexInformation))
  98. {
  99. //Node exists
  100. _Content[myPath].Nodes[myVertexInformation].AddBackwardEdges(backwardDestination, validUUIDs);
  101. }
  102. else
  103. {
  104. //Node does not exist
  105. throw new ExpressionGraphInternalException("The node does not exist in this LevelKey.");
  106. }
  107. }
  108. else
  109. {
  110. //LevelKey does not exist
  111. throw new ExpressionGraphInternalException("The LevelKey does not exist in this ExpressionLevel.");
  112. }
  113. }
  114. }
  115. private VertexInformation GenerateVertexInfoFromLevelKeyAndVertexID(long myVertexTypeName, long myVertexID)
  116. {
  117. return new VertexInformation(myVertexTypeName, myVertexID);
  118. }
  119. public void AddForwardEdgeToNode(LevelKey levelKey, VertexInformation myVertexInformation, EdgeKey forwardDirection, VertexInformation destination, IComparable edgeWeight)
  120. {
  121. lock (_Content)
  122. {
  123. if (_Content.ContainsKey(levelKey))
  124. {
  125. //the level exists
  126. if (_Content[levelKey].Nodes.ContainsKey(myVertexInformation))
  127. {
  128. //Node exists
  129. _Content[levelKey].Nodes[myVertexInformation].AddForwardEdge(forwardDirection, destination, edgeWeight);
  130. }
  131. else
  132. {
  133. //Node does not exist
  134. throw new ExpressionGraphInternalException("The node does not exist in this LevelKey.");
  135. }
  136. }
  137. else
  138. {
  139. //LevelKey does not exist
  140. throw new ExpressionGraphInternalException("The LevelKey does not exist in this ExpressionLevel.");
  141. }
  142. }
  143. }
  144. public void AddNode(LevelKey levelKey, IExpressionNode expressionNode)
  145. {
  146. lock (_Content)
  147. {
  148. if (_Content.ContainsKey(levelKey))
  149. {
  150. var tempInt64 = expressionNode.GetIVertex().VertexID;
  151. var node = expressionNode.VertexInformation;
  152. if (_Content[levelKey].Nodes.ContainsKey(node))
  153. {
  154. //the node already exist, so update its edges
  155. foreach (var aBackwardEdge in expressionNode.BackwardEdges)
  156. {
  157. _Content[levelKey].Nodes[node].AddBackwardEdges(aBackwardEdge.Value);
  158. }
  159. foreach (var aForwardsEdge in expressionNode.ForwardEdges)
  160. {
  161. _Content[levelKey].Nodes[node].AddForwardEdges(aForwardsEdge.Value);
  162. }
  163. }
  164. else
  165. {
  166. _Content[levelKey].Nodes.Add(node, expressionNode);
  167. }
  168. }
  169. else
  170. {
  171. _Content.Add(levelKey, new ExpressionLevelEntry(levelKey));
  172. _Content[levelKey].Nodes.Add(expressionNode.VertexInformation, expressionNode);
  173. }
  174. }
  175. }
  176. public void AddEmptyLevelKey(LevelKey levelKey)
  177. {
  178. lock (_Content)
  179. {
  180. if (!_Content.ContainsKey(levelKey))
  181. {
  182. _Content.Add(levelKey, new ExpressionLevelEntry(levelKey));
  183. }
  184. }
  185. }
  186. public IEnumerable<IExpressionNode> GetNodes(LevelKey myLevelKey)
  187. {
  188. lock (_Content)
  189. {
  190. if (_Content.ContainsKey(myLevelKey))
  191. {
  192. return _Content[myLevelKey].Nodes.Values;
  193. }
  194. return null;
  195. }
  196. }
  197. public Dictionary<VertexInformation, IExpressionNode> GetNode(LevelKey myLevelKey)
  198. {
  199. lock (_Content)
  200. {
  201. if (_Content.ContainsKey(myLevelKey))
  202. return _Content[myLevelKey].Nodes;
  203. return null;
  204. }
  205. }
  206. public void RemoveNode(LevelKey myLevelKey, VertexInformation myToBeRemovedVertex)
  207. {
  208. lock (_Content)
  209. {
  210. if (_Content.ContainsKey(myLevelKey))
  211. {
  212. _Content[myLevelKey].Nodes.Remove(myToBeRemovedVertex);
  213. }
  214. }
  215. }
  216. public Boolean HasLevelKey(LevelKey myLevelKey)
  217. {
  218. lock (_Content)
  219. {
  220. return _Content.ContainsKey(myLevelKey);
  221. }
  222. }
  223. #endregion
  224. #region override
  225. public override string ToString()
  226. {
  227. return String.Format("Level#: {0}", ExpressionLevels.Count);
  228. }
  229. #endregion
  230. }
  231. }