@cyberismo/data-handler 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (306) hide show
  1. package/LICENSE +702 -0
  2. package/dist/card-metadata-updater.d.ts +33 -0
  3. package/dist/card-metadata-updater.js +121 -0
  4. package/dist/card-metadata-updater.js.map +1 -0
  5. package/dist/command-handler.d.ts +96 -0
  6. package/dist/command-handler.js +557 -0
  7. package/dist/command-handler.js.map +1 -0
  8. package/dist/command-manager.d.ts +43 -0
  9. package/dist/command-manager.js +73 -0
  10. package/dist/command-manager.js.map +1 -0
  11. package/dist/commands/calculate.d.ts +86 -0
  12. package/dist/commands/calculate.js +444 -0
  13. package/dist/commands/calculate.js.map +1 -0
  14. package/dist/commands/create.d.ts +114 -0
  15. package/dist/commands/create.js +389 -0
  16. package/dist/commands/create.js.map +1 -0
  17. package/dist/commands/edit.d.ts +37 -0
  18. package/dist/commands/edit.js +99 -0
  19. package/dist/commands/edit.js.map +1 -0
  20. package/dist/commands/export-site.d.ts +45 -0
  21. package/dist/commands/export-site.js +301 -0
  22. package/dist/commands/export-site.js.map +1 -0
  23. package/dist/commands/export.d.ts +53 -0
  24. package/dist/commands/export.js +251 -0
  25. package/dist/commands/export.js.map +1 -0
  26. package/dist/commands/import.d.ts +53 -0
  27. package/dist/commands/import.js +133 -0
  28. package/dist/commands/import.js.map +1 -0
  29. package/dist/commands/index.d.ts +26 -0
  30. package/dist/commands/index.js +27 -0
  31. package/dist/commands/index.js.map +1 -0
  32. package/dist/commands/move.d.ts +55 -0
  33. package/dist/commands/move.js +341 -0
  34. package/dist/commands/move.js.map +1 -0
  35. package/dist/commands/remove.d.ts +38 -0
  36. package/dist/commands/remove.js +192 -0
  37. package/dist/commands/remove.js.map +1 -0
  38. package/dist/commands/rename.d.ts +46 -0
  39. package/dist/commands/rename.js +289 -0
  40. package/dist/commands/rename.js.map +1 -0
  41. package/dist/commands/show.d.ts +124 -0
  42. package/dist/commands/show.js +345 -0
  43. package/dist/commands/show.js.map +1 -0
  44. package/dist/commands/transition.d.ts +27 -0
  45. package/dist/commands/transition.js +92 -0
  46. package/dist/commands/transition.js.map +1 -0
  47. package/dist/commands/update.d.ts +29 -0
  48. package/dist/commands/update.js +64 -0
  49. package/dist/commands/update.js.map +1 -0
  50. package/dist/commands/validate.d.ts +143 -0
  51. package/dist/commands/validate.js +689 -0
  52. package/dist/commands/validate.js.map +1 -0
  53. package/dist/containers/card-container.d.ts +44 -0
  54. package/dist/containers/card-container.js +282 -0
  55. package/dist/containers/card-container.js.map +1 -0
  56. package/dist/containers/project/project-paths.d.ts +46 -0
  57. package/dist/containers/project/project-paths.js +105 -0
  58. package/dist/containers/project/project-paths.js.map +1 -0
  59. package/dist/containers/project/resource-collector.d.ts +86 -0
  60. package/dist/containers/project/resource-collector.js +331 -0
  61. package/dist/containers/project/resource-collector.js.map +1 -0
  62. package/dist/containers/project.d.ts +351 -0
  63. package/dist/containers/project.js +896 -0
  64. package/dist/containers/project.js.map +1 -0
  65. package/dist/containers/template.d.ts +108 -0
  66. package/dist/containers/template.js +433 -0
  67. package/dist/containers/template.js.map +1 -0
  68. package/dist/exceptions/index.d.ts +19 -0
  69. package/dist/exceptions/index.js +26 -0
  70. package/dist/exceptions/index.js.map +1 -0
  71. package/dist/index.d.ts +16 -0
  72. package/dist/index.js +15 -0
  73. package/dist/index.js.map +1 -0
  74. package/dist/interfaces/adoc.d.ts +12 -0
  75. package/dist/interfaces/adoc.js +13 -0
  76. package/dist/interfaces/adoc.js.map +1 -0
  77. package/dist/interfaces/macros.d.ts +45 -0
  78. package/dist/interfaces/macros.js +13 -0
  79. package/dist/interfaces/macros.js.map +1 -0
  80. package/dist/interfaces/project-interfaces.d.ts +121 -0
  81. package/dist/interfaces/project-interfaces.js +21 -0
  82. package/dist/interfaces/project-interfaces.js.map +1 -0
  83. package/dist/interfaces/request-status-interfaces.d.ts +28 -0
  84. package/dist/interfaces/request-status-interfaces.js +20 -0
  85. package/dist/interfaces/request-status-interfaces.js.map +1 -0
  86. package/dist/interfaces/resource-interfaces.d.ts +117 -0
  87. package/dist/interfaces/resource-interfaces.js +20 -0
  88. package/dist/interfaces/resource-interfaces.js.map +1 -0
  89. package/dist/macros/base-macro.d.ts +31 -0
  90. package/dist/macros/base-macro.js +126 -0
  91. package/dist/macros/base-macro.js.map +1 -0
  92. package/dist/macros/common.d.ts +17 -0
  93. package/dist/macros/common.js +23 -0
  94. package/dist/macros/common.js.map +1 -0
  95. package/dist/macros/createCards/index.d.ts +36 -0
  96. package/dist/macros/createCards/index.js +35 -0
  97. package/dist/macros/createCards/index.js.map +1 -0
  98. package/dist/macros/createCards/metadata.d.ts +14 -0
  99. package/dist/macros/createCards/metadata.js +18 -0
  100. package/dist/macros/createCards/metadata.js.map +1 -0
  101. package/dist/macros/graph/index.d.ts +29 -0
  102. package/dist/macros/graph/index.js +91 -0
  103. package/dist/macros/graph/index.js.map +1 -0
  104. package/dist/macros/graph/metadata.d.ts +14 -0
  105. package/dist/macros/graph/metadata.js +18 -0
  106. package/dist/macros/graph/metadata.js.map +1 -0
  107. package/dist/macros/index.d.ts +93 -0
  108. package/dist/macros/index.js +237 -0
  109. package/dist/macros/index.js.map +1 -0
  110. package/dist/macros/report/index.d.ts +26 -0
  111. package/dist/macros/report/index.js +70 -0
  112. package/dist/macros/report/index.js.map +1 -0
  113. package/dist/macros/report/metadata.d.ts +14 -0
  114. package/dist/macros/report/metadata.js +18 -0
  115. package/dist/macros/report/metadata.js.map +1 -0
  116. package/dist/macros/scoreCard/index.d.ts +30 -0
  117. package/dist/macros/scoreCard/index.js +38 -0
  118. package/dist/macros/scoreCard/index.js.map +1 -0
  119. package/dist/macros/scoreCard/metadata.d.ts +14 -0
  120. package/dist/macros/scoreCard/metadata.js +18 -0
  121. package/dist/macros/scoreCard/metadata.js.map +1 -0
  122. package/dist/macros/task-queue.d.ts +46 -0
  123. package/dist/macros/task-queue.js +69 -0
  124. package/dist/macros/task-queue.js.map +1 -0
  125. package/dist/module-manager.d.ts +62 -0
  126. package/dist/module-manager.js +350 -0
  127. package/dist/module-manager.js.map +1 -0
  128. package/dist/permissions/action-guard.d.ts +28 -0
  129. package/dist/permissions/action-guard.js +61 -0
  130. package/dist/permissions/action-guard.js.map +1 -0
  131. package/dist/project-settings.d.ts +42 -0
  132. package/dist/project-settings.js +120 -0
  133. package/dist/project-settings.js.map +1 -0
  134. package/dist/resources/array-handler.d.ts +28 -0
  135. package/dist/resources/array-handler.js +116 -0
  136. package/dist/resources/array-handler.js.map +1 -0
  137. package/dist/resources/card-type-resource.d.ts +72 -0
  138. package/dist/resources/card-type-resource.js +334 -0
  139. package/dist/resources/card-type-resource.js.map +1 -0
  140. package/dist/resources/create-defaults.d.ts +81 -0
  141. package/dist/resources/create-defaults.js +184 -0
  142. package/dist/resources/create-defaults.js.map +1 -0
  143. package/dist/resources/field-type-resource.d.ts +88 -0
  144. package/dist/resources/field-type-resource.js +411 -0
  145. package/dist/resources/field-type-resource.js.map +1 -0
  146. package/dist/resources/file-resource.d.ts +50 -0
  147. package/dist/resources/file-resource.js +301 -0
  148. package/dist/resources/file-resource.js.map +1 -0
  149. package/dist/resources/folder-resource.d.ts +66 -0
  150. package/dist/resources/folder-resource.js +100 -0
  151. package/dist/resources/folder-resource.js.map +1 -0
  152. package/dist/resources/graph-model-resource.d.ts +78 -0
  153. package/dist/resources/graph-model-resource.js +164 -0
  154. package/dist/resources/graph-model-resource.js.map +1 -0
  155. package/dist/resources/graph-view-resource.d.ts +78 -0
  156. package/dist/resources/graph-view-resource.js +163 -0
  157. package/dist/resources/graph-view-resource.js.map +1 -0
  158. package/dist/resources/link-type-resource.d.ts +62 -0
  159. package/dist/resources/link-type-resource.js +150 -0
  160. package/dist/resources/link-type-resource.js.map +1 -0
  161. package/dist/resources/report-resource.d.ts +77 -0
  162. package/dist/resources/report-resource.js +171 -0
  163. package/dist/resources/report-resource.js.map +1 -0
  164. package/dist/resources/resource-object.d.ts +108 -0
  165. package/dist/resources/resource-object.js +147 -0
  166. package/dist/resources/resource-object.js.map +1 -0
  167. package/dist/resources/template-resource.d.ts +82 -0
  168. package/dist/resources/template-resource.js +173 -0
  169. package/dist/resources/template-resource.js.map +1 -0
  170. package/dist/resources/workflow-resource.d.ts +67 -0
  171. package/dist/resources/workflow-resource.js +156 -0
  172. package/dist/resources/workflow-resource.js.map +1 -0
  173. package/dist/types/queries.d.ts +142 -0
  174. package/dist/types/queries.js +16 -0
  175. package/dist/types/queries.js.map +1 -0
  176. package/dist/utils/card-utils.d.ts +34 -0
  177. package/dist/utils/card-utils.js +78 -0
  178. package/dist/utils/card-utils.js.map +1 -0
  179. package/dist/utils/clingo-fact-builder.d.ts +58 -0
  180. package/dist/utils/clingo-fact-builder.js +126 -0
  181. package/dist/utils/clingo-fact-builder.js.map +1 -0
  182. package/dist/utils/clingo-facts.d.ts +97 -0
  183. package/dist/utils/clingo-facts.js +352 -0
  184. package/dist/utils/clingo-facts.js.map +1 -0
  185. package/dist/utils/clingo-parser.d.ts +59 -0
  186. package/dist/utils/clingo-parser.js +403 -0
  187. package/dist/utils/clingo-parser.js.map +1 -0
  188. package/dist/utils/clingo-program-builder.d.ts +39 -0
  189. package/dist/utils/clingo-program-builder.js +57 -0
  190. package/dist/utils/clingo-program-builder.js.map +1 -0
  191. package/dist/utils/common-utils.d.ts +24 -0
  192. package/dist/utils/common-utils.js +47 -0
  193. package/dist/utils/common-utils.js.map +1 -0
  194. package/dist/utils/constants.d.ts +18 -0
  195. package/dist/utils/constants.js +27 -0
  196. package/dist/utils/constants.js.map +1 -0
  197. package/dist/utils/csv.d.ts +18 -0
  198. package/dist/utils/csv.js +45 -0
  199. package/dist/utils/csv.js.map +1 -0
  200. package/dist/utils/file-utils.d.ts +69 -0
  201. package/dist/utils/file-utils.js +158 -0
  202. package/dist/utils/file-utils.js.map +1 -0
  203. package/dist/utils/json.d.ts +61 -0
  204. package/dist/utils/json.js +108 -0
  205. package/dist/utils/json.js.map +1 -0
  206. package/dist/utils/lexorank.d.ts +59 -0
  207. package/dist/utils/lexorank.js +159 -0
  208. package/dist/utils/lexorank.js.map +1 -0
  209. package/dist/utils/log-utils.d.ts +40 -0
  210. package/dist/utils/log-utils.js +109 -0
  211. package/dist/utils/log-utils.js.map +1 -0
  212. package/dist/utils/random.d.ts +19 -0
  213. package/dist/utils/random.js +34 -0
  214. package/dist/utils/random.js.map +1 -0
  215. package/dist/utils/resource-utils.d.ts +45 -0
  216. package/dist/utils/resource-utils.js +137 -0
  217. package/dist/utils/resource-utils.js.map +1 -0
  218. package/dist/utils/sanitize-svg.d.ts +18 -0
  219. package/dist/utils/sanitize-svg.js +38 -0
  220. package/dist/utils/sanitize-svg.js.map +1 -0
  221. package/dist/utils/user-preferences.d.ts +64 -0
  222. package/dist/utils/user-preferences.js +106 -0
  223. package/dist/utils/user-preferences.js.map +1 -0
  224. package/dist/utils/validate.d.ts +26 -0
  225. package/dist/utils/validate.js +53 -0
  226. package/dist/utils/validate.js.map +1 -0
  227. package/dist/utils/value-utils.d.ts +58 -0
  228. package/dist/utils/value-utils.js +181 -0
  229. package/dist/utils/value-utils.js.map +1 -0
  230. package/package.json +67 -0
  231. package/src/card-metadata-updater.ts +182 -0
  232. package/src/command-handler.ts +686 -0
  233. package/src/command-manager.ts +99 -0
  234. package/src/commands/calculate.ts +591 -0
  235. package/src/commands/create.ts +559 -0
  236. package/src/commands/edit.ts +123 -0
  237. package/src/commands/export-site.ts +356 -0
  238. package/src/commands/export.ts +315 -0
  239. package/src/commands/import.ts +169 -0
  240. package/src/commands/index.ts +42 -0
  241. package/src/commands/move.ts +451 -0
  242. package/src/commands/remove.ts +244 -0
  243. package/src/commands/rename.ts +378 -0
  244. package/src/commands/show.ts +442 -0
  245. package/src/commands/transition.ts +127 -0
  246. package/src/commands/update.ts +76 -0
  247. package/src/commands/validate.ts +962 -0
  248. package/src/containers/card-container.ts +378 -0
  249. package/src/containers/project/project-paths.ts +127 -0
  250. package/src/containers/project/resource-collector.ts +379 -0
  251. package/src/containers/project.ts +1135 -0
  252. package/src/containers/template.ts +573 -0
  253. package/src/exceptions/index.ts +29 -0
  254. package/src/index.ts +33 -0
  255. package/src/interfaces/adoc.ts +18 -0
  256. package/src/interfaces/macros.ts +54 -0
  257. package/src/interfaces/project-interfaces.ts +208 -0
  258. package/src/interfaces/request-status-interfaces.ts +30 -0
  259. package/src/interfaces/resource-interfaces.ts +179 -0
  260. package/src/macros/base-macro.ts +176 -0
  261. package/src/macros/common.ts +24 -0
  262. package/src/macros/createCards/index.ts +57 -0
  263. package/src/macros/createCards/metadata.ts +21 -0
  264. package/src/macros/graph/index.ts +130 -0
  265. package/src/macros/graph/metadata.ts +21 -0
  266. package/src/macros/index.ts +321 -0
  267. package/src/macros/report/index.ts +88 -0
  268. package/src/macros/report/metadata.ts +21 -0
  269. package/src/macros/scoreCard/index.ts +55 -0
  270. package/src/macros/scoreCard/metadata.ts +21 -0
  271. package/src/macros/task-queue.ts +79 -0
  272. package/src/module-manager.ts +443 -0
  273. package/src/permissions/action-guard.ts +77 -0
  274. package/src/project-settings.ts +140 -0
  275. package/src/resources/array-handler.ts +141 -0
  276. package/src/resources/card-type-resource.ts +455 -0
  277. package/src/resources/create-defaults.ts +216 -0
  278. package/src/resources/field-type-resource.ts +533 -0
  279. package/src/resources/file-resource.ts +433 -0
  280. package/src/resources/folder-resource.ts +140 -0
  281. package/src/resources/graph-model-resource.ts +205 -0
  282. package/src/resources/graph-view-resource.ts +199 -0
  283. package/src/resources/link-type-resource.ts +191 -0
  284. package/src/resources/report-resource.ts +224 -0
  285. package/src/resources/resource-object.ts +246 -0
  286. package/src/resources/template-resource.ts +210 -0
  287. package/src/resources/workflow-resource.ts +205 -0
  288. package/src/types/queries.ts +149 -0
  289. package/src/utils/card-utils.ts +83 -0
  290. package/src/utils/clingo-fact-builder.ts +167 -0
  291. package/src/utils/clingo-facts.ts +550 -0
  292. package/src/utils/clingo-parser.ts +519 -0
  293. package/src/utils/clingo-program-builder.ts +71 -0
  294. package/src/utils/common-utils.ts +54 -0
  295. package/src/utils/constants.ts +32 -0
  296. package/src/utils/csv.ts +53 -0
  297. package/src/utils/file-utils.ts +182 -0
  298. package/src/utils/json.ts +118 -0
  299. package/src/utils/lexorank.ts +180 -0
  300. package/src/utils/log-utils.ts +127 -0
  301. package/src/utils/random.ts +37 -0
  302. package/src/utils/resource-utils.ts +180 -0
  303. package/src/utils/sanitize-svg.ts +46 -0
  304. package/src/utils/user-preferences.ts +126 -0
  305. package/src/utils/validate.ts +66 -0
  306. package/src/utils/value-utils.ts +189 -0
@@ -0,0 +1,99 @@
1
+ /**
2
+ Cyberismo
3
+ Copyright © Cyberismo Ltd and contributors 2024
4
+
5
+ This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License version 3 as published by the Free Software Foundation.
6
+
7
+ This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
8
+
9
+ You should have received a copy of the GNU Affero General Public
10
+ License along with this program. If not, see <https://www.gnu.org/licenses/>.
11
+ */
12
+
13
+ import {
14
+ Calculate,
15
+ Create,
16
+ Edit,
17
+ Export,
18
+ ExportSite,
19
+ Import,
20
+ Move,
21
+ Remove,
22
+ Rename,
23
+ Show,
24
+ Transition,
25
+ Update,
26
+ Validate,
27
+ } from './commands/index.js';
28
+ import { Project } from './containers/project.js';
29
+
30
+ // Handles commands and ensures that no extra instances are created.
31
+ export class CommandManager {
32
+ private static instance: CommandManager;
33
+
34
+ public project: Project;
35
+ public calculateCmd: Calculate;
36
+ public createCmd: Create;
37
+ public editCmd: Edit;
38
+ public exportSiteCmd: ExportSite;
39
+ public exportCmd: Export;
40
+ public importCmd: Import;
41
+ public moveCmd: Move;
42
+ public removeCmd: Remove;
43
+ public renameCmd: Rename;
44
+ public showCmd: Show;
45
+ public transitionCmd: Transition;
46
+ public updateCmd: Update;
47
+ public validateCmd: Validate;
48
+
49
+ constructor(path: string) {
50
+ this.project = new Project(path);
51
+ this.validateCmd = Validate.getInstance();
52
+
53
+ this.showCmd = new Show(this.project);
54
+ this.calculateCmd = new Calculate(this.project);
55
+ this.createCmd = new Create(this.project, this.calculateCmd);
56
+ this.editCmd = new Edit(this.project, this.calculateCmd);
57
+ this.exportCmd = new Export(this.project, this.calculateCmd, this.showCmd);
58
+ this.exportSiteCmd = new ExportSite(
59
+ this.project,
60
+ this.calculateCmd,
61
+ this.showCmd,
62
+ );
63
+ this.importCmd = new Import(this.project, this.createCmd);
64
+ this.moveCmd = new Move(this.project, this.calculateCmd);
65
+ this.removeCmd = new Remove(this.project, this.calculateCmd);
66
+ this.renameCmd = new Rename(this.project, this.calculateCmd);
67
+ this.transitionCmd = new Transition(this.project, this.calculateCmd);
68
+ this.updateCmd = new Update(this.project);
69
+ }
70
+
71
+ /**
72
+ * Some commands needs initialization that cannot be performed inside constructor.
73
+ * Add such calls here.
74
+ */
75
+ public async initialize() {
76
+ await this.project.collectModuleResources();
77
+ }
78
+
79
+ /**
80
+ * Either creates a new instance, or passes the current one.
81
+ * New instance is created, if path differs, or there is no previous instance.
82
+ * @param path Project path.
83
+ * @returns Instance of this class.
84
+ */
85
+ public static async getInstance(path: string): Promise<CommandManager> {
86
+ if (
87
+ CommandManager.instance &&
88
+ CommandManager.instance.project.basePath !== path
89
+ ) {
90
+ CommandManager.instance = new CommandManager(path);
91
+ await CommandManager.instance.initialize();
92
+ }
93
+ if (!CommandManager.instance) {
94
+ CommandManager.instance = new CommandManager(path);
95
+ await CommandManager.instance.initialize();
96
+ }
97
+ return CommandManager.instance;
98
+ }
99
+ }
@@ -0,0 +1,591 @@
1
+ /**
2
+ Cyberismo
3
+ Copyright © Cyberismo Ltd and contributors 2024
4
+ This program is free software: you can redistribute it and/or modify it under
5
+ the terms of the GNU Affero General Public License version 3 as published by
6
+ the Free Software Foundation.
7
+ This program is distributed in the hope that it will be useful, but WITHOUT
8
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
9
+ FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
10
+ details. You should have received a copy of the GNU Affero General Public
11
+ License along with this program. If not, see <https://www.gnu.org/licenses/>.
12
+ */
13
+
14
+ // node
15
+ import { basename, join, resolve } from 'node:path';
16
+ import { readFile, rm } from 'node:fs/promises';
17
+ import { spawnSync } from 'node:child_process';
18
+ import { sanitizeSvgBase64 } from '../utils/sanitize-svg.js';
19
+
20
+ import type {
21
+ BaseResult,
22
+ ParseResult,
23
+ QueryName,
24
+ QueryResult,
25
+ } from '../types/queries.js';
26
+ import type { Card } from '../interfaces/project-interfaces.js';
27
+ import ClingoParser from '../utils/clingo-parser.js';
28
+ import { pathExists } from '../utils/file-utils.js';
29
+ import { Mutex } from 'async-mutex';
30
+ import Handlebars from 'handlebars';
31
+ import { type Project, ResourcesFrom } from '../containers/project.js';
32
+ import { flattenCardArray } from '../utils/card-utils.js';
33
+ import { logger } from '../utils/log-utils.js';
34
+ import {
35
+ createCardFacts,
36
+ createCardTypeFacts,
37
+ createFieldTypeFacts,
38
+ createLinkTypeFacts,
39
+ createModuleFacts,
40
+ createProjectFacts,
41
+ createReportFacts,
42
+ createTemplateFacts,
43
+ createWorkflowFacts,
44
+ } from '../utils/clingo-facts.js';
45
+ import { CardMetadataUpdater } from '../card-metadata-updater.js';
46
+ import { generateRandomString } from '../utils/random.js';
47
+ import type {
48
+ CardType,
49
+ FieldType,
50
+ LinkType,
51
+ ReportMetadata,
52
+ TemplateMetadata,
53
+ Workflow,
54
+ } from '../interfaces/resource-interfaces.js';
55
+ import { solve, setBaseProgram } from '@cyberismo/node-clingo';
56
+ import { lpFiles } from '@cyberismo/resources';
57
+
58
+ // Define names for the base programs
59
+ const BASE_PROGRAM_KEY = 'base';
60
+ const QUERY_LANGUAGE_KEY = 'queryLanguage';
61
+
62
+ // Class that calculates with logic program card / project level calculations.
63
+ export class Calculate {
64
+ private static mutex = new Mutex();
65
+
66
+ private pythonBinary: string = 'python';
67
+ constructor(private project: Project) {}
68
+
69
+ // Storage for in-memory program content
70
+ private logicProgram: string = '';
71
+ private modules: string = '';
72
+
73
+ private modulesInitialized = false;
74
+
75
+ // Initialize modules during construction
76
+ private async initializeModules() {
77
+ try {
78
+ // Collect all available calculations at initialization time
79
+ const modules = await this.generateModules();
80
+ this.modules = modules; // Store initial modules in logicProgram
81
+ } catch (error) {
82
+ logger.error(`Failed to initialize modules: ${error}`);
83
+ }
84
+ }
85
+
86
+ // Wrapper to run onCreation query.
87
+ private async creationQuery(cardKeys: string[]) {
88
+ if (!cardKeys) return undefined;
89
+ return this.runQuery('onCreation', {
90
+ cardKeys,
91
+ });
92
+ }
93
+
94
+ // Generate card tree content
95
+ private async generateCardTreeContent(parentCard: Card | undefined) {
96
+ const cards = await this.getCards(parentCard);
97
+
98
+ let content = '% SECTION: CARDS_START\n';
99
+
100
+ for (const card of cards) {
101
+ const cardContent = await createCardFacts(card, this.project);
102
+ content += `% SECTION: CARD_${card.key}_START\n`;
103
+ content += `% Card ${card.key}\n`;
104
+ content += `${cardContent}\n`;
105
+ content += `% SECTION: CARD_${card.key}_END\n\n`;
106
+ }
107
+
108
+ content += '% SECTION: CARDS_END';
109
+
110
+ return content;
111
+ }
112
+
113
+ //
114
+ private async generateCardTypes() {
115
+ const cardTypes = await this.project.cardTypes();
116
+ let content = '';
117
+
118
+ for (const cardType of await Promise.all(
119
+ cardTypes.map((c) => this.project.resource<CardType>(c.name)),
120
+ )) {
121
+ if (!cardType) continue;
122
+
123
+ const cardTypeContent = createCardTypeFacts(cardType);
124
+ content += `% CardType ${cardType.name}\n${cardTypeContent}\n\n`;
125
+ }
126
+
127
+ return content;
128
+ }
129
+
130
+ //
131
+ private async generateFieldTypes() {
132
+ const fieldTypes = await this.project.fieldTypes();
133
+ let content = '';
134
+
135
+ for (const fieldType of await Promise.all(
136
+ fieldTypes.map((m) => this.project.resource<FieldType>(m.name)),
137
+ )) {
138
+ if (!fieldType) continue;
139
+
140
+ const fieldTypeContent = createFieldTypeFacts(fieldType);
141
+ content += `% FieldType ${fieldType.name}\n${fieldTypeContent}\n\n`;
142
+ }
143
+ return content;
144
+ }
145
+
146
+ // Collects all linkTypes from the project
147
+ private async generateLinkTypes() {
148
+ const linkTypes = await this.project.linkTypes();
149
+ let content = '';
150
+
151
+ for (const linkType of await Promise.all(
152
+ linkTypes.map((c) => this.project.resource<LinkType>(c.name)),
153
+ )) {
154
+ if (!linkType) continue;
155
+
156
+ const linkTypeContent = createLinkTypeFacts(linkType);
157
+ content += `% LinkType ${linkType.name}\n${linkTypeContent}\n\n`;
158
+ }
159
+
160
+ return content;
161
+ }
162
+
163
+ // Generates logic programs related to modules (and project itself).
164
+ private async generateModules() {
165
+ const modules = await this.project.modules();
166
+ let content = '';
167
+ for (const module of await Promise.all(
168
+ modules.map((mod) => this.project.module(mod.name)),
169
+ )) {
170
+ if (!module) continue;
171
+ const moduleContent = createModuleFacts(module);
172
+ content = content.concat(moduleContent);
173
+ }
174
+ const projectContent = createProjectFacts(this.project.projectPrefix);
175
+ content = content.concat(projectContent);
176
+ return content;
177
+ }
178
+
179
+ // Collects all logic calculation files from project (local and imported modules)
180
+ private async generateCalculations() {
181
+ // Collect all available calculations
182
+ const calculations = await this.project.calculations(ResourcesFrom.all);
183
+ let content = '% SECTION: MODULES_START\n';
184
+
185
+ // Process modules content
186
+ for (const calculationFile of calculations) {
187
+ if (calculationFile.path) {
188
+ const moduleLogicFile = resolve(
189
+ join(calculationFile.path, basename(calculationFile.name)),
190
+ );
191
+
192
+ const filePath = moduleLogicFile.endsWith('.lp')
193
+ ? moduleLogicFile
194
+ : moduleLogicFile + '.lp';
195
+
196
+ if (pathExists(filePath)) {
197
+ try {
198
+ const moduleContent = await readFile(filePath, 'utf-8');
199
+ content += `% SECTION: MODULE_${calculationFile.name}_START\n`;
200
+ content += `% Module ${calculationFile.name}\n`;
201
+ content += `${moduleContent}\n`;
202
+ content += `% SECTION: MODULE_${calculationFile.name}_END\n\n`;
203
+ } catch (error) {
204
+ logger.warn(
205
+ `Failed to read module ${calculationFile.name}: ${error}`,
206
+ );
207
+ }
208
+ }
209
+ }
210
+ }
211
+ content += '% SECTION: MODULES_END';
212
+ return content;
213
+ }
214
+
215
+ // Generates logic programs related to reports.
216
+ private async generateReports() {
217
+ const reports = await this.project.reports();
218
+ let content = '';
219
+
220
+ for (const report of await Promise.all(
221
+ reports.map((r) => this.project.resource<ReportMetadata>(r.name)),
222
+ )) {
223
+ if (!report) continue;
224
+ content += createReportFacts(report);
225
+ }
226
+ return content;
227
+ }
228
+
229
+ // Generates logic programs related to templates (including their cards).
230
+ private async generateTemplates() {
231
+ const templates = await this.project.templates();
232
+ let content = '';
233
+
234
+ for (const template of await Promise.all(
235
+ templates.map((r) => this.project.resource<TemplateMetadata>(r.name)),
236
+ )) {
237
+ if (!template) continue;
238
+
239
+ let templateContent = createTemplateFacts(template);
240
+ const cards = await this.getCards(undefined, template.name);
241
+ for (const card of cards) {
242
+ const cardContent = await createCardFacts(card, this.project);
243
+ templateContent = templateContent.concat(cardContent);
244
+ }
245
+ content = content.concat(templateContent);
246
+ }
247
+ return content;
248
+ }
249
+
250
+ //
251
+ private async generateWorkFlows() {
252
+ const workflows = await this.project.workflows();
253
+ let content = '';
254
+
255
+ // loop through workflows
256
+ for (const workflow of await Promise.all(
257
+ workflows.map((m) => this.project.resource<Workflow>(m.name)),
258
+ )) {
259
+ if (!workflow) continue;
260
+
261
+ const workflowContent = createWorkflowFacts(workflow);
262
+ content += `% Workflow ${workflow.name}\n${workflowContent}\n\n`;
263
+ }
264
+
265
+ return content;
266
+ }
267
+
268
+ // Gets either all the cards (no parent), or a subtree.
269
+ private async getCards(
270
+ card: Card | undefined,
271
+ templateName?: string,
272
+ ): Promise<Card[]> {
273
+ let cards: Card[] = [];
274
+ if (!card) {
275
+ return templateName
276
+ ? this.project.templateCards(templateName)
277
+ : this.project.cards();
278
+ }
279
+ if (card.children) {
280
+ cards = flattenCardArray(card.children);
281
+ }
282
+ card.children = [];
283
+ cards.unshift(card);
284
+ return cards;
285
+ }
286
+
287
+ // Checks that Clingo successfully returned result.
288
+ private async parseClingoResult(
289
+ data: string[],
290
+ ): Promise<ParseResult<BaseResult>> {
291
+ const parser = new ClingoParser();
292
+ return parser.parseInput(data.join('\n'));
293
+ }
294
+
295
+ //
296
+ private async run(
297
+ data: {
298
+ query?: string;
299
+ },
300
+ argMode: 'graph' | 'query' = 'query',
301
+ ): Promise<string[]> {
302
+ if (!data.query) {
303
+ throw new Error('Must provide query to run a clingo program');
304
+ }
305
+
306
+ const res = await Calculate.mutex.runExclusive(async () => {
307
+ // For queries, use both base and queryLanguage
308
+ const basePrograms =
309
+ argMode === 'query'
310
+ ? [BASE_PROGRAM_KEY, QUERY_LANGUAGE_KEY]
311
+ : BASE_PROGRAM_KEY;
312
+
313
+ // Then solve with the program - need to pass the program as parameter
314
+ return solve(data.query as string, basePrograms);
315
+ });
316
+
317
+ logger.trace(
318
+ {
319
+ query: data.query,
320
+ },
321
+ `Ran Clingo solve command`,
322
+ );
323
+
324
+ if (res && res.answers && res.answers.length > 0) {
325
+ logger.trace({
326
+ result: res.answers,
327
+ });
328
+ return res.answers;
329
+ }
330
+
331
+ throw new Error('Failed to run Clingo solve. No answers returned.');
332
+ }
333
+
334
+ /**
335
+ * Generates a logic program.
336
+ * @param cardKey Optional, sub-card tree defining card
337
+ */
338
+ public async generate(cardKey?: string) {
339
+ await Calculate.mutex.runExclusive(async () => {
340
+ if (!this.modulesInitialized) {
341
+ await this.initializeModules();
342
+ this.modulesInitialized = true;
343
+ }
344
+
345
+ let card: Card | undefined;
346
+ if (cardKey) {
347
+ card = await this.project.findSpecificCard(cardKey, {
348
+ metadata: true,
349
+ children: true,
350
+ content: false,
351
+ parent: false,
352
+ });
353
+ if (!card) {
354
+ throw new Error(`Card '${cardKey}' not found`);
355
+ }
356
+ }
357
+ const calculations = await this.generateCalculations();
358
+ const cardTreeContent = await this.generateCardTreeContent(card);
359
+ const workFlows = await this.generateWorkFlows();
360
+ const cardTypes = await this.generateCardTypes();
361
+ const fieldTypes = await this.generateFieldTypes();
362
+ const linkTypes = await this.generateLinkTypes();
363
+ const reports = await this.generateReports();
364
+ const templates = await this.generateTemplates();
365
+
366
+ // Combine all resources
367
+ const resourcesProgram =
368
+ '% SECTION: RESOURCES_START\n' +
369
+ calculations +
370
+ workFlows +
371
+ cardTypes +
372
+ fieldTypes +
373
+ linkTypes +
374
+ reports +
375
+ templates +
376
+ '% SECTION: RESOURCES_END';
377
+
378
+ // Create base program content
379
+ const baseProgram =
380
+ '% SECTION: BASE_START\n' +
381
+ lpFiles.common.base +
382
+ '\n% SECTION: BASE_END\n\n' +
383
+ this.modules +
384
+ '\n\n' +
385
+ cardTreeContent +
386
+ '\n\n' +
387
+ resourcesProgram;
388
+
389
+ // Set the base programs separately
390
+ setBaseProgram(baseProgram, BASE_PROGRAM_KEY);
391
+ setBaseProgram(
392
+ '% SECTION: QUERY_LANGUAGE_START\n' +
393
+ lpFiles.common.queryLanguage +
394
+ '\n% SECTION: QUERY_LANGUAGE_END',
395
+ QUERY_LANGUAGE_KEY,
396
+ );
397
+
398
+ // Also store the base program (without query language) for updates
399
+ this.logicProgram = baseProgram;
400
+ });
401
+ }
402
+
403
+ /**
404
+ * When card changes, update the card specific calculations.
405
+ * @param changedCard Card that was changed.
406
+ */
407
+ public async handleCardChanged(changedCard: Card) {
408
+ await this.generate(changedCard.key);
409
+ return { statusCode: 200 };
410
+ }
411
+
412
+ /**
413
+ * When cards are removed, automatically remove card-specific calculations.
414
+ * @param deletedCard Card that is to be removed.
415
+ */
416
+ public async handleDeleteCard(deletedCard: Card) {
417
+ if (!deletedCard) {
418
+ return;
419
+ }
420
+
421
+ await Calculate.mutex.runExclusive(async () => {
422
+ const affectedCards = await this.getCards(deletedCard);
423
+
424
+ // Filter out deleted cards' content from the cardsProgram string
425
+ for (const card of affectedCards) {
426
+ // Remove card-specific content from the in-memory program using section markers
427
+ const cardSectionPattern = new RegExp(
428
+ `% SECTION: CARD_${card.key}_START[\\s\\S]*?% SECTION: CARD_${card.key}_END\\n\\n`,
429
+ 'g',
430
+ );
431
+ this.logicProgram = this.logicProgram.replace(cardSectionPattern, '');
432
+ }
433
+
434
+ // Update the base program after modifying logicProgram
435
+ setBaseProgram(this.logicProgram, BASE_PROGRAM_KEY);
436
+ });
437
+ }
438
+
439
+ /**
440
+ * When new cards are added, automatically calculate card-specific values.
441
+ * @param cards Added cards.
442
+ */
443
+ public async handleNewCards(cards: Card[]) {
444
+ if (!cards) {
445
+ return;
446
+ }
447
+
448
+ // Only proceed if we already have a logic program
449
+ if (!this.logicProgram) {
450
+ return;
451
+ }
452
+
453
+ await Calculate.mutex.runExclusive(async () => {
454
+ // Generate content for all cards (including new ones)
455
+ const cardTreeContent = await this.generateCardTreeContent(undefined);
456
+
457
+ // Update the main logic program with the new cards program
458
+ if (
459
+ this.logicProgram.includes('% SECTION: CARDS_START') &&
460
+ this.logicProgram.includes('% SECTION: CARDS_END')
461
+ ) {
462
+ this.logicProgram = this.logicProgram.replace(
463
+ /% SECTION: CARDS_START[\s\S]*?% SECTION: CARDS_END/,
464
+ cardTreeContent,
465
+ );
466
+ }
467
+
468
+ // Update the base program after modifying logicProgram
469
+ setBaseProgram(this.logicProgram, BASE_PROGRAM_KEY);
470
+ });
471
+
472
+ const cardKeys = cards.map((item) => item.key);
473
+ const queryResult = await this.creationQuery(cardKeys);
474
+ if (
475
+ !queryResult ||
476
+ queryResult.at(0) === undefined ||
477
+ queryResult.at(0)?.updateFields === undefined
478
+ ) {
479
+ return;
480
+ }
481
+
482
+ const fieldsToUpdate = queryResult.at(0)!.updateFields;
483
+ await CardMetadataUpdater.apply(this.project, fieldsToUpdate);
484
+ }
485
+
486
+ /**
487
+ * Runs given logic program and creates a graph using clingraph
488
+ * @param data Provide a query or/and a file which can be given to clingraph
489
+ * @param timeout Maximum amount of milliseconds clingraph is allowed to run
490
+ * @returns a base64 encoded image as a string
491
+ */
492
+ public async runGraph(
493
+ data: { query?: string; file?: string },
494
+ timeout?: number,
495
+ ) {
496
+ const clingoOutput = await this.run(data, 'graph');
497
+
498
+ // unlikely we ever get a collision
499
+ const randomId = generateRandomString(36, 20);
500
+
501
+ const clingGraphArgs = [
502
+ '--out=render',
503
+ '--format=svg',
504
+ '--type=digraph',
505
+ `--name-format=${randomId}`,
506
+ `--dir=${this.project.paths.tempFolder}`,
507
+ ];
508
+
509
+ // python is only used for windows
510
+ const pythonArgs = [
511
+ '-c',
512
+ `from clingraph import main; import sys; sys.argv = ["sys.argv[0]", ${clingGraphArgs
513
+ .map((arg) => `'${arg.replace(/\\/g, '\\\\')}'`)
514
+ .join(',')}]; sys.exit(main())`,
515
+ ];
516
+
517
+ const clingraph = spawnSync(this.pythonBinary, pythonArgs, {
518
+ encoding: 'utf8',
519
+ // below looks like it could be replaced by just joining dots, but
520
+ // it breaks it because elements contain newlines, which must be
521
+ // replaced by dots
522
+ input: clingoOutput.join('\n').replaceAll('\n', '.') + '.',
523
+ timeout,
524
+ maxBuffer: 1024 * 1024 * 100,
525
+ });
526
+
527
+ if (clingraph.status !== 0) {
528
+ throw new Error(`Graph: Failed to run clingraph ${clingraph.stderr}`);
529
+ }
530
+
531
+ const filePath = join(this.project.paths.tempFolder, randomId);
532
+
533
+ let fileData;
534
+ try {
535
+ fileData = await readFile(filePath + '.svg');
536
+ } catch (e) {
537
+ throw new Error(
538
+ `Graph: Failed to read image file after generating graph: ${e}`,
539
+ );
540
+ } finally {
541
+ await rm(filePath, { force: true });
542
+ await rm(filePath + '.svg', { force: true });
543
+ }
544
+
545
+ // Sanitizing SVG before returning it
546
+ return sanitizeSvgBase64(fileData);
547
+ }
548
+
549
+ /**
550
+ * Runs a logic program using clingo.
551
+ * @param filePath Path to a query file to be run in relation to current working directory
552
+ * @returns parsed program output
553
+ */
554
+ public async runLogicProgram(data: { query?: string; file?: string }) {
555
+ const clingoOutput = await this.run(data);
556
+
557
+ return this.parseClingoResult(clingoOutput);
558
+ }
559
+
560
+ /**
561
+ * Runs a pre-defined query.
562
+ * @param queryName Name of the query file without extension
563
+ * @param options Any object that contains state for handlebars
564
+ * @returns parsed program output
565
+ */
566
+ public async runQuery<T extends QueryName>(
567
+ queryName: T,
568
+ options?: unknown,
569
+ ): Promise<QueryResult<T>[]> {
570
+ let content = lpFiles.queries[queryName];
571
+ if (options && typeof options === 'object') {
572
+ const handlebars = Handlebars.create();
573
+ const compiled = handlebars.compile(content);
574
+ content = compiled(options);
575
+ }
576
+
577
+ const clingoOutput = await this.run(
578
+ {
579
+ query: content,
580
+ },
581
+ 'query',
582
+ );
583
+
584
+ const result = await this.parseClingoResult(clingoOutput);
585
+
586
+ if (result.error) {
587
+ throw new Error(result.error);
588
+ }
589
+ return result.results as QueryResult<T>[];
590
+ }
591
+ }