@comake/skl-js-engine 1.5.4 → 1.5.6

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/dist/JsExecutor/PermissionBuilder.d.ts +14 -0
  2. package/dist/JsExecutor/PermissionBuilder.d.ts.map +1 -0
  3. package/dist/JsExecutor/PermissionBuilder.js +58 -0
  4. package/dist/JsExecutor/PermissionBuilder.js.map +1 -0
  5. package/dist/JsExecutor/constants.d.ts +27 -0
  6. package/dist/JsExecutor/constants.d.ts.map +1 -0
  7. package/dist/JsExecutor/constants.js +30 -0
  8. package/dist/JsExecutor/constants.js.map +1 -0
  9. package/dist/JsExecutor/denoUtils.d.ts +10 -0
  10. package/dist/JsExecutor/denoUtils.d.ts.map +1 -0
  11. package/dist/JsExecutor/denoUtils.js +24 -0
  12. package/dist/JsExecutor/denoUtils.js.map +1 -0
  13. package/dist/JsExecutor/errors.d.ts +40 -0
  14. package/dist/JsExecutor/errors.d.ts.map +1 -0
  15. package/dist/JsExecutor/errors.js +67 -0
  16. package/dist/JsExecutor/errors.js.map +1 -0
  17. package/dist/JsExecutor/examples/basic/index.d.ts +1 -0
  18. package/dist/JsExecutor/examples/basic/index.d.ts.map +1 -0
  19. package/dist/JsExecutor/examples/basic/index.js +46 -0
  20. package/dist/JsExecutor/examples/basic/index.js.map +1 -0
  21. package/dist/JsExecutor/examples/basic/process.d.ts +1 -0
  22. package/dist/JsExecutor/examples/basic/process.d.ts.map +1 -0
  23. package/dist/JsExecutor/examples/basic/process.js +34 -0
  24. package/dist/JsExecutor/examples/basic/process.js.map +1 -0
  25. package/dist/JsExecutor/examples/jsExecutor/index.d.ts +2 -0
  26. package/dist/JsExecutor/examples/jsExecutor/index.d.ts.map +1 -0
  27. package/dist/JsExecutor/examples/jsExecutor/index.js +47 -0
  28. package/dist/JsExecutor/examples/jsExecutor/index.js.map +1 -0
  29. package/dist/JsExecutor/examples/jsExecutor/process.d.ts +2 -0
  30. package/dist/JsExecutor/examples/jsExecutor/process.d.ts.map +1 -0
  31. package/dist/JsExecutor/examples/jsExecutor/process.js +58 -0
  32. package/dist/JsExecutor/examples/jsExecutor/process.js.map +1 -0
  33. package/dist/JsExecutor/index.d.ts +7 -0
  34. package/dist/JsExecutor/index.d.ts.map +1 -0
  35. package/dist/JsExecutor/index.js +36 -0
  36. package/dist/JsExecutor/index.js.map +1 -0
  37. package/dist/JsExecutor/jsExecutor.d.ts +68 -0
  38. package/dist/JsExecutor/jsExecutor.d.ts.map +1 -0
  39. package/dist/JsExecutor/jsExecutor.js +171 -0
  40. package/dist/JsExecutor/jsExecutor.js.map +1 -0
  41. package/dist/JsExecutor/jsonRpc/JsonRpcClient.d.ts +165 -0
  42. package/dist/JsExecutor/jsonRpc/JsonRpcClient.d.ts.map +1 -0
  43. package/dist/JsExecutor/jsonRpc/JsonRpcClient.js +335 -0
  44. package/dist/JsExecutor/jsonRpc/JsonRpcClient.js.map +1 -0
  45. package/dist/JsExecutor/jsonRpc/JsonRpcServer.d.ts +125 -0
  46. package/dist/JsExecutor/jsonRpc/JsonRpcServer.d.ts.map +1 -0
  47. package/dist/JsExecutor/jsonRpc/JsonRpcServer.js +391 -0
  48. package/dist/JsExecutor/jsonRpc/JsonRpcServer.js.map +1 -0
  49. package/dist/JsExecutor/jsonRpc/index.d.ts +4 -0
  50. package/dist/JsExecutor/jsonRpc/index.d.ts.map +1 -0
  51. package/dist/JsExecutor/jsonRpc/index.js +23 -0
  52. package/dist/JsExecutor/jsonRpc/index.js.map +1 -0
  53. package/dist/JsExecutor/jsonRpc/types.d.ts +193 -0
  54. package/dist/JsExecutor/jsonRpc/types.d.ts.map +1 -0
  55. package/dist/JsExecutor/jsonRpc/types.js +37 -0
  56. package/dist/JsExecutor/jsonRpc/types.js.map +1 -0
  57. package/dist/JsExecutor/transport/Transport.d.ts +74 -0
  58. package/dist/JsExecutor/transport/Transport.d.ts.map +1 -0
  59. package/dist/JsExecutor/transport/Transport.js +14 -0
  60. package/dist/JsExecutor/transport/Transport.js.map +1 -0
  61. package/dist/JsExecutor/transport/base/BaseTransport.d.ts +51 -0
  62. package/dist/JsExecutor/transport/base/BaseTransport.d.ts.map +1 -0
  63. package/dist/JsExecutor/transport/base/BaseTransport.js +68 -0
  64. package/dist/JsExecutor/transport/base/BaseTransport.js.map +1 -0
  65. package/dist/JsExecutor/transport/index.d.ts +13 -0
  66. package/dist/JsExecutor/transport/index.d.ts.map +1 -0
  67. package/dist/JsExecutor/transport/index.js +36 -0
  68. package/dist/JsExecutor/transport/index.js.map +1 -0
  69. package/dist/JsExecutor/transport/process/ProcessManager.d.ts +96 -0
  70. package/dist/JsExecutor/transport/process/ProcessManager.d.ts.map +1 -0
  71. package/dist/JsExecutor/transport/process/ProcessManager.js +219 -0
  72. package/dist/JsExecutor/transport/process/ProcessManager.js.map +1 -0
  73. package/dist/JsExecutor/transport/stdio/ChildStdioTransport.d.ts +87 -0
  74. package/dist/JsExecutor/transport/stdio/ChildStdioTransport.d.ts.map +1 -0
  75. package/dist/JsExecutor/transport/stdio/ChildStdioTransport.js +219 -0
  76. package/dist/JsExecutor/transport/stdio/ChildStdioTransport.js.map +1 -0
  77. package/dist/JsExecutor/transport/stdio/ParentStdioTransport.d.ts +81 -0
  78. package/dist/JsExecutor/transport/stdio/ParentStdioTransport.d.ts.map +1 -0
  79. package/dist/JsExecutor/transport/stdio/ParentStdioTransport.js +259 -0
  80. package/dist/JsExecutor/transport/stdio/ParentStdioTransport.js.map +1 -0
  81. package/dist/JsExecutor/transport/utils/MessageUtils.d.ts +68 -0
  82. package/dist/JsExecutor/transport/utils/MessageUtils.d.ts.map +1 -0
  83. package/dist/JsExecutor/transport/utils/MessageUtils.js +135 -0
  84. package/dist/JsExecutor/transport/utils/MessageUtils.js.map +1 -0
  85. package/dist/JsExecutor/transport/utils/PollingUtils.d.ts +53 -0
  86. package/dist/JsExecutor/transport/utils/PollingUtils.d.ts.map +1 -0
  87. package/dist/JsExecutor/transport/utils/PollingUtils.js +92 -0
  88. package/dist/JsExecutor/transport/utils/PollingUtils.js.map +1 -0
  89. package/dist/JsExecutor/types.d.ts +113 -0
  90. package/dist/JsExecutor/types.d.ts.map +1 -0
  91. package/dist/JsExecutor/types.js +3 -0
  92. package/dist/JsExecutor/types.js.map +1 -0
  93. package/dist/SklEngine.d.ts +132 -0
  94. package/dist/SklEngine.d.ts.map +1 -0
  95. package/dist/SklEngine.js +1514 -0
  96. package/dist/SklEngine.js.map +1 -0
  97. package/dist/SklEngineOptions.d.ts +58 -0
  98. package/dist/SklEngineOptions.d.ts.map +1 -0
  99. package/dist/SklEngineOptions.js +3 -0
  100. package/dist/SklEngineOptions.js.map +1 -0
  101. package/dist/constants.d.ts +96 -0
  102. package/dist/constants.d.ts.map +1 -0
  103. package/dist/constants.js +103 -0
  104. package/dist/constants.js.map +1 -0
  105. package/dist/customCapabilities.d.ts +22 -0
  106. package/dist/customCapabilities.d.ts.map +1 -0
  107. package/dist/customCapabilities.js +45 -0
  108. package/dist/customCapabilities.js.map +1 -0
  109. package/dist/examples/customCapabilitiesExample.d.ts +2 -0
  110. package/dist/examples/customCapabilitiesExample.d.ts.map +1 -0
  111. package/dist/examples/customCapabilitiesExample.js +59 -0
  112. package/dist/examples/customCapabilitiesExample.js.map +1 -0
  113. package/dist/executor.js +216 -0
  114. package/dist/hooks/globalHooks.d.ts +50 -0
  115. package/dist/hooks/globalHooks.d.ts.map +1 -0
  116. package/dist/hooks/globalHooks.js +164 -0
  117. package/dist/hooks/globalHooks.js.map +1 -0
  118. package/dist/hooks/types.d.ts +9 -0
  119. package/dist/hooks/types.d.ts.map +1 -0
  120. package/dist/hooks/types.js +3 -0
  121. package/dist/hooks/types.js.map +1 -0
  122. package/dist/index.d.ts +41 -0
  123. package/dist/index.d.ts.map +1 -0
  124. package/dist/index.js +68 -0
  125. package/dist/index.js.map +1 -0
  126. package/dist/logger.d.ts +35 -0
  127. package/dist/logger.d.ts.map +1 -0
  128. package/dist/logger.js +119 -0
  129. package/dist/logger.js.map +1 -0
  130. package/dist/mapping/Mapper.d.ts +13 -0
  131. package/dist/mapping/Mapper.d.ts.map +1 -0
  132. package/dist/mapping/Mapper.js +64 -0
  133. package/dist/mapping/Mapper.js.map +1 -0
  134. package/dist/storage/FindOperator.d.ts +19 -0
  135. package/dist/storage/FindOperator.d.ts.map +1 -0
  136. package/dist/storage/FindOperator.js +23 -0
  137. package/dist/storage/FindOperator.js.map +1 -0
  138. package/dist/storage/FindOptionsTypes.d.ts +79 -0
  139. package/dist/storage/FindOptionsTypes.d.ts.map +1 -0
  140. package/dist/storage/FindOptionsTypes.js +3 -0
  141. package/dist/storage/FindOptionsTypes.js.map +1 -0
  142. package/dist/storage/GroupOptionTypes.d.ts +29 -0
  143. package/dist/storage/GroupOptionTypes.d.ts.map +1 -0
  144. package/dist/storage/GroupOptionTypes.js +3 -0
  145. package/dist/storage/GroupOptionTypes.js.map +1 -0
  146. package/dist/storage/operator/And.d.ts +4 -0
  147. package/dist/storage/operator/And.d.ts.map +1 -0
  148. package/dist/storage/operator/And.js +13 -0
  149. package/dist/storage/operator/And.js.map +1 -0
  150. package/dist/storage/operator/Contains.d.ts +3 -0
  151. package/dist/storage/operator/Contains.d.ts.map +1 -0
  152. package/dist/storage/operator/Contains.js +13 -0
  153. package/dist/storage/operator/Contains.js.map +1 -0
  154. package/dist/storage/operator/Equal.d.ts +5 -0
  155. package/dist/storage/operator/Equal.d.ts.map +1 -0
  156. package/dist/storage/operator/Equal.js +13 -0
  157. package/dist/storage/operator/Equal.js.map +1 -0
  158. package/dist/storage/operator/Exists.d.ts +3 -0
  159. package/dist/storage/operator/Exists.d.ts.map +1 -0
  160. package/dist/storage/operator/Exists.js +10 -0
  161. package/dist/storage/operator/Exists.js.map +1 -0
  162. package/dist/storage/operator/GreaterThan.d.ts +4 -0
  163. package/dist/storage/operator/GreaterThan.d.ts.map +1 -0
  164. package/dist/storage/operator/GreaterThan.js +13 -0
  165. package/dist/storage/operator/GreaterThan.js.map +1 -0
  166. package/dist/storage/operator/GreaterThanOrEqual.d.ts +4 -0
  167. package/dist/storage/operator/GreaterThanOrEqual.d.ts.map +1 -0
  168. package/dist/storage/operator/GreaterThanOrEqual.js +13 -0
  169. package/dist/storage/operator/GreaterThanOrEqual.js.map +1 -0
  170. package/dist/storage/operator/In.d.ts +4 -0
  171. package/dist/storage/operator/In.d.ts.map +1 -0
  172. package/dist/storage/operator/In.js +13 -0
  173. package/dist/storage/operator/In.js.map +1 -0
  174. package/dist/storage/operator/Inverse.d.ts +3 -0
  175. package/dist/storage/operator/Inverse.d.ts.map +1 -0
  176. package/dist/storage/operator/Inverse.js +13 -0
  177. package/dist/storage/operator/Inverse.js.map +1 -0
  178. package/dist/storage/operator/InversePath.d.ts +7 -0
  179. package/dist/storage/operator/InversePath.d.ts.map +1 -0
  180. package/dist/storage/operator/InversePath.js +13 -0
  181. package/dist/storage/operator/InversePath.js.map +1 -0
  182. package/dist/storage/operator/InverseRelation.d.ts +9 -0
  183. package/dist/storage/operator/InverseRelation.d.ts.map +1 -0
  184. package/dist/storage/operator/InverseRelation.js +13 -0
  185. package/dist/storage/operator/InverseRelation.js.map +1 -0
  186. package/dist/storage/operator/InverseRelationOrder.d.ts +8 -0
  187. package/dist/storage/operator/InverseRelationOrder.d.ts.map +1 -0
  188. package/dist/storage/operator/InverseRelationOrder.js +13 -0
  189. package/dist/storage/operator/InverseRelationOrder.js.map +1 -0
  190. package/dist/storage/operator/LessThan.d.ts +4 -0
  191. package/dist/storage/operator/LessThan.d.ts.map +1 -0
  192. package/dist/storage/operator/LessThan.js +13 -0
  193. package/dist/storage/operator/LessThan.js.map +1 -0
  194. package/dist/storage/operator/LessThanOrEqual.d.ts +4 -0
  195. package/dist/storage/operator/LessThanOrEqual.d.ts.map +1 -0
  196. package/dist/storage/operator/LessThanOrEqual.js +13 -0
  197. package/dist/storage/operator/LessThanOrEqual.js.map +1 -0
  198. package/dist/storage/operator/Not.d.ts +3 -0
  199. package/dist/storage/operator/Not.d.ts.map +1 -0
  200. package/dist/storage/operator/Not.js +13 -0
  201. package/dist/storage/operator/Not.js.map +1 -0
  202. package/dist/storage/operator/OneOrMorePath.d.ts +7 -0
  203. package/dist/storage/operator/OneOrMorePath.d.ts.map +1 -0
  204. package/dist/storage/operator/OneOrMorePath.js +13 -0
  205. package/dist/storage/operator/OneOrMorePath.js.map +1 -0
  206. package/dist/storage/operator/Or.d.ts +4 -0
  207. package/dist/storage/operator/Or.d.ts.map +1 -0
  208. package/dist/storage/operator/Or.js +13 -0
  209. package/dist/storage/operator/Or.js.map +1 -0
  210. package/dist/storage/operator/Sequence.d.ts +3 -0
  211. package/dist/storage/operator/Sequence.d.ts.map +1 -0
  212. package/dist/storage/operator/Sequence.js +13 -0
  213. package/dist/storage/operator/Sequence.js.map +1 -0
  214. package/dist/storage/operator/SequencePath.d.ts +7 -0
  215. package/dist/storage/operator/SequencePath.d.ts.map +1 -0
  216. package/dist/storage/operator/SequencePath.js +13 -0
  217. package/dist/storage/operator/SequencePath.js.map +1 -0
  218. package/dist/storage/operator/ZeroOrMorePath.d.ts +7 -0
  219. package/dist/storage/operator/ZeroOrMorePath.d.ts.map +1 -0
  220. package/dist/storage/operator/ZeroOrMorePath.js +13 -0
  221. package/dist/storage/operator/ZeroOrMorePath.js.map +1 -0
  222. package/dist/storage/query-adapter/QueryAdapter.d.ts +99 -0
  223. package/dist/storage/query-adapter/QueryAdapter.d.ts.map +1 -0
  224. package/dist/storage/query-adapter/QueryAdapter.js +3 -0
  225. package/dist/storage/query-adapter/QueryAdapter.js.map +1 -0
  226. package/dist/storage/query-adapter/sparql/SparqlQueryAdapter.d.ts +40 -0
  227. package/dist/storage/query-adapter/sparql/SparqlQueryAdapter.d.ts.map +1 -0
  228. package/dist/storage/query-adapter/sparql/SparqlQueryAdapter.js +315 -0
  229. package/dist/storage/query-adapter/sparql/SparqlQueryAdapter.js.map +1 -0
  230. package/dist/storage/query-adapter/sparql/SparqlQueryAdapterOptions.d.ts +34 -0
  231. package/dist/storage/query-adapter/sparql/SparqlQueryAdapterOptions.d.ts.map +1 -0
  232. package/dist/storage/query-adapter/sparql/SparqlQueryAdapterOptions.js +3 -0
  233. package/dist/storage/query-adapter/sparql/SparqlQueryAdapterOptions.js.map +1 -0
  234. package/dist/storage/query-adapter/sparql/SparqlQueryBuilder.d.ts +104 -0
  235. package/dist/storage/query-adapter/sparql/SparqlQueryBuilder.d.ts.map +1 -0
  236. package/dist/storage/query-adapter/sparql/SparqlQueryBuilder.js +1210 -0
  237. package/dist/storage/query-adapter/sparql/SparqlQueryBuilder.js.map +1 -0
  238. package/dist/storage/query-adapter/sparql/SparqlUpdateBuilder.d.ts +44 -0
  239. package/dist/storage/query-adapter/sparql/SparqlUpdateBuilder.d.ts.map +1 -0
  240. package/dist/storage/query-adapter/sparql/SparqlUpdateBuilder.js +397 -0
  241. package/dist/storage/query-adapter/sparql/SparqlUpdateBuilder.js.map +1 -0
  242. package/dist/storage/query-adapter/sparql/VariableGenerator.d.ts +5 -0
  243. package/dist/storage/query-adapter/sparql/VariableGenerator.d.ts.map +1 -0
  244. package/dist/storage/query-adapter/sparql/VariableGenerator.js +14 -0
  245. package/dist/storage/query-adapter/sparql/VariableGenerator.js.map +1 -0
  246. package/dist/storage/query-adapter/sparql/query-executor/InMemorySparqlQueryExecutor.d.ts +19 -0
  247. package/dist/storage/query-adapter/sparql/query-executor/InMemorySparqlQueryExecutor.d.ts.map +1 -0
  248. package/dist/storage/query-adapter/sparql/query-executor/InMemorySparqlQueryExecutor.js +90 -0
  249. package/dist/storage/query-adapter/sparql/query-executor/InMemorySparqlQueryExecutor.js.map +1 -0
  250. package/dist/storage/query-adapter/sparql/query-executor/SparqlEndpointQueryExecutor.d.ts +27 -0
  251. package/dist/storage/query-adapter/sparql/query-executor/SparqlEndpointQueryExecutor.d.ts.map +1 -0
  252. package/dist/storage/query-adapter/sparql/query-executor/SparqlEndpointQueryExecutor.js +116 -0
  253. package/dist/storage/query-adapter/sparql/query-executor/SparqlEndpointQueryExecutor.js.map +1 -0
  254. package/dist/storage/query-adapter/sparql/query-executor/SparqlQueryExecutor.d.ts +34 -0
  255. package/dist/storage/query-adapter/sparql/query-executor/SparqlQueryExecutor.d.ts.map +1 -0
  256. package/dist/storage/query-adapter/sparql/query-executor/SparqlQueryExecutor.js +3 -0
  257. package/dist/storage/query-adapter/sparql/query-executor/SparqlQueryExecutor.js.map +1 -0
  258. package/dist/tools/explain-findall-sparql.d.ts +2 -0
  259. package/dist/tools/explain-findall-sparql.d.ts.map +1 -0
  260. package/dist/tools/explain-findall-sparql.js +303 -0
  261. package/dist/tools/explain-findall-sparql.js.map +1 -0
  262. package/dist/util/PerformanceLogger.d.ts +28 -0
  263. package/dist/util/PerformanceLogger.d.ts.map +1 -0
  264. package/dist/util/PerformanceLogger.js +223 -0
  265. package/dist/util/PerformanceLogger.js.map +1 -0
  266. package/dist/util/ReadCacheHelper.d.ts +14 -0
  267. package/dist/util/ReadCacheHelper.d.ts.map +1 -0
  268. package/dist/util/ReadCacheHelper.js +61 -0
  269. package/dist/util/ReadCacheHelper.js.map +1 -0
  270. package/dist/util/SparqlUtil.d.ts +72 -0
  271. package/dist/util/SparqlUtil.d.ts.map +1 -0
  272. package/dist/util/SparqlUtil.js +456 -0
  273. package/dist/util/SparqlUtil.js.map +1 -0
  274. package/dist/util/TripleUtil.d.ts +10 -0
  275. package/dist/util/TripleUtil.d.ts.map +1 -0
  276. package/dist/util/TripleUtil.js +431 -0
  277. package/dist/util/TripleUtil.js.map +1 -0
  278. package/dist/util/Types.d.ts +275 -0
  279. package/dist/util/Types.d.ts.map +1 -0
  280. package/dist/util/Types.js +3 -0
  281. package/dist/util/Types.js.map +1 -0
  282. package/dist/util/Util.d.ts +26 -0
  283. package/dist/util/Util.d.ts.map +1 -0
  284. package/dist/util/Util.js +138 -0
  285. package/dist/util/Util.js.map +1 -0
  286. package/dist/util/Vocabularies/Shared.d.ts +13 -0
  287. package/dist/util/Vocabularies/Shared.d.ts.map +1 -0
  288. package/dist/util/Vocabularies/Shared.js +96 -0
  289. package/dist/util/Vocabularies/Shared.js.map +1 -0
  290. package/dist/util/Vocabularies/helper.d.ts +5 -0
  291. package/dist/util/Vocabularies/helper.d.ts.map +1 -0
  292. package/dist/util/Vocabularies/helper.js +10 -0
  293. package/dist/util/Vocabularies/helper.js.map +1 -0
  294. package/dist/util/Vocabularies/index.d.ts +8 -0
  295. package/dist/util/Vocabularies/index.d.ts.map +1 -0
  296. package/dist/util/Vocabularies/index.js +114 -0
  297. package/dist/util/Vocabularies/index.js.map +1 -0
  298. package/dist/util/safeJsonStringify.d.ts +1 -0
  299. package/dist/util/safeJsonStringify.d.ts.map +1 -0
  300. package/dist/util/safeJsonStringify.js +19 -0
  301. package/dist/util/safeJsonStringify.js.map +1 -0
  302. package/package.json +1 -1
  303. package/src/SklEngine.ts +6 -0
  304. package/src/storage/query-adapter/sparql/SparqlUpdateBuilder.ts +106 -1
  305. package/src/util/TripleUtil.ts +19 -0
  306. package/src/util/Types.ts +6 -0
@@ -0,0 +1,1210 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.SparqlQueryBuilder = void 0;
7
+ /* eslint-disable no-inline-comments */
8
+ /* eslint-disable line-comment-position */
9
+ /* eslint-disable @typescript-eslint/prefer-nullish-coalescing */
10
+ /* eslint-disable @typescript-eslint/no-shadow */
11
+ /* eslint-disable @typescript-eslint/naming-convention */
12
+ /* eslint-disable multiline-comment-style */
13
+ /* eslint-disable capitalized-comments */
14
+ /* eslint-disable indent */
15
+ const data_model_1 = __importDefault(require("@rdfjs/data-model"));
16
+ const constants_1 = require("../../../constants");
17
+ const PerformanceLogger_1 = require("../../../util/PerformanceLogger");
18
+ const SparqlUtil_1 = require("../../../util/SparqlUtil");
19
+ const TripleUtil_1 = require("../../../util/TripleUtil");
20
+ const Util_1 = require("../../../util/Util");
21
+ const Vocabularies_1 = require("../../../util/Vocabularies");
22
+ const FindOperator_1 = require("../../FindOperator");
23
+ const VariableGenerator_1 = require("./VariableGenerator");
24
+ class SparqlQueryBuilder {
25
+ constructor() {
26
+ this.variableGenerator = new VariableGenerator_1.VariableGenerator();
27
+ }
28
+ buildEntitySelectPatternsFromOptions(subject, options) {
29
+ const span = PerformanceLogger_1.PerformanceLogger.startSpan('QueryBuilder.buildSelect', {
30
+ hasWhere: !!options?.where,
31
+ hasRelations: !!options?.relations
32
+ });
33
+ try {
34
+ const relations = options?.select ? undefined : options?.relations;
35
+ const whereQueryData = this.createWhereQueryData(subject, options?.where, true);
36
+ const orderQueryData = this.createOrderQueryData(subject, options?.order);
37
+ const relationsQueryData = this.createRelationsQueryData(subject, relations);
38
+ // Handle subqueries
39
+ if (options?.subQueries && options.subQueries.length > 0) {
40
+ const subQueryPatterns = this.createSubQueryPatterns(options.subQueries);
41
+ whereQueryData.values.unshift(...subQueryPatterns);
42
+ }
43
+ const patterns = whereQueryData.values;
44
+ if (whereQueryData.triples.length === 0 &&
45
+ (whereQueryData.filters.length > 0 ||
46
+ orderQueryData.triples.length > 0 ||
47
+ (whereQueryData.values.length === 0 &&
48
+ whereQueryData.graphValues.length === 0 &&
49
+ whereQueryData.graphTriples.length === 0))) {
50
+ if (relationsQueryData.unionPatterns.length > 0) {
51
+ /* relationsQueryData.unionPatterns.push(
52
+ createSparqlGraphPattern(subject, [ createSparqlBasicGraphPattern([ entityGraphTriple ]) ])
53
+ ); */
54
+ }
55
+ else {
56
+ const entityGraphFilterPattern = this.createEntityGraphFilterPattern(subject);
57
+ // patterns.push(createSparqlGraphPattern(subject, [ createSparqlBasicGraphPattern([ entityGraphTriple ]) ]));
58
+ patterns.push(entityGraphFilterPattern);
59
+ }
60
+ }
61
+ else if (!options?.where?.id) {
62
+ const entityGraphFilterPattern = this.createEntityGraphFilterPattern(subject);
63
+ const entityIsGraphFilter = (0, SparqlUtil_1.createSparqlExistsOperation)([entityGraphFilterPattern]);
64
+ whereQueryData.filters.push(entityIsGraphFilter);
65
+ }
66
+ // Add union patterns to the patterns
67
+ if (relationsQueryData.unionPatterns.length > 0) {
68
+ patterns.push((0, SparqlUtil_1.createSparqlUnion)(relationsQueryData.unionPatterns));
69
+ }
70
+ const wherePatterns = this.createWherePatternsFromQueryData(patterns, whereQueryData.triples, whereQueryData.filters, orderQueryData.triples, orderQueryData.filters, whereQueryData.patterns ?? [], undefined, whereQueryData.binds);
71
+ // For ID-only queries, we need to include union patterns in graphWhere
72
+ // because that's what gets used in the CONSTRUCT query
73
+ const graphWhereRelationsPatterns = relationsQueryData.unionPatterns.length > 0
74
+ ? [(0, SparqlUtil_1.createSparqlUnion)(relationsQueryData.unionPatterns), ...relationsQueryData.patterns]
75
+ : relationsQueryData.patterns;
76
+ const graphWherePatterns = this.createWherePatternsFromQueryData(whereQueryData.graphValues, whereQueryData.graphTriples, whereQueryData.graphFilters, undefined, undefined, graphWhereRelationsPatterns);
77
+ // Create variables for each order expression and update the orders to use them
78
+ const selectVariables = orderQueryData.orders.map(order => {
79
+ const variable = this.createVariable();
80
+ return {
81
+ variable,
82
+ expression: order.expression
83
+ };
84
+ });
85
+ const orders = selectVariables.map((selectVar, index) => ({
86
+ expression: selectVar.variable,
87
+ descending: orderQueryData.orders[index].descending
88
+ }));
89
+ if (orders.length === 0) {
90
+ orders.push({
91
+ expression: SparqlUtil_1.entityVariable
92
+ });
93
+ }
94
+ selectVariables.push(...(whereQueryData.selectVariables ?? []));
95
+ const isRelationsQueryDataEmpty = relationsQueryData.unionPatterns.length === 0 && relationsQueryData.patterns.length === 0 && relationsQueryData.selectionTriples.length === 0;
96
+ const returnData = {
97
+ where: wherePatterns,
98
+ orders,
99
+ ...orderQueryData.groupByParent ? { group: subject } : {},
100
+ graphWhere: graphWherePatterns,
101
+ graphSelectionTriples: relationsQueryData.selectionTriples,
102
+ ...isRelationsQueryDataEmpty ? {} : { relationsQueryData }
103
+ };
104
+ if (selectVariables.length > 0) {
105
+ returnData.selectVariables = selectVariables;
106
+ }
107
+ PerformanceLogger_1.PerformanceLogger.endSpan(span, {
108
+ patternCount: wherePatterns.length,
109
+ tripleCount: whereQueryData.triples.length,
110
+ hasFilters: whereQueryData.filters.length > 0,
111
+ hasOrders: orders.length > 0
112
+ });
113
+ return returnData;
114
+ }
115
+ catch (error) {
116
+ PerformanceLogger_1.PerformanceLogger.endSpan(span, { error: true });
117
+ throw error;
118
+ }
119
+ }
120
+ createSubQueryPatterns(subQueries) {
121
+ return subQueries.map((subQuery) => {
122
+ const subQueryWhere = this.createWhereQueryData(SparqlUtil_1.entityVariable, subQuery.where);
123
+ const queryGroup = [];
124
+ if (subQuery.groupBy && Array.isArray(subQuery.groupBy)) {
125
+ subQuery.groupBy.forEach((group) => {
126
+ queryGroup.push({
127
+ expression: data_model_1.default.variable(group)
128
+ });
129
+ });
130
+ }
131
+ const selectQuery = {
132
+ type: 'query',
133
+ queryType: 'SELECT',
134
+ variables: subQuery.select,
135
+ where: this.createWherePatternsFromQueryData(subQueryWhere.values, subQueryWhere.triples, subQueryWhere.filters, undefined, undefined, subQueryWhere.patterns ?? []),
136
+ group: queryGroup.length > 0 ? queryGroup : undefined,
137
+ having: subQuery.having ? this.createWhereQueryData(SparqlUtil_1.entityVariable, subQuery.having).filters : undefined,
138
+ prefixes: {}
139
+ };
140
+ return (0, SparqlUtil_1.createSparqlSelectGroup)([selectQuery]);
141
+ });
142
+ }
143
+ createEntityGraphFilterPattern(subject) {
144
+ const entityFilterTriple = { subject, predicate: this.createVariable(), object: this.createVariable() };
145
+ return (0, SparqlUtil_1.createSparqlGraphPattern)(subject, [(0, SparqlUtil_1.createSparqlBasicGraphPattern)([entityFilterTriple])]);
146
+ }
147
+ buildConstructFromEntitySelectQuery(graphWhere, graphSelectionTriples, select, selectVariables) {
148
+ const span = PerformanceLogger_1.PerformanceLogger.startSpan('QueryBuilder.buildConstruct', { hasSelect: !!select });
149
+ try {
150
+ let triples;
151
+ let where = [];
152
+ if (select) {
153
+ triples = this.createSelectPattern(select, SparqlUtil_1.entityVariable);
154
+ where = [(0, SparqlUtil_1.createSparqlOptional)([(0, SparqlUtil_1.createSparqlBasicGraphPattern)(triples)]), ...graphWhere];
155
+ }
156
+ else {
157
+ triples = [SparqlUtil_1.entityGraphTriple, ...graphSelectionTriples];
158
+ /* Skip if the where contains a union pattern */
159
+ if (graphWhere.some(pattern => pattern.type === 'union')) {
160
+ where = graphWhere;
161
+ }
162
+ else {
163
+ where = [
164
+ ...graphWhere,
165
+ (0, SparqlUtil_1.createSparqlGraphPattern)(SparqlUtil_1.entityVariable, [(0, SparqlUtil_1.createSparqlBasicGraphPattern)([SparqlUtil_1.entityGraphTriple])])
166
+ ];
167
+ }
168
+ }
169
+ // // Add select variables to the query
170
+ // if (selectVariables?.length) {
171
+ // where = [
172
+ // ...where,
173
+ // ...selectVariables.map(({ variable, expression }) => ({
174
+ // type: 'bind' as const,
175
+ // expression,
176
+ // variable,
177
+ // })),
178
+ // ];
179
+ // }
180
+ const result = (0, SparqlUtil_1.createSparqlConstructQuery)(triples, where);
181
+ PerformanceLogger_1.PerformanceLogger.endSpan(span, { tripleCount: triples.length, patternCount: where.length });
182
+ return result;
183
+ }
184
+ catch (error) {
185
+ PerformanceLogger_1.PerformanceLogger.endSpan(span, { error: true });
186
+ throw error;
187
+ }
188
+ }
189
+ createWhereQueryData(subject, where, isTopLevel = false) {
190
+ // Optimization: if the top-level where contains an ID constraint plus additional filters,
191
+ // also push the ID constraint into graphWhere. This allows the outer CONSTRUCT query
192
+ // to be restricted by VALUES (?entity) which can be substantially faster on some SPARQL backends.
193
+ //
194
+ // Non-breaking: keep the existing behavior where `id` participates in the main WHERE too.
195
+ if (isTopLevel && where && 'id' in where && Object.keys(where).length > 1) {
196
+ const { id, ...restWhere } = where;
197
+ if (id !== undefined) {
198
+ const idQueryData = this.createWhereQueryDataForIdValue(subject, id);
199
+ const restQueryData = this.createWhereQueryData(subject, restWhere, false);
200
+ return {
201
+ ...restQueryData,
202
+ values: [...idQueryData.values, ...restQueryData.values],
203
+ filters: [...idQueryData.filters, ...restQueryData.filters],
204
+ triples: [...idQueryData.triples, ...restQueryData.triples],
205
+ graphValues: [...idQueryData.values, ...restQueryData.graphValues],
206
+ graphFilters: [...idQueryData.filters, ...restQueryData.graphFilters],
207
+ graphTriples: [...idQueryData.triples, ...restQueryData.graphTriples]
208
+ };
209
+ }
210
+ }
211
+ if (isTopLevel && Object.keys(where ?? {}).length === 1 && 'id' in where) {
212
+ const { values, filters, triples } = this.createWhereQueryDataForIdValue(subject, where.id);
213
+ return {
214
+ values: [],
215
+ filters: [],
216
+ triples: [],
217
+ graphValues: values,
218
+ graphFilters: filters,
219
+ graphTriples: triples,
220
+ binds: []
221
+ };
222
+ }
223
+ // Handle binds if specified in where options
224
+ const binds = [];
225
+ if (where?.binds) {
226
+ binds.push(...where.binds.map((bind) => ({
227
+ type: 'bind',
228
+ expression: bind.expression,
229
+ variable: bind.variable
230
+ })));
231
+ // Delete binds from where as it's a special key
232
+ const { binds: _, ...restWhere } = where;
233
+ where = restWhere;
234
+ }
235
+ const whereQueryData = Object.entries(where ?? {}).reduce((obj, [key, value]) => {
236
+ const whereQueryDataForField = this.createWhereQueryDataForField(subject, key, value);
237
+ return {
238
+ values: [...obj.values, ...whereQueryDataForField.values],
239
+ triples: [...obj.triples, ...whereQueryDataForField.triples],
240
+ filters: [...obj.filters, ...whereQueryDataForField.filters],
241
+ patterns: [...obj.patterns ?? [], ...whereQueryDataForField.patterns ?? []],
242
+ binds: [...obj.binds ?? [], ...whereQueryDataForField.binds ?? []]
243
+ };
244
+ }, { values: [], triples: [], filters: [], patterns: [], binds });
245
+ return {
246
+ ...whereQueryData,
247
+ graphValues: [],
248
+ graphFilters: [],
249
+ graphTriples: [],
250
+ patterns: whereQueryData.patterns ?? [],
251
+ binds: whereQueryData.binds ?? []
252
+ };
253
+ }
254
+ createWhereQueryDataForField(subject, field, value) {
255
+ if (field === 'id') {
256
+ return this.createWhereQueryDataForIdValue(subject, value);
257
+ }
258
+ if (field === 'type') {
259
+ return this.createWhereQueryDataForType(subject, value);
260
+ }
261
+ if (field === 'and') {
262
+ return this.createWhereQueryDataForAndClause(subject, value);
263
+ }
264
+ if (field === 'or') {
265
+ return this.createWhereQueryDataForOrClause(subject, value);
266
+ }
267
+ const predicate = data_model_1.default.namedNode(field);
268
+ return this.createWhereQueryDataFromKeyValue(subject, predicate, value);
269
+ }
270
+ createWhereQueryDataForAndClause(subject, whereClauses) {
271
+ // Combine all nested where conditions - all must match
272
+ return whereClauses.reduce((acc, nestedWhere) => {
273
+ const nestedData = this.createWhereQueryData(subject, nestedWhere);
274
+ return {
275
+ values: [...acc.values, ...nestedData.values],
276
+ triples: [...acc.triples, ...nestedData.triples],
277
+ filters: [...acc.filters, ...nestedData.filters],
278
+ patterns: [...acc.patterns ?? [], ...nestedData.patterns ?? []],
279
+ binds: [...acc.binds ?? [], ...nestedData.binds ?? []]
280
+ };
281
+ }, { values: [], triples: [], filters: [], patterns: [], binds: [] });
282
+ }
283
+ createWhereQueryDataForOrClause(subject, whereClauses) {
284
+ // Create UNION patterns - any condition can match
285
+ const unionPatterns = whereClauses.map((nestedWhere) => {
286
+ const nestedData = this.createWhereQueryData(subject, nestedWhere);
287
+ const patterns = [];
288
+ // Add values patterns
289
+ if (nestedData.values.length > 0) {
290
+ patterns.push(...nestedData.values);
291
+ }
292
+ // Add triples as basic graph pattern
293
+ if (nestedData.triples.length > 0) {
294
+ patterns.push((0, SparqlUtil_1.createSparqlBasicGraphPattern)(nestedData.triples));
295
+ }
296
+ // Add nested patterns (for deeply nested and/or)
297
+ if (nestedData.patterns && nestedData.patterns.length > 0) {
298
+ patterns.push(...nestedData.patterns);
299
+ }
300
+ // Add filters
301
+ if (nestedData.filters.length > 0) {
302
+ patterns.push((0, SparqlUtil_1.createFilterPatternFromFilters)(nestedData.filters));
303
+ }
304
+ // Add binds
305
+ if (nestedData.binds && nestedData.binds.length > 0) {
306
+ patterns.push(...nestedData.binds);
307
+ }
308
+ return (0, SparqlUtil_1.createSparqlSelectGroup)(patterns);
309
+ });
310
+ return {
311
+ values: [],
312
+ triples: [],
313
+ filters: [],
314
+ patterns: [(0, SparqlUtil_1.createSparqlUnion)(unionPatterns)],
315
+ binds: []
316
+ };
317
+ }
318
+ createWhereQueryDataForIdValue(term, value) {
319
+ let filters = [];
320
+ let values = [];
321
+ let triples = [];
322
+ if (FindOperator_1.FindOperator.isFindOperator(value)) {
323
+ ({ filters, values, triples } = this.resolveFindOperatorAsExpressionForId(term, value));
324
+ }
325
+ else {
326
+ values = [
327
+ {
328
+ type: 'values',
329
+ values: [
330
+ {
331
+ [`?${term.value}`]: data_model_1.default.namedNode(value)
332
+ }
333
+ ]
334
+ }
335
+ ];
336
+ }
337
+ return {
338
+ values,
339
+ filters,
340
+ triples
341
+ };
342
+ }
343
+ createWhereQueryDataForType(subject, value) {
344
+ if (FindOperator_1.FindOperator.isFindOperator(value)) {
345
+ if (value.operator === 'and') {
346
+ // For AND on types, generate a triple for each type
347
+ const typeValues = value.value;
348
+ // Create a triple for each value in the array, safely converting to string
349
+ const triples = typeValues.map((typeVal) => {
350
+ const typeStr = typeof typeVal === 'string' ? typeVal : typeVal?.toString() || '';
351
+ return {
352
+ subject,
353
+ predicate: SparqlUtil_1.allTypesAndSuperTypesPath,
354
+ object: data_model_1.default.namedNode(typeStr)
355
+ };
356
+ });
357
+ return {
358
+ values: [],
359
+ filters: [],
360
+ triples: [
361
+ ...triples
362
+ ]
363
+ };
364
+ }
365
+ if (value.operator === 'inverse') {
366
+ const inversePredicate = (0, SparqlUtil_1.createSparqlInversePredicate)([SparqlUtil_1.allTypesAndSuperTypesPath]);
367
+ const inverseWhereQueryData = this.createWhereQueryDataFromKeyValue(subject, inversePredicate, value.value);
368
+ const variable = this.createVariable();
369
+ return {
370
+ values: inverseWhereQueryData.values,
371
+ filters: inverseWhereQueryData.filters,
372
+ triples: inverseWhereQueryData.triples
373
+ };
374
+ }
375
+ if (value.operator === 'sequence') {
376
+ const sequencePredicate = (0, SparqlUtil_1.createSparqlSequencePredicate)([SparqlUtil_1.allTypesAndSuperTypesPath]);
377
+ return this.createWhereQueryDataFromKeyValue(subject, sequencePredicate, value.value);
378
+ }
379
+ if (Array.isArray(value)) {
380
+ const triples = value.map(typeVal => ({
381
+ subject,
382
+ predicate: SparqlUtil_1.allTypesAndSuperTypesPath,
383
+ object: typeof typeVal === 'string' ? data_model_1.default.namedNode(typeVal) : data_model_1.default.namedNode(typeVal.toString())
384
+ }));
385
+ return {
386
+ values: [],
387
+ filters: [],
388
+ triples: [
389
+ ...triples
390
+ ]
391
+ };
392
+ }
393
+ const variable = this.createVariable();
394
+ const triple = { subject, predicate: SparqlUtil_1.allTypesAndSuperTypesPath, object: variable };
395
+ const { filter, valuePattern, tripleInFilter } = this.resolveFindOperatorAsExpressionWithMultipleValues(variable, value, triple);
396
+ return {
397
+ values: valuePattern ? [valuePattern] : [],
398
+ filters: filter ? [filter] : [],
399
+ triples: tripleInFilter ? [] : [triple]
400
+ };
401
+ }
402
+ return {
403
+ values: [],
404
+ filters: [],
405
+ triples: [
406
+ {
407
+ subject,
408
+ predicate: SparqlUtil_1.allTypesAndSuperTypesPath,
409
+ object: data_model_1.default.namedNode(value)
410
+ }
411
+ ]
412
+ };
413
+ }
414
+ createWhereQueryDataFromKeyValue(subject, predicate, value) {
415
+ if (Array.isArray(value) && FindOperator_1.FindOperator.isFindOperator(value[0])) {
416
+ return this.createWhereQueryDataForMultipleFindOperators(subject, predicate, value);
417
+ }
418
+ if (FindOperator_1.FindOperator.isFindOperator(value)) {
419
+ return this.createWhereQueryDataForFindOperator(subject, predicate, value);
420
+ }
421
+ if (Array.isArray(value)) {
422
+ return value.reduce((obj, valueItem) => {
423
+ const valueWhereQueryData = this.createWhereQueryDataFromKeyValue(subject, predicate, valueItem);
424
+ return {
425
+ values: [...obj.values, ...valueWhereQueryData.values],
426
+ filters: [...obj.filters, ...valueWhereQueryData.filters],
427
+ triples: [...obj.triples, ...valueWhereQueryData.triples],
428
+ patterns: [...obj.patterns ?? [], ...valueWhereQueryData.patterns ?? []]
429
+ };
430
+ }, { values: [], filters: [], triples: [], patterns: [] });
431
+ }
432
+ if (typeof value === 'object') {
433
+ if ('@value' in value) {
434
+ return this.createWhereQueryDataForValueObject('subject' in value ? value.subject : subject, predicate, value);
435
+ }
436
+ return this.createWhereQueryDataForNestedWhere(subject, predicate, value);
437
+ }
438
+ const term = this.resolveValueToTerm(value);
439
+ return {
440
+ values: [],
441
+ filters: [],
442
+ triples: [{ subject, predicate, object: term }]
443
+ };
444
+ }
445
+ createWhereQueryDataForFindOperator(subject, predicate, operator) {
446
+ if (operator.operator === 'inverse') {
447
+ const inversePredicate = (0, SparqlUtil_1.createSparqlInversePredicate)([predicate]);
448
+ return this.createWhereQueryDataFromKeyValue(operator.subject ? operator.subject : subject, inversePredicate, operator.value);
449
+ }
450
+ if (operator.operator === 'sequence') {
451
+ const sequencePredicate = (0, SparqlUtil_1.createSparqlSequencePredicate)([predicate]);
452
+ return this.createWhereQueryDataFromKeyValue(operator.subject ? operator.subject : subject, sequencePredicate, operator.value);
453
+ }
454
+ if (FindOperator_1.FindOperator.isPathOperator(operator)) {
455
+ const pathPredicate = this.pathOperatorToPropertyPath(operator);
456
+ const combinedPredicate = (0, SparqlUtil_1.createSparqlSequencePredicate)([predicate, pathPredicate]);
457
+ return this.createWhereQueryDataFromKeyValue(subject, combinedPredicate, operator.value.value);
458
+ }
459
+ const variable = this.createVariable();
460
+ const triple = { subject, predicate, object: variable };
461
+ const { filter, valuePattern, tripleInFilter } = this.resolveFindOperatorAsExpressionWithMultipleValues(variable, operator, triple);
462
+ return {
463
+ values: valuePattern ? [valuePattern] : [],
464
+ filters: filter ? [filter] : [],
465
+ triples: tripleInFilter ? [] : [triple]
466
+ };
467
+ }
468
+ pathOperatorToPropertyPath(operator) {
469
+ if (operator.operator === 'inversePath') {
470
+ let subPredicate;
471
+ const { subPath } = operator.value;
472
+ if (typeof subPath === 'string') {
473
+ subPredicate = data_model_1.default.namedNode(subPath);
474
+ }
475
+ else {
476
+ subPredicate = this.pathOperatorToPropertyPath(subPath);
477
+ }
478
+ return (0, SparqlUtil_1.createSparqlInversePredicate)([subPredicate]);
479
+ }
480
+ if (operator.operator === 'sequencePath') {
481
+ const { subPath } = operator.value;
482
+ const subPredicates = subPath.map((sequencePart) => {
483
+ if (typeof sequencePart === 'string') {
484
+ return data_model_1.default.namedNode(sequencePart);
485
+ }
486
+ return this.pathOperatorToPropertyPath(sequencePart);
487
+ });
488
+ return (0, SparqlUtil_1.createSparqlSequencePredicate)(subPredicates);
489
+ }
490
+ if (operator.operator === 'zeroOrMorePath') {
491
+ const { subPath } = operator.value;
492
+ let subPredicate;
493
+ if (typeof subPath === 'string') {
494
+ subPredicate = data_model_1.default.namedNode(subPath);
495
+ }
496
+ else {
497
+ subPredicate = this.pathOperatorToPropertyPath(subPath);
498
+ }
499
+ return (0, SparqlUtil_1.createSparqlZeroOrMorePredicate)([subPredicate]);
500
+ }
501
+ if (operator.operator === 'oneOrMorePath') {
502
+ const { subPath } = operator.value;
503
+ let subPredicate;
504
+ if (typeof subPath === 'string') {
505
+ subPredicate = data_model_1.default.namedNode(subPath);
506
+ }
507
+ else {
508
+ subPredicate = this.pathOperatorToPropertyPath(subPath);
509
+ }
510
+ return (0, SparqlUtil_1.createSparqlOneOrMorePredicate)([subPredicate]);
511
+ }
512
+ throw new Error(`Operator ${operator.operator} not supported`);
513
+ }
514
+ createWhereQueryDataForMultipleFindOperators(subject, predicate, operators) {
515
+ const variable = this.createVariable();
516
+ const triple = { subject, predicate, object: variable };
517
+ const whereQueryData = {
518
+ values: [],
519
+ filters: [],
520
+ triples: [triple]
521
+ };
522
+ return operators.reduce((obj, operator) => {
523
+ const { filter, valuePattern } = this.resolveFindOperatorAsExpressionWithMultipleValues(variable, operator, triple);
524
+ if (valuePattern) {
525
+ obj.values.push(valuePattern);
526
+ }
527
+ if (filter) {
528
+ obj.filters.push(filter);
529
+ }
530
+ return obj;
531
+ }, whereQueryData);
532
+ }
533
+ createWhereQueryDataForNestedWhere(subject, predicate, where) {
534
+ const subNodeVariable = this.createVariable();
535
+ const subWhereQueryData = this.createWhereQueryData(subNodeVariable, where);
536
+ return {
537
+ values: [...subWhereQueryData.values, ...subWhereQueryData.graphValues],
538
+ filters: subWhereQueryData.filters,
539
+ triples: [{ subject, predicate, object: subNodeVariable }, ...subWhereQueryData.triples],
540
+ patterns: [...subWhereQueryData.patterns ?? []]
541
+ };
542
+ }
543
+ createWhereQueryDataForValueObject(subject, predicate, valueObject) {
544
+ const term = this.valueObjectToTerm(valueObject);
545
+ if (valueObject.isOptional) {
546
+ return {
547
+ values: [],
548
+ filters: [],
549
+ triples: [],
550
+ patterns: [(0, SparqlUtil_1.createSparqlOptional)([(0, SparqlUtil_1.createSparqlBasicGraphPattern)([{ subject, predicate, object: term }])])]
551
+ };
552
+ }
553
+ return {
554
+ values: [],
555
+ filters: [],
556
+ triples: [{ subject, predicate, object: term }]
557
+ };
558
+ }
559
+ valueObjectToTerm(valueObject) {
560
+ let typeOrLanguage;
561
+ let value;
562
+ if ('@type' in valueObject && valueObject['@type'] === '@json') {
563
+ typeOrLanguage = Vocabularies_1.RDF.JSON;
564
+ value = JSON.stringify(valueObject['@value']);
565
+ }
566
+ else {
567
+ typeOrLanguage = ('@type' in valueObject ? valueObject['@type'] : valueObject['@language']);
568
+ value = valueObject['@value'].toString();
569
+ }
570
+ return (0, TripleUtil_1.valueToLiteral)(value, typeOrLanguage);
571
+ }
572
+ resolveFindOperatorAsExpressionWithMultipleValues(leftSide, operator, triple, dontUseValuePattern = false) {
573
+ if (operator.operator === 'and') {
574
+ // For AND operators, we'll create an AND filter with multiple equality comparisons
575
+ const values = operator.value;
576
+ if (values.length === 0) {
577
+ // No conditions to add
578
+ return {};
579
+ }
580
+ if (values.length === 1) {
581
+ // If only one value, treat as equality
582
+ return {
583
+ filter: (0, SparqlUtil_1.createSparqlEqualOperation)(leftSide, this.resolveValueToExpression(values[0]))
584
+ };
585
+ }
586
+ // Create individual equality conditions for each value
587
+ const equalityConditions = values.map(val => (0, SparqlUtil_1.createSparqlEqualOperation)(leftSide, this.resolveValueToExpression(val)));
588
+ // Combine with AND operation
589
+ return {
590
+ filter: {
591
+ type: 'operation',
592
+ operator: '&&',
593
+ args: equalityConditions
594
+ }
595
+ };
596
+ }
597
+ if (operator.operator === 'in') {
598
+ const resolvedValue = this.resolveValueToExpression(operator.value);
599
+ if (Array.isArray(resolvedValue) && !dontUseValuePattern) {
600
+ return {
601
+ valuePattern: {
602
+ type: 'values',
603
+ values: resolvedValue.map((value) => ({ [`?${leftSide.value}`]: value }))
604
+ }
605
+ };
606
+ }
607
+ return {
608
+ filter: (0, SparqlUtil_1.createSparqlInOperation)(leftSide, resolvedValue)
609
+ };
610
+ }
611
+ if (operator.operator === 'not') {
612
+ const resolvedExpression = this.resolveValueToExpression(operator.value);
613
+ return {
614
+ filter: this.buildNotOperationForMultiValued(leftSide, resolvedExpression, triple),
615
+ tripleInFilter: true
616
+ };
617
+ }
618
+ if (operator.operator === 'exists') {
619
+ return {
620
+ filter: (0, SparqlUtil_1.createSparqlExistsOperation)([(0, SparqlUtil_1.createSparqlBasicGraphPattern)([triple])]),
621
+ tripleInFilter: true
622
+ };
623
+ }
624
+ if (operator.operator === 'contains') {
625
+ const searchString = this.resolveValueToExpression(operator.value);
626
+ const filter = (0, SparqlUtil_1.createSparqlContainsOperation)(
627
+ // Directly use the variable as an expression
628
+ (0, SparqlUtil_1.createSparqlLcaseOperation)(data_model_1.default.variable(leftSide.value)), (0, SparqlUtil_1.createSparqlLcaseOperation)(data_model_1.default.literal(searchString.value.toLowerCase())));
629
+ return {
630
+ filter
631
+ };
632
+ }
633
+ const resolvedExpression = this.resolveValueToExpression(operator.value);
634
+ switch (operator.operator) {
635
+ case 'equal':
636
+ return { filter: (0, SparqlUtil_1.createSparqlEqualOperation)(leftSide, resolvedExpression) };
637
+ case 'gt':
638
+ return { filter: (0, SparqlUtil_1.createSparqlGtOperation)(leftSide, resolvedExpression) };
639
+ case 'gte':
640
+ return { filter: (0, SparqlUtil_1.createSparqlGteOperation)(leftSide, resolvedExpression) };
641
+ case 'lt':
642
+ return { filter: (0, SparqlUtil_1.createSparqlLtOperation)(leftSide, resolvedExpression) };
643
+ case 'lte':
644
+ return { filter: (0, SparqlUtil_1.createSparqlLteOperation)(leftSide, resolvedExpression) };
645
+ default:
646
+ throw new Error(`Unsupported operator "${operator.operator}"`);
647
+ }
648
+ }
649
+ resolveFindOperatorAsExpressionForId(leftSide, operator) {
650
+ switch (operator.operator) {
651
+ case 'inversePath': {
652
+ const predicate = this.pathOperatorToPropertyPath(operator);
653
+ return this.createWhereQueryDataFromKeyValue(leftSide, predicate, operator.value.value);
654
+ }
655
+ case 'in': {
656
+ const resolvedValue = this.resolveValueToExpression(operator.value);
657
+ return {
658
+ triples: [],
659
+ filters: [],
660
+ values: [
661
+ {
662
+ type: 'values',
663
+ values: resolvedValue.map((value) => ({ [`?${leftSide.value}`]: value }))
664
+ }
665
+ ]
666
+ };
667
+ }
668
+ case 'not':
669
+ return {
670
+ triples: [],
671
+ values: [],
672
+ filters: [
673
+ this.buildNotOperationForId(leftSide, this.resolveValueToExpression(operator.value))
674
+ ]
675
+ };
676
+ case 'equal':
677
+ return {
678
+ triples: [],
679
+ values: [],
680
+ filters: [(0, SparqlUtil_1.createSparqlEqualOperation)(leftSide, this.resolveValueToExpression(operator.value))]
681
+ };
682
+ default:
683
+ throw new Error(`Unsupported operator "${operator.operator}"`);
684
+ }
685
+ }
686
+ resolveValueToExpression(value) {
687
+ if (FindOperator_1.FindOperator.isFindOperator(value)) {
688
+ return value;
689
+ }
690
+ if (Array.isArray(value)) {
691
+ return value.map((valueItem) => this.resolveValueToTerm(valueItem));
692
+ }
693
+ return this.resolveValueToTerm(value);
694
+ }
695
+ buildNotOperationForMultiValued(leftSide, rightSide, triple) {
696
+ let filterExpression;
697
+ const isFindOperator = FindOperator_1.FindOperator.isFindOperator(rightSide);
698
+ if (isFindOperator && rightSide.operator === 'exists') {
699
+ return (0, SparqlUtil_1.createSparqlNotExistsOperation)([(0, SparqlUtil_1.createSparqlBasicGraphPattern)([triple])]);
700
+ }
701
+ if (isFindOperator) {
702
+ let expression;
703
+ try {
704
+ ({ filter: expression } = this.resolveFindOperatorAsExpressionWithMultipleValues(leftSide, rightSide, triple, true));
705
+ }
706
+ catch {
707
+ throw new Error(`Unsupported Not sub operator "${rightSide.operator}"`);
708
+ }
709
+ filterExpression = (0, SparqlUtil_1.createSparqlFilterWithExpression)(expression);
710
+ }
711
+ else {
712
+ filterExpression = (0, SparqlUtil_1.createSparqlFilterWithExpression)((0, SparqlUtil_1.createSparqlEqualOperation)(leftSide, rightSide));
713
+ }
714
+ return (0, SparqlUtil_1.createSparqlNotExistsOperation)([
715
+ (0, SparqlUtil_1.createSparqlSelectGroup)([(0, SparqlUtil_1.createSparqlBasicGraphPattern)([triple]), filterExpression])
716
+ ]);
717
+ }
718
+ buildNotOperationForId(leftSide, rightSide) {
719
+ if (FindOperator_1.FindOperator.isFindOperator(rightSide)) {
720
+ const resolvedValue = this.resolveValueToExpression(rightSide.value);
721
+ switch (rightSide.operator) {
722
+ case 'in':
723
+ return (0, SparqlUtil_1.createSparqlNotInOperation)(leftSide, resolvedValue);
724
+ case 'equal':
725
+ return (0, SparqlUtil_1.createSparqlNotEqualOperation)(leftSide, resolvedValue);
726
+ default:
727
+ throw new Error(`Unsupported Not sub operator "${rightSide.operator}"`);
728
+ }
729
+ }
730
+ return (0, SparqlUtil_1.createSparqlNotEqualOperation)(leftSide, rightSide);
731
+ }
732
+ resolveValueToTerm(value) {
733
+ if (typeof value === 'object' && '@value' in value) {
734
+ return (0, TripleUtil_1.valueToLiteral)(value['@value'], '@type' in value ? value['@type'] : undefined);
735
+ }
736
+ if ((0, Util_1.isUrl)(value)) {
737
+ return data_model_1.default.namedNode(value);
738
+ }
739
+ return (0, TripleUtil_1.valueToLiteral)(value);
740
+ }
741
+ createOrderQueryData(subject, order, isNested = false) {
742
+ if (!order) {
743
+ // Default is descending by id
744
+ return { triples: [], orders: [], filters: [] };
745
+ }
746
+ return Object.entries(order).reduce((obj, [property, orderValue]) => {
747
+ const orderQueryData = this.createOrderQueryDataForProperty(subject, property, orderValue, isNested);
748
+ obj.orders = [...obj.orders, ...orderQueryData.orders];
749
+ obj.triples = [...obj.triples, ...orderQueryData.triples];
750
+ obj.filters = [...obj.filters, ...orderQueryData.filters];
751
+ obj.groupByParent = obj.groupByParent ?? orderQueryData.groupByParent;
752
+ return obj;
753
+ }, { triples: [], orders: [], filters: [] });
754
+ }
755
+ createOrderQueryDataForProperty(subject, property, orderValue, isNested = false) {
756
+ const predicate = data_model_1.default.namedNode(property);
757
+ if (FindOperator_1.FindOperator.isFindOperator(orderValue)) {
758
+ const variable = this.createVariable();
759
+ const inverseRelationTriple = {
760
+ subject,
761
+ predicate: (0, SparqlUtil_1.createSparqlInversePredicate)([predicate]),
762
+ object: variable
763
+ };
764
+ const subRelationOperatorValue = orderValue
765
+ .value;
766
+ const subRelationOrderQueryData = this.createOrderQueryData(variable, subRelationOperatorValue.order, true);
767
+ const subRelationWhereQueryData = this.createWhereQueryData(variable, subRelationOperatorValue.where);
768
+ // Create aggregate expressions for each order, but don't nest aggregates
769
+ const aggregateOrders = subRelationOrderQueryData.orders.map(order => {
770
+ const baseExpression = 'type' in order.expression && order.expression.type === 'aggregate'
771
+ ? order.expression.expression
772
+ : order.expression;
773
+ // Create the aggregate expression first
774
+ const aggregateExpression = {
775
+ type: 'aggregate',
776
+ expression: baseExpression,
777
+ aggregation: order.descending ? 'max' : 'min'
778
+ };
779
+ return {
780
+ expression: aggregateExpression,
781
+ descending: order.descending
782
+ };
783
+ });
784
+ return {
785
+ triples: [inverseRelationTriple, ...subRelationOrderQueryData.triples, ...subRelationWhereQueryData.triples],
786
+ filters: subRelationWhereQueryData.filters,
787
+ orders: aggregateOrders,
788
+ groupByParent: true,
789
+ patterns: [...subRelationWhereQueryData.patterns ?? [], ...subRelationOrderQueryData.patterns ?? []]
790
+ };
791
+ }
792
+ if (property === 'id') {
793
+ return {
794
+ triples: [],
795
+ filters: [],
796
+ orders: [
797
+ {
798
+ expression: subject,
799
+ descending: orderValue === 'DESC' || orderValue === 'desc'
800
+ }
801
+ ]
802
+ };
803
+ }
804
+ const variable = this.createVariable();
805
+ const isDescending = orderValue === 'DESC' || orderValue === 'desc';
806
+ return {
807
+ triples: [{ subject, predicate, object: variable }],
808
+ filters: [],
809
+ orders: [
810
+ {
811
+ expression: isNested
812
+ ? {
813
+ type: 'aggregate',
814
+ expression: variable,
815
+ aggregation: isDescending ? 'max' : 'min'
816
+ }
817
+ : variable,
818
+ descending: isDescending
819
+ }
820
+ ]
821
+ };
822
+ }
823
+ createRelationsQueryData(subject, relations) {
824
+ if (!relations) {
825
+ return { patterns: [], selectionTriples: [], unionPatterns: [] };
826
+ }
827
+ const response = Object.entries(relations).reduce((obj, [property, relationsValue]) => {
828
+ const predicate = data_model_1.default.namedNode(property);
829
+ if (typeof relationsValue === 'object') {
830
+ if (FindOperator_1.FindOperator.isFindOperator(relationsValue)) {
831
+ const { patterns, selectionTriples, unionPatterns } = this.createRelationsQueryDataForInverseRelation(subject, predicate, relationsValue);
832
+ return {
833
+ patterns: [...obj.patterns, ...patterns],
834
+ selectionTriples: [...obj.selectionTriples, ...selectionTriples],
835
+ unionPatterns: [...obj.unionPatterns ?? [], ...unionPatterns ?? []]
836
+ };
837
+ }
838
+ const { patterns, selectionTriples, unionPatterns } = this.createRelationsQueryDataForNestedRelation(subject, predicate, relationsValue);
839
+ return {
840
+ patterns: [...obj.patterns, ...patterns],
841
+ selectionTriples: [...obj.selectionTriples, ...selectionTriples],
842
+ unionPatterns: [
843
+ ...obj.unionPatterns ?? [],
844
+ ...unionPatterns ?? []
845
+ ]
846
+ };
847
+ }
848
+ const variable = this.createVariable();
849
+ const relationPattern = (0, SparqlUtil_1.createSparqlSelectGroup)([
850
+ (0, SparqlUtil_1.createSparqlBasicGraphPattern)([{ subject, predicate, object: variable }]),
851
+ (0, SparqlUtil_1.createSparqlGraphPattern)(variable, [(0, SparqlUtil_1.createSparqlBasicGraphPattern)([SparqlUtil_1.entityGraphTriple])])
852
+ ]);
853
+ return {
854
+ unionPatterns: [...obj.unionPatterns ?? [], relationPattern],
855
+ patterns: [...obj.patterns],
856
+ selectionTriples: [...obj.selectionTriples]
857
+ };
858
+ }, { patterns: [], selectionTriples: [], unionPatterns: [] });
859
+ return {
860
+ patterns: [...response?.patterns ?? []],
861
+ selectionTriples: [...response?.selectionTriples ?? []],
862
+ unionPatterns: [...response?.unionPatterns ?? []]
863
+ };
864
+ }
865
+ createRelationsQueryDataForInverseRelation(subject, predicate, relationsValue) {
866
+ const variable = this.createVariable();
867
+ const inverseRelationTriple = {
868
+ subject,
869
+ predicate: (0, SparqlUtil_1.createSparqlInversePredicate)([predicate]),
870
+ object: variable
871
+ };
872
+ if (typeof relationsValue.value === 'object' && relationsValue.value.relations) {
873
+ const subRelationsQueryData = this.createRelationsQueryData(variable, relationsValue.value.relations);
874
+ const unionPatterns = [];
875
+ unionPatterns.push((0, SparqlUtil_1.createSparqlSelectGroup)([
876
+ (0, SparqlUtil_1.createSparqlBasicGraphPattern)([inverseRelationTriple]),
877
+ (0, SparqlUtil_1.createSparqlGraphPattern)(variable, [(0, SparqlUtil_1.createSparqlBasicGraphPattern)([SparqlUtil_1.entityGraphTriple])])
878
+ ]));
879
+ for (const subRelationUnionPattern of subRelationsQueryData.unionPatterns) {
880
+ unionPatterns.push((0, SparqlUtil_1.createSparqlSelectGroup)([
881
+ (0, SparqlUtil_1.createSparqlBasicGraphPattern)([inverseRelationTriple]),
882
+ ...subRelationUnionPattern.type === 'group'
883
+ ? subRelationUnionPattern.patterns
884
+ : [subRelationUnionPattern]
885
+ ]));
886
+ }
887
+ return {
888
+ patterns: [],
889
+ selectionTriples: [...subRelationsQueryData.selectionTriples],
890
+ unionPatterns
891
+ };
892
+ }
893
+ const unionPatterns = [];
894
+ unionPatterns.push((0, SparqlUtil_1.createSparqlSelectGroup)([
895
+ (0, SparqlUtil_1.createSparqlBasicGraphPattern)([inverseRelationTriple]),
896
+ (0, SparqlUtil_1.createSparqlGraphPattern)(variable, [(0, SparqlUtil_1.createSparqlBasicGraphPattern)([SparqlUtil_1.entityGraphTriple])])
897
+ ]));
898
+ return {
899
+ patterns: [],
900
+ selectionTriples: [],
901
+ unionPatterns
902
+ };
903
+ }
904
+ createRelationsQueryDataForNestedRelation(subject, predicate, relationsValue) {
905
+ const variable = this.createVariable();
906
+ const relationTriple = { subject, predicate, object: variable };
907
+ const unionPatterns = [];
908
+ const subRelationsQueryData = this.createRelationsQueryData(variable, relationsValue);
909
+ // subRelationsQueryData.patterns.push(createSparqlBasicGraphPattern([ relationTriple ]));
910
+ // unionPatterns.push(...subRelationsQueryData.patterns);
911
+ // unionPatterns.push(
912
+ // createSparqlGraphPattern(variable, [ createSparqlBasicGraphPattern([ entityGraphTriple ]) ])
913
+ // );
914
+ /* const relationPattern = subRelationsQueryData.patterns.length > 0
915
+ ? createSparqlSelectGroup([
916
+ ...patterns,
917
+ ...subRelationsQueryData.patterns,
918
+ ...subRelationsQueryData.unionPatterns
919
+ ])
920
+ : undefined; */
921
+ /* We always need to include the first level of the property in the union patterns */
922
+ unionPatterns.push((0, SparqlUtil_1.createSparqlSelectGroup)([
923
+ (0, SparqlUtil_1.createSparqlBasicGraphPattern)([relationTriple]),
924
+ (0, SparqlUtil_1.createSparqlGraphPattern)(variable, [(0, SparqlUtil_1.createSparqlBasicGraphPattern)([SparqlUtil_1.entityGraphTriple])])
925
+ ]));
926
+ for (const subRelationUnionPattern of subRelationsQueryData.unionPatterns) {
927
+ unionPatterns.push((0, SparqlUtil_1.createSparqlSelectGroup)([
928
+ (0, SparqlUtil_1.createSparqlBasicGraphPattern)([relationTriple]),
929
+ ...subRelationUnionPattern.type === 'group' ? subRelationUnionPattern.patterns : [subRelationUnionPattern]
930
+ ]));
931
+ }
932
+ return {
933
+ patterns: [],
934
+ selectionTriples: [...subRelationsQueryData.selectionTriples],
935
+ unionPatterns
936
+ };
937
+ }
938
+ createVariable() {
939
+ return data_model_1.default.variable(this.variableGenerator.getNext());
940
+ }
941
+ createSelectPattern(select, subject) {
942
+ if (Array.isArray(select)) {
943
+ return select.map((selectPredicate) => ({
944
+ subject,
945
+ predicate: data_model_1.default.namedNode(selectPredicate),
946
+ object: this.createVariable()
947
+ }));
948
+ }
949
+ return Object.entries(select).reduce((arr, [key, value]) => {
950
+ const variable = this.createVariable();
951
+ arr.push({ subject, predicate: data_model_1.default.namedNode(key), object: variable });
952
+ if (typeof value === 'object') {
953
+ arr = [...arr, ...this.createSelectPattern(value, variable)];
954
+ }
955
+ return arr;
956
+ }, []);
957
+ }
958
+ createWherePatternsFromQueryData(initialPatterns, triples, filters, orderTriples, orderFilters, additionalPatterns, serviceTriples, binds) {
959
+ let patterns = initialPatterns;
960
+ // Add binds at the beginning if they exist
961
+ if (binds && binds.length > 0) {
962
+ patterns = [...patterns, ...binds];
963
+ }
964
+ if (triples.length > 0) {
965
+ patterns.push((0, SparqlUtil_1.createSparqlBasicGraphPattern)(triples));
966
+ }
967
+ if (orderTriples && orderTriples.length > 0) {
968
+ const optionalPatterns = [(0, SparqlUtil_1.createSparqlBasicGraphPattern)(orderTriples)];
969
+ if (orderFilters && orderFilters.length > 0) {
970
+ optionalPatterns.push((0, SparqlUtil_1.createFilterPatternFromFilters)(orderFilters));
971
+ }
972
+ patterns.push((0, SparqlUtil_1.createSparqlOptional)(optionalPatterns));
973
+ }
974
+ if (filters.length > 0) {
975
+ patterns.push((0, SparqlUtil_1.createFilterPatternFromFilters)(filters));
976
+ }
977
+ if (serviceTriples) {
978
+ for (const [service, sTriples] of Object.entries(serviceTriples)) {
979
+ patterns.unshift((0, SparqlUtil_1.createSparqlServicePattern)(service, sTriples));
980
+ }
981
+ }
982
+ if (additionalPatterns) {
983
+ patterns = [...patterns, ...additionalPatterns];
984
+ }
985
+ return patterns;
986
+ }
987
+ createGroupPatternForPath(entityVariable, path) {
988
+ const segments = path.split('~');
989
+ let currentSubject = entityVariable;
990
+ const patterns = [];
991
+ // Create a chain of patterns for each segment
992
+ segments.forEach((predicate, index) => {
993
+ const object = this.createVariable();
994
+ patterns.push({
995
+ type: 'bgp',
996
+ triples: [
997
+ {
998
+ subject: currentSubject,
999
+ predicate: data_model_1.default.namedNode(predicate),
1000
+ object
1001
+ }
1002
+ ]
1003
+ });
1004
+ currentSubject = object;
1005
+ });
1006
+ // Return the final variable (last object) and all patterns
1007
+ return {
1008
+ variable: currentSubject,
1009
+ patterns
1010
+ };
1011
+ }
1012
+ async buildGroupByQuery(options) {
1013
+ const span = PerformanceLogger_1.PerformanceLogger.startSpan('QueryBuilder.buildGroupBy', {
1014
+ hasGroupBy: !!options.groupBy,
1015
+ hasDateRange: !!options.dateRange
1016
+ });
1017
+ try {
1018
+ const entityVariable = data_model_1.default.variable('entity');
1019
+ const queryData = this.buildEntitySelectPatternsFromOptions(entityVariable, {
1020
+ where: options.where || {}
1021
+ });
1022
+ // Add group variables and patterns with mapping
1023
+ const groupVariables = [];
1024
+ const groupPatterns = [];
1025
+ const variableMapping = {};
1026
+ if (options.groupBy) {
1027
+ options.groupBy.forEach(path => {
1028
+ const { variable: groupVar, patterns } = this.createGroupPatternForPath(entityVariable, path);
1029
+ groupVariables.push(groupVar);
1030
+ variableMapping[groupVar.value] = path;
1031
+ groupPatterns.push(...patterns);
1032
+ });
1033
+ }
1034
+ // Add date handling if specified
1035
+ if (options.dateRange) {
1036
+ const dateVar = this.createVariable();
1037
+ variableMapping[dateVar.value] = 'date';
1038
+ const datePattern = {
1039
+ type: 'bgp',
1040
+ triples: [
1041
+ {
1042
+ subject: entityVariable,
1043
+ predicate: data_model_1.default.namedNode(constants_1.EngineConstants.prop.dateCreated),
1044
+ object: dateVar
1045
+ }
1046
+ ]
1047
+ };
1048
+ const dateFilter = {
1049
+ type: 'filter',
1050
+ expression: {
1051
+ type: 'operation',
1052
+ operator: '&&',
1053
+ args: [
1054
+ {
1055
+ type: 'operation',
1056
+ operator: '>=',
1057
+ args: [dateVar, data_model_1.default.literal(options.dateRange.start, constants_1.XSD.dateTime)]
1058
+ },
1059
+ {
1060
+ type: 'operation',
1061
+ operator: '<=',
1062
+ args: [dateVar, data_model_1.default.literal(options.dateRange.end, constants_1.XSD.dateTime)]
1063
+ }
1064
+ ]
1065
+ }
1066
+ };
1067
+ groupPatterns.push(datePattern, dateFilter);
1068
+ if (options.dateGrouping) {
1069
+ const dateGroupVar = this.createVariable();
1070
+ groupVariables.push(dateGroupVar);
1071
+ variableMapping[dateGroupVar.value] = 'dateGroup';
1072
+ const dateGroupBind = this.createDateGroupingBind(dateVar, dateGroupVar, options.dateGrouping);
1073
+ groupPatterns.push(dateGroupBind);
1074
+ }
1075
+ }
1076
+ // Create count and entityIds variables
1077
+ const countVar = this.createVariable();
1078
+ const entityIdsVar = this.createVariable();
1079
+ variableMapping[countVar.value] = 'count';
1080
+ variableMapping[entityIdsVar.value] = 'entityIds';
1081
+ // Combine all patterns
1082
+ const combinedWhere = [...queryData.where, ...groupPatterns];
1083
+ // Create select query with aggregations
1084
+ const selectQuery = (0, SparqlUtil_1.createSparqlSelectQuery)([
1085
+ ...groupVariables,
1086
+ {
1087
+ expression: {
1088
+ type: 'aggregate',
1089
+ aggregation: 'count',
1090
+ distinct: true,
1091
+ expression: entityVariable
1092
+ },
1093
+ variable: countVar
1094
+ },
1095
+ {
1096
+ expression: {
1097
+ type: 'aggregate',
1098
+ aggregation: 'group_concat',
1099
+ distinct: true,
1100
+ separator: ' ',
1101
+ expression: entityVariable
1102
+ },
1103
+ variable: entityIdsVar
1104
+ }
1105
+ ], combinedWhere, [], // Orders
1106
+ groupVariables.length > 0 ? groupVariables : undefined, // Group by
1107
+ options.limit, options.offset);
1108
+ const result = { query: selectQuery, variableMapping };
1109
+ PerformanceLogger_1.PerformanceLogger.endSpan(span, {
1110
+ groupCount: groupVariables.length,
1111
+ hasDateGrouping: !!options.dateGrouping
1112
+ });
1113
+ return result;
1114
+ }
1115
+ catch (error) {
1116
+ PerformanceLogger_1.PerformanceLogger.endSpan(span, { error: true });
1117
+ throw error;
1118
+ }
1119
+ }
1120
+ // Helper function for date grouping
1121
+ createDateGroupingBind(dateVar, dateGroupVar, grouping) {
1122
+ return {
1123
+ type: 'bind',
1124
+ expression: {
1125
+ type: 'operation',
1126
+ operator: 'CONCAT',
1127
+ args: [
1128
+ this.createYearExpression(dateVar),
1129
+ data_model_1.default.literal('-'),
1130
+ this.createMonthExpression(dateVar),
1131
+ ...this.createDayExpressionParts(dateVar, grouping)
1132
+ ]
1133
+ },
1134
+ variable: dateGroupVar
1135
+ };
1136
+ }
1137
+ createYearExpression(dateVar) {
1138
+ return {
1139
+ type: 'operation',
1140
+ operator: 'STR',
1141
+ args: [
1142
+ {
1143
+ type: 'operation',
1144
+ operator: 'YEAR',
1145
+ args: [dateVar]
1146
+ }
1147
+ ]
1148
+ };
1149
+ }
1150
+ createMonthExpression(dateVar) {
1151
+ return this.createPaddedDatePartExpression(dateVar, 'MONTH');
1152
+ }
1153
+ createDayExpression(dateVar) {
1154
+ return this.createPaddedDatePartExpression(dateVar, 'DAY');
1155
+ }
1156
+ createPaddedDatePartExpression(dateVar, datePart) {
1157
+ const comparisonValue = data_model_1.default.literal('10', constants_1.XSD.integer);
1158
+ return {
1159
+ type: 'operation',
1160
+ operator: 'IF',
1161
+ args: [
1162
+ this.createLessThanComparison(dateVar, datePart, comparisonValue),
1163
+ this.createPaddedStringExpression(dateVar, datePart),
1164
+ this.createUnpaddedStringExpression(dateVar, datePart)
1165
+ ]
1166
+ };
1167
+ }
1168
+ createLessThanComparison(dateVar, datePart, comparisonValue) {
1169
+ return {
1170
+ type: 'operation',
1171
+ operator: '<',
1172
+ args: [
1173
+ {
1174
+ type: 'operation',
1175
+ operator: datePart,
1176
+ args: [dateVar]
1177
+ },
1178
+ comparisonValue
1179
+ ]
1180
+ };
1181
+ }
1182
+ createPaddedStringExpression(dateVar, datePart) {
1183
+ return {
1184
+ type: 'operation',
1185
+ operator: 'CONCAT',
1186
+ args: [data_model_1.default.literal('0'), this.createUnpaddedStringExpression(dateVar, datePart)]
1187
+ };
1188
+ }
1189
+ createUnpaddedStringExpression(dateVar, datePart) {
1190
+ return {
1191
+ type: 'operation',
1192
+ operator: 'STR',
1193
+ args: [
1194
+ {
1195
+ type: 'operation',
1196
+ operator: datePart,
1197
+ args: [dateVar]
1198
+ }
1199
+ ]
1200
+ };
1201
+ }
1202
+ createDayExpressionParts(dateVar, grouping) {
1203
+ if (grouping === 'day') {
1204
+ return [data_model_1.default.literal('-'), this.createDayExpression(dateVar)];
1205
+ }
1206
+ return [data_model_1.default.literal('')];
1207
+ }
1208
+ }
1209
+ exports.SparqlQueryBuilder = SparqlQueryBuilder;
1210
+ //# sourceMappingURL=SparqlQueryBuilder.js.map