@coasys/ad4m 0.11.2-dev.4 → 0.11.2-dev.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1182,7 +1182,7 @@ export class PerspectiveProxy {
1182
1182
  requiredPredicates: string[],
1183
1183
  requiredTriples: Array<{predicate: string, target?: string}>,
1184
1184
  properties: Map<string, { predicate: string, resolveLanguage?: string }>,
1185
- collections: Map<string, { predicate: string, instanceFilter?: string }>
1185
+ collections: Map<string, { predicate: string, instanceFilter?: string, condition?: string }>
1186
1186
  } | null> {
1187
1187
  try {
1188
1188
  // Get SDNA code from perspective - it's stored as a link
@@ -1286,7 +1286,7 @@ export class PerspectiveProxy {
1286
1286
  //console.log("properties", properties);
1287
1287
 
1288
1288
  // Extract collection metadata
1289
- const collections = new Map<string, { predicate: string, instanceFilter?: string }>();
1289
+ const collections = new Map<string, { predicate: string, instanceFilter?: string, condition?: string }>();
1290
1290
  const collectionResults = await this.infer(`subject_class("${className}", C), collection(C, Coll)`);
1291
1291
  //console.log("collectionResults", collectionResults);
1292
1292
  if (collectionResults) {
@@ -1294,6 +1294,7 @@ export class PerspectiveProxy {
1294
1294
  const collName = result.Coll;
1295
1295
  let predicate: string | null = null;
1296
1296
  let instanceFilter: string | undefined = undefined;
1297
+ let condition: string | undefined = undefined;
1297
1298
 
1298
1299
  // Try to extract predicate from collection_adder first
1299
1300
  const adderResults = await this.infer(`subject_class("${className}", C), collection_adder(C, "${collName}", Adder)`);
@@ -1304,6 +1305,9 @@ export class PerspectiveProxy {
1304
1305
  predicate = predicateMatch[1] || predicateMatch[2];
1305
1306
  }
1306
1307
  }
1308
+
1309
+ // Note: condition is not stored in SDNA, it's read from class metadata
1310
+ // It will be populated by augmentMetadataWithCollectionOptions() when needed
1307
1311
 
1308
1312
  // Parse collection_getter from SDNA to extract predicate and instanceFilter
1309
1313
  // Format 1 (findall): collection_getter(c, Base, "comments", List) :- findall(C, triple(Base, "todo://comment", C), List).
@@ -1345,7 +1349,7 @@ export class PerspectiveProxy {
1345
1349
  }
1346
1350
 
1347
1351
  if (predicate) {
1348
- collections.set(collName, { predicate, instanceFilter });
1352
+ collections.set(collName, { predicate, instanceFilter, condition });
1349
1353
  }
1350
1354
  }
1351
1355
  }
@@ -1364,7 +1368,7 @@ export class PerspectiveProxy {
1364
1368
  requiredPredicates: string[],
1365
1369
  requiredTriples: Array<{predicate: string, target?: string}>,
1366
1370
  properties: Map<string, { predicate: string, resolveLanguage?: string }>,
1367
- collections: Map<string, { predicate: string }>
1371
+ collections: Map<string, { predicate: string, instanceFilter?: string, condition?: string }>
1368
1372
  }): string {
1369
1373
  if (metadata.requiredTriples.length === 0) {
1370
1374
  // No required triples - any node with links is an instance
@@ -1433,6 +1437,7 @@ export class PerspectiveProxy {
1433
1437
  /**
1434
1438
  * Gets collection values using SurrealDB when Prolog fails.
1435
1439
  * This is used as a fallback in SdnaOnly mode where link data isn't in Prolog.
1440
+ * Note: This is used by Subject.ts (legacy pattern). Ad4mModel.ts uses getModelMetadata() instead.
1436
1441
  */
1437
1442
  async getCollectionValuesViaSurreal(baseExpression: string, className: string, collectionName: string): Promise<any[]> {
1438
1443
  const metadata = await this.getSubjectClassMetadataFromSDNA(className);
@@ -1455,6 +1460,35 @@ export class PerspectiveProxy {
1455
1460
  }
1456
1461
 
1457
1462
  let values = result.map(r => r.value).filter(v => v !== "" && v !== '');
1463
+
1464
+ // Apply condition filtering if present
1465
+ if (collMeta.condition && values.length > 0) {
1466
+ try {
1467
+ const filteredValues: string[] = [];
1468
+
1469
+ for (const value of values) {
1470
+ let condition = collMeta.condition
1471
+ .replace(/\$perspective/g, `'${this.uuid}'`)
1472
+ .replace(/\$base/g, `'${baseExpression}'`)
1473
+ .replace(/Target/g, `'${value.replace(/'/g, "\\'")}'`);
1474
+
1475
+ // If condition starts with WHERE, wrap in array length check
1476
+ if (condition.trim().startsWith('WHERE')) {
1477
+ condition = `array::len(SELECT * FROM link ${condition}) > 0`;
1478
+ }
1479
+
1480
+ const filterResult = await this.querySurrealDB(`RETURN ${condition}`);
1481
+ const isTrue = filterResult === true || (Array.isArray(filterResult) && filterResult.length > 0 && filterResult[0] === true);
1482
+ if (isTrue) {
1483
+ filteredValues.push(value);
1484
+ }
1485
+ }
1486
+
1487
+ values = filteredValues;
1488
+ } catch (error) {
1489
+ console.warn(`Failed to apply condition filter for ${collectionName}:`, error);
1490
+ }
1491
+ }
1458
1492
 
1459
1493
  // Apply instance filter if present - batch-check all values at once
1460
1494
  if (collMeta.instanceFilter) {
@@ -1485,7 +1519,7 @@ export class PerspectiveProxy {
1485
1519
  requiredPredicates: string[],
1486
1520
  requiredTriples: Array<{predicate: string, target?: string}>,
1487
1521
  properties: Map<string, { predicate: string, resolveLanguage?: string }>,
1488
- collections: Map<string, { predicate: string, instanceFilter?: string }>
1522
+ collections: Map<string, { predicate: string, instanceFilter?: string, condition?: string }>
1489
1523
  }
1490
1524
  ): Promise<string[]> {
1491
1525
  if (expressions.length === 0) {
@@ -1511,10 +1545,12 @@ export class PerspectiveProxy {
1511
1545
  if (triple.target) {
1512
1546
  // Flag: must match both predicate AND exact target value
1513
1547
  const escapedTarget = escapeSurrealString(triple.target);
1514
- checkQuery = `SELECT in.uri AS uri FROM link WHERE in.uri IN [${escapedExpressions}] AND predicate = '${escapedPredicate}' AND out.uri = '${escapedTarget}' GROUP BY in.uri`;
1548
+ // Note: Removed GROUP BY because it was causing SurrealDB to only return one result
1549
+ checkQuery = `SELECT in.uri AS uri FROM link WHERE in.uri IN [${escapedExpressions}] AND predicate = '${escapedPredicate}' AND out.uri = '${escapedTarget}'`;
1515
1550
  } else {
1516
1551
  // Property: just check predicate exists
1517
- checkQuery = `SELECT in.uri AS uri FROM link WHERE in.uri IN [${escapedExpressions}] AND predicate = '${escapedPredicate}' GROUP BY in.uri`;
1552
+ // Note: Removed GROUP BY because it was causing SurrealDB to only return one result
1553
+ checkQuery = `SELECT in.uri AS uri FROM link WHERE in.uri IN [${escapedExpressions}] AND predicate = '${escapedPredicate}'`;
1518
1554
  }
1519
1555
 
1520
1556
  const result = await this.querySurrealDB(checkQuery);