@coasys/ad4m 0.13.0-test-3 → 0.13.0-test-5
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 +6717 -15519
- package/lib/index.js +4811 -13662
- package/lib/src/Ad4mClient.d.ts +7 -3
- package/lib/src/Ad4mClient.js +39 -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 +76 -44
- package/lib/src/model/Ad4mModel.js +635 -479
- package/lib/src/model/Ad4mModel.test.js +371 -815
- package/lib/src/model/ModelQueryBuilder.d.ts +15 -27
- package/lib/src/model/ModelQueryBuilder.js +163 -224
- package/lib/src/model/decorators.d.ts +22 -0
- package/lib/src/model/decorators.js +7 -0
- package/lib/src/model/hydration.d.ts +3 -93
- package/lib/src/model/hydration.js +3 -970
- 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/query-utils.d.ts +1 -0
- package/lib/src/model/query-utils.js +46 -1
- package/lib/src/model/shacl-gen.js +4 -0
- package/lib/src/model/types.d.ts +51 -43
- 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 +13 -17
- package/lib/src/perspectives/PerspectiveClient.js +179 -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 +106 -71
- package/lib/src/perspectives/PerspectiveProxy.js +289 -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 +6 -18
- package/rollup.config.js +0 -7
- package/src/Ad4mClient.test.ts +1179 -1340
- package/src/Ad4mClient.ts +40 -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 +766 -1173
- package/src/model/Ad4mModel.ts +664 -529
- package/src/model/ModelQueryBuilder.ts +203 -251
- package/src/model/decorators.ts +29 -0
- package/src/model/hydration.ts +3 -1037
- 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/query-utils.ts +47 -2
- package/src/model/shacl-gen.ts +5 -0
- package/src/model/types.ts +55 -44
- 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 +247 -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 +331 -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,59 @@ 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
|
+
* Evaluate property getters for a batch of instances in a single RPC call.
|
|
617
|
+
* Returns a map of `{ instanceId: { prop: value, ... } }`.
|
|
618
|
+
*
|
|
619
|
+
* Use this instead of `Ad4mModel.evaluateGetters()` for lazy-loading
|
|
620
|
+
* getter-backed properties on visible items (e.g. `replyingTo` on messages).
|
|
621
|
+
*
|
|
622
|
+
* @param className - The model class name (e.g. "Message")
|
|
623
|
+
* @param instanceIds - Array of instance base expression URIs
|
|
624
|
+
* @param shapeJson - Shape metadata JSON from the model class
|
|
625
|
+
* @param propertyNames - Optional subset of property names to evaluate
|
|
626
|
+
* @returns Map of instance ID → evaluated property values
|
|
627
|
+
*/
|
|
628
|
+
async evaluateGetters(
|
|
629
|
+
className: string,
|
|
630
|
+
instanceIds: string[],
|
|
631
|
+
shapeJson: string,
|
|
632
|
+
propertyNames?: string[],
|
|
633
|
+
): Promise<Record<string, Record<string, any>>> {
|
|
634
|
+
return await this.#client.evaluateGetters(
|
|
635
|
+
this.#handle.uuid, className, instanceIds, shapeJson, propertyNames,
|
|
636
|
+
);
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
/**
|
|
640
|
+
* Subscribe to model query changes. Builds trigger SPARQL from the model shape
|
|
641
|
+
* internally in Rust, registers a subscription, runs the initial query, and
|
|
642
|
+
* pushes updated results when relevant links change.
|
|
643
|
+
*
|
|
644
|
+
* The subscription reuses the same GraphQL subscription channel as subscribeQuery().
|
|
645
|
+
* Use keepAliveQuery() / disposeQuerySubscription() with the returned subscriptionId.
|
|
646
|
+
*
|
|
647
|
+
* @param className - The model class name
|
|
648
|
+
* @param queryJson - JSON-serialized query parameters (same as modelQuery)
|
|
649
|
+
* @param shapeJson - Optional shape metadata JSON from the model class
|
|
650
|
+
* @returns Object with `subscriptionId` and initial `result`
|
|
651
|
+
*/
|
|
652
|
+
async modelSubscribe(className: string, queryJson: string, shapeJson?: string): Promise<{ subscriptionId: string, result: any }> {
|
|
653
|
+
return await this.#client.modelSubscribe(this.#handle.uuid, className, queryJson, shapeJson);
|
|
654
|
+
}
|
|
655
|
+
|
|
581
656
|
/**
|
|
582
657
|
* Adds a new link to the perspective.
|
|
583
658
|
*
|
|
@@ -1070,7 +1145,7 @@ export class PerspectiveProxy {
|
|
|
1070
1145
|
* Adds Social DNA code to the perspective.
|
|
1071
1146
|
*
|
|
1072
1147
|
* **Recommended:** Use {@link addShacl} instead, which accepts the `SHACLShape` type directly.
|
|
1073
|
-
* This method is primarily for the
|
|
1148
|
+
* This method is primarily for the RPC API layer and legacy Prolog code.
|
|
1074
1149
|
*
|
|
1075
1150
|
* @param name - Unique name for this SDNA definition
|
|
1076
1151
|
* @param sdnaCode - Prolog SDNA code (legacy, can be empty string if shaclJson provided)
|
|
@@ -1331,7 +1406,7 @@ export class PerspectiveProxy {
|
|
|
1331
1406
|
*/
|
|
1332
1407
|
async subjectClasses(): Promise<string[]> {
|
|
1333
1408
|
try {
|
|
1334
|
-
// Query SHACL class links directly — no need for a separate
|
|
1409
|
+
// Query SHACL class links directly — no need for a separate RPC endpoint
|
|
1335
1410
|
const classLinks = await this.get(new LinkQuery({
|
|
1336
1411
|
predicate: "rdf://type",
|
|
1337
1412
|
target: "ad4m://SubjectClass"
|
|
@@ -1423,7 +1498,9 @@ export class PerspectiveProxy {
|
|
|
1423
1498
|
return exprAddr as B extends undefined ? T : string;
|
|
1424
1499
|
}
|
|
1425
1500
|
|
|
1426
|
-
|
|
1501
|
+
// Return the expression address directly — callers should use Ad4mModel or
|
|
1502
|
+
// getSubjectData() to interact with the created instance.
|
|
1503
|
+
return exprAddr as unknown as B extends undefined ? T : string;
|
|
1427
1504
|
}
|
|
1428
1505
|
|
|
1429
1506
|
async getSubjectData<T>(subjectClass: T, exprAddr: string): Promise<T> {
|
|
@@ -1505,7 +1582,7 @@ export class PerspectiveProxy {
|
|
|
1505
1582
|
let className = await this.stringOrTemplateObjectToSubjectClassName(subjectClass)
|
|
1506
1583
|
|
|
1507
1584
|
// Get metadata from SHACL links
|
|
1508
|
-
const metadata = await this.
|
|
1585
|
+
const metadata = await this.getSubjectClassMetadata(className);
|
|
1509
1586
|
if (!metadata) {
|
|
1510
1587
|
console.warn(`isSubjectInstance: No SHACL metadata found for class ${className}`);
|
|
1511
1588
|
return false;
|
|
@@ -1517,44 +1594,17 @@ export class PerspectiveProxy {
|
|
|
1517
1594
|
return links.length > 0;
|
|
1518
1595
|
}
|
|
1519
1596
|
|
|
1520
|
-
//
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
} else {
|
|
1526
|
-
query = new LinkQuery({ source: expression, predicate: triple.predicate });
|
|
1527
|
-
}
|
|
1528
|
-
const links = await this.get(query);
|
|
1597
|
+
// Build a single SPARQL ASK query with all required triple patterns
|
|
1598
|
+
const patterns = metadata.requiredTriples.map((t, i) => {
|
|
1599
|
+
const target = t.target ? `<${t.target}>` : `?t${i}`;
|
|
1600
|
+
return `<${expression}> <${t.predicate}> ${target} .`;
|
|
1601
|
+
}).join('\n ');
|
|
1529
1602
|
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
}
|
|
1533
|
-
}
|
|
1534
|
-
|
|
1535
|
-
return true;
|
|
1603
|
+
const result = await this.querySparql(`ASK WHERE {\n ${patterns}\n}`);
|
|
1604
|
+
return result === true;
|
|
1536
1605
|
}
|
|
1537
1606
|
|
|
1538
1607
|
|
|
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
1608
|
/**
|
|
1559
1609
|
* Gets subject class metadata from SHACL links using SHACLShape.fromLinks().
|
|
1560
1610
|
* Retrieves the SHACL shape and extracts metadata for instance queries.
|
|
@@ -1564,16 +1614,6 @@ export class PerspectiveProxy {
|
|
|
1564
1614
|
requiredTriples: Array<{predicate: string, target?: string}>,
|
|
1565
1615
|
properties: Map<string, { predicate: string, resolveLanguage?: string }>,
|
|
1566
1616
|
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
1617
|
} | null> {
|
|
1578
1618
|
try {
|
|
1579
1619
|
// Use getShacl() to retrieve the shape via SHACLShape.fromLinks()
|
|
@@ -1629,6 +1669,7 @@ export class PerspectiveProxy {
|
|
|
1629
1669
|
return null;
|
|
1630
1670
|
}
|
|
1631
1671
|
}
|
|
1672
|
+
|
|
1632
1673
|
/**
|
|
1633
1674
|
* Generates a SPARQL query to find instances based on class metadata.
|
|
1634
1675
|
*/
|
|
@@ -1649,239 +1690,17 @@ export class PerspectiveProxy {
|
|
|
1649
1690
|
return Array.from(sources).map(s => ({ base: s }));
|
|
1650
1691
|
}
|
|
1651
1692
|
|
|
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
|
-
}
|
|
1682
|
-
|
|
1683
|
-
/**
|
|
1684
|
-
* Gets a property value using SPARQL when Prolog fails.
|
|
1685
|
-
* This is used as a fallback in SdnaOnly mode where link data isn't in Prolog.
|
|
1686
|
-
*/
|
|
1687
|
-
async getPropertyValueViaSparql(baseExpression: string, className: string, propertyName: string): Promise<any> {
|
|
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
|
-
});
|
|
1693
|
+
// Build a single SPARQL SELECT with all required triple patterns joined
|
|
1694
|
+
const patterns = metadata.requiredTriples.map((t, i) => {
|
|
1695
|
+
const target = t.target ? `<${t.target}>` : `?t${i}`;
|
|
1696
|
+
return `?base <${t.predicate}> ${target} .`;
|
|
1697
|
+
}).join('\n ');
|
|
1864
1698
|
|
|
1865
|
-
|
|
1866
|
-
}
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
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;
|
|
1699
|
+
const results = await this.querySparql(
|
|
1700
|
+
`SELECT DISTINCT ?base WHERE {\n ${patterns}\n}`
|
|
1701
|
+
);
|
|
1702
|
+
if (!Array.isArray(results)) return [];
|
|
1703
|
+
return results.map((row: any) => ({ base: row.base }));
|
|
1885
1704
|
}
|
|
1886
1705
|
|
|
1887
1706
|
/** Returns all subject instances of the given subject class as proxy objects.
|
|
@@ -1906,7 +1725,7 @@ export class PerspectiveProxy {
|
|
|
1906
1725
|
for(let className of classes) {
|
|
1907
1726
|
//console.log(`getAllSubjectInstances: Processing class ${className}`);
|
|
1908
1727
|
// Query SDNA for metadata, then query SPARQL for instances
|
|
1909
|
-
const metadata = await this.
|
|
1728
|
+
const metadata = await this.getSubjectClassMetadata(className);
|
|
1910
1729
|
//console.log(`getAllSubjectInstances: Got metadata for ${className}:`, metadata);
|
|
1911
1730
|
if (metadata) {
|
|
1912
1731
|
const results = await this.findInstancesByMetadata(metadata);
|
|
@@ -1924,9 +1743,9 @@ export class PerspectiveProxy {
|
|
|
1924
1743
|
// Load the instance data from links
|
|
1925
1744
|
await instance.get();
|
|
1926
1745
|
} else {
|
|
1927
|
-
//
|
|
1928
|
-
|
|
1929
|
-
|
|
1746
|
+
// Return plain data for string-based lookups
|
|
1747
|
+
const data = await this.getSubjectData(className, result.base);
|
|
1748
|
+
instance = { id: result.base, ...data };
|
|
1930
1749
|
}
|
|
1931
1750
|
instances.push(instance as unknown as T);
|
|
1932
1751
|
//console.log(`getAllSubjectInstances: Successfully created subject for ${result.base}`);
|
|
@@ -1942,41 +1761,6 @@ export class PerspectiveProxy {
|
|
|
1942
1761
|
return instances
|
|
1943
1762
|
}
|
|
1944
1763
|
|
|
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
1764
|
|
|
1981
1765
|
/**
|
|
1982
1766
|
* Find a subject class by matching an object's properties/relations against SHACL shapes.
|
|
@@ -2012,16 +1796,17 @@ export class PerspectiveProxy {
|
|
|
2012
1796
|
return null;
|
|
2013
1797
|
}
|
|
2014
1798
|
|
|
2015
|
-
//
|
|
2016
|
-
const
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
|
|
2021
|
-
|
|
2022
|
-
|
|
2023
|
-
|
|
2024
|
-
|
|
1799
|
+
// Single SPARQL query to get all class shapes with their properties and collections
|
|
1800
|
+
const results = await this.querySparql(`
|
|
1801
|
+
SELECT ?shape ?predicate ?target WHERE {
|
|
1802
|
+
{ ?shape <rdf://type> <ad4m://SubjectClass> . BIND(<rdf://type> AS ?predicate) BIND(<ad4m://SubjectClass> AS ?target) }
|
|
1803
|
+
UNION
|
|
1804
|
+
{ ?shape <sh://property> ?target . BIND(<sh://property> AS ?predicate) }
|
|
1805
|
+
UNION
|
|
1806
|
+
{ ?shape <sh://collection> ?target . BIND(<sh://collection> AS ?predicate) }
|
|
1807
|
+
}
|
|
1808
|
+
`);
|
|
1809
|
+
if (!Array.isArray(results) || results.length === 0) return null;
|
|
2025
1810
|
|
|
2026
1811
|
// Build a map of className -> { properties, relations }
|
|
2027
1812
|
const classShapes: Map<string, { shapeUri: string, properties: string[], relations: string[] }> = new Map();
|
|
@@ -2029,7 +1814,7 @@ export class PerspectiveProxy {
|
|
|
2029
1814
|
// First pass: find all subject classes
|
|
2030
1815
|
for (const r of results) {
|
|
2031
1816
|
if (r.predicate === 'rdf://type' && r.target === 'ad4m://SubjectClass') {
|
|
2032
|
-
const source = r.
|
|
1817
|
+
const source = r.shape;
|
|
2033
1818
|
const sepIdx = source.indexOf('://');
|
|
2034
1819
|
if (sepIdx < 0) continue;
|
|
2035
1820
|
const className = source.substring(sepIdx + 3).split('/').pop();
|
|
@@ -2043,7 +1828,7 @@ export class PerspectiveProxy {
|
|
|
2043
1828
|
if (r.predicate === 'sh://property' || r.predicate === 'sh://collection') {
|
|
2044
1829
|
// Match shape source to class (e.g., "recipe://RecipeShape" -> "Recipe")
|
|
2045
1830
|
for (const [className, shape] of classShapes) {
|
|
2046
|
-
if (r.
|
|
1831
|
+
if (r.shape.endsWith(`${className}Shape`)) {
|
|
2047
1832
|
const dotIdx = r.target.lastIndexOf('.');
|
|
2048
1833
|
if (dotIdx < 0) continue;
|
|
2049
1834
|
const name = r.target.substring(dotIdx + 1);
|
|
@@ -2160,6 +1945,204 @@ export class PerspectiveProxy {
|
|
|
2160
1945
|
this.#ensuredSubjectClasses.clear();
|
|
2161
1946
|
}
|
|
2162
1947
|
|
|
1948
|
+
/**
|
|
1949
|
+
* Returns a list of all class names that have been registered as SHACL
|
|
1950
|
+
* subject classes in this perspective (via `ensureSDNASubjectClass` or `addSdna`).
|
|
1951
|
+
*
|
|
1952
|
+
* @returns Array of class name strings (e.g. `["Channel", "Message", "Task"]`)
|
|
1953
|
+
*/
|
|
1954
|
+
async listRegisteredClasses(): Promise<string[]> {
|
|
1955
|
+
const results = await this.querySparql(
|
|
1956
|
+
`SELECT DISTINCT ?class WHERE { ?class <rdf://type> <ad4m://SubjectClass> . }`
|
|
1957
|
+
);
|
|
1958
|
+
if (!Array.isArray(results)) return [];
|
|
1959
|
+
return results.map((row: any) => {
|
|
1960
|
+
const uri: string = row.class || '';
|
|
1961
|
+
// Extract class name from URI like "recipe://Recipe" or "flux://Channel"
|
|
1962
|
+
const hashIdx = uri.lastIndexOf('#');
|
|
1963
|
+
if (hashIdx >= 0) return uri.substring(hashIdx + 1);
|
|
1964
|
+
const slashIdx = uri.lastIndexOf('/');
|
|
1965
|
+
if (slashIdx >= 0) return uri.substring(slashIdx + 1);
|
|
1966
|
+
return uri;
|
|
1967
|
+
}).filter(Boolean);
|
|
1968
|
+
}
|
|
1969
|
+
|
|
1970
|
+
/**
|
|
1971
|
+
* Returns the SHACL shape metadata for a registered class, including
|
|
1972
|
+
* property names, predicates, datatypes, and cardinality constraints.
|
|
1973
|
+
*
|
|
1974
|
+
* @param className - The name of the registered class
|
|
1975
|
+
* @returns Shape metadata or `null` if the class is not registered
|
|
1976
|
+
*/
|
|
1977
|
+
async getClassShape(className: string): Promise<{
|
|
1978
|
+
className: string;
|
|
1979
|
+
shapeUri: string;
|
|
1980
|
+
properties: Array<{
|
|
1981
|
+
name: string;
|
|
1982
|
+
predicate: string;
|
|
1983
|
+
datatype?: string;
|
|
1984
|
+
required: boolean;
|
|
1985
|
+
collection: boolean;
|
|
1986
|
+
writable: boolean;
|
|
1987
|
+
options?: Array<{ value: string; label?: string }>;
|
|
1988
|
+
}>;
|
|
1989
|
+
} | null> {
|
|
1990
|
+
// Find the shape URI for this class
|
|
1991
|
+
const safeName = className.replace(/['"\\]/g, '');
|
|
1992
|
+
const shapeResults = await this.querySparql(
|
|
1993
|
+
`SELECT ?shapeUri ?targetClass WHERE {
|
|
1994
|
+
?targetClass <rdf://type> <ad4m://SubjectClass> .
|
|
1995
|
+
?targetClass <ad4m://shape> ?shapeUri .
|
|
1996
|
+
FILTER(STRENDS(STR(?targetClass), "/${safeName}") || STRENDS(STR(?targetClass), "#${safeName}"))
|
|
1997
|
+
} LIMIT 1`
|
|
1998
|
+
);
|
|
1999
|
+
if (!Array.isArray(shapeResults) || shapeResults.length === 0) return null;
|
|
2000
|
+
|
|
2001
|
+
const shapeUri = shapeResults[0].shapeUri;
|
|
2002
|
+
const classUri = shapeResults[0].targetClass;
|
|
2003
|
+
|
|
2004
|
+
// Query all properties for this shape, including sh:in
|
|
2005
|
+
const propResults = await this.querySparql(
|
|
2006
|
+
`SELECT ?propShape ?path ?datatype ?minCount ?maxCount ?writable ?propType ?shIn WHERE {
|
|
2007
|
+
<${shapeUri}> <sh://property> ?propShape .
|
|
2008
|
+
?propShape <sh://path> ?path .
|
|
2009
|
+
?propShape <rdf://type> ?propType .
|
|
2010
|
+
OPTIONAL { ?propShape <sh://datatype> ?datatype }
|
|
2011
|
+
OPTIONAL { ?propShape <sh://minCount> ?minCount }
|
|
2012
|
+
OPTIONAL { ?propShape <sh://maxCount> ?maxCount }
|
|
2013
|
+
OPTIONAL { ?propShape <ad4m://writable> ?writable }
|
|
2014
|
+
OPTIONAL { ?propShape <sh://in> ?shIn }
|
|
2015
|
+
}`
|
|
2016
|
+
);
|
|
2017
|
+
|
|
2018
|
+
const properties = (Array.isArray(propResults) ? propResults : []).map((row: any) => {
|
|
2019
|
+
// Extract property name from shape URI like "recipe://Recipe.name"
|
|
2020
|
+
const propUri: string = row.propShape || '';
|
|
2021
|
+
const dotIdx = propUri.lastIndexOf('.');
|
|
2022
|
+
const name = dotIdx >= 0 ? propUri.substring(dotIdx + 1) : propUri;
|
|
2023
|
+
|
|
2024
|
+
const minCount = parseInt(row.minCount || '0', 10);
|
|
2025
|
+
const isCollection = row.propType === 'ad4m://CollectionShape';
|
|
2026
|
+
const writable = row.writable === 'true' || row.writable === 'literal:true';
|
|
2027
|
+
|
|
2028
|
+
// Parse sh:in values if present
|
|
2029
|
+
let options: Array<{ value: string; label?: string }> | undefined;
|
|
2030
|
+
if (row.shIn) {
|
|
2031
|
+
try {
|
|
2032
|
+
let raw = row.shIn;
|
|
2033
|
+
// Strip literal: prefix if present, and URI-decode for robustness
|
|
2034
|
+
if (raw.startsWith('literal:string:')) {
|
|
2035
|
+
raw = decodeURIComponent(raw.substring('literal:string:'.length));
|
|
2036
|
+
}
|
|
2037
|
+
options = JSON.parse(raw);
|
|
2038
|
+
} catch { /* ignore parse errors */ }
|
|
2039
|
+
}
|
|
2040
|
+
|
|
2041
|
+
return {
|
|
2042
|
+
name,
|
|
2043
|
+
predicate: row.path || '',
|
|
2044
|
+
datatype: row.datatype || undefined,
|
|
2045
|
+
required: minCount > 0,
|
|
2046
|
+
collection: isCollection,
|
|
2047
|
+
writable,
|
|
2048
|
+
...(options && options.length > 0 ? { options } : {}),
|
|
2049
|
+
};
|
|
2050
|
+
});
|
|
2051
|
+
|
|
2052
|
+
return { className: safeName, shapeUri, properties };
|
|
2053
|
+
}
|
|
2054
|
+
|
|
2055
|
+
/**
|
|
2056
|
+
* Detects which registered subject classes a given instance conforms to,
|
|
2057
|
+
* based on its stored triples matching the class shapes' required properties.
|
|
2058
|
+
*
|
|
2059
|
+
* @param baseExpression - The instance's base expression / subject URI
|
|
2060
|
+
* @returns Array of class names the instance matches
|
|
2061
|
+
*/
|
|
2062
|
+
async getInstanceClasses(baseExpression: string): Promise<string[]> {
|
|
2063
|
+
const safeId = baseExpression.replace(/['"\\]/g, '');
|
|
2064
|
+
// Get all registered classes and their required conformance properties
|
|
2065
|
+
const results = await this.querySparql(
|
|
2066
|
+
`SELECT DISTINCT ?class WHERE {
|
|
2067
|
+
?class <rdf://type> <ad4m://SubjectClass> .
|
|
2068
|
+
?class <ad4m://shape> ?shape .
|
|
2069
|
+
?shape <sh://property> ?propShape .
|
|
2070
|
+
?propShape <sh://minCount> ?mc .
|
|
2071
|
+
FILTER(?mc >= 1)
|
|
2072
|
+
?propShape <sh://path> ?path .
|
|
2073
|
+
<${safeId}> ?path ?_val .
|
|
2074
|
+
}`
|
|
2075
|
+
);
|
|
2076
|
+
if (!Array.isArray(results)) return [];
|
|
2077
|
+
return results.map((row: any) => {
|
|
2078
|
+
const uri: string = row.class || '';
|
|
2079
|
+
const hashIdx = uri.lastIndexOf('#');
|
|
2080
|
+
if (hashIdx >= 0) return uri.substring(hashIdx + 1);
|
|
2081
|
+
const slashIdx = uri.lastIndexOf('/');
|
|
2082
|
+
if (slashIdx >= 0) return uri.substring(slashIdx + 1);
|
|
2083
|
+
return uri;
|
|
2084
|
+
}).filter(Boolean);
|
|
2085
|
+
}
|
|
2086
|
+
|
|
2087
|
+
/**
|
|
2088
|
+
* Returns all named options (sh:in values) for a registered class, grouped by property.
|
|
2089
|
+
*
|
|
2090
|
+
* @param className - The name of the registered class
|
|
2091
|
+
* @returns Record mapping property name → array of { value, label } options
|
|
2092
|
+
*/
|
|
2093
|
+
async getNamedOptions(className: string): Promise<Record<string, Array<{ value: string; label?: string }>>> {
|
|
2094
|
+
const shape = await this.getClassShape(className);
|
|
2095
|
+
if (!shape) return {};
|
|
2096
|
+
const result: Record<string, Array<{ value: string; label?: string }>> = {};
|
|
2097
|
+
for (const prop of shape.properties) {
|
|
2098
|
+
if (prop.options && prop.options.length > 0) {
|
|
2099
|
+
result[prop.name] = prop.options;
|
|
2100
|
+
}
|
|
2101
|
+
}
|
|
2102
|
+
return result;
|
|
2103
|
+
}
|
|
2104
|
+
|
|
2105
|
+
/**
|
|
2106
|
+
* Adds a named option (sh:in value) to a property of a registered class.
|
|
2107
|
+
* This appends to the existing sh:in list stored on the property shape.
|
|
2108
|
+
*
|
|
2109
|
+
* @param className - The registered class name
|
|
2110
|
+
* @param propertyName - The property to add the option to
|
|
2111
|
+
* @param value - The RDF value for the option
|
|
2112
|
+
* @param label - Optional human-readable label
|
|
2113
|
+
*/
|
|
2114
|
+
async addNamedOption(className: string, propertyName: string, value: string, label?: string): Promise<void> {
|
|
2115
|
+
const shape = await this.getClassShape(className);
|
|
2116
|
+
if (!shape) throw new Error(`Class "${className}" not found`);
|
|
2117
|
+
|
|
2118
|
+
const prop = shape.properties.find(p => p.name === propertyName);
|
|
2119
|
+
if (!prop) throw new Error(`Property "${propertyName}" not found on class "${className}"`);
|
|
2120
|
+
|
|
2121
|
+
// Build the property shape URI
|
|
2122
|
+
const ns = extractNamespaceFromUri(shape.shapeUri);
|
|
2123
|
+
const propShapeId = `${ns}${className}.${propertyName}`;
|
|
2124
|
+
|
|
2125
|
+
// Get existing options
|
|
2126
|
+
const existing = prop.options || [];
|
|
2127
|
+
// Don't add duplicates
|
|
2128
|
+
if (existing.some(o => o.value === value)) return;
|
|
2129
|
+
|
|
2130
|
+
const updated = [...existing, { value, ...(label ? { label } : {}) }];
|
|
2131
|
+
|
|
2132
|
+
// Remove old sh:in link if exists by querying for it
|
|
2133
|
+
const oldLinks = await this.get(new LinkQuery({ source: propShapeId, predicate: "sh://in" }));
|
|
2134
|
+
for (const oldLink of oldLinks) {
|
|
2135
|
+
await this.remove(oldLink);
|
|
2136
|
+
}
|
|
2137
|
+
|
|
2138
|
+
// Add new sh:in link
|
|
2139
|
+
await this.add(new Link({
|
|
2140
|
+
source: propShapeId,
|
|
2141
|
+
predicate: "sh://in",
|
|
2142
|
+
target: `literal:string:${JSON.stringify(updated)}`
|
|
2143
|
+
}));
|
|
2144
|
+
}
|
|
2145
|
+
|
|
2163
2146
|
getNeighbourhoodProxy(): NeighbourhoodProxy {
|
|
2164
2147
|
return this.#client.getNeighbourhoodProxy(this.#handle.uuid)
|
|
2165
2148
|
}
|