@coasys/ad4m 0.11.2-dev-multi-user-email-2 → 0.11.2-dev-sdna-only-prolog
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/lib/index.cjs +634 -103
- package/lib/index.js +634 -103
- package/lib/src/model/Ad4mModel.d.ts +22 -0
- package/lib/src/model/Ad4mModel.js +226 -59
- package/lib/src/model/Subject.js +45 -24
- package/lib/src/model/decorators.js +37 -11
- package/lib/src/perspectives/PerspectiveProxy.d.ts +21 -0
- package/lib/src/perspectives/PerspectiveProxy.js +331 -11
- package/package.json +2 -2
- package/src/model/Ad4mModel.ts +258 -78
- package/src/model/Subject.ts +47 -25
- package/src/model/decorators.ts +39 -12
- package/src/perspectives/PerspectiveProxy.ts +383 -12
|
@@ -310,6 +310,28 @@ export declare class Ad4mModel {
|
|
|
310
310
|
* Allows subclasses to access the perspective while keeping it private from external code.
|
|
311
311
|
*/
|
|
312
312
|
protected get perspective(): PerspectiveProxy;
|
|
313
|
+
/**
|
|
314
|
+
* Get property metadata from decorator (Phase 1: Prolog-free refactor)
|
|
315
|
+
* @private
|
|
316
|
+
*/
|
|
317
|
+
private getPropertyMetadata;
|
|
318
|
+
/**
|
|
319
|
+
* Get collection metadata from decorator (Phase 1: Prolog-free refactor)
|
|
320
|
+
* @private
|
|
321
|
+
*/
|
|
322
|
+
private getCollectionMetadata;
|
|
323
|
+
/**
|
|
324
|
+
* Generate property setter action from metadata (Phase 1: Prolog-free refactor)
|
|
325
|
+
* Replaces Prolog query: property_setter(C, key, Setter)
|
|
326
|
+
* @private
|
|
327
|
+
*/
|
|
328
|
+
private generatePropertySetterAction;
|
|
329
|
+
/**
|
|
330
|
+
* Generate collection action from metadata (Phase 1: Prolog-free refactor)
|
|
331
|
+
* Replaces Prolog queries: collection_adder, collection_remover, collection_setter
|
|
332
|
+
* @private
|
|
333
|
+
*/
|
|
334
|
+
private generateCollectionAction;
|
|
313
335
|
static assignValuesToInstance(perspective: PerspectiveProxy, instance: Ad4mModel, values: ValueTuple[]): Promise<void>;
|
|
314
336
|
private getData;
|
|
315
337
|
static queryToProlog(perspective: PerspectiveProxy, query: Query, modelClassName?: string | null): Promise<string>;
|
|
@@ -13,7 +13,7 @@ var _Ad4mModel_baseExpression, _Ad4mModel_subjectClassName, _Ad4mModel_source, _
|
|
|
13
13
|
import { Literal } from "../Literal";
|
|
14
14
|
import { Link } from "../links/Links";
|
|
15
15
|
import { makeRandomPrologAtom, ModelOptions } from "./decorators";
|
|
16
|
-
import {
|
|
16
|
+
import { propertyNameToSetterName } from "./util";
|
|
17
17
|
function capitalize(word) {
|
|
18
18
|
return word.charAt(0).toUpperCase() + word.slice(1);
|
|
19
19
|
}
|
|
@@ -466,6 +466,70 @@ export class Ad4mModel {
|
|
|
466
466
|
get perspective() {
|
|
467
467
|
return __classPrivateFieldGet(this, _Ad4mModel_perspective, "f");
|
|
468
468
|
}
|
|
469
|
+
/**
|
|
470
|
+
* Get property metadata from decorator (Phase 1: Prolog-free refactor)
|
|
471
|
+
* @private
|
|
472
|
+
*/
|
|
473
|
+
getPropertyMetadata(key) {
|
|
474
|
+
const proto = Object.getPrototypeOf(this);
|
|
475
|
+
return proto.__properties?.[key];
|
|
476
|
+
}
|
|
477
|
+
/**
|
|
478
|
+
* Get collection metadata from decorator (Phase 1: Prolog-free refactor)
|
|
479
|
+
* @private
|
|
480
|
+
*/
|
|
481
|
+
getCollectionMetadata(key) {
|
|
482
|
+
const proto = Object.getPrototypeOf(this);
|
|
483
|
+
return proto.__collections?.[key];
|
|
484
|
+
}
|
|
485
|
+
/**
|
|
486
|
+
* Generate property setter action from metadata (Phase 1: Prolog-free refactor)
|
|
487
|
+
* Replaces Prolog query: property_setter(C, key, Setter)
|
|
488
|
+
* @private
|
|
489
|
+
*/
|
|
490
|
+
generatePropertySetterAction(key, metadata) {
|
|
491
|
+
if (metadata.setter) {
|
|
492
|
+
// Custom setter - throw error for now (Phase 2)
|
|
493
|
+
throw new Error(`Custom setter for property "${key}" not yet supported without Prolog. ` +
|
|
494
|
+
`Use standard @Property decorator or enable Prolog for custom setters.`);
|
|
495
|
+
}
|
|
496
|
+
if (!metadata.through) {
|
|
497
|
+
throw new Error(`Property "${key}" has no 'through' predicate defined`);
|
|
498
|
+
}
|
|
499
|
+
return [{
|
|
500
|
+
action: "setSingleTarget",
|
|
501
|
+
source: "this",
|
|
502
|
+
predicate: metadata.through,
|
|
503
|
+
target: "value",
|
|
504
|
+
...(metadata.local && { local: true })
|
|
505
|
+
}];
|
|
506
|
+
}
|
|
507
|
+
/**
|
|
508
|
+
* Generate collection action from metadata (Phase 1: Prolog-free refactor)
|
|
509
|
+
* Replaces Prolog queries: collection_adder, collection_remover, collection_setter
|
|
510
|
+
* @private
|
|
511
|
+
*/
|
|
512
|
+
generateCollectionAction(key, actionType) {
|
|
513
|
+
const metadata = this.getCollectionMetadata(key);
|
|
514
|
+
if (!metadata) {
|
|
515
|
+
throw new Error(`Collection "${key}" has no metadata defined`);
|
|
516
|
+
}
|
|
517
|
+
if (!metadata.through) {
|
|
518
|
+
throw new Error(`Collection "${key}" has no 'through' predicate defined`);
|
|
519
|
+
}
|
|
520
|
+
const actionMap = {
|
|
521
|
+
adder: "addLink",
|
|
522
|
+
remover: "removeLink",
|
|
523
|
+
setter: "collectionSetter"
|
|
524
|
+
};
|
|
525
|
+
return [{
|
|
526
|
+
action: actionMap[actionType],
|
|
527
|
+
source: "this",
|
|
528
|
+
predicate: metadata.through,
|
|
529
|
+
target: "value",
|
|
530
|
+
...(metadata.local && { local: true })
|
|
531
|
+
}];
|
|
532
|
+
}
|
|
469
533
|
static async assignValuesToInstance(perspective, instance, values) {
|
|
470
534
|
// Map properties to object
|
|
471
535
|
const propsObject = Object.fromEntries(await Promise.all(values.map(async ([name, value, resolve]) => {
|
|
@@ -522,11 +586,96 @@ export class Ad4mModel {
|
|
|
522
586
|
subject_class("${__classPrivateFieldGet(this, _Ad4mModel_subjectClassName, "f")}", SubjectClass),
|
|
523
587
|
${subQueries.join(", ")}
|
|
524
588
|
`;
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
const
|
|
528
|
-
|
|
529
|
-
|
|
589
|
+
// Try Prolog first
|
|
590
|
+
try {
|
|
591
|
+
const result = await __classPrivateFieldGet(this, _Ad4mModel_perspective, "f").infer(fullQuery);
|
|
592
|
+
if (result?.[0] && result[0].Properties && result[0].Properties.length > 0) {
|
|
593
|
+
const { Properties, Collections, Timestamp, Author } = result[0];
|
|
594
|
+
const values = [...Properties, ...Collections, ["timestamp", Timestamp], ["author", Author]];
|
|
595
|
+
await Ad4mModel.assignValuesToInstance(__classPrivateFieldGet(this, _Ad4mModel_perspective, "f"), this, values);
|
|
596
|
+
return this;
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
catch (e) {
|
|
600
|
+
console.log(`Prolog getData failed for ${__classPrivateFieldGet(this, _Ad4mModel_baseExpression, "f")}, falling back to SurrealDB`);
|
|
601
|
+
}
|
|
602
|
+
// Fallback to SurrealDB (SdnaOnly mode)
|
|
603
|
+
try {
|
|
604
|
+
const ctor = this.constructor;
|
|
605
|
+
const metadata = ctor.getModelMetadata();
|
|
606
|
+
// Query for all links from this specific node (base expression)
|
|
607
|
+
const linksQuery = `
|
|
608
|
+
SELECT id, predicate, out.uri AS target, author, timestamp
|
|
609
|
+
FROM link
|
|
610
|
+
WHERE in.uri = '${__classPrivateFieldGet(this, _Ad4mModel_baseExpression, "f")}'
|
|
611
|
+
ORDER BY timestamp ASC
|
|
612
|
+
`;
|
|
613
|
+
const links = await __classPrivateFieldGet(this, _Ad4mModel_perspective, "f").querySurrealDB(linksQuery);
|
|
614
|
+
if (links && links.length > 0) {
|
|
615
|
+
let maxTimestamp = null;
|
|
616
|
+
let latestAuthor = null;
|
|
617
|
+
// Process properties
|
|
618
|
+
for (const [propName, propMeta] of Object.entries(metadata.properties)) {
|
|
619
|
+
const matching = links.filter((l) => l.predicate === propMeta.predicate);
|
|
620
|
+
if (matching.length > 0) {
|
|
621
|
+
const link = matching[0]; // Take first/latest
|
|
622
|
+
let value = link.target;
|
|
623
|
+
// Track timestamp/author
|
|
624
|
+
if (link.timestamp && (!maxTimestamp || link.timestamp > maxTimestamp)) {
|
|
625
|
+
maxTimestamp = link.timestamp;
|
|
626
|
+
latestAuthor = link.author;
|
|
627
|
+
}
|
|
628
|
+
// Handle resolveLanguage
|
|
629
|
+
if (propMeta.resolveLanguage && propMeta.resolveLanguage !== 'literal') {
|
|
630
|
+
try {
|
|
631
|
+
const expression = await __classPrivateFieldGet(this, _Ad4mModel_perspective, "f").getExpression(value);
|
|
632
|
+
if (expression) {
|
|
633
|
+
try {
|
|
634
|
+
value = JSON.parse(expression.data);
|
|
635
|
+
}
|
|
636
|
+
catch {
|
|
637
|
+
value = expression.data;
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
catch (e) {
|
|
642
|
+
console.warn(`Failed to resolve expression for ${propName}:`, e);
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
else if (typeof value === 'string' && value.startsWith('literal://')) {
|
|
646
|
+
// Parse literal URL
|
|
647
|
+
try {
|
|
648
|
+
const parsed = Literal.fromUrl(value).get();
|
|
649
|
+
value = parsed.data !== undefined ? parsed.data : parsed;
|
|
650
|
+
}
|
|
651
|
+
catch (e) {
|
|
652
|
+
// Keep original value
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
// Apply transform if exists
|
|
656
|
+
if (propMeta.transform && typeof propMeta.transform === 'function') {
|
|
657
|
+
value = propMeta.transform(value);
|
|
658
|
+
}
|
|
659
|
+
this[propName] = value;
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
// Process collections
|
|
663
|
+
for (const [collName, collMeta] of Object.entries(metadata.collections)) {
|
|
664
|
+
const matching = links.filter((l) => l.predicate === collMeta.predicate);
|
|
665
|
+
// Links are already sorted by timestamp ASC from the query, so map preserves order
|
|
666
|
+
this[collName] = matching.map((l) => l.target);
|
|
667
|
+
}
|
|
668
|
+
// Set author and timestamp
|
|
669
|
+
if (latestAuthor) {
|
|
670
|
+
this.author = latestAuthor;
|
|
671
|
+
}
|
|
672
|
+
if (maxTimestamp) {
|
|
673
|
+
this.timestamp = maxTimestamp;
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
catch (e) {
|
|
678
|
+
console.error(`SurrealDB getData also failed for ${__classPrivateFieldGet(this, _Ad4mModel_baseExpression, "f")}:`, e);
|
|
530
679
|
}
|
|
531
680
|
return this;
|
|
532
681
|
}
|
|
@@ -1110,7 +1259,7 @@ WHERE ${whereConditions.join(' AND ')}
|
|
|
1110
1259
|
if (!currentValue || currentValue === "" || currentValue === 0 || isEmptyObject) {
|
|
1111
1260
|
let convertedValue = target;
|
|
1112
1261
|
// Only process if target has a value
|
|
1113
|
-
if (target !== undefined && target !== null) {
|
|
1262
|
+
if (target !== undefined && target !== null && target !== '') {
|
|
1114
1263
|
// Check if we need to resolve a non-literal language expression
|
|
1115
1264
|
if (propMeta.resolveLanguage != undefined && propMeta.resolveLanguage !== 'literal' && typeof target === 'string') {
|
|
1116
1265
|
// For non-literal languages, resolve the expression via perspective.getExpression()
|
|
@@ -1129,7 +1278,8 @@ WHERE ${whereConditions.join(' AND ')}
|
|
|
1129
1278
|
}
|
|
1130
1279
|
}
|
|
1131
1280
|
catch (e) {
|
|
1132
|
-
console.warn(`Failed to resolve expression for ${propName}:`, e);
|
|
1281
|
+
console.warn(`Failed to resolve expression for ${propName} with target "${target}":`, e);
|
|
1282
|
+
console.warn("Falling back to raw value");
|
|
1133
1283
|
convertedValue = target; // Fall back to raw value
|
|
1134
1284
|
}
|
|
1135
1285
|
}
|
|
@@ -1574,65 +1724,72 @@ WHERE ${whereConditions.join(' AND ')}
|
|
|
1574
1724
|
}
|
|
1575
1725
|
}
|
|
1576
1726
|
async setProperty(key, value, batchId) {
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
await __classPrivateFieldGet(this, _Ad4mModel_perspective, "f").
|
|
1589
|
-
}
|
|
1727
|
+
// Phase 1: Use metadata instead of Prolog queries
|
|
1728
|
+
const metadata = this.getPropertyMetadata(key);
|
|
1729
|
+
if (!metadata) {
|
|
1730
|
+
console.warn(`Property "${key}" has no metadata, skipping`);
|
|
1731
|
+
return;
|
|
1732
|
+
}
|
|
1733
|
+
// Generate actions from metadata (replaces Prolog query)
|
|
1734
|
+
const actions = this.generatePropertySetterAction(key, metadata);
|
|
1735
|
+
// Get resolve language from metadata (replaces Prolog query)
|
|
1736
|
+
let resolveLanguage = metadata.resolveLanguage;
|
|
1737
|
+
if (resolveLanguage) {
|
|
1738
|
+
value = await __classPrivateFieldGet(this, _Ad4mModel_perspective, "f").createExpression(value, resolveLanguage);
|
|
1739
|
+
}
|
|
1740
|
+
await __classPrivateFieldGet(this, _Ad4mModel_perspective, "f").executeAction(actions, __classPrivateFieldGet(this, _Ad4mModel_baseExpression, "f"), [{ name: "value", value }], batchId);
|
|
1590
1741
|
}
|
|
1591
1742
|
async setCollectionSetter(key, value, batchId) {
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1743
|
+
// Phase 1: Use metadata instead of Prolog queries
|
|
1744
|
+
const metadata = this.getCollectionMetadata(key);
|
|
1745
|
+
if (!metadata) {
|
|
1746
|
+
console.warn(`Collection "${key}" has no metadata, skipping`);
|
|
1747
|
+
return;
|
|
1748
|
+
}
|
|
1749
|
+
// Generate actions from metadata (replaces Prolog query)
|
|
1750
|
+
const actions = this.generateCollectionAction(key, 'setter');
|
|
1751
|
+
if (value) {
|
|
1752
|
+
if (Array.isArray(value)) {
|
|
1753
|
+
await __classPrivateFieldGet(this, _Ad4mModel_perspective, "f").executeAction(actions, __classPrivateFieldGet(this, _Ad4mModel_baseExpression, "f"), value.map((v) => ({ name: "value", value: v })), batchId);
|
|
1754
|
+
}
|
|
1755
|
+
else {
|
|
1756
|
+
await __classPrivateFieldGet(this, _Ad4mModel_perspective, "f").executeAction(actions, __classPrivateFieldGet(this, _Ad4mModel_baseExpression, "f"), [{ name: "value", value }], batchId);
|
|
1604
1757
|
}
|
|
1605
1758
|
}
|
|
1606
1759
|
}
|
|
1607
1760
|
async setCollectionAdder(key, value, batchId) {
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1761
|
+
// Phase 1: Use metadata instead of Prolog queries
|
|
1762
|
+
const metadata = this.getCollectionMetadata(key);
|
|
1763
|
+
if (!metadata) {
|
|
1764
|
+
console.warn(`Collection "${key}" has no metadata, skipping`);
|
|
1765
|
+
return;
|
|
1766
|
+
}
|
|
1767
|
+
// Generate actions from metadata (replaces Prolog query)
|
|
1768
|
+
const actions = this.generateCollectionAction(key, 'adder');
|
|
1769
|
+
if (value) {
|
|
1770
|
+
if (Array.isArray(value)) {
|
|
1771
|
+
await Promise.all(value.map((v) => __classPrivateFieldGet(this, _Ad4mModel_perspective, "f").executeAction(actions, __classPrivateFieldGet(this, _Ad4mModel_baseExpression, "f"), [{ name: "value", value: v }], batchId)));
|
|
1772
|
+
}
|
|
1773
|
+
else {
|
|
1774
|
+
await __classPrivateFieldGet(this, _Ad4mModel_perspective, "f").executeAction(actions, __classPrivateFieldGet(this, _Ad4mModel_baseExpression, "f"), [{ name: "value", value }], batchId);
|
|
1620
1775
|
}
|
|
1621
1776
|
}
|
|
1622
1777
|
}
|
|
1623
1778
|
async setCollectionRemover(key, value, batchId) {
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1779
|
+
// Phase 1: Use metadata instead of Prolog queries
|
|
1780
|
+
const metadata = this.getCollectionMetadata(key);
|
|
1781
|
+
if (!metadata) {
|
|
1782
|
+
console.warn(`Collection "${key}" has no metadata, skipping`);
|
|
1783
|
+
return;
|
|
1784
|
+
}
|
|
1785
|
+
// Generate actions from metadata (replaces Prolog query)
|
|
1786
|
+
const actions = this.generateCollectionAction(key, 'remover');
|
|
1787
|
+
if (value) {
|
|
1788
|
+
if (Array.isArray(value)) {
|
|
1789
|
+
await Promise.all(value.map((v) => __classPrivateFieldGet(this, _Ad4mModel_perspective, "f").executeAction(actions, __classPrivateFieldGet(this, _Ad4mModel_baseExpression, "f"), [{ name: "value", value: v }], batchId)));
|
|
1790
|
+
}
|
|
1791
|
+
else {
|
|
1792
|
+
await __classPrivateFieldGet(this, _Ad4mModel_perspective, "f").executeAction(actions, __classPrivateFieldGet(this, _Ad4mModel_baseExpression, "f"), [{ name: "value", value }], batchId);
|
|
1636
1793
|
}
|
|
1637
1794
|
}
|
|
1638
1795
|
}
|
|
@@ -1716,11 +1873,21 @@ WHERE ${whereConditions.join(' AND ')}
|
|
|
1716
1873
|
break;
|
|
1717
1874
|
}
|
|
1718
1875
|
}
|
|
1719
|
-
else if (Array.isArray(value)
|
|
1720
|
-
|
|
1876
|
+
else if (Array.isArray(value)) {
|
|
1877
|
+
// Handle all arrays as collections, even empty ones
|
|
1878
|
+
if (value.length > 0) {
|
|
1879
|
+
await this.setCollectionSetter(key, value, batchId);
|
|
1880
|
+
}
|
|
1881
|
+
// Skip empty arrays - don't try to set them as properties
|
|
1721
1882
|
}
|
|
1722
1883
|
else if (value !== undefined && value !== null && value !== "") {
|
|
1723
1884
|
if (setProperties) {
|
|
1885
|
+
// Check if this is a collection property (has collection metadata)
|
|
1886
|
+
const collMetadata = this.getCollectionMetadata(key);
|
|
1887
|
+
if (collMetadata) {
|
|
1888
|
+
// Skip - it's a collection, not a regular property
|
|
1889
|
+
continue;
|
|
1890
|
+
}
|
|
1724
1891
|
await this.setProperty(key, value, batchId);
|
|
1725
1892
|
}
|
|
1726
1893
|
}
|
package/lib/src/model/Subject.js
CHANGED
|
@@ -55,36 +55,44 @@ export class Subject {
|
|
|
55
55
|
Object.defineProperty(this, p, {
|
|
56
56
|
configurable: true,
|
|
57
57
|
get: async () => {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
let
|
|
61
|
-
if (
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
58
|
+
// Try Prolog first
|
|
59
|
+
try {
|
|
60
|
+
let results = await __classPrivateFieldGet(this, _Subject_perspective, "f").infer(`subject_class("${__classPrivateFieldGet(this, _Subject_subjectClassName, "f")}", C), property_getter(C, "${__classPrivateFieldGet(this, _Subject_baseExpression, "f")}", "${p}", Value)`);
|
|
61
|
+
if (results && results.length > 0 && results[0].Value !== undefined && results[0].Value !== null && results[0].Value !== '') {
|
|
62
|
+
let expressionURI = results[0].Value;
|
|
63
|
+
if (resolveExpressionURI) {
|
|
64
|
+
try {
|
|
65
|
+
if (expressionURI) {
|
|
66
|
+
const expression = await __classPrivateFieldGet(this, _Subject_perspective, "f").getExpression(expressionURI);
|
|
67
|
+
try {
|
|
68
|
+
return JSON.parse(expression.data);
|
|
69
|
+
}
|
|
70
|
+
catch (e) {
|
|
71
|
+
return expression.data;
|
|
72
|
+
}
|
|
67
73
|
}
|
|
68
|
-
|
|
69
|
-
return
|
|
74
|
+
else {
|
|
75
|
+
return expressionURI;
|
|
70
76
|
}
|
|
71
77
|
}
|
|
72
|
-
|
|
78
|
+
catch (err) {
|
|
73
79
|
return expressionURI;
|
|
74
80
|
}
|
|
75
81
|
}
|
|
76
|
-
|
|
82
|
+
else {
|
|
77
83
|
return expressionURI;
|
|
78
84
|
}
|
|
79
85
|
}
|
|
80
|
-
else {
|
|
81
|
-
return expressionURI;
|
|
82
|
-
}
|
|
83
86
|
}
|
|
84
|
-
|
|
85
|
-
|
|
87
|
+
catch (err) {
|
|
88
|
+
// Prolog query failed, fall through to SurrealDB
|
|
86
89
|
}
|
|
87
|
-
|
|
90
|
+
// Fallback to SurrealDB if Prolog returned nothing (SdnaOnly mode)
|
|
91
|
+
try {
|
|
92
|
+
return await __classPrivateFieldGet(this, _Subject_perspective, "f").getPropertyValueViaSurreal(__classPrivateFieldGet(this, _Subject_baseExpression, "f"), __classPrivateFieldGet(this, _Subject_subjectClassName, "f"), p);
|
|
93
|
+
}
|
|
94
|
+
catch (err) {
|
|
95
|
+
console.warn(`Failed to get property ${p} via SurrealDB:`, err);
|
|
88
96
|
return undefined;
|
|
89
97
|
}
|
|
90
98
|
}
|
|
@@ -118,12 +126,25 @@ export class Subject {
|
|
|
118
126
|
Object.defineProperty(this, c, {
|
|
119
127
|
configurable: true,
|
|
120
128
|
get: async () => {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
let
|
|
124
|
-
|
|
129
|
+
// Try Prolog first
|
|
130
|
+
try {
|
|
131
|
+
let results = await __classPrivateFieldGet(this, _Subject_perspective, "f").infer(`subject_class("${__classPrivateFieldGet(this, _Subject_subjectClassName, "f")}", C), collection_getter(C, "${__classPrivateFieldGet(this, _Subject_baseExpression, "f")}", "${c}", Value)`);
|
|
132
|
+
if (results && results.length > 0 && results[0].Value && Array.isArray(results[0].Value) && results[0].Value.length > 0) {
|
|
133
|
+
let collectionContent = results[0].Value.filter((v) => v !== "" && v !== '');
|
|
134
|
+
if (collectionContent.length > 0) {
|
|
135
|
+
return collectionContent;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
125
138
|
}
|
|
126
|
-
|
|
139
|
+
catch (err) {
|
|
140
|
+
// Prolog query failed, fall through to SurrealDB
|
|
141
|
+
}
|
|
142
|
+
// Fallback to SurrealDB if Prolog returned nothing (SdnaOnly mode)
|
|
143
|
+
try {
|
|
144
|
+
return await __classPrivateFieldGet(this, _Subject_perspective, "f").getCollectionValuesViaSurreal(__classPrivateFieldGet(this, _Subject_baseExpression, "f"), __classPrivateFieldGet(this, _Subject_subjectClassName, "f"), c);
|
|
145
|
+
}
|
|
146
|
+
catch (err) {
|
|
147
|
+
console.warn(`Failed to get collection ${c} via SurrealDB:`, err);
|
|
127
148
|
return [];
|
|
128
149
|
}
|
|
129
150
|
}
|
|
@@ -73,20 +73,46 @@ export function InstanceQuery(options) {
|
|
|
73
73
|
if (options && options.condition) {
|
|
74
74
|
query += ', ' + options.condition;
|
|
75
75
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
76
|
+
// Try Prolog first
|
|
77
|
+
try {
|
|
78
|
+
let results = await perspective.infer(query);
|
|
79
|
+
if (results && results !== false && typeof results !== "string" && results.length > 0) {
|
|
80
|
+
for (let result of results) {
|
|
81
|
+
let instance = result.Instance;
|
|
82
|
+
let subject = new Subject(perspective, instance, subjectClassName);
|
|
83
|
+
await subject.init();
|
|
84
|
+
instances.push(subject);
|
|
85
|
+
}
|
|
86
|
+
return instances;
|
|
87
|
+
}
|
|
79
88
|
}
|
|
80
|
-
|
|
81
|
-
|
|
89
|
+
catch (e) {
|
|
90
|
+
// Prolog failed, fall through to SurrealDB
|
|
82
91
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
92
|
+
// Fallback to SurrealDB (SdnaOnly mode)
|
|
93
|
+
// Get all instances first
|
|
94
|
+
let allInstances = await perspective.getAllSubjectInstances(subjectClassName);
|
|
95
|
+
// Filter by where clause if provided
|
|
96
|
+
if (options && options.where) {
|
|
97
|
+
let filtered = [];
|
|
98
|
+
for (let instance of allInstances) {
|
|
99
|
+
let matches = true;
|
|
100
|
+
for (let prop in options.where) {
|
|
101
|
+
let expectedValue = options.where[prop];
|
|
102
|
+
//@ts-ignore
|
|
103
|
+
let actualValue = await instance[prop];
|
|
104
|
+
if (actualValue !== expectedValue) {
|
|
105
|
+
matches = false;
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
if (matches) {
|
|
110
|
+
filtered.push(instance);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return filtered;
|
|
88
114
|
}
|
|
89
|
-
return
|
|
115
|
+
return allInstances;
|
|
90
116
|
};
|
|
91
117
|
};
|
|
92
118
|
}
|
|
@@ -566,6 +566,27 @@ export declare class PerspectiveProxy {
|
|
|
566
566
|
* that matches the given properties will be used.
|
|
567
567
|
*/
|
|
568
568
|
getSubjectProxy<T>(base: string, subjectClass: T): Promise<T>;
|
|
569
|
+
/**
|
|
570
|
+
* Extracts subject class metadata from SDNA by parsing the Prolog text.
|
|
571
|
+
* Parses the instance rule to extract required predicates.
|
|
572
|
+
* Returns required predicates that define what makes something an instance,
|
|
573
|
+
* plus a map of property/collection names to their predicates.
|
|
574
|
+
*/
|
|
575
|
+
private getSubjectClassMetadataFromSDNA;
|
|
576
|
+
/**
|
|
577
|
+
* Generates a SurrealDB query to find instances based on class metadata.
|
|
578
|
+
*/
|
|
579
|
+
private generateSurrealInstanceQuery;
|
|
580
|
+
/**
|
|
581
|
+
* Gets a property value using SurrealDB when Prolog fails.
|
|
582
|
+
* This is used as a fallback in SdnaOnly mode where link data isn't in Prolog.
|
|
583
|
+
*/
|
|
584
|
+
getPropertyValueViaSurreal(baseExpression: string, className: string, propertyName: string): Promise<any>;
|
|
585
|
+
/**
|
|
586
|
+
* Gets collection values using SurrealDB when Prolog fails.
|
|
587
|
+
* This is used as a fallback in SdnaOnly mode where link data isn't in Prolog.
|
|
588
|
+
*/
|
|
589
|
+
getCollectionValuesViaSurreal(baseExpression: string, className: string, collectionName: string): Promise<any[]>;
|
|
569
590
|
/** Returns all subject instances of the given subject class as proxy objects.
|
|
570
591
|
* @param subjectClass Either a string with the name of the subject class, or an object
|
|
571
592
|
* with the properties of the subject class. In the latter case, all subject classes
|