@coasys/ad4m 0.13.0-test-3 → 0.13.0-test-4
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.
- package/README.md +2 -25
- package/jest-setup.ts +1 -1
- package/lib/index.cjs +2406 -11070
- package/lib/index.js +2209 -10925
- package/lib/src/Ad4mClient.d.ts +7 -3
- package/lib/src/Ad4mClient.js +29 -13
- package/lib/src/agent/Agent.js +26 -272
- package/lib/src/agent/AgentClient.d.ts +10 -21
- package/lib/src/agent/AgentClient.js +75 -427
- package/lib/src/agent/AgentStatus.js +2 -37
- package/lib/src/ai/AIClient.d.ts +12 -4
- package/lib/src/ai/AIClient.js +81 -285
- package/lib/src/ai/AITypes.d.ts +55 -0
- package/lib/src/ai/AITypes.js +18 -0
- package/lib/src/ai/Tasks.js +10 -133
- package/lib/src/apiClient.d.ts +52 -0
- package/lib/src/apiClient.js +231 -0
- package/lib/src/expression/Expression.d.ts +1 -1
- package/lib/src/expression/Expression.js +13 -122
- package/lib/src/expression/ExpressionClient.d.ts +3 -3
- package/lib/src/expression/ExpressionClient.js +13 -76
- package/lib/src/expression/ExpressionRef.js +2 -25
- package/lib/src/generated/api/AIModelLoadingStatus.d.ts +7 -0
- package/lib/src/generated/api/AIModelLoadingStatus.js +2 -0
- package/lib/src/generated/api/AddLinkExpressionRequest.d.ts +6 -0
- package/lib/src/generated/api/AddLinkExpressionRequest.js +1 -0
- package/lib/src/generated/api/AddLinkRequest.d.ts +6 -0
- package/lib/src/generated/api/AddLinkRequest.js +1 -0
- package/lib/src/generated/api/AddLinksBulkRequest.d.ts +6 -0
- package/lib/src/generated/api/AddLinksBulkRequest.js +1 -0
- package/lib/src/generated/api/AddSdnaRequest.d.ts +6 -0
- package/lib/src/generated/api/AddSdnaRequest.js +2 -0
- package/lib/src/generated/api/ApplyTemplateRequest.d.ts +4 -0
- package/lib/src/generated/api/ApplyTemplateRequest.js +2 -0
- package/lib/src/generated/api/AuthInfoInput.d.ts +10 -0
- package/lib/src/generated/api/AuthInfoInput.js +1 -0
- package/lib/src/generated/api/CapabilityInput.d.ts +5 -0
- package/lib/src/generated/api/CapabilityInput.js +1 -0
- package/lib/src/generated/api/CommitBatchRequest.d.ts +3 -0
- package/lib/src/generated/api/CommitBatchRequest.js +2 -0
- package/lib/src/generated/api/ComputeLogEntry.d.ts +9 -0
- package/lib/src/generated/api/ComputeLogEntry.js +2 -0
- package/lib/src/generated/api/CreateExpressionRequest.d.ts +4 -0
- package/lib/src/generated/api/CreateExpressionRequest.js +2 -0
- package/lib/src/generated/api/CreatePerspectiveRequest.d.ts +3 -0
- package/lib/src/generated/api/CreatePerspectiveRequest.js +2 -0
- package/lib/src/generated/api/CreateSubjectRequest.d.ts +6 -0
- package/lib/src/generated/api/CreateSubjectRequest.js +2 -0
- package/lib/src/generated/api/EmbedRequest.d.ts +4 -0
- package/lib/src/generated/api/EmbedRequest.js +2 -0
- package/lib/src/generated/api/EntanglementProofInput.d.ts +6 -0
- package/lib/src/generated/api/EntanglementProofInput.js +2 -0
- package/lib/src/generated/api/ExecuteCommandsRequest.d.ts +4 -0
- package/lib/src/generated/api/ExecuteCommandsRequest.js +2 -0
- package/lib/src/generated/api/ExportRequest.d.ts +5 -0
- package/lib/src/generated/api/ExportRequest.js +2 -0
- package/lib/src/generated/api/ExpressionManyRequest.d.ts +3 -0
- package/lib/src/generated/api/ExpressionManyRequest.js +2 -0
- package/lib/src/generated/api/ExpressionProof.d.ts +4 -0
- package/lib/src/generated/api/ExpressionProof.js +2 -0
- package/lib/src/generated/api/ExpressionProofInput.d.ts +6 -0
- package/lib/src/generated/api/ExpressionProofInput.js +2 -0
- package/lib/src/generated/api/FriendSendMessageRequest.d.ts +3 -0
- package/lib/src/generated/api/FriendSendMessageRequest.js +2 -0
- package/lib/src/generated/api/FriendsListRequest.d.ts +3 -0
- package/lib/src/generated/api/FriendsListRequest.js +2 -0
- package/lib/src/generated/api/GenerateAgentRequest.d.ts +3 -0
- package/lib/src/generated/api/GenerateAgentRequest.js +2 -0
- package/lib/src/generated/api/GenerateJwtRequest.d.ts +4 -0
- package/lib/src/generated/api/GenerateJwtRequest.js +2 -0
- package/lib/src/generated/api/GetSubjectDataRequest.d.ts +4 -0
- package/lib/src/generated/api/GetSubjectDataRequest.js +2 -0
- package/lib/src/generated/api/ImportAgentRequest.d.ts +6 -0
- package/lib/src/generated/api/ImportAgentRequest.js +2 -0
- package/lib/src/generated/api/ImportRequest.d.ts +4 -0
- package/lib/src/generated/api/ImportRequest.js +2 -0
- package/lib/src/generated/api/JoinNeighbourhoodRequest.d.ts +3 -0
- package/lib/src/generated/api/JoinNeighbourhoodRequest.js +2 -0
- package/lib/src/generated/api/LanguageMetaInput.d.ts +6 -0
- package/lib/src/generated/api/LanguageMetaInput.js +2 -0
- package/lib/src/generated/api/Link.d.ts +5 -0
- package/lib/src/generated/api/Link.js +2 -0
- package/lib/src/generated/api/LinkExpression.d.ts +10 -0
- package/lib/src/generated/api/LinkExpression.js +1 -0
- package/lib/src/generated/api/LinkExpressionInput.d.ts +10 -0
- package/lib/src/generated/api/LinkExpressionInput.js +1 -0
- package/lib/src/generated/api/LinkInput.d.ts +5 -0
- package/lib/src/generated/api/LinkInput.js +2 -0
- package/lib/src/generated/api/LinkLanguageTemplatesRequest.d.ts +3 -0
- package/lib/src/generated/api/LinkLanguageTemplatesRequest.js +2 -0
- package/lib/src/generated/api/LinkMutations.d.ts +6 -0
- package/lib/src/generated/api/LinkMutations.js +1 -0
- package/lib/src/generated/api/LinkStatus.d.ts +1 -0
- package/lib/src/generated/api/LinkStatus.js +2 -0
- package/lib/src/generated/api/LockAgentRequest.d.ts +3 -0
- package/lib/src/generated/api/LockAgentRequest.js +2 -0
- package/lib/src/generated/api/ModelType.d.ts +1 -0
- package/lib/src/generated/api/ModelType.js +2 -0
- package/lib/src/generated/api/NotificationInput.d.ts +10 -0
- package/lib/src/generated/api/NotificationInput.js +2 -0
- package/lib/src/generated/api/OpenLinkRequest.d.ts +3 -0
- package/lib/src/generated/api/OpenLinkRequest.js +2 -0
- package/lib/src/generated/api/PermitCapabilityRequest.d.ts +3 -0
- package/lib/src/generated/api/PermitCapabilityRequest.js +2 -0
- package/lib/src/generated/api/PerspectiveInput.d.ts +4 -0
- package/lib/src/generated/api/PerspectiveInput.js +1 -0
- package/lib/src/generated/api/PromptRequest.d.ts +4 -0
- package/lib/src/generated/api/PromptRequest.js +2 -0
- package/lib/src/generated/api/PublishLanguageRequest.d.ts +5 -0
- package/lib/src/generated/api/PublishLanguageRequest.js +1 -0
- package/lib/src/generated/api/PublishNeighbourhoodRequest.d.ts +6 -0
- package/lib/src/generated/api/PublishNeighbourhoodRequest.js +1 -0
- package/lib/src/generated/api/QueryRequest.d.ts +7 -0
- package/lib/src/generated/api/QueryRequest.js +2 -0
- package/lib/src/generated/api/RemoveLinkRequest.d.ts +5 -0
- package/lib/src/generated/api/RemoveLinkRequest.js +1 -0
- package/lib/src/generated/api/RemoveLinksBulkRequest.d.ts +5 -0
- package/lib/src/generated/api/RemoveLinksBulkRequest.js +1 -0
- package/lib/src/generated/api/ResourceInput.d.ts +4 -0
- package/lib/src/generated/api/ResourceInput.js +2 -0
- package/lib/src/generated/api/SetDefaultModelRequest.d.ts +4 -0
- package/lib/src/generated/api/SetDefaultModelRequest.js +1 -0
- package/lib/src/generated/api/SetFreeHostingEnabledRequest.d.ts +3 -0
- package/lib/src/generated/api/SetFreeHostingEnabledRequest.js +2 -0
- package/lib/src/generated/api/SetMultiUserRequest.d.ts +3 -0
- package/lib/src/generated/api/SetMultiUserRequest.js +2 -0
- package/lib/src/generated/api/SetStatusRequest.d.ts +3 -0
- package/lib/src/generated/api/SetStatusRequest.js +2 -0
- package/lib/src/generated/api/SignMessageRequest.d.ts +3 -0
- package/lib/src/generated/api/SignMessageRequest.js +2 -0
- package/lib/src/generated/api/TrustedAgentsRequest.d.ts +3 -0
- package/lib/src/generated/api/TrustedAgentsRequest.js +2 -0
- package/lib/src/generated/api/UnlockAgentRequest.d.ts +4 -0
- package/lib/src/generated/api/UnlockAgentRequest.js +2 -0
- package/lib/src/generated/api/UpdateLinkRequest.d.ts +7 -0
- package/lib/src/generated/api/UpdateLinkRequest.js +1 -0
- package/lib/src/generated/api/UpdatePerspectiveRequest.d.ts +3 -0
- package/lib/src/generated/api/UpdatePerspectiveRequest.js +2 -0
- package/lib/src/generated/api/UserStatistics.d.ts +9 -0
- package/lib/src/generated/api/UserStatistics.js +2 -0
- package/lib/src/generated/api/VerificationRequestResult.d.ts +6 -0
- package/lib/src/generated/api/VerificationRequestResult.js +2 -0
- package/lib/src/generated/api/VerifySignatureRequest.d.ts +5 -0
- package/lib/src/generated/api/VerifySignatureRequest.js +2 -0
- package/lib/src/generated/api/WriteSettingsRequest.d.ts +3 -0
- package/lib/src/generated/api/WriteSettingsRequest.js +2 -0
- package/lib/src/generated/api/index.d.ts +42 -0
- package/lib/src/generated/api/index.js +7 -0
- package/lib/src/index.d.ts +6 -1
- package/lib/src/index.js +6 -1
- package/lib/src/jestSetup.d.ts +0 -1
- package/lib/src/jestSetup.js +1 -1
- package/lib/src/language/Icon.js +2 -21
- package/lib/src/language/Language.d.ts +1 -1
- package/lib/src/language/Language.js +10 -76
- package/lib/src/language/LanguageClient.d.ts +2 -2
- package/lib/src/language/LanguageClient.js +15 -105
- package/lib/src/language/LanguageHandle.js +2 -41
- package/lib/src/language/LanguageMeta.js +4 -75
- package/lib/src/language/LanguageRef.js +2 -25
- package/lib/src/links/Links.js +14 -111
- package/lib/src/model/Ad4mModel.d.ts +57 -38
- package/lib/src/model/Ad4mModel.js +594 -460
- package/lib/src/model/Ad4mModel.test.js +371 -815
- package/lib/src/model/ModelQueryBuilder.d.ts +9 -21
- package/lib/src/model/ModelQueryBuilder.js +138 -217
- package/lib/src/model/decorators.d.ts +22 -0
- package/lib/src/model/decorators.js +7 -0
- package/lib/src/model/hydration.d.ts +7 -60
- package/lib/src/model/hydration.js +7 -716
- package/lib/src/model/index.d.ts +0 -4
- package/lib/src/model/index.js +0 -4
- package/lib/src/model/model-manifest.d.ts +41 -0
- package/lib/src/model/model-manifest.js +11 -0
- package/lib/src/model/query-sparql.d.ts +4 -67
- package/lib/src/model/query-sparql.js +26 -322
- package/lib/src/model/query-sparql.test.js +35 -218
- package/lib/src/model/shacl-gen.js +4 -0
- package/lib/src/model/types.d.ts +47 -38
- package/lib/src/model/types.js +4 -1
- package/lib/src/neighbourhood/Neighbourhood.js +4 -32
- package/lib/src/neighbourhood/NeighbourhoodClient.d.ts +3 -3
- package/lib/src/neighbourhood/NeighbourhoodClient.js +40 -203
- package/lib/src/perspectives/LinkQuery.js +2 -42
- package/lib/src/perspectives/Perspective.d.ts +1 -1
- package/lib/src/perspectives/Perspective.js +10 -52
- package/lib/src/perspectives/PerspectiveClient.d.ts +12 -17
- package/lib/src/perspectives/PerspectiveClient.js +169 -429
- package/lib/src/perspectives/PerspectiveClient.test.js +55 -0
- package/lib/src/perspectives/PerspectiveDiff.js +4 -31
- package/lib/src/perspectives/PerspectiveHandle.js +3 -43
- package/lib/src/perspectives/PerspectiveProxy.d.ts +92 -71
- package/lib/src/perspectives/PerspectiveProxy.js +273 -310
- package/lib/src/runtime/RuntimeClient.d.ts +6 -6
- package/lib/src/runtime/RuntimeClient.js +93 -467
- package/lib/src/runtime/RuntimeTypes.d.ts +101 -0
- package/lib/src/runtime/RuntimeTypes.js +45 -0
- package/lib/src/shacl/SHACLShape.d.ts +7 -0
- package/lib/src/shacl/SHACLShape.js +19 -0
- package/lib/src/shacl/SHACLShape.test.js +82 -0
- package/lib/src/typeDefs.js +3 -628
- package/package.json +9 -24
- package/rollup.config.js +0 -7
- package/src/Ad4mClient.test.ts +1179 -1340
- package/src/Ad4mClient.ts +30 -15
- package/src/agent/Agent.ts +0 -99
- package/src/agent/AgentClient.ts +112 -526
- package/src/agent/AgentStatus.ts +0 -12
- package/src/ai/AIClient.ts +96 -296
- package/src/ai/AITypes.ts +66 -0
- package/src/ai/Tasks.ts +0 -52
- package/src/apiClient.ts +283 -0
- package/src/expression/Expression.ts +3 -43
- package/src/expression/ExpressionClient.ts +14 -77
- package/src/expression/ExpressionRef.ts +0 -6
- package/src/generated/api/AIModelLoadingStatus.ts +3 -0
- package/src/generated/api/AddLinkExpressionRequest.ts +4 -0
- package/src/generated/api/AddLinkRequest.ts +4 -0
- package/src/generated/api/AddLinksBulkRequest.ts +4 -0
- package/src/generated/api/AddSdnaRequest.ts +3 -0
- package/src/generated/api/ApplyTemplateRequest.ts +3 -0
- package/src/generated/api/AuthInfoInput.ts +4 -0
- package/src/generated/api/CapabilityInput.ts +4 -0
- package/src/generated/api/CommitBatchRequest.ts +3 -0
- package/src/generated/api/ComputeLogEntry.ts +3 -0
- package/src/generated/api/CreateExpressionRequest.ts +3 -0
- package/src/generated/api/CreatePerspectiveRequest.ts +3 -0
- package/src/generated/api/CreateSubjectRequest.ts +3 -0
- package/src/generated/api/EmbedRequest.ts +3 -0
- package/src/generated/api/EntanglementProofInput.ts +3 -0
- package/src/generated/api/ExecuteCommandsRequest.ts +3 -0
- package/src/generated/api/ExportRequest.ts +3 -0
- package/src/generated/api/ExpressionManyRequest.ts +3 -0
- package/src/generated/api/ExpressionProof.ts +3 -0
- package/src/generated/api/ExpressionProofInput.ts +3 -0
- package/src/generated/api/FriendSendMessageRequest.ts +3 -0
- package/src/generated/api/FriendsListRequest.ts +3 -0
- package/src/generated/api/GenerateAgentRequest.ts +3 -0
- package/src/generated/api/GenerateJwtRequest.ts +3 -0
- package/src/generated/api/GetSubjectDataRequest.ts +3 -0
- package/src/generated/api/ImportAgentRequest.ts +3 -0
- package/src/generated/api/ImportRequest.ts +3 -0
- package/src/generated/api/JoinNeighbourhoodRequest.ts +3 -0
- package/src/generated/api/LanguageMetaInput.ts +3 -0
- package/src/generated/api/Link.ts +3 -0
- package/src/generated/api/LinkExpression.ts +6 -0
- package/src/generated/api/LinkExpressionInput.ts +6 -0
- package/src/generated/api/LinkInput.ts +3 -0
- package/src/generated/api/LinkLanguageTemplatesRequest.ts +3 -0
- package/src/generated/api/LinkMutations.ts +5 -0
- package/src/generated/api/LinkStatus.ts +3 -0
- package/src/generated/api/LockAgentRequest.ts +3 -0
- package/src/generated/api/ModelType.ts +3 -0
- package/src/generated/api/NotificationInput.ts +3 -0
- package/src/generated/api/OpenLinkRequest.ts +3 -0
- package/src/generated/api/PermitCapabilityRequest.ts +3 -0
- package/src/generated/api/PerspectiveInput.ts +4 -0
- package/src/generated/api/PromptRequest.ts +3 -0
- package/src/generated/api/PublishLanguageRequest.ts +4 -0
- package/src/generated/api/PublishNeighbourhoodRequest.ts +4 -0
- package/src/generated/api/QueryRequest.ts +6 -0
- package/src/generated/api/RemoveLinkRequest.ts +4 -0
- package/src/generated/api/RemoveLinksBulkRequest.ts +4 -0
- package/src/generated/api/ResourceInput.ts +3 -0
- package/src/generated/api/SetDefaultModelRequest.ts +4 -0
- package/src/generated/api/SetFreeHostingEnabledRequest.ts +3 -0
- package/src/generated/api/SetMultiUserRequest.ts +3 -0
- package/src/generated/api/SetStatusRequest.ts +3 -0
- package/src/generated/api/SignMessageRequest.ts +3 -0
- package/src/generated/api/TrustedAgentsRequest.ts +3 -0
- package/src/generated/api/UnlockAgentRequest.ts +3 -0
- package/src/generated/api/UpdateLinkRequest.ts +5 -0
- package/src/generated/api/UpdatePerspectiveRequest.ts +3 -0
- package/src/generated/api/UserStatistics.ts +3 -0
- package/src/generated/api/VerificationRequestResult.ts +3 -0
- package/src/generated/api/VerifySignatureRequest.ts +3 -0
- package/src/generated/api/WriteSettingsRequest.ts +3 -0
- package/src/generated/api/index.ts +49 -0
- package/src/index.ts +6 -1
- package/src/jestSetup.ts +1 -1
- package/src/language/Icon.ts +0 -4
- package/src/language/Language.ts +2 -23
- package/src/language/LanguageClient.ts +21 -132
- package/src/language/LanguageHandle.ts +0 -14
- package/src/language/LanguageMeta.ts +0 -29
- package/src/language/LanguageRef.ts +0 -7
- package/src/links/Links.ts +0 -37
- package/src/model/Ad4mModel.test.ts +708 -876
- package/src/model/Ad4mModel.ts +613 -508
- package/src/model/ModelQueryBuilder.ts +178 -244
- package/src/model/decorators.ts +29 -0
- package/src/model/hydration.ts +9 -766
- package/src/model/index.ts +0 -4
- package/src/model/query-sparql.test.ts +34 -242
- package/src/model/query-sparql.ts +27 -337
- package/src/model/shacl-gen.ts +5 -0
- package/src/model/types.ts +51 -39
- package/src/neighbourhood/Neighbourhood.ts +0 -9
- package/src/neighbourhood/NeighbourhoodClient.ts +42 -207
- package/src/neighbourhood/NeighbourhoodProxy.test.ts +34 -36
- package/src/perspectives/LinkQuery.ts +0 -15
- package/src/perspectives/Perspective.ts +1 -12
- package/src/perspectives/PerspectiveClient.test.ts +206 -148
- package/src/perspectives/PerspectiveClient.ts +228 -442
- package/src/perspectives/PerspectiveDiff.ts +0 -8
- package/src/perspectives/PerspectiveHandle.ts +1 -12
- package/src/perspectives/PerspectiveProxy.test.ts +41 -0
- package/src/perspectives/PerspectiveProxy.ts +307 -348
- package/src/runtime/RuntimeClient.ts +112 -474
- package/src/runtime/RuntimeTypes.ts +144 -0
- package/src/shacl/SHACLShape.test.ts +94 -0
- package/src/shacl/SHACLShape.ts +27 -0
- package/src/typeDefs.ts +3 -5
- package/tsconfig.json +3 -7
- package/shims/reflect-metadata.ts +0 -0
- package/shims/type-graphql.ts +0 -75
- package/src/PubSub.ts +0 -17
- package/src/agent/AgentResolver.ts +0 -263
- package/src/ai/AIResolver.ts +0 -372
- package/src/buildSchema.ts +0 -43
- package/src/expression/ExpressionResolver.ts +0 -64
- package/src/language/LanguageResolver.ts +0 -98
- package/src/model/Subject.ts +0 -159
- package/src/model/hydration-batch.ts +0 -205
- package/src/model/hydration-batching.test.ts +0 -563
- package/src/model/prolog-facts.ts +0 -145
- package/src/model/query-prolog.ts +0 -148
- package/src/model/query-sparql-batch.ts +0 -301
- package/src/model/subscription-delta.test.ts +0 -207
- package/src/model/subscription-pool.ts +0 -211
- package/src/neighbourhood/NeighbourhoodResolver.ts +0 -100
- package/src/perspectives/PerspectiveResolver.ts +0 -364
- package/src/runtime/RuntimeResolver.ts +0 -749
- package/src/unwrapApolloResult.ts +0 -16
|
@@ -1,148 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Prolog query building helpers for the Ad4mModel query pipeline.
|
|
3
|
-
*
|
|
4
|
-
* These pure functions translate the Ad4mModel `Query` DSL into Prolog clause
|
|
5
|
-
* fragments that are assembled by `Ad4mModel.queryToProlog()`.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import type { Where, WhereCondition, Order, ParentScope } from "./types";
|
|
9
|
-
import { capitalize } from "./util";
|
|
10
|
-
|
|
11
|
-
// ── Query clause builders ────────────────────────────────────────────────────
|
|
12
|
-
|
|
13
|
-
export function buildParentQuery(
|
|
14
|
-
parent: ParentScope | undefined,
|
|
15
|
-
resolvedPredicate?: string,
|
|
16
|
-
): string {
|
|
17
|
-
if (!parent || !resolvedPredicate) return '';
|
|
18
|
-
return `triple("${parent.id}", "${resolvedPredicate}", Base)`;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export function buildAuthorAndTimestampQuery(): string {
|
|
22
|
-
// Gets the author and timestamp of a Ad4mModel instance (based on the first link mentioning the base)
|
|
23
|
-
return `
|
|
24
|
-
findall(
|
|
25
|
-
[T, A],
|
|
26
|
-
link(Base, _, _, T, A),
|
|
27
|
-
AllLinks
|
|
28
|
-
),
|
|
29
|
-
sort(AllLinks, SortedLinks),
|
|
30
|
-
SortedLinks = [[Timestamp, Author]|_]
|
|
31
|
-
`;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export function buildPropertiesQuery(properties?: string[]): string {
|
|
35
|
-
// Gets the name, value, and resolve boolean for all (or some) properties on a Ad4mModel instance
|
|
36
|
-
// Resolves literals (if property_resolve/2 is true) to their value - either the data field if it is
|
|
37
|
-
// an Expression in JSON literal, or the direct literal value if it is a simple literal
|
|
38
|
-
// If no properties are provided, all are included
|
|
39
|
-
return `
|
|
40
|
-
findall([PropertyName, PropertyValue, Resolve], (
|
|
41
|
-
% Constrain to specified properties if provided
|
|
42
|
-
${properties ? `member(PropertyName, [${properties.map((name) => `"${name}"`).join(", ")}]),` : ""}
|
|
43
|
-
resolve_property(SubjectClass, Base, PropertyName, PropertyValue, Resolve)
|
|
44
|
-
), Properties)
|
|
45
|
-
`;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export function buildWhereQuery(where: Where = {}): string {
|
|
49
|
-
// Constrains the query to instances that match the provided where conditions
|
|
50
|
-
// 'id' maps to the Prolog 'Base' variable (the base expression of the instance).
|
|
51
|
-
|
|
52
|
-
function formatValue(value) {
|
|
53
|
-
// Wrap strings in quotes
|
|
54
|
-
return typeof value === "string" ? `"${value}"` : value;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
return (Object.entries(where) as [string, WhereCondition][])
|
|
58
|
-
.map(([key, value]) => {
|
|
59
|
-
const isSpecial = ["id", "author", "timestamp"].includes(key);
|
|
60
|
-
const getter = `resolve_property(SubjectClass, Base, "${key}", Value${key}, _)`;
|
|
61
|
-
// For 'id' the Prolog variable is always 'Base'
|
|
62
|
-
const field = key === "id" ? "Base" : capitalize(key);
|
|
63
|
-
|
|
64
|
-
// Handle direct array values (for IN conditions)
|
|
65
|
-
if (Array.isArray(value)) {
|
|
66
|
-
const formattedValues = value.map((v) => formatValue(v)).join(", ");
|
|
67
|
-
if (isSpecial) return `member(${field}, [${formattedValues}])`;
|
|
68
|
-
else return `${getter}, member(Value${key}, [${formattedValues}])`;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// Handle operation object
|
|
72
|
-
if (typeof value === "object" && value !== null) {
|
|
73
|
-
const { not, between, lt, lte, gt, gte } = value;
|
|
74
|
-
|
|
75
|
-
// Handle NOT operation
|
|
76
|
-
if (not !== undefined) {
|
|
77
|
-
if (Array.isArray(not)) {
|
|
78
|
-
// NOT IN array
|
|
79
|
-
const formattedValues = not.map((v) => formatValue(v)).join(", ");
|
|
80
|
-
if (isSpecial) return `\\+ member(${field}, [${formattedValues}])`;
|
|
81
|
-
else return `${getter}, \\+ member(Value${key}, [${formattedValues}])`;
|
|
82
|
-
} else {
|
|
83
|
-
// NOT EQUAL
|
|
84
|
-
if (isSpecial) return `${field} \\= ${formatValue(not)}`;
|
|
85
|
-
else return `${getter}, Value${key} \\= ${formatValue(not)}`;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// Handle BETWEEN
|
|
90
|
-
if (between !== undefined && Array.isArray(between) && between.length === 2) {
|
|
91
|
-
if (isSpecial) return `${field} >= ${between[0]}, ${field} =< ${between[1]}`;
|
|
92
|
-
else return `${getter}, Value${key} >= ${between[0]}, Value${key} =< ${between[1]}`;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
// Handle lt, lte, gt, & gte operations
|
|
96
|
-
const operators = [
|
|
97
|
-
{ value: lt, symbol: "<" }, // LESS THAN
|
|
98
|
-
{ value: lte, symbol: "=<" }, // LESS THAN OR EQUAL TO
|
|
99
|
-
{ value: gt, symbol: ">" }, // GREATER THAN
|
|
100
|
-
{ value: gte, symbol: ">=" }, // GREATER THAN OR EQUAL TO
|
|
101
|
-
];
|
|
102
|
-
|
|
103
|
-
for (const { value, symbol } of operators) {
|
|
104
|
-
if (value !== undefined)
|
|
105
|
-
return isSpecial ? `${field} ${symbol} ${value}` : `${getter}, Value${key} ${symbol} ${value}`;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
// Default to direct equality
|
|
110
|
-
if (isSpecial) return `${field} = ${formatValue(value)}`;
|
|
111
|
-
else return `${getter}, Value${key} = ${formatValue(value)}`;
|
|
112
|
-
})
|
|
113
|
-
.join(", ");
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
export function buildCountQuery(count?: boolean): string {
|
|
117
|
-
return count ? "length(UnsortedInstances, TotalCount)" : "";
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
export function buildOrderQuery(order?: Order): string {
|
|
121
|
-
if (!order) return "SortedInstances = UnsortedInstances";
|
|
122
|
-
const entries = Object.entries(order);
|
|
123
|
-
if (entries.length === 1) {
|
|
124
|
-
const [propertyName, direction] = entries[0];
|
|
125
|
-
return `sort_instances(UnsortedInstances, "${propertyName}", "${direction}", SortedInstances)`;
|
|
126
|
-
}
|
|
127
|
-
// Multi-field sort: sort from least-significant to most-significant key
|
|
128
|
-
// so that the final (primary) sort preserves secondary-key ordering for equal values.
|
|
129
|
-
// The merge_sort implementation is stable (equal elements keep original order).
|
|
130
|
-
const clauses: string[] = [];
|
|
131
|
-
for (let i = entries.length - 1; i >= 0; i--) {
|
|
132
|
-
const [propertyName, direction] = entries[i];
|
|
133
|
-
const inputVar = i === entries.length - 1 ? "UnsortedInstances" : `MultiSortIntermediate${i + 1}`;
|
|
134
|
-
const outputVar = i === 0 ? "SortedInstances" : `MultiSortIntermediate${i}`;
|
|
135
|
-
clauses.push(`sort_instances(${inputVar}, "${propertyName}", "${direction}", ${outputVar})`);
|
|
136
|
-
}
|
|
137
|
-
return clauses.join(",\n ");
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
export function buildOffsetQuery(offset?: number): string {
|
|
141
|
-
if (!offset || offset < 0) return "InstancesWithOffset = SortedInstances";
|
|
142
|
-
return `skipN(SortedInstances, ${offset}, InstancesWithOffset)`;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
export function buildLimitQuery(limit?: number): string {
|
|
146
|
-
if (!limit || limit < 0) return "AllInstances = InstancesWithOffset";
|
|
147
|
-
return `takeN(InstancesWithOffset, ${limit}, AllInstances)`;
|
|
148
|
-
}
|
|
@@ -1,301 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Batch SPARQL query builder for Ad4mModel eager-loading (includes).
|
|
3
|
-
*
|
|
4
|
-
* Uses direct triple + named graph storage model.
|
|
5
|
-
* Each AD4M link is stored as: <source> <predicate> <target> in a named graph
|
|
6
|
-
* with metadata in the default graph keyed by graph IRI.
|
|
7
|
-
*
|
|
8
|
-
* @module
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import { resolveParentPredicate } from "./query-common";
|
|
12
|
-
import { getRelationsMetadata } from "./decorators";
|
|
13
|
-
import type { RelationMetadataEntry } from "./decorators";
|
|
14
|
-
import { formatSPARQLValue } from "./query-sparql";
|
|
15
|
-
import { Literal } from "../Literal";
|
|
16
|
-
import type { Query, IncludeMap, ModelMetadata, PropertyMetadata } from "./types";
|
|
17
|
-
|
|
18
|
-
// ──────────────────────────────────────────────────────────
|
|
19
|
-
// Helpers
|
|
20
|
-
// ──────────────────────────────────────────────────────────
|
|
21
|
-
|
|
22
|
-
function iri(value: string): string {
|
|
23
|
-
return `<${value}>`;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Check if a string value looks like a URI (has a scheme).
|
|
28
|
-
*/
|
|
29
|
-
function looksLikeUri(value: string): boolean {
|
|
30
|
-
return /^[a-zA-Z][a-zA-Z0-9+\-._]*:/.test(value);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Convert a JS value to its literal: IRI form.
|
|
35
|
-
*/
|
|
36
|
-
function valueToLiteralIri(value: any): string {
|
|
37
|
-
if (typeof value === 'string') {
|
|
38
|
-
if (looksLikeUri(value)) return value;
|
|
39
|
-
return Literal.from(value).toUrl();
|
|
40
|
-
}
|
|
41
|
-
if (typeof value === 'number') return Literal.from(value).toUrl();
|
|
42
|
-
if (typeof value === 'boolean') return Literal.from(value).toUrl();
|
|
43
|
-
return Literal.from(String(value)).toUrl();
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Determine if a property stores values as literal: IRIs.
|
|
48
|
-
*/
|
|
49
|
-
function isLiteralStoredProperty(propMeta: PropertyMetadata): boolean {
|
|
50
|
-
if (propMeta.flag) return false;
|
|
51
|
-
if (propMeta.resolveLanguage && propMeta.resolveLanguage !== "literal") return false;
|
|
52
|
-
return true;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
interface DepthBranch {
|
|
56
|
-
depth: number;
|
|
57
|
-
relationName: string;
|
|
58
|
-
parentDepth: number;
|
|
59
|
-
parentPredicate: string;
|
|
60
|
-
targetClass: any;
|
|
61
|
-
targetMetadata: ModelMetadata;
|
|
62
|
-
includeMap?: IncludeMap;
|
|
63
|
-
direction: 'forward' | 'reverse';
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
function buildConformanceJoins(metadata: ModelMetadata, sourceVar: string, prefix: string): { joins: string[]; filters: string[] } {
|
|
67
|
-
const joins: string[] = [];
|
|
68
|
-
const filters: string[] = [];
|
|
69
|
-
let hasConformance = false;
|
|
70
|
-
|
|
71
|
-
for (const [, propMeta] of Object.entries(metadata.properties)) {
|
|
72
|
-
if (propMeta.required) {
|
|
73
|
-
if (propMeta.getter) continue;
|
|
74
|
-
hasConformance = true;
|
|
75
|
-
if (propMeta.flag && propMeta.initial) {
|
|
76
|
-
joins.push(`
|
|
77
|
-
${sourceVar} ${iri(propMeta.predicate)} ${iri(propMeta.initial)} .`);
|
|
78
|
-
} else {
|
|
79
|
-
joins.push(`
|
|
80
|
-
${sourceVar} ${iri(propMeta.predicate)} ?${prefix}_cfTarget_${propMeta.name} .`);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
if (!hasConformance) {
|
|
86
|
-
for (const [, propMeta] of Object.entries(metadata.properties)) {
|
|
87
|
-
if (propMeta.initial) {
|
|
88
|
-
if (propMeta.flag) {
|
|
89
|
-
joins.push(`
|
|
90
|
-
${sourceVar} ${iri(propMeta.predicate)} ${iri(propMeta.initial)} .`);
|
|
91
|
-
} else {
|
|
92
|
-
joins.push(`
|
|
93
|
-
${sourceVar} ${iri(propMeta.predicate)} ?${prefix}_cfInitAny_${propMeta.name} .`);
|
|
94
|
-
}
|
|
95
|
-
break;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
return { joins, filters };
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
function flattenIncludeTree(
|
|
104
|
-
parentDepth: number,
|
|
105
|
-
parentClass: any,
|
|
106
|
-
includeMap: IncludeMap,
|
|
107
|
-
nextDepth: { value: number },
|
|
108
|
-
): DepthBranch[] {
|
|
109
|
-
const branches: DepthBranch[] = [];
|
|
110
|
-
const relMeta = getRelationsMetadata(parentClass);
|
|
111
|
-
|
|
112
|
-
for (const [relName, includeValue] of Object.entries(includeMap)) {
|
|
113
|
-
const meta: RelationMetadataEntry | undefined = relMeta[relName];
|
|
114
|
-
if (!meta || !meta.target) continue;
|
|
115
|
-
|
|
116
|
-
const TargetClass = meta.target();
|
|
117
|
-
const targetMetadata: ModelMetadata = (TargetClass as any).getModelMetadata
|
|
118
|
-
? (TargetClass as any).getModelMetadata()
|
|
119
|
-
: { className: "Unknown", properties: {}, relations: {} };
|
|
120
|
-
|
|
121
|
-
const depth = nextDepth.value++;
|
|
122
|
-
const direction = meta.kind === 'belongsToOne' || meta.kind === 'belongsToMany'
|
|
123
|
-
? 'reverse' : 'forward';
|
|
124
|
-
|
|
125
|
-
const nestedInclude = typeof includeValue === 'object' && includeValue !== null
|
|
126
|
-
? (includeValue as any).include
|
|
127
|
-
: undefined;
|
|
128
|
-
|
|
129
|
-
branches.push({
|
|
130
|
-
depth,
|
|
131
|
-
relationName: relName,
|
|
132
|
-
parentDepth,
|
|
133
|
-
parentPredicate: meta.predicate,
|
|
134
|
-
targetClass: TargetClass,
|
|
135
|
-
targetMetadata,
|
|
136
|
-
includeMap: nestedInclude,
|
|
137
|
-
direction,
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
if (nestedInclude) {
|
|
141
|
-
branches.push(...flattenIncludeTree(depth, TargetClass, nestedInclude, nextDepth));
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
return branches;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
// ──────────────────────────────────────────────────────────
|
|
149
|
-
// Main entry point
|
|
150
|
-
// ──────────────────────────────────────────────────────────
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* Build a single SPARQL query that fetches root instances plus all
|
|
154
|
-
* included relations in one query using UNION branches.
|
|
155
|
-
*
|
|
156
|
-
* Uses direct triple patterns in named graphs with metadata in default graph.
|
|
157
|
-
*/
|
|
158
|
-
export function buildBatchSPARQLQuery(
|
|
159
|
-
rootMetadata: ModelMetadata,
|
|
160
|
-
query: Query,
|
|
161
|
-
rootModelClass: any,
|
|
162
|
-
): string {
|
|
163
|
-
const includeMap = query.include;
|
|
164
|
-
if (!includeMap || Object.keys(includeMap).length === 0) {
|
|
165
|
-
throw new Error("buildBatchSPARQLQuery requires query.include to be non-empty");
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
const nextDepth = { value: 1 };
|
|
169
|
-
const branches = flattenIncludeTree(0, rootModelClass, includeMap, nextDepth);
|
|
170
|
-
|
|
171
|
-
// Root conformance
|
|
172
|
-
const rootConformance = buildConformanceJoins(rootMetadata, "?source", "root");
|
|
173
|
-
const rootJoins = [...rootConformance.joins];
|
|
174
|
-
const rootFilters = [...rootConformance.filters];
|
|
175
|
-
|
|
176
|
-
if (query.parent) {
|
|
177
|
-
const parentPredicate = resolveParentPredicate(query.parent, rootModelClass);
|
|
178
|
-
rootJoins.push(`
|
|
179
|
-
${iri(query.parent.id)} ${iri(parentPredicate)} ?source .`);
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
if (query.where) {
|
|
183
|
-
for (const [propertyName, condition] of Object.entries(query.where)) {
|
|
184
|
-
if (propertyName === "base" || propertyName === "id") {
|
|
185
|
-
if (Array.isArray(condition)) {
|
|
186
|
-
const formatted = (condition as any[]).map(v => iri(v)).join(", ");
|
|
187
|
-
rootFilters.push(`?source IN (${formatted})`);
|
|
188
|
-
} else if (typeof condition === "string") {
|
|
189
|
-
rootFilters.push(`?source = ${iri(condition)}`);
|
|
190
|
-
}
|
|
191
|
-
continue;
|
|
192
|
-
}
|
|
193
|
-
if (propertyName === "author" || propertyName === "timestamp") continue;
|
|
194
|
-
|
|
195
|
-
const propMeta = rootMetadata.properties[propertyName];
|
|
196
|
-
if (!propMeta) continue;
|
|
197
|
-
|
|
198
|
-
const useParseLiteral = isLiteralStoredProperty(propMeta);
|
|
199
|
-
|
|
200
|
-
if (typeof condition === "string" || typeof condition === "number" || typeof condition === "boolean") {
|
|
201
|
-
if (useParseLiteral) {
|
|
202
|
-
// For literal-stored properties, just add a join for conformance — filtering is done in JS
|
|
203
|
-
rootJoins.push(`
|
|
204
|
-
?source ${iri(propMeta.predicate)} ?root_wTarget_eq_${propertyName} .`);
|
|
205
|
-
} else {
|
|
206
|
-
rootJoins.push(`
|
|
207
|
-
?source ${iri(propMeta.predicate)} ${iri(String(condition))} .`);
|
|
208
|
-
}
|
|
209
|
-
} else if (Array.isArray(condition)) {
|
|
210
|
-
if (useParseLiteral) {
|
|
211
|
-
// For literal-stored properties, just add a join for conformance — filtering is done in JS
|
|
212
|
-
rootJoins.push(`
|
|
213
|
-
?source ${iri(propMeta.predicate)} ?root_wTarget_${propertyName} .`);
|
|
214
|
-
} else {
|
|
215
|
-
const formatted = (condition as any[]).map(v => iri(v)).join(", ");
|
|
216
|
-
rootJoins.push(`
|
|
217
|
-
?source ${iri(propMeta.predicate)} ?root_wTarget_${propertyName} .`);
|
|
218
|
-
rootFilters.push(`?root_wTarget_${propertyName} IN (${formatted})`);
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
const rootJoinClause = rootJoins.join("\n");
|
|
225
|
-
const rootFilterClause = rootFilters.length > 0
|
|
226
|
-
? `FILTER(\n ${rootFilters.join(" &&\n ")}\n )`
|
|
227
|
-
: "";
|
|
228
|
-
|
|
229
|
-
const unionBranches: string[] = [];
|
|
230
|
-
|
|
231
|
-
// Depth 0: root — direct triple pattern with named graph
|
|
232
|
-
unionBranches.push(`
|
|
233
|
-
{
|
|
234
|
-
${rootJoinClause}
|
|
235
|
-
GRAPH ?linkGraph { ?source ?predicate ?target . }
|
|
236
|
-
FILTER(isIRI(?source) && isIRI(?predicate))
|
|
237
|
-
?linkGraph <ad4m://ontology/author> ?author .
|
|
238
|
-
?linkGraph <ad4m://ontology/timestamp> ?timestamp .
|
|
239
|
-
${rootFilterClause}
|
|
240
|
-
BIND("0" AS ?depth)
|
|
241
|
-
BIND("" AS ?parentBase)
|
|
242
|
-
BIND("" AS ?relationName)
|
|
243
|
-
}
|
|
244
|
-
`);
|
|
245
|
-
|
|
246
|
-
// Depth N: include branches
|
|
247
|
-
for (const branch of branches) {
|
|
248
|
-
const childConformance = buildConformanceJoins(branch.targetMetadata, "?source", `d${branch.depth}`);
|
|
249
|
-
const childJoinClause = childConformance.joins.join("\n");
|
|
250
|
-
const childFilterClause = childConformance.filters.length > 0
|
|
251
|
-
? `FILTER(\n ${childConformance.filters.join(" &&\n ")}\n )`
|
|
252
|
-
: "";
|
|
253
|
-
|
|
254
|
-
const parentJoinClause = branch.parentDepth === 0 ? rootJoinClause.replace(/\?source/g, '?parentBase') : '';
|
|
255
|
-
const parentFilterClause = branch.parentDepth === 0 ? rootFilterClause.replace(/\?source/g, '?parentBase') : '';
|
|
256
|
-
|
|
257
|
-
if (branch.direction === 'forward') {
|
|
258
|
-
// Forward: parent --predicate--> child (source)
|
|
259
|
-
unionBranches.push(`
|
|
260
|
-
{
|
|
261
|
-
# Depth ${branch.depth}: ${branch.relationName} (forward)
|
|
262
|
-
?parentBase ${iri(branch.parentPredicate)} ?source .
|
|
263
|
-
${childJoinClause}
|
|
264
|
-
GRAPH ?linkGraph { ?source ?predicate ?target . }
|
|
265
|
-
FILTER(isIRI(?source) && isIRI(?predicate))
|
|
266
|
-
?linkGraph <ad4m://ontology/author> ?author .
|
|
267
|
-
?linkGraph <ad4m://ontology/timestamp> ?timestamp .
|
|
268
|
-
${childFilterClause}
|
|
269
|
-
${parentJoinClause}
|
|
270
|
-
${parentFilterClause}
|
|
271
|
-
BIND("${branch.depth}" AS ?depth)
|
|
272
|
-
BIND("${branch.relationName}" AS ?relationName)
|
|
273
|
-
}
|
|
274
|
-
`);
|
|
275
|
-
} else {
|
|
276
|
-
// Reverse: child (source) --predicate--> parent
|
|
277
|
-
unionBranches.push(`
|
|
278
|
-
{
|
|
279
|
-
# Depth ${branch.depth}: ${branch.relationName} (reverse)
|
|
280
|
-
?source ${iri(branch.parentPredicate)} ?parentBase .
|
|
281
|
-
${childJoinClause}
|
|
282
|
-
GRAPH ?linkGraph { ?source ?predicate ?target . }
|
|
283
|
-
FILTER(isIRI(?source) && isIRI(?predicate))
|
|
284
|
-
?linkGraph <ad4m://ontology/author> ?author .
|
|
285
|
-
?linkGraph <ad4m://ontology/timestamp> ?timestamp .
|
|
286
|
-
${childFilterClause}
|
|
287
|
-
${parentJoinClause}
|
|
288
|
-
${parentFilterClause}
|
|
289
|
-
BIND("${branch.depth}" AS ?depth)
|
|
290
|
-
BIND("${branch.relationName}" AS ?relationName)
|
|
291
|
-
}
|
|
292
|
-
`);
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
return `
|
|
297
|
-
SELECT ?depth ?parentBase ?relationName ?source ?predicate ?target ?author ?timestamp WHERE {
|
|
298
|
-
${unionBranches.join("\n UNION\n")}
|
|
299
|
-
}
|
|
300
|
-
`.trim();
|
|
301
|
-
}
|
|
@@ -1,207 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Tests for the delta fast-path logic used in subscription-pool.ts.
|
|
3
|
-
*
|
|
4
|
-
* The delta optimisation in pooledSubscribe's onResult handler:
|
|
5
|
-
* 1. Checks if new rows are a strict superset of previous rows
|
|
6
|
-
* 2. Extracts added rows (new triples not in prev set)
|
|
7
|
-
* 3. Verifies ALL added rows have source URIs not already in previous results
|
|
8
|
-
* 4. If condition 3 fails (existing source got new links), falls back to full rehydration
|
|
9
|
-
*
|
|
10
|
-
* These tests validate the detection logic in isolation.
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
type Row = { source: string; predicate: string; target: string };
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Replicates the delta-path eligibility check from subscription-pool.ts
|
|
17
|
-
*/
|
|
18
|
-
function detectDeltaPath(prevArr: Row[], newArr: Row[]): {
|
|
19
|
-
useDelta: boolean;
|
|
20
|
-
addedRows: Row[];
|
|
21
|
-
reason?: string;
|
|
22
|
-
} {
|
|
23
|
-
if (newArr.length <= prevArr.length) {
|
|
24
|
-
return { useDelta: false, addedRows: [], reason: 'no growth' };
|
|
25
|
-
}
|
|
26
|
-
if (newArr.length - prevArr.length > 5) {
|
|
27
|
-
return { useDelta: false, addedRows: [], reason: 'delta too large' };
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const prevSources = new Set(prevArr.map(r => `${r.source}|${r.predicate}|${r.target}`));
|
|
31
|
-
const addedRows = newArr.filter(r => !prevSources.has(`${r.source}|${r.predicate}|${r.target}`));
|
|
32
|
-
|
|
33
|
-
const newSources = new Set(newArr.map(r => `${r.source}|${r.predicate}|${r.target}`));
|
|
34
|
-
const removedRows = prevArr.filter(r => !newSources.has(`${r.source}|${r.predicate}|${r.target}`));
|
|
35
|
-
|
|
36
|
-
if (removedRows.length > 0) {
|
|
37
|
-
return { useDelta: false, addedRows, reason: 'rows removed' };
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// Key check: all added rows must have NEW source URIs
|
|
41
|
-
const prevSourceIds = new Set(prevArr.map(r => r.source));
|
|
42
|
-
const allNewSources = addedRows.every(r => !prevSourceIds.has(r.source));
|
|
43
|
-
|
|
44
|
-
if (!allNewSources) {
|
|
45
|
-
return { useDelta: false, addedRows, reason: 'existing source got new links' };
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
return { useDelta: true, addedRows };
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
describe('delta fast-path detection', () => {
|
|
52
|
-
const prev: Row[] = [
|
|
53
|
-
{ source: 'a', predicate: 'p1', target: 't1' },
|
|
54
|
-
{ source: 'a', predicate: 'p2', target: 't2' },
|
|
55
|
-
{ source: 'b', predicate: 'p1', target: 't3' },
|
|
56
|
-
];
|
|
57
|
-
|
|
58
|
-
it('appends genuinely new items via delta path', () => {
|
|
59
|
-
const next: Row[] = [
|
|
60
|
-
...prev,
|
|
61
|
-
{ source: 'c', predicate: 'p1', target: 't4' },
|
|
62
|
-
];
|
|
63
|
-
const result = detectDeltaPath(prev, next);
|
|
64
|
-
expect(result.useDelta).toBe(true);
|
|
65
|
-
expect(result.addedRows).toHaveLength(1);
|
|
66
|
-
expect(result.addedRows[0].source).toBe('c');
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
it('falls back when existing source gets new links', () => {
|
|
70
|
-
const next: Row[] = [
|
|
71
|
-
...prev,
|
|
72
|
-
{ source: 'a', predicate: 'p3', target: 't5' }, // source 'a' already exists
|
|
73
|
-
];
|
|
74
|
-
const result = detectDeltaPath(prev, next);
|
|
75
|
-
expect(result.useDelta).toBe(false);
|
|
76
|
-
expect(result.reason).toBe('existing source got new links');
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
it('falls back when rows are removed (even if new ones added)', () => {
|
|
80
|
-
const next: Row[] = [
|
|
81
|
-
{ source: 'a', predicate: 'p1', target: 't1' },
|
|
82
|
-
{ source: 'a', predicate: 'p2', target: 't2' },
|
|
83
|
-
// source 'b' row removed, two new sources added so length grows
|
|
84
|
-
{ source: 'c', predicate: 'p1', target: 't4' },
|
|
85
|
-
{ source: 'd', predicate: 'p1', target: 't5' },
|
|
86
|
-
];
|
|
87
|
-
const result = detectDeltaPath(prev, next);
|
|
88
|
-
expect(result.useDelta).toBe(false);
|
|
89
|
-
expect(result.reason).toBe('rows removed');
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
it('falls back when delta is too large (>5 new rows)', () => {
|
|
93
|
-
const next: Row[] = [
|
|
94
|
-
...prev,
|
|
95
|
-
...Array.from({ length: 6 }, (_, i) => ({
|
|
96
|
-
source: `new${i}`, predicate: 'p1', target: `t${10 + i}`,
|
|
97
|
-
})),
|
|
98
|
-
];
|
|
99
|
-
const result = detectDeltaPath(prev, next);
|
|
100
|
-
expect(result.useDelta).toBe(false);
|
|
101
|
-
expect(result.reason).toBe('delta too large');
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
it('falls back when no growth', () => {
|
|
105
|
-
const result = detectDeltaPath(prev, prev);
|
|
106
|
-
expect(result.useDelta).toBe(false);
|
|
107
|
-
expect(result.reason).toBe('no growth');
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
it('handles multiple new sources in one delta', () => {
|
|
111
|
-
const next: Row[] = [
|
|
112
|
-
...prev,
|
|
113
|
-
{ source: 'c', predicate: 'p1', target: 't4' },
|
|
114
|
-
{ source: 'd', predicate: 'p1', target: 't5' },
|
|
115
|
-
];
|
|
116
|
-
const result = detectDeltaPath(prev, next);
|
|
117
|
-
expect(result.useDelta).toBe(true);
|
|
118
|
-
expect(result.addedRows).toHaveLength(2);
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
// ── Test #8: Rapid successive additions ──
|
|
122
|
-
it('handles 10 items added in quick succession without duplicates', () => {
|
|
123
|
-
let current = [...prev];
|
|
124
|
-
for (let i = 0; i < 10; i++) {
|
|
125
|
-
const next = [...current, { source: `rapid-${i}`, predicate: 'p1', target: `t-rapid-${i}` }];
|
|
126
|
-
const result = detectDeltaPath(current, next);
|
|
127
|
-
// Each individual addition should be eligible for delta
|
|
128
|
-
expect(result.useDelta).toBe(true);
|
|
129
|
-
expect(result.addedRows).toHaveLength(1);
|
|
130
|
-
expect(result.addedRows[0].source).toBe(`rapid-${i}`);
|
|
131
|
-
current = next;
|
|
132
|
-
}
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
// ── Test #9: Addition + removal in same batch ──
|
|
136
|
-
it('falls back when one item added and another removed simultaneously', () => {
|
|
137
|
-
const next: Row[] = [
|
|
138
|
-
{ source: 'a', predicate: 'p1', target: 't1' },
|
|
139
|
-
{ source: 'a', predicate: 'p2', target: 't2' },
|
|
140
|
-
// source 'b' removed, source 'c' added
|
|
141
|
-
{ source: 'c', predicate: 'p1', target: 't4' },
|
|
142
|
-
{ source: 'd', predicate: 'p1', target: 't5' },
|
|
143
|
-
];
|
|
144
|
-
const result = detectDeltaPath(prev, next);
|
|
145
|
-
expect(result.useDelta).toBe(false);
|
|
146
|
-
expect(result.reason).toBe('rows removed');
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
// ── Test #10: Delta with nested relations (detection only) ──
|
|
150
|
-
it('delta path detects new items correctly even when sources have complex URIs', () => {
|
|
151
|
-
const next: Row[] = [
|
|
152
|
-
...prev,
|
|
153
|
-
{ source: 'flux://post/123/comment/456', predicate: 'flux://belongs_to', target: 'flux://post/123' },
|
|
154
|
-
];
|
|
155
|
-
const result = detectDeltaPath(prev, next);
|
|
156
|
-
expect(result.useDelta).toBe(true);
|
|
157
|
-
expect(result.addedRows).toHaveLength(1);
|
|
158
|
-
expect(result.addedRows[0].source).toBe('flux://post/123/comment/456');
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
// ── Test #11: Empty previous result + new items ──
|
|
162
|
-
it('handles empty previous array with new items arriving', () => {
|
|
163
|
-
const empty: Row[] = [];
|
|
164
|
-
const next: Row[] = [
|
|
165
|
-
{ source: 'x', predicate: 'p1', target: 't1' },
|
|
166
|
-
];
|
|
167
|
-
const result = detectDeltaPath(empty, next);
|
|
168
|
-
expect(result.useDelta).toBe(true);
|
|
169
|
-
expect(result.addedRows).toHaveLength(1);
|
|
170
|
-
});
|
|
171
|
-
|
|
172
|
-
it('handles empty previous with no new items', () => {
|
|
173
|
-
const result = detectDeltaPath([], []);
|
|
174
|
-
expect(result.useDelta).toBe(false);
|
|
175
|
-
expect(result.reason).toBe('no growth');
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
// ── Test #12: Source URI with special characters ──
|
|
179
|
-
it('handles URIs with encoded characters, fragments, and query params', () => {
|
|
180
|
-
const specialPrev: Row[] = [
|
|
181
|
-
{ source: 'flux://item%20one', predicate: 'p1', target: 't1' },
|
|
182
|
-
{ source: 'flux://item#section', predicate: 'p2', target: 't2' },
|
|
183
|
-
{ source: 'flux://item?key=val&foo=bar', predicate: 'p3', target: 't3' },
|
|
184
|
-
];
|
|
185
|
-
const specialNext: Row[] = [
|
|
186
|
-
...specialPrev,
|
|
187
|
-
{ source: 'flux://new%C3%A9item', predicate: 'p1', target: 't4' },
|
|
188
|
-
];
|
|
189
|
-
const result = detectDeltaPath(specialPrev, specialNext);
|
|
190
|
-
expect(result.useDelta).toBe(true);
|
|
191
|
-
expect(result.addedRows).toHaveLength(1);
|
|
192
|
-
expect(result.addedRows[0].source).toBe('flux://new%C3%A9item');
|
|
193
|
-
});
|
|
194
|
-
|
|
195
|
-
it('correctly identifies existing source with special chars getting new links', () => {
|
|
196
|
-
const specialPrev: Row[] = [
|
|
197
|
-
{ source: 'flux://item#section', predicate: 'p1', target: 't1' },
|
|
198
|
-
];
|
|
199
|
-
const specialNext: Row[] = [
|
|
200
|
-
...specialPrev,
|
|
201
|
-
{ source: 'flux://item#section', predicate: 'p2', target: 't2' },
|
|
202
|
-
];
|
|
203
|
-
const result = detectDeltaPath(specialPrev, specialNext);
|
|
204
|
-
expect(result.useDelta).toBe(false);
|
|
205
|
-
expect(result.reason).toBe('existing source got new links');
|
|
206
|
-
});
|
|
207
|
-
});
|