flowGraphContext.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. import { __decorate } from "../tslib.es6.js";
  2. import { serialize } from "../Misc/decorators.js";
  3. import { RandomGUID } from "../Misc/guid.js";
  4. import { defaultValueParseFunction, defaultValueSerializationFunction } from "./serialization.js";
  5. import { Observable } from "../Misc/observable.js";
  6. /**
  7. * @experimental
  8. * The context represents the current state and execution of the flow graph.
  9. * It contains both user-defined variables, which are derived from
  10. * a more general variable definition, and execution variables that
  11. * are set by the blocks.
  12. */
  13. export class FlowGraphContext {
  14. constructor(params) {
  15. /**
  16. * A randomly generated GUID for each context.
  17. */
  18. this.uniqueId = RandomGUID();
  19. /**
  20. * These are the variables defined by a user.
  21. */
  22. this._userVariables = {};
  23. /**
  24. * These are the variables set by the blocks.
  25. */
  26. this._executionVariables = {};
  27. /**
  28. * These are the values for the data connection points
  29. */
  30. this._connectionValues = {};
  31. /**
  32. * These are blocks that have currently pending tasks/listeners that need to be cleaned up.
  33. */
  34. this._pendingBlocks = [];
  35. /**
  36. * A monotonically increasing ID for each execution.
  37. * Incremented for every block executed.
  38. */
  39. this._executionId = 0;
  40. /**
  41. * Observable that is triggered when a node is executed.
  42. */
  43. this.onNodeExecutedObservable = new Observable();
  44. this._configuration = params;
  45. }
  46. /**
  47. * Check if a user-defined variable is defined.
  48. * @param name the name of the variable
  49. * @returns true if the variable is defined
  50. */
  51. hasVariable(name) {
  52. return name in this._userVariables;
  53. }
  54. /**
  55. * Set a user-defined variable.
  56. * @param name the name of the variable
  57. * @param value the value of the variable
  58. */
  59. setVariable(name, value) {
  60. this._userVariables[name] = value;
  61. }
  62. /**
  63. * Get a user-defined variable.
  64. * @param name the name of the variable
  65. * @returns the value of the variable
  66. */
  67. getVariable(name) {
  68. return this._userVariables[name];
  69. }
  70. /**
  71. * Gets all user variables map
  72. */
  73. get userVariables() {
  74. return this._userVariables;
  75. }
  76. _getUniqueIdPrefixedName(obj, name) {
  77. return `${obj.uniqueId}_${name}`;
  78. }
  79. /**
  80. * Set an internal execution variable
  81. * @internal
  82. * @param name
  83. * @param value
  84. */
  85. _setExecutionVariable(block, name, value) {
  86. this._executionVariables[this._getUniqueIdPrefixedName(block, name)] = value;
  87. }
  88. /**
  89. * Get an internal execution variable
  90. * @internal
  91. * @param name
  92. * @returns
  93. */
  94. _getExecutionVariable(block, name, defaultValue) {
  95. if (this._hasExecutionVariable(block, name)) {
  96. return this._executionVariables[this._getUniqueIdPrefixedName(block, name)];
  97. }
  98. else {
  99. return defaultValue;
  100. }
  101. }
  102. /**
  103. * Delete an internal execution variable
  104. * @internal
  105. * @param block
  106. * @param name
  107. */
  108. _deleteExecutionVariable(block, name) {
  109. delete this._executionVariables[this._getUniqueIdPrefixedName(block, name)];
  110. }
  111. /**
  112. * Check if an internal execution variable is defined
  113. * @internal
  114. * @param block
  115. * @param name
  116. * @returns
  117. */
  118. _hasExecutionVariable(block, name) {
  119. return this._getUniqueIdPrefixedName(block, name) in this._executionVariables;
  120. }
  121. /**
  122. * Check if a connection value is defined
  123. * @internal
  124. * @param connectionPoint
  125. * @returns
  126. */
  127. _hasConnectionValue(connectionPoint) {
  128. return connectionPoint.uniqueId in this._connectionValues;
  129. }
  130. /**
  131. * Set a connection value
  132. * @internal
  133. * @param connectionPoint
  134. * @param value
  135. */
  136. _setConnectionValue(connectionPoint, value) {
  137. this._connectionValues[connectionPoint.uniqueId] = value;
  138. }
  139. /**
  140. * Get a connection value
  141. * @internal
  142. * @param connectionPoint
  143. * @returns
  144. */
  145. _getConnectionValue(connectionPoint) {
  146. return this._connectionValues[connectionPoint.uniqueId];
  147. }
  148. /**
  149. * Get the configuration
  150. * @internal
  151. * @param name
  152. * @param value
  153. */
  154. get configuration() {
  155. return this._configuration;
  156. }
  157. /**
  158. * Add a block to the list of blocks that have pending tasks.
  159. * @internal
  160. * @param block
  161. */
  162. _addPendingBlock(block) {
  163. this._pendingBlocks.push(block);
  164. }
  165. /**
  166. * Remove a block from the list of blocks that have pending tasks.
  167. * @internal
  168. * @param block
  169. */
  170. _removePendingBlock(block) {
  171. const index = this._pendingBlocks.indexOf(block);
  172. if (index !== -1) {
  173. this._pendingBlocks.splice(index, 1);
  174. }
  175. }
  176. /**
  177. * Clear all pending blocks.
  178. * @internal
  179. */
  180. _clearPendingBlocks() {
  181. for (const block of this._pendingBlocks) {
  182. block._cancelPendingTasks(this);
  183. }
  184. this._pendingBlocks.length = 0;
  185. }
  186. /**
  187. * @internal
  188. * Function that notifies the node executed observable
  189. * @param node
  190. */
  191. _notifyExecuteNode(node) {
  192. this.onNodeExecutedObservable.notifyObservers(node);
  193. }
  194. /**
  195. * @internal
  196. */
  197. _increaseExecutionId() {
  198. this._executionId++;
  199. }
  200. /**
  201. * A monotonically increasing ID for each execution.
  202. * Incremented for every block executed.
  203. */
  204. get executionId() {
  205. return this._executionId;
  206. }
  207. /**
  208. * Serializes a context
  209. * @param serializationObject the object to write the values in
  210. * @param valueSerializationFunction a function to serialize complex values
  211. */
  212. serialize(serializationObject = {}, valueSerializationFunction = defaultValueSerializationFunction) {
  213. serializationObject.uniqueId = this.uniqueId;
  214. serializationObject._userVariables = {};
  215. for (const key in this._userVariables) {
  216. valueSerializationFunction(key, this._userVariables[key], serializationObject._userVariables);
  217. }
  218. serializationObject._connectionValues = {};
  219. for (const key in this._connectionValues) {
  220. valueSerializationFunction(key, this._connectionValues[key], serializationObject._connectionValues);
  221. }
  222. }
  223. /**
  224. * @returns the class name of the object.
  225. */
  226. getClassName() {
  227. return "FGContext";
  228. }
  229. /**
  230. * Parses a context
  231. * @param serializationObject the object containing the context serialization values
  232. * @param options the options for parsing the context
  233. * @returns
  234. */
  235. static Parse(serializationObject, options) {
  236. const result = options.graph.createContext();
  237. const valueParseFunction = options.valueParseFunction ?? defaultValueParseFunction;
  238. result.uniqueId = serializationObject.uniqueId;
  239. for (const key in serializationObject._userVariables) {
  240. const value = valueParseFunction(key, serializationObject._userVariables, result._configuration.scene);
  241. result._userVariables[key] = value;
  242. }
  243. for (const key in serializationObject._connectionValues) {
  244. const value = valueParseFunction(key, serializationObject._connectionValues, result._configuration.scene);
  245. result._connectionValues[key] = value;
  246. }
  247. return result;
  248. }
  249. }
  250. __decorate([
  251. serialize()
  252. ], FlowGraphContext.prototype, "uniqueId", void 0);
  253. //# sourceMappingURL=flowGraphContext.js.map