@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
|
@@ -4,13 +4,11 @@ import { LinkQuery } from "./LinkQuery";
|
|
|
4
4
|
import { PerspectiveHandle, PerspectiveState } from './PerspectiveHandle'
|
|
5
5
|
import { Perspective } from "./Perspective";
|
|
6
6
|
import { Literal } from "../Literal";
|
|
7
|
-
import { Subject } from "../model/Subject";
|
|
8
7
|
import { ExpressionRendered } from "../expression/Expression";
|
|
9
8
|
import { NeighbourhoodProxy } from "../neighbourhood/NeighbourhoodProxy";
|
|
10
9
|
import { NeighbourhoodExpression } from "../neighbourhood/Neighbourhood";
|
|
11
10
|
import { AIClient } from "../ai/AIClient";
|
|
12
|
-
|
|
13
|
-
import { gql } from "@apollo/client/core";
|
|
11
|
+
|
|
14
12
|
import { getPropertiesMetadata, getRelationsMetadata } from "../model/decorators";
|
|
15
13
|
import { getCachedResult, setCachedResult, invalidatePerspectiveCache } from "../model/query-cache";
|
|
16
14
|
import { AllInstancesResult } from "../model/types";
|
|
@@ -20,9 +18,19 @@ import { SHACLFlow, LinkPattern } from "../shacl/SHACLFlow";
|
|
|
20
18
|
|
|
21
19
|
type QueryCallback = (result: AllInstancesResult) => void;
|
|
22
20
|
|
|
21
|
+
/** Extract namespace prefix from a URI (everything up to and including the last / or #) */
|
|
22
|
+
function extractNamespaceFromUri(uri: string): string {
|
|
23
|
+
const hashIdx = uri.lastIndexOf('#');
|
|
24
|
+
if (hashIdx >= 0) return uri.substring(0, hashIdx + 1);
|
|
25
|
+
const slashIdx = uri.lastIndexOf('/');
|
|
26
|
+
if (slashIdx >= 0) return uri.substring(0, slashIdx + 1);
|
|
27
|
+
const colonIdx = uri.lastIndexOf(':');
|
|
28
|
+
if (colonIdx >= 0) return uri.substring(0, colonIdx + 1);
|
|
29
|
+
return uri;
|
|
30
|
+
}
|
|
23
31
|
|
|
24
32
|
|
|
25
|
-
// Generic
|
|
33
|
+
// Generic unsubscribe interface for event subscriptions
|
|
26
34
|
interface Unsubscribable {
|
|
27
35
|
unsubscribe(): void;
|
|
28
36
|
}
|
|
@@ -32,7 +40,7 @@ interface Unsubscribable {
|
|
|
32
40
|
* This class handles:
|
|
33
41
|
* - Keeping the subscription alive by sending periodic keepalive signals
|
|
34
42
|
* - Managing callbacks for result updates
|
|
35
|
-
* - Subscribing to query updates via
|
|
43
|
+
* - Subscribing to query updates via WebSocket subscriptions
|
|
36
44
|
* - Maintaining the latest query result
|
|
37
45
|
* - Ensuring subscription is fully initialized before allowing access
|
|
38
46
|
* - Cleaning up resources when disposed
|
|
@@ -119,22 +127,32 @@ export class QuerySubscriptionProxy {
|
|
|
119
127
|
initialResult = await this.#client.subscribeQuery(this.#uuid, this.#query);
|
|
120
128
|
this.#subscriptionId = initialResult.subscriptionId;
|
|
121
129
|
|
|
122
|
-
// Process the initial result immediately for fast UX
|
|
123
|
-
|
|
130
|
+
// Process the initial result immediately for fast UX.
|
|
131
|
+
// The subscribeQuery() RPC call already returns the initial result,
|
|
132
|
+
// so treat that as successful initialization instead of waiting for a
|
|
133
|
+
// follow-up WebSocket update that may never arrive until the query changes.
|
|
134
|
+
if (initialResult.result !== undefined) {
|
|
124
135
|
this.#latestResult = initialResult.result;
|
|
125
136
|
this.#notifyCallbacks(initialResult.result);
|
|
137
|
+
|
|
138
|
+
if (this.#initResolve) {
|
|
139
|
+
this.#initResolve(true);
|
|
140
|
+
this.#initResolve = undefined;
|
|
141
|
+
this.#initReject = undefined;
|
|
142
|
+
}
|
|
126
143
|
} else {
|
|
127
144
|
console.warn('⚠️ No initial result returned from subscribeQuery!');
|
|
128
|
-
}
|
|
129
145
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
146
|
+
// Only keep the initialization timeout when the backend did not
|
|
147
|
+
// provide an initial result up front.
|
|
148
|
+
this.#initTimeoutId = setTimeout(() => {
|
|
149
|
+
console.error('Subscription initialization timed out after 30 seconds. Resubscribing...');
|
|
150
|
+
// Recursively retry subscription, catching any errors
|
|
151
|
+
this.subscribe().catch(error => {
|
|
152
|
+
console.error('Error during subscription retry after timeout:', error);
|
|
153
|
+
});
|
|
154
|
+
}, 30000);
|
|
155
|
+
}
|
|
138
156
|
|
|
139
157
|
// Subscribe to query updates
|
|
140
158
|
this.#unsubscribe = this.#client.subscribeToQueryUpdates(
|
|
@@ -281,7 +299,7 @@ export class QuerySubscriptionProxy {
|
|
|
281
299
|
*
|
|
282
300
|
* This method:
|
|
283
301
|
* 1. Stops the keepalive timer
|
|
284
|
-
* 2. Unsubscribes from
|
|
302
|
+
* 2. Unsubscribes from subscription updates
|
|
285
303
|
* 3. Clears all registered callbacks
|
|
286
304
|
* 4. Cleans up any pending initialization timeout
|
|
287
305
|
*
|
|
@@ -376,6 +394,10 @@ export class PerspectiveProxy {
|
|
|
376
394
|
|
|
377
395
|
#handle: PerspectiveHandle
|
|
378
396
|
#client: PerspectiveClient
|
|
397
|
+
|
|
398
|
+
/** @internal Exposed for ModelQueryBuilder subscription management */
|
|
399
|
+
get client(): PerspectiveClient { return this.#client; }
|
|
400
|
+
|
|
379
401
|
#perspectiveLinkAddedCallbacks: LinkCallback[]
|
|
380
402
|
#perspectiveLinkRemovedCallbacks: LinkCallback[]
|
|
381
403
|
#perspectiveLinkUpdatedCallbacks: LinkCallback[]
|
|
@@ -578,6 +600,35 @@ export class PerspectiveProxy {
|
|
|
578
600
|
return result;
|
|
579
601
|
}
|
|
580
602
|
|
|
603
|
+
/**
|
|
604
|
+
* Execute a model query — the executor-side replacement for SPARQL query building + JS hydration.
|
|
605
|
+
*
|
|
606
|
+
* @param className - The model class name (e.g. "Recipe")
|
|
607
|
+
* @param queryJson - Structured query as JSON string
|
|
608
|
+
* @param shapeJson - Optional shape metadata JSON from the model class
|
|
609
|
+
* @returns Object with `instances` array and `totalCount`
|
|
610
|
+
*/
|
|
611
|
+
async modelQuery(className: string, queryJson: string, shapeJson?: string): Promise<{ instances: any[], totalCount: number }> {
|
|
612
|
+
return await this.#client.modelQuery(this.#handle.uuid, className, queryJson, shapeJson);
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
/**
|
|
616
|
+
* Subscribe to model query changes. Builds trigger SPARQL from the model shape
|
|
617
|
+
* internally in Rust, registers a subscription, runs the initial query, and
|
|
618
|
+
* pushes updated results when relevant links change.
|
|
619
|
+
*
|
|
620
|
+
* The subscription reuses the same GraphQL subscription channel as subscribeQuery().
|
|
621
|
+
* Use keepAliveQuery() / disposeQuerySubscription() with the returned subscriptionId.
|
|
622
|
+
*
|
|
623
|
+
* @param className - The model class name
|
|
624
|
+
* @param queryJson - JSON-serialized query parameters (same as modelQuery)
|
|
625
|
+
* @param shapeJson - Optional shape metadata JSON from the model class
|
|
626
|
+
* @returns Object with `subscriptionId` and initial `result`
|
|
627
|
+
*/
|
|
628
|
+
async modelSubscribe(className: string, queryJson: string, shapeJson?: string): Promise<{ subscriptionId: string, result: any }> {
|
|
629
|
+
return await this.#client.modelSubscribe(this.#handle.uuid, className, queryJson, shapeJson);
|
|
630
|
+
}
|
|
631
|
+
|
|
581
632
|
/**
|
|
582
633
|
* Adds a new link to the perspective.
|
|
583
634
|
*
|
|
@@ -1070,7 +1121,7 @@ export class PerspectiveProxy {
|
|
|
1070
1121
|
* Adds Social DNA code to the perspective.
|
|
1071
1122
|
*
|
|
1072
1123
|
* **Recommended:** Use {@link addShacl} instead, which accepts the `SHACLShape` type directly.
|
|
1073
|
-
* This method is primarily for the
|
|
1124
|
+
* This method is primarily for the RPC API layer and legacy Prolog code.
|
|
1074
1125
|
*
|
|
1075
1126
|
* @param name - Unique name for this SDNA definition
|
|
1076
1127
|
* @param sdnaCode - Prolog SDNA code (legacy, can be empty string if shaclJson provided)
|
|
@@ -1331,7 +1382,7 @@ export class PerspectiveProxy {
|
|
|
1331
1382
|
*/
|
|
1332
1383
|
async subjectClasses(): Promise<string[]> {
|
|
1333
1384
|
try {
|
|
1334
|
-
// Query SHACL class links directly — no need for a separate
|
|
1385
|
+
// Query SHACL class links directly — no need for a separate RPC endpoint
|
|
1335
1386
|
const classLinks = await this.get(new LinkQuery({
|
|
1336
1387
|
predicate: "rdf://type",
|
|
1337
1388
|
target: "ad4m://SubjectClass"
|
|
@@ -1423,7 +1474,9 @@ export class PerspectiveProxy {
|
|
|
1423
1474
|
return exprAddr as B extends undefined ? T : string;
|
|
1424
1475
|
}
|
|
1425
1476
|
|
|
1426
|
-
|
|
1477
|
+
// Return the expression address directly — callers should use Ad4mModel or
|
|
1478
|
+
// getSubjectData() to interact with the created instance.
|
|
1479
|
+
return exprAddr as unknown as B extends undefined ? T : string;
|
|
1427
1480
|
}
|
|
1428
1481
|
|
|
1429
1482
|
async getSubjectData<T>(subjectClass: T, exprAddr: string): Promise<T> {
|
|
@@ -1505,7 +1558,7 @@ export class PerspectiveProxy {
|
|
|
1505
1558
|
let className = await this.stringOrTemplateObjectToSubjectClassName(subjectClass)
|
|
1506
1559
|
|
|
1507
1560
|
// Get metadata from SHACL links
|
|
1508
|
-
const metadata = await this.
|
|
1561
|
+
const metadata = await this.getSubjectClassMetadata(className);
|
|
1509
1562
|
if (!metadata) {
|
|
1510
1563
|
console.warn(`isSubjectInstance: No SHACL metadata found for class ${className}`);
|
|
1511
1564
|
return false;
|
|
@@ -1517,44 +1570,17 @@ export class PerspectiveProxy {
|
|
|
1517
1570
|
return links.length > 0;
|
|
1518
1571
|
}
|
|
1519
1572
|
|
|
1520
|
-
//
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
} else {
|
|
1526
|
-
query = new LinkQuery({ source: expression, predicate: triple.predicate });
|
|
1527
|
-
}
|
|
1528
|
-
const links = await this.get(query);
|
|
1529
|
-
|
|
1530
|
-
if (!links || links.length === 0) {
|
|
1531
|
-
return false;
|
|
1532
|
-
}
|
|
1533
|
-
}
|
|
1573
|
+
// Build a single SPARQL ASK query with all required triple patterns
|
|
1574
|
+
const patterns = metadata.requiredTriples.map((t, i) => {
|
|
1575
|
+
const target = t.target ? `<${t.target}>` : `?t${i}`;
|
|
1576
|
+
return `<${expression}> <${t.predicate}> ${target} .`;
|
|
1577
|
+
}).join('\n ');
|
|
1534
1578
|
|
|
1535
|
-
|
|
1579
|
+
const result = await this.querySparql(`ASK WHERE {\n ${patterns}\n}`);
|
|
1580
|
+
return result === true;
|
|
1536
1581
|
}
|
|
1537
1582
|
|
|
1538
1583
|
|
|
1539
|
-
/** For an existing subject instance (existing in the perspective's links)
|
|
1540
|
-
* this function returns a proxy object that can be used to access the subject's
|
|
1541
|
-
* properties and methods.
|
|
1542
|
-
*
|
|
1543
|
-
* @param base URI of the subject's root expression
|
|
1544
|
-
* @param subjectClass Either a string with the name of the subject class, or an object
|
|
1545
|
-
* with the properties of the subject class. In the latter case, the first subject class
|
|
1546
|
-
* that matches the given properties will be used.
|
|
1547
|
-
*/
|
|
1548
|
-
async getSubjectProxy<T>(base: string, subjectClass: T): Promise<T> {
|
|
1549
|
-
if(!await this.isSubjectInstance(base, subjectClass)) {
|
|
1550
|
-
throw `Expression ${base} is not a subject instance of given class: ${JSON.stringify(subjectClass)}`
|
|
1551
|
-
}
|
|
1552
|
-
let className = await this.stringOrTemplateObjectToSubjectClassName(subjectClass)
|
|
1553
|
-
let subject = new Subject(this, base, className)
|
|
1554
|
-
await subject.init()
|
|
1555
|
-
return subject as unknown as T
|
|
1556
|
-
}
|
|
1557
|
-
|
|
1558
1584
|
/**
|
|
1559
1585
|
* Gets subject class metadata from SHACL links using SHACLShape.fromLinks().
|
|
1560
1586
|
* Retrieves the SHACL shape and extracts metadata for instance queries.
|
|
@@ -1564,16 +1590,6 @@ export class PerspectiveProxy {
|
|
|
1564
1590
|
requiredTriples: Array<{predicate: string, target?: string}>,
|
|
1565
1591
|
properties: Map<string, { predicate: string, resolveLanguage?: string }>,
|
|
1566
1592
|
relations: Map<string, { predicate: string, instanceFilter?: string, condition?: string }>
|
|
1567
|
-
} | null> {
|
|
1568
|
-
return this.getSubjectClassMetadataFromSDNA(className);
|
|
1569
|
-
}
|
|
1570
|
-
|
|
1571
|
-
/** @deprecated Use getSubjectClassMetadata() */
|
|
1572
|
-
async getSubjectClassMetadataFromSDNA(className: string): Promise<{
|
|
1573
|
-
requiredPredicates: string[],
|
|
1574
|
-
requiredTriples: Array<{predicate: string, target?: string}>,
|
|
1575
|
-
properties: Map<string, { predicate: string, resolveLanguage?: string }>,
|
|
1576
|
-
relations: Map<string, { predicate: string, instanceFilter?: string, condition?: string }>
|
|
1577
1593
|
} | null> {
|
|
1578
1594
|
try {
|
|
1579
1595
|
// Use getShacl() to retrieve the shape via SHACLShape.fromLinks()
|
|
@@ -1629,6 +1645,7 @@ export class PerspectiveProxy {
|
|
|
1629
1645
|
return null;
|
|
1630
1646
|
}
|
|
1631
1647
|
}
|
|
1648
|
+
|
|
1632
1649
|
/**
|
|
1633
1650
|
* Generates a SPARQL query to find instances based on class metadata.
|
|
1634
1651
|
*/
|
|
@@ -1649,239 +1666,17 @@ export class PerspectiveProxy {
|
|
|
1649
1666
|
return Array.from(sources).map(s => ({ base: s }));
|
|
1650
1667
|
}
|
|
1651
1668
|
|
|
1652
|
-
//
|
|
1653
|
-
const
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
} else {
|
|
1658
|
-
query = new LinkQuery({ predicate: first.predicate });
|
|
1659
|
-
}
|
|
1660
|
-
const candidateLinks = await this.get(query);
|
|
1661
|
-
let candidates = [...new Set(candidateLinks.map(l => l.data.source))];
|
|
1662
|
-
|
|
1663
|
-
// Filter by remaining required triples
|
|
1664
|
-
for (let i = 1; i < metadata.requiredTriples.length; i++) {
|
|
1665
|
-
const triple = metadata.requiredTriples[i];
|
|
1666
|
-
const remaining: string[] = [];
|
|
1667
|
-
for (const expr of candidates) {
|
|
1668
|
-
let q: LinkQuery;
|
|
1669
|
-
if (triple.target) {
|
|
1670
|
-
q = new LinkQuery({ source: expr, predicate: triple.predicate, target: triple.target });
|
|
1671
|
-
} else {
|
|
1672
|
-
q = new LinkQuery({ source: expr, predicate: triple.predicate });
|
|
1673
|
-
}
|
|
1674
|
-
const links = await this.get(q);
|
|
1675
|
-
if (links.length > 0) remaining.push(expr);
|
|
1676
|
-
}
|
|
1677
|
-
candidates = remaining;
|
|
1678
|
-
}
|
|
1679
|
-
|
|
1680
|
-
return candidates.map(c => ({ base: c }));
|
|
1681
|
-
}
|
|
1669
|
+
// Build a single SPARQL SELECT with all required triple patterns joined
|
|
1670
|
+
const patterns = metadata.requiredTriples.map((t, i) => {
|
|
1671
|
+
const target = t.target ? `<${t.target}>` : `?t${i}`;
|
|
1672
|
+
return `?base <${t.predicate}> ${target} .`;
|
|
1673
|
+
}).join('\n ');
|
|
1682
1674
|
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
const metadata = await this.getSubjectClassMetadataFromSDNA(className);
|
|
1689
|
-
if (!metadata) {
|
|
1690
|
-
return undefined;
|
|
1691
|
-
}
|
|
1692
|
-
|
|
1693
|
-
const propMeta = metadata.properties.get(propertyName);
|
|
1694
|
-
if (!propMeta) {
|
|
1695
|
-
return undefined;
|
|
1696
|
-
}
|
|
1697
|
-
|
|
1698
|
-
const links = await this.get(new LinkQuery({ source: baseExpression, predicate: propMeta.predicate }));
|
|
1699
|
-
|
|
1700
|
-
if (!links || links.length === 0) {
|
|
1701
|
-
return undefined;
|
|
1702
|
-
}
|
|
1703
|
-
|
|
1704
|
-
const value = links[0].data.target;
|
|
1705
|
-
|
|
1706
|
-
// Handle expression resolution if needed
|
|
1707
|
-
if (propMeta.resolveLanguage && value) {
|
|
1708
|
-
try {
|
|
1709
|
-
const expression = await this.getExpression(value);
|
|
1710
|
-
try {
|
|
1711
|
-
return JSON.parse(expression.data);
|
|
1712
|
-
} catch (e) {
|
|
1713
|
-
return expression.data;
|
|
1714
|
-
}
|
|
1715
|
-
} catch (err) {
|
|
1716
|
-
return value;
|
|
1717
|
-
}
|
|
1718
|
-
}
|
|
1719
|
-
|
|
1720
|
-
return value;
|
|
1721
|
-
}
|
|
1722
|
-
|
|
1723
|
-
/**
|
|
1724
|
-
* Gets relation values using SPARQL when Prolog fails.
|
|
1725
|
-
* This is used as a fallback in SdnaOnly mode where link data isn't in Prolog.
|
|
1726
|
-
* Note: This is used by Subject.ts (legacy pattern). Ad4mModel.ts uses getModelMetadata() instead.
|
|
1727
|
-
*/
|
|
1728
|
-
async getRelationValuesViaSparql(baseExpression: string, className: string, relationName: string): Promise<any[]> {
|
|
1729
|
-
const metadata = await this.getSubjectClassMetadataFromSDNA(className);
|
|
1730
|
-
if (!metadata) {
|
|
1731
|
-
return [];
|
|
1732
|
-
}
|
|
1733
|
-
|
|
1734
|
-
const relMeta = metadata.relations.get(relationName);
|
|
1735
|
-
if (!relMeta) {
|
|
1736
|
-
return [];
|
|
1737
|
-
}
|
|
1738
|
-
|
|
1739
|
-
const links = await this.get(new LinkQuery({ source: baseExpression, predicate: relMeta.predicate }));
|
|
1740
|
-
|
|
1741
|
-
if (!links || links.length === 0) {
|
|
1742
|
-
return [];
|
|
1743
|
-
}
|
|
1744
|
-
|
|
1745
|
-
// Sort by timestamp ascending
|
|
1746
|
-
links.sort((a, b) => new Date(a.timestamp).getTime() - new Date(b.timestamp).getTime());
|
|
1747
|
-
|
|
1748
|
-
let values = links.map(l => l.data.target).filter(v => v !== "" && v !== '');
|
|
1749
|
-
|
|
1750
|
-
// Apply condition filtering if present
|
|
1751
|
-
if (relMeta.condition && values.length > 0) {
|
|
1752
|
-
try {
|
|
1753
|
-
const filteredValues: string[] = [];
|
|
1754
|
-
|
|
1755
|
-
for (const value of values) {
|
|
1756
|
-
let condition = relMeta.condition
|
|
1757
|
-
.replace(/\$perspective/g, `'${this.uuid}'`)
|
|
1758
|
-
.replace(/\$base/g, `'${baseExpression}'`)
|
|
1759
|
-
.replace(/Target/g, `'${value.replace(/'/g, "\\'")}'`);
|
|
1760
|
-
|
|
1761
|
-
// Parse condition to extract link query parameters
|
|
1762
|
-
// Conditions typically look like: WHERE in.uri = 'X' AND predicate = 'Y' AND out.uri = 'Z'
|
|
1763
|
-
// or: array::len(SELECT * FROM link WHERE ...) > 0
|
|
1764
|
-
// Convert to queryLinks calls
|
|
1765
|
-
const sourceMatch = condition.match(/in\.uri\s*=\s*'([^']+)'/);
|
|
1766
|
-
const predicateMatch = condition.match(/predicate\s*=\s*'([^']+)'/);
|
|
1767
|
-
const targetMatch = condition.match(/out\.uri\s*=\s*'([^']+)'/);
|
|
1768
|
-
|
|
1769
|
-
if (sourceMatch || predicateMatch || targetMatch) {
|
|
1770
|
-
const linkQuery: any = {};
|
|
1771
|
-
if (sourceMatch) linkQuery.source = sourceMatch[1];
|
|
1772
|
-
if (predicateMatch) linkQuery.predicate = predicateMatch[1];
|
|
1773
|
-
if (targetMatch) linkQuery.target = targetMatch[1];
|
|
1774
|
-
|
|
1775
|
-
const matchingLinks = await this.get(new LinkQuery(linkQuery));
|
|
1776
|
-
if (matchingLinks.length > 0) {
|
|
1777
|
-
filteredValues.push(value);
|
|
1778
|
-
}
|
|
1779
|
-
} else {
|
|
1780
|
-
// Can't parse condition, include by default
|
|
1781
|
-
filteredValues.push(value);
|
|
1782
|
-
}
|
|
1783
|
-
}
|
|
1784
|
-
|
|
1785
|
-
values = filteredValues;
|
|
1786
|
-
} catch (error) {
|
|
1787
|
-
console.warn(`Failed to apply condition filter for ${relationName}:`, error);
|
|
1788
|
-
}
|
|
1789
|
-
}
|
|
1790
|
-
|
|
1791
|
-
// Apply instance filter if present - batch-check all values at once
|
|
1792
|
-
if (relMeta.instanceFilter) {
|
|
1793
|
-
try {
|
|
1794
|
-
const filterMetadata = await this.getSubjectClassMetadataFromSDNA(relMeta.instanceFilter);
|
|
1795
|
-
if (!filterMetadata) {
|
|
1796
|
-
// Fallback to sequential checks if metadata isn't available
|
|
1797
|
-
return this.filterInstancesSequential(values, relMeta.instanceFilter);
|
|
1798
|
-
}
|
|
1799
|
-
|
|
1800
|
-
return await this.batchCheckSubjectInstances(values, filterMetadata);
|
|
1801
|
-
} catch (err) {
|
|
1802
|
-
// Fallback to sequential checks on error
|
|
1803
|
-
return this.filterInstancesSequential(values, relMeta.instanceFilter);
|
|
1804
|
-
}
|
|
1805
|
-
}
|
|
1806
|
-
|
|
1807
|
-
return values;
|
|
1808
|
-
}
|
|
1809
|
-
|
|
1810
|
-
/**
|
|
1811
|
-
* Batch-checks multiple expressions against subject class metadata using a single or limited SPARQL queries.
|
|
1812
|
-
* This avoids N+1 query problems by checking all values at once.
|
|
1813
|
-
*/
|
|
1814
|
-
async batchCheckSubjectInstances(
|
|
1815
|
-
expressions: string[],
|
|
1816
|
-
metadata: {
|
|
1817
|
-
requiredPredicates: string[],
|
|
1818
|
-
requiredTriples: Array<{predicate: string, target?: string}>,
|
|
1819
|
-
properties: Map<string, { predicate: string, resolveLanguage?: string }>,
|
|
1820
|
-
relations: Map<string, { predicate: string, instanceFilter?: string, condition?: string }>
|
|
1821
|
-
}
|
|
1822
|
-
): Promise<string[]> {
|
|
1823
|
-
if (expressions.length === 0) {
|
|
1824
|
-
return [];
|
|
1825
|
-
}
|
|
1826
|
-
|
|
1827
|
-
// If no required triples, check which expressions have any links
|
|
1828
|
-
if (metadata.requiredTriples.length === 0) {
|
|
1829
|
-
const results: string[] = [];
|
|
1830
|
-
for (const expr of expressions) {
|
|
1831
|
-
const links = await this.get(new LinkQuery({ source: expr }));
|
|
1832
|
-
if (links.length > 0) results.push(expr);
|
|
1833
|
-
}
|
|
1834
|
-
return results;
|
|
1835
|
-
}
|
|
1836
|
-
|
|
1837
|
-
// For each required triple, find which expressions match
|
|
1838
|
-
const validExpressionSets: Set<string>[] = [];
|
|
1839
|
-
|
|
1840
|
-
for (const triple of metadata.requiredTriples) {
|
|
1841
|
-
const matchingExprs = new Set<string>();
|
|
1842
|
-
for (const expr of expressions) {
|
|
1843
|
-
let query: LinkQuery;
|
|
1844
|
-
if (triple.target) {
|
|
1845
|
-
query = new LinkQuery({ source: expr, predicate: triple.predicate, target: triple.target });
|
|
1846
|
-
} else {
|
|
1847
|
-
query = new LinkQuery({ source: expr, predicate: triple.predicate });
|
|
1848
|
-
}
|
|
1849
|
-
const links = await this.get(query);
|
|
1850
|
-
if (links.length > 0) matchingExprs.add(expr);
|
|
1851
|
-
}
|
|
1852
|
-
validExpressionSets.push(matchingExprs);
|
|
1853
|
-
}
|
|
1854
|
-
|
|
1855
|
-
// Find intersection: expressions that passed ALL required triple checks
|
|
1856
|
-
if (validExpressionSets.length === 0) {
|
|
1857
|
-
return expressions;
|
|
1858
|
-
}
|
|
1859
|
-
|
|
1860
|
-
const firstSet = validExpressionSets[0];
|
|
1861
|
-
const validExpressions = expressions.filter(expr => {
|
|
1862
|
-
return validExpressionSets.every(set => set.has(expr));
|
|
1863
|
-
});
|
|
1864
|
-
|
|
1865
|
-
return validExpressions;
|
|
1866
|
-
}
|
|
1867
|
-
|
|
1868
|
-
/**
|
|
1869
|
-
* Fallback sequential instance checking when batch checking isn't available.
|
|
1870
|
-
*/
|
|
1871
|
-
private async filterInstancesSequential(values: string[], instanceFilter: string): Promise<string[]> {
|
|
1872
|
-
const filteredValues = [];
|
|
1873
|
-
for (const value of values) {
|
|
1874
|
-
try {
|
|
1875
|
-
const isInstance = await this.isSubjectInstance(value, instanceFilter);
|
|
1876
|
-
if (isInstance) {
|
|
1877
|
-
filteredValues.push(value);
|
|
1878
|
-
}
|
|
1879
|
-
} catch (err) {
|
|
1880
|
-
// Skip values that fail instance check
|
|
1881
|
-
continue;
|
|
1882
|
-
}
|
|
1883
|
-
}
|
|
1884
|
-
return filteredValues;
|
|
1675
|
+
const results = await this.querySparql(
|
|
1676
|
+
`SELECT DISTINCT ?base WHERE {\n ${patterns}\n}`
|
|
1677
|
+
);
|
|
1678
|
+
if (!Array.isArray(results)) return [];
|
|
1679
|
+
return results.map((row: any) => ({ base: row.base }));
|
|
1885
1680
|
}
|
|
1886
1681
|
|
|
1887
1682
|
/** Returns all subject instances of the given subject class as proxy objects.
|
|
@@ -1906,7 +1701,7 @@ export class PerspectiveProxy {
|
|
|
1906
1701
|
for(let className of classes) {
|
|
1907
1702
|
//console.log(`getAllSubjectInstances: Processing class ${className}`);
|
|
1908
1703
|
// Query SDNA for metadata, then query SPARQL for instances
|
|
1909
|
-
const metadata = await this.
|
|
1704
|
+
const metadata = await this.getSubjectClassMetadata(className);
|
|
1910
1705
|
//console.log(`getAllSubjectInstances: Got metadata for ${className}:`, metadata);
|
|
1911
1706
|
if (metadata) {
|
|
1912
1707
|
const results = await this.findInstancesByMetadata(metadata);
|
|
@@ -1924,9 +1719,9 @@ export class PerspectiveProxy {
|
|
|
1924
1719
|
// Load the instance data from links
|
|
1925
1720
|
await instance.get();
|
|
1926
1721
|
} else {
|
|
1927
|
-
//
|
|
1928
|
-
|
|
1929
|
-
|
|
1722
|
+
// Return plain data for string-based lookups
|
|
1723
|
+
const data = await this.getSubjectData(className, result.base);
|
|
1724
|
+
instance = { id: result.base, ...data };
|
|
1930
1725
|
}
|
|
1931
1726
|
instances.push(instance as unknown as T);
|
|
1932
1727
|
//console.log(`getAllSubjectInstances: Successfully created subject for ${result.base}`);
|
|
@@ -1942,41 +1737,6 @@ export class PerspectiveProxy {
|
|
|
1942
1737
|
return instances
|
|
1943
1738
|
}
|
|
1944
1739
|
|
|
1945
|
-
/** Returns all subject proxies of the given subject class as proxy objects.
|
|
1946
|
-
* @param subjectClass Either a string with the name of the subject class, or an object
|
|
1947
|
-
* with the properties of the subject class. In the latter case, all subject classes
|
|
1948
|
-
* that match the given properties will be used.
|
|
1949
|
-
*/
|
|
1950
|
-
async getAllSubjectProxies<T>(subjectClass: T): Promise<T[]> {
|
|
1951
|
-
let classes = []
|
|
1952
|
-
if(typeof subjectClass === "string") {
|
|
1953
|
-
classes = [subjectClass]
|
|
1954
|
-
} else {
|
|
1955
|
-
classes = await this.subjectClassesByTemplate(subjectClass as object)
|
|
1956
|
-
}
|
|
1957
|
-
|
|
1958
|
-
let instances = []
|
|
1959
|
-
for(let className of classes) {
|
|
1960
|
-
// Query SDNA for metadata, then query SPARQL for instances
|
|
1961
|
-
const metadata = await this.getSubjectClassMetadataFromSDNA(className);
|
|
1962
|
-
if (metadata) {
|
|
1963
|
-
const results = await this.findInstancesByMetadata(metadata);
|
|
1964
|
-
|
|
1965
|
-
|
|
1966
|
-
for (const result of results || []) {
|
|
1967
|
-
try {
|
|
1968
|
-
let subject = new Subject(this, result.base, className);
|
|
1969
|
-
await subject.init();
|
|
1970
|
-
instances.push(subject as unknown as T);
|
|
1971
|
-
} catch (e) {
|
|
1972
|
-
// Skip subjects that fail to initialize
|
|
1973
|
-
}
|
|
1974
|
-
}
|
|
1975
|
-
}
|
|
1976
|
-
}
|
|
1977
|
-
return instances
|
|
1978
|
-
}
|
|
1979
|
-
|
|
1980
1740
|
|
|
1981
1741
|
/**
|
|
1982
1742
|
* Find a subject class by matching an object's properties/relations against SHACL shapes.
|
|
@@ -2012,16 +1772,17 @@ export class PerspectiveProxy {
|
|
|
2012
1772
|
return null;
|
|
2013
1773
|
}
|
|
2014
1774
|
|
|
2015
|
-
//
|
|
2016
|
-
const
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
|
|
2021
|
-
|
|
2022
|
-
|
|
2023
|
-
|
|
2024
|
-
|
|
1775
|
+
// Single SPARQL query to get all class shapes with their properties and collections
|
|
1776
|
+
const results = await this.querySparql(`
|
|
1777
|
+
SELECT ?shape ?predicate ?target WHERE {
|
|
1778
|
+
{ ?shape <rdf://type> <ad4m://SubjectClass> . BIND(<rdf://type> AS ?predicate) BIND(<ad4m://SubjectClass> AS ?target) }
|
|
1779
|
+
UNION
|
|
1780
|
+
{ ?shape <sh://property> ?target . BIND(<sh://property> AS ?predicate) }
|
|
1781
|
+
UNION
|
|
1782
|
+
{ ?shape <sh://collection> ?target . BIND(<sh://collection> AS ?predicate) }
|
|
1783
|
+
}
|
|
1784
|
+
`);
|
|
1785
|
+
if (!Array.isArray(results) || results.length === 0) return null;
|
|
2025
1786
|
|
|
2026
1787
|
// Build a map of className -> { properties, relations }
|
|
2027
1788
|
const classShapes: Map<string, { shapeUri: string, properties: string[], relations: string[] }> = new Map();
|
|
@@ -2029,7 +1790,7 @@ export class PerspectiveProxy {
|
|
|
2029
1790
|
// First pass: find all subject classes
|
|
2030
1791
|
for (const r of results) {
|
|
2031
1792
|
if (r.predicate === 'rdf://type' && r.target === 'ad4m://SubjectClass') {
|
|
2032
|
-
const source = r.
|
|
1793
|
+
const source = r.shape;
|
|
2033
1794
|
const sepIdx = source.indexOf('://');
|
|
2034
1795
|
if (sepIdx < 0) continue;
|
|
2035
1796
|
const className = source.substring(sepIdx + 3).split('/').pop();
|
|
@@ -2043,7 +1804,7 @@ export class PerspectiveProxy {
|
|
|
2043
1804
|
if (r.predicate === 'sh://property' || r.predicate === 'sh://collection') {
|
|
2044
1805
|
// Match shape source to class (e.g., "recipe://RecipeShape" -> "Recipe")
|
|
2045
1806
|
for (const [className, shape] of classShapes) {
|
|
2046
|
-
if (r.
|
|
1807
|
+
if (r.shape.endsWith(`${className}Shape`)) {
|
|
2047
1808
|
const dotIdx = r.target.lastIndexOf('.');
|
|
2048
1809
|
if (dotIdx < 0) continue;
|
|
2049
1810
|
const name = r.target.substring(dotIdx + 1);
|
|
@@ -2160,6 +1921,204 @@ export class PerspectiveProxy {
|
|
|
2160
1921
|
this.#ensuredSubjectClasses.clear();
|
|
2161
1922
|
}
|
|
2162
1923
|
|
|
1924
|
+
/**
|
|
1925
|
+
* Returns a list of all class names that have been registered as SHACL
|
|
1926
|
+
* subject classes in this perspective (via `ensureSDNASubjectClass` or `addSdna`).
|
|
1927
|
+
*
|
|
1928
|
+
* @returns Array of class name strings (e.g. `["Channel", "Message", "Task"]`)
|
|
1929
|
+
*/
|
|
1930
|
+
async listRegisteredClasses(): Promise<string[]> {
|
|
1931
|
+
const results = await this.querySparql(
|
|
1932
|
+
`SELECT DISTINCT ?class WHERE { ?class <rdf://type> <ad4m://SubjectClass> . }`
|
|
1933
|
+
);
|
|
1934
|
+
if (!Array.isArray(results)) return [];
|
|
1935
|
+
return results.map((row: any) => {
|
|
1936
|
+
const uri: string = row.class || '';
|
|
1937
|
+
// Extract class name from URI like "recipe://Recipe" or "flux://Channel"
|
|
1938
|
+
const hashIdx = uri.lastIndexOf('#');
|
|
1939
|
+
if (hashIdx >= 0) return uri.substring(hashIdx + 1);
|
|
1940
|
+
const slashIdx = uri.lastIndexOf('/');
|
|
1941
|
+
if (slashIdx >= 0) return uri.substring(slashIdx + 1);
|
|
1942
|
+
return uri;
|
|
1943
|
+
}).filter(Boolean);
|
|
1944
|
+
}
|
|
1945
|
+
|
|
1946
|
+
/**
|
|
1947
|
+
* Returns the SHACL shape metadata for a registered class, including
|
|
1948
|
+
* property names, predicates, datatypes, and cardinality constraints.
|
|
1949
|
+
*
|
|
1950
|
+
* @param className - The name of the registered class
|
|
1951
|
+
* @returns Shape metadata or `null` if the class is not registered
|
|
1952
|
+
*/
|
|
1953
|
+
async getClassShape(className: string): Promise<{
|
|
1954
|
+
className: string;
|
|
1955
|
+
shapeUri: string;
|
|
1956
|
+
properties: Array<{
|
|
1957
|
+
name: string;
|
|
1958
|
+
predicate: string;
|
|
1959
|
+
datatype?: string;
|
|
1960
|
+
required: boolean;
|
|
1961
|
+
collection: boolean;
|
|
1962
|
+
writable: boolean;
|
|
1963
|
+
options?: Array<{ value: string; label?: string }>;
|
|
1964
|
+
}>;
|
|
1965
|
+
} | null> {
|
|
1966
|
+
// Find the shape URI for this class
|
|
1967
|
+
const safeName = className.replace(/['"\\]/g, '');
|
|
1968
|
+
const shapeResults = await this.querySparql(
|
|
1969
|
+
`SELECT ?shapeUri ?targetClass WHERE {
|
|
1970
|
+
?targetClass <rdf://type> <ad4m://SubjectClass> .
|
|
1971
|
+
?targetClass <ad4m://shape> ?shapeUri .
|
|
1972
|
+
FILTER(STRENDS(STR(?targetClass), "/${safeName}") || STRENDS(STR(?targetClass), "#${safeName}"))
|
|
1973
|
+
} LIMIT 1`
|
|
1974
|
+
);
|
|
1975
|
+
if (!Array.isArray(shapeResults) || shapeResults.length === 0) return null;
|
|
1976
|
+
|
|
1977
|
+
const shapeUri = shapeResults[0].shapeUri;
|
|
1978
|
+
const classUri = shapeResults[0].targetClass;
|
|
1979
|
+
|
|
1980
|
+
// Query all properties for this shape, including sh:in
|
|
1981
|
+
const propResults = await this.querySparql(
|
|
1982
|
+
`SELECT ?propShape ?path ?datatype ?minCount ?maxCount ?writable ?propType ?shIn WHERE {
|
|
1983
|
+
<${shapeUri}> <sh://property> ?propShape .
|
|
1984
|
+
?propShape <sh://path> ?path .
|
|
1985
|
+
?propShape <rdf://type> ?propType .
|
|
1986
|
+
OPTIONAL { ?propShape <sh://datatype> ?datatype }
|
|
1987
|
+
OPTIONAL { ?propShape <sh://minCount> ?minCount }
|
|
1988
|
+
OPTIONAL { ?propShape <sh://maxCount> ?maxCount }
|
|
1989
|
+
OPTIONAL { ?propShape <ad4m://writable> ?writable }
|
|
1990
|
+
OPTIONAL { ?propShape <sh://in> ?shIn }
|
|
1991
|
+
}`
|
|
1992
|
+
);
|
|
1993
|
+
|
|
1994
|
+
const properties = (Array.isArray(propResults) ? propResults : []).map((row: any) => {
|
|
1995
|
+
// Extract property name from shape URI like "recipe://Recipe.name"
|
|
1996
|
+
const propUri: string = row.propShape || '';
|
|
1997
|
+
const dotIdx = propUri.lastIndexOf('.');
|
|
1998
|
+
const name = dotIdx >= 0 ? propUri.substring(dotIdx + 1) : propUri;
|
|
1999
|
+
|
|
2000
|
+
const minCount = parseInt(row.minCount || '0', 10);
|
|
2001
|
+
const isCollection = row.propType === 'ad4m://CollectionShape';
|
|
2002
|
+
const writable = row.writable === 'true' || row.writable === 'literal:true';
|
|
2003
|
+
|
|
2004
|
+
// Parse sh:in values if present
|
|
2005
|
+
let options: Array<{ value: string; label?: string }> | undefined;
|
|
2006
|
+
if (row.shIn) {
|
|
2007
|
+
try {
|
|
2008
|
+
let raw = row.shIn;
|
|
2009
|
+
// Strip literal: prefix if present, and URI-decode for robustness
|
|
2010
|
+
if (raw.startsWith('literal:string:')) {
|
|
2011
|
+
raw = decodeURIComponent(raw.substring('literal:string:'.length));
|
|
2012
|
+
}
|
|
2013
|
+
options = JSON.parse(raw);
|
|
2014
|
+
} catch { /* ignore parse errors */ }
|
|
2015
|
+
}
|
|
2016
|
+
|
|
2017
|
+
return {
|
|
2018
|
+
name,
|
|
2019
|
+
predicate: row.path || '',
|
|
2020
|
+
datatype: row.datatype || undefined,
|
|
2021
|
+
required: minCount > 0,
|
|
2022
|
+
collection: isCollection,
|
|
2023
|
+
writable,
|
|
2024
|
+
...(options && options.length > 0 ? { options } : {}),
|
|
2025
|
+
};
|
|
2026
|
+
});
|
|
2027
|
+
|
|
2028
|
+
return { className: safeName, shapeUri, properties };
|
|
2029
|
+
}
|
|
2030
|
+
|
|
2031
|
+
/**
|
|
2032
|
+
* Detects which registered subject classes a given instance conforms to,
|
|
2033
|
+
* based on its stored triples matching the class shapes' required properties.
|
|
2034
|
+
*
|
|
2035
|
+
* @param baseExpression - The instance's base expression / subject URI
|
|
2036
|
+
* @returns Array of class names the instance matches
|
|
2037
|
+
*/
|
|
2038
|
+
async getInstanceClasses(baseExpression: string): Promise<string[]> {
|
|
2039
|
+
const safeId = baseExpression.replace(/['"\\]/g, '');
|
|
2040
|
+
// Get all registered classes and their required conformance properties
|
|
2041
|
+
const results = await this.querySparql(
|
|
2042
|
+
`SELECT DISTINCT ?class WHERE {
|
|
2043
|
+
?class <rdf://type> <ad4m://SubjectClass> .
|
|
2044
|
+
?class <ad4m://shape> ?shape .
|
|
2045
|
+
?shape <sh://property> ?propShape .
|
|
2046
|
+
?propShape <sh://minCount> ?mc .
|
|
2047
|
+
FILTER(?mc >= 1)
|
|
2048
|
+
?propShape <sh://path> ?path .
|
|
2049
|
+
<${safeId}> ?path ?_val .
|
|
2050
|
+
}`
|
|
2051
|
+
);
|
|
2052
|
+
if (!Array.isArray(results)) return [];
|
|
2053
|
+
return results.map((row: any) => {
|
|
2054
|
+
const uri: string = row.class || '';
|
|
2055
|
+
const hashIdx = uri.lastIndexOf('#');
|
|
2056
|
+
if (hashIdx >= 0) return uri.substring(hashIdx + 1);
|
|
2057
|
+
const slashIdx = uri.lastIndexOf('/');
|
|
2058
|
+
if (slashIdx >= 0) return uri.substring(slashIdx + 1);
|
|
2059
|
+
return uri;
|
|
2060
|
+
}).filter(Boolean);
|
|
2061
|
+
}
|
|
2062
|
+
|
|
2063
|
+
/**
|
|
2064
|
+
* Returns all named options (sh:in values) for a registered class, grouped by property.
|
|
2065
|
+
*
|
|
2066
|
+
* @param className - The name of the registered class
|
|
2067
|
+
* @returns Record mapping property name → array of { value, label } options
|
|
2068
|
+
*/
|
|
2069
|
+
async getNamedOptions(className: string): Promise<Record<string, Array<{ value: string; label?: string }>>> {
|
|
2070
|
+
const shape = await this.getClassShape(className);
|
|
2071
|
+
if (!shape) return {};
|
|
2072
|
+
const result: Record<string, Array<{ value: string; label?: string }>> = {};
|
|
2073
|
+
for (const prop of shape.properties) {
|
|
2074
|
+
if (prop.options && prop.options.length > 0) {
|
|
2075
|
+
result[prop.name] = prop.options;
|
|
2076
|
+
}
|
|
2077
|
+
}
|
|
2078
|
+
return result;
|
|
2079
|
+
}
|
|
2080
|
+
|
|
2081
|
+
/**
|
|
2082
|
+
* Adds a named option (sh:in value) to a property of a registered class.
|
|
2083
|
+
* This appends to the existing sh:in list stored on the property shape.
|
|
2084
|
+
*
|
|
2085
|
+
* @param className - The registered class name
|
|
2086
|
+
* @param propertyName - The property to add the option to
|
|
2087
|
+
* @param value - The RDF value for the option
|
|
2088
|
+
* @param label - Optional human-readable label
|
|
2089
|
+
*/
|
|
2090
|
+
async addNamedOption(className: string, propertyName: string, value: string, label?: string): Promise<void> {
|
|
2091
|
+
const shape = await this.getClassShape(className);
|
|
2092
|
+
if (!shape) throw new Error(`Class "${className}" not found`);
|
|
2093
|
+
|
|
2094
|
+
const prop = shape.properties.find(p => p.name === propertyName);
|
|
2095
|
+
if (!prop) throw new Error(`Property "${propertyName}" not found on class "${className}"`);
|
|
2096
|
+
|
|
2097
|
+
// Build the property shape URI
|
|
2098
|
+
const ns = extractNamespaceFromUri(shape.shapeUri);
|
|
2099
|
+
const propShapeId = `${ns}${className}.${propertyName}`;
|
|
2100
|
+
|
|
2101
|
+
// Get existing options
|
|
2102
|
+
const existing = prop.options || [];
|
|
2103
|
+
// Don't add duplicates
|
|
2104
|
+
if (existing.some(o => o.value === value)) return;
|
|
2105
|
+
|
|
2106
|
+
const updated = [...existing, { value, ...(label ? { label } : {}) }];
|
|
2107
|
+
|
|
2108
|
+
// Remove old sh:in link if exists by querying for it
|
|
2109
|
+
const oldLinks = await this.get(new LinkQuery({ source: propShapeId, predicate: "sh://in" }));
|
|
2110
|
+
for (const oldLink of oldLinks) {
|
|
2111
|
+
await this.remove(oldLink);
|
|
2112
|
+
}
|
|
2113
|
+
|
|
2114
|
+
// Add new sh:in link
|
|
2115
|
+
await this.add(new Link({
|
|
2116
|
+
source: propShapeId,
|
|
2117
|
+
predicate: "sh://in",
|
|
2118
|
+
target: `literal:string:${JSON.stringify(updated)}`
|
|
2119
|
+
}));
|
|
2120
|
+
}
|
|
2121
|
+
|
|
2163
2122
|
getNeighbourhoodProxy(): NeighbourhoodProxy {
|
|
2164
2123
|
return this.#client.getNeighbourhoodProxy(this.#handle.uuid)
|
|
2165
2124
|
}
|