@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.
@@ -1080,16 +1080,54 @@ export class PerspectiveProxy {
1080
1080
  */
1081
1081
  async isSubjectInstance<T>(expression: string, subjectClass: T): Promise<boolean> {
1082
1082
  let className = await this.stringOrTemplateObjectToSubjectClassName(subjectClass)
1083
- let isInstance = false;
1084
- const maxAttempts = 5;
1085
- let attempts = 0;
1086
1083
 
1087
- while (attempts < maxAttempts && !isInstance) {
1088
- isInstance = await this.infer(`subject_class("${className}", C), instance(C, "${expression}")`);
1089
- attempts++;
1090
- }
1084
+ // Get metadata from SDNA using Prolog metaprogramming
1085
+ const metadata = await this.getSubjectClassMetadataFromSDNA(className);
1086
+ if (!metadata) {
1087
+ return false;
1088
+ }
1089
+
1090
+ // If no required triples, any expression with links is an instance
1091
+ if (metadata.requiredTriples.length === 0) {
1092
+ console.log("no required triples, checking surrealDB");
1093
+ const checkQuery = `SELECT count() AS count FROM link WHERE in.uri = '${expression}'`;
1094
+ console.log("checkQuery", checkQuery);
1095
+ const result = await this.querySurrealDB(checkQuery);
1096
+ console.log("result", result);
1097
+ const count = result[0]?.count ?? 0;
1098
+ const countValue = typeof count === 'object' && count?.Int !== undefined ? count.Int : count;
1099
+ return countValue > 0;
1100
+ }
1091
1101
 
1092
- return isInstance
1102
+ // Check if the expression has all required triples (predicate + optional exact target)
1103
+ for (const triple of metadata.requiredTriples) {
1104
+ console.log("checking triple", triple);
1105
+ let checkQuery: string;
1106
+ if (triple.target) {
1107
+ // Flag: must match both predicate AND exact target value
1108
+ checkQuery = `SELECT count() AS count FROM link WHERE in.uri = '${expression}' AND predicate = '${triple.predicate}' AND out.uri = '${triple.target}'`;
1109
+ } else {
1110
+ // Property: just check predicate exists
1111
+ checkQuery = `SELECT count() AS count FROM link WHERE in.uri = '${expression}' AND predicate = '${triple.predicate}'`;
1112
+ }
1113
+ console.log("checkQuery", checkQuery);
1114
+ const result = await this.querySurrealDB(checkQuery);
1115
+ console.log("result", result);
1116
+
1117
+ if (!result || result.length === 0) {
1118
+ return false;
1119
+ }
1120
+
1121
+ const count = result[0]?.count ?? 0;
1122
+ // Handle potential object response like {Int: 0}
1123
+ const countValue = typeof count === 'object' && count?.Int !== undefined ? count.Int : count;
1124
+
1125
+ if (countValue === 0) {
1126
+ return false;
1127
+ }
1128
+ }
1129
+
1130
+ return true;
1093
1131
  }
1094
1132
 
1095
1133
 
@@ -1112,6 +1150,303 @@ export class PerspectiveProxy {
1112
1150
  return subject as unknown as T
1113
1151
  }
1114
1152
 
1153
+ /**
1154
+ * Extracts subject class metadata from SDNA by parsing the Prolog text.
1155
+ * Parses the instance rule to extract required predicates.
1156
+ * Returns required predicates that define what makes something an instance,
1157
+ * plus a map of property/collection names to their predicates.
1158
+ */
1159
+ private async getSubjectClassMetadataFromSDNA(className: string): Promise<{
1160
+ requiredPredicates: string[],
1161
+ requiredTriples: Array<{predicate: string, target?: string}>,
1162
+ properties: Map<string, { predicate: string, resolveLanguage?: string }>,
1163
+ collections: Map<string, { predicate: string, instanceFilter?: string }>
1164
+ } | null> {
1165
+ try {
1166
+ // Get SDNA code from perspective - it's stored as a link
1167
+ const sdnaLinks = await this.get(new LinkQuery({
1168
+ source: `literal://string:${className}`,
1169
+ predicate: "ad4m://sdna"
1170
+ }));
1171
+
1172
+ //console.log(`getSubjectClassMetadataFromSDNA: sdnaLinks for ${className}:`, sdnaLinks);
1173
+
1174
+ if (!sdnaLinks || sdnaLinks.length === 0) {
1175
+ console.warn(`No SDNA found for class ${className}`);
1176
+ return null;
1177
+ }
1178
+
1179
+ if (!sdnaLinks[0].data.target) {
1180
+ console.error(`SDNA link for ${className} has no target:`, sdnaLinks[0]);
1181
+ return null;
1182
+ }
1183
+
1184
+ // Extract SDNA code from the literal
1185
+ const sdnaCode = Literal.fromUrl(sdnaLinks[0].data.target).get();
1186
+ //console.log("sdnaCode for", className, ":", sdnaCode.substring(0, 200));
1187
+
1188
+ // Store required triples as {predicate, target?}
1189
+ // target is only set for flags (exact matches), otherwise undefined
1190
+ const requiredTriples: Array<{predicate: string, target?: string}> = [];
1191
+
1192
+ // Parse the instance rule from the SDNA code
1193
+ // Format: instance(c, Base) :- triple(Base, "pred1", _), triple(Base, "pred2", "exact_value").
1194
+ const instanceRuleMatch = sdnaCode.match(/instance\([^,]+,\s*\w+\)\s*:-\s*(.+?)\./s);
1195
+
1196
+ if (instanceRuleMatch) {
1197
+ const ruleBody = instanceRuleMatch[1];
1198
+ console.log("instance rule body:", ruleBody);
1199
+
1200
+ // Extract all triple(Base, "predicate", Target) patterns
1201
+ // Match both: triple(Base, "pred", _) and triple(Base, "pred", "value")
1202
+ const tripleRegex = /triple\([^,]+,\s*"([^"]+)",\s*(?:"([^"]+)"|_)\)/g;
1203
+ let match;
1204
+
1205
+ while ((match = tripleRegex.exec(ruleBody)) !== null) {
1206
+ const predicate = match[1];
1207
+ const target = match[2]; // undefined if matched "_"
1208
+ requiredTriples.push({ predicate, target });
1209
+ console.log(`Extracted triple: predicate="${predicate}", target="${target}"`);
1210
+ }
1211
+ } else {
1212
+ console.warn(`No instance rule found in SDNA for ${className}`);
1213
+ }
1214
+
1215
+ // For backward compatibility, also maintain requiredPredicates array
1216
+ const requiredPredicates = requiredTriples.map(t => t.predicate);
1217
+ console.log("extracted requiredTriples:", requiredTriples);
1218
+
1219
+ // Extract property metadata
1220
+ const properties = new Map<string, { predicate: string, resolveLanguage?: string }>();
1221
+ const propertyResults = await this.infer(`subject_class("${className}", C), property(C, P)`);
1222
+ //console.log("propertyResults", propertyResults);
1223
+
1224
+ if (propertyResults) {
1225
+ for (const result of propertyResults) {
1226
+ const propName = result.P;
1227
+ let predicate: string | null = null;
1228
+
1229
+ // Try to extract predicate from property_setter first
1230
+ const setterResults = await this.infer(`subject_class("${className}", C), property_setter(C, "${propName}", Setter)`);
1231
+ if (setterResults && setterResults.length > 0) {
1232
+ const setterString = setterResults[0].Setter;
1233
+ const predicateMatch = setterString.match(/predicate:\s*"([^"]+)"|predicate:\s*([^,}\]]+)/);
1234
+ if (predicateMatch) {
1235
+ predicate = predicateMatch[1] || predicateMatch[2];
1236
+ }
1237
+ }
1238
+
1239
+ // If no setter, try to extract from SDNA property_getter Prolog code
1240
+ if (!predicate) {
1241
+ // Parse the SDNA code for property_getter definition
1242
+ const getterMatch = sdnaCode.match(new RegExp(`property_getter\\([^,]+,\\s*[^,]+,\\s*"${propName}"[^)]*\\)\\s*:-\\s*triple\\([^,]+,\\s*"([^"]+)"`));
1243
+ if (getterMatch) {
1244
+ predicate = getterMatch[1];
1245
+ }
1246
+ }
1247
+
1248
+ if (predicate) {
1249
+ // Check if property has resolveLanguage
1250
+ const resolveResults = await this.infer(`subject_class("${className}", C), property_resolve_language(C, "${propName}", Lang)`);
1251
+ const resolveLanguage = resolveResults && resolveResults.length > 0 ? resolveResults[0].Lang : undefined;
1252
+
1253
+ properties.set(propName, { predicate, resolveLanguage });
1254
+ }
1255
+ }
1256
+ }
1257
+ //console.log("properties", properties);
1258
+
1259
+ // Extract collection metadata
1260
+ const collections = new Map<string, { predicate: string, instanceFilter?: string }>();
1261
+ const collectionResults = await this.infer(`subject_class("${className}", C), collection(C, Coll)`);
1262
+ //console.log("collectionResults", collectionResults);
1263
+ if (collectionResults) {
1264
+ for (const result of collectionResults) {
1265
+ const collName = result.Coll;
1266
+ let predicate: string | null = null;
1267
+ let instanceFilter: string | undefined = undefined;
1268
+
1269
+ // Try to extract predicate from collection_adder first
1270
+ const adderResults = await this.infer(`subject_class("${className}", C), collection_adder(C, "${collName}", Adder)`);
1271
+ if (adderResults && adderResults.length > 0) {
1272
+ const adderString = adderResults[0].Adder;
1273
+ const predicateMatch = adderString.match(/predicate:\s*"([^"]+)"|predicate:\s*([^,}\]]+)/);
1274
+ if (predicateMatch) {
1275
+ predicate = predicateMatch[1] || predicateMatch[2];
1276
+ }
1277
+ }
1278
+
1279
+ // Parse collection_getter from SDNA to extract predicate and instanceFilter
1280
+ // Format 1 (findall): collection_getter(c, Base, "comments", List) :- findall(C, triple(Base, "todo://comment", C), List).
1281
+ // Format 2 (setof): collection_getter(c, Base, "messages", List) :- setof(Target, (triple(Base, "flux://entry_type", Target), ...), List).
1282
+ // Use a line-based match to avoid capturing multiple collections
1283
+ const getterLinePattern = new RegExp(`collection_getter\\([^,]+,\\s*[^,]+,\\s*"${collName}"[^)]*\\)\\s*:-[^.]+\\.`);
1284
+ const getterLineMatch = sdnaCode.match(getterLinePattern);
1285
+
1286
+ if (getterLineMatch) {
1287
+ const getterLine = getterLineMatch[0];
1288
+ // Extract the body between setof/findall and the final ).
1289
+ // Pattern: findall(Var, Body, List) or setof(Var, (Body), List)
1290
+ const bodyPattern = /(?:setof|findall)\([^,]+,\s*(.+),\s*\w+\)\./;
1291
+ const bodyMatch = getterLine.match(bodyPattern);
1292
+
1293
+ if (bodyMatch) {
1294
+ let getterBody = bodyMatch[1];
1295
+ // Remove outer parentheses if present (setof case)
1296
+ if (getterBody.startsWith('(') && getterBody.endsWith(')')) {
1297
+ getterBody = getterBody.substring(1, getterBody.length - 1);
1298
+ }
1299
+ console.log(`Collection "${collName}" getter body:`, getterBody);
1300
+
1301
+ // Extract predicate from triple(Base, "predicate", Target)
1302
+ if (!predicate) {
1303
+ const tripleMatch = getterBody.match(/triple\([^,]+,\s*"([^"]+)"/);
1304
+ if (tripleMatch) {
1305
+ predicate = tripleMatch[1];
1306
+ }
1307
+ }
1308
+
1309
+ // Check for instance filter: subject_class("ClassName", OtherClass)
1310
+ const instanceMatch = getterBody.match(/subject_class\("([^"]+)"/);
1311
+ if (instanceMatch) {
1312
+ instanceFilter = instanceMatch[1];
1313
+ console.log(`Collection "${collName}" has instanceFilter: ${instanceFilter}`);
1314
+ }
1315
+ }
1316
+ }
1317
+
1318
+ if (predicate) {
1319
+ collections.set(collName, { predicate, instanceFilter });
1320
+ }
1321
+ }
1322
+ }
1323
+ //console.log("collections", collections);
1324
+ return { requiredPredicates, requiredTriples, properties, collections };
1325
+ } catch (e) {
1326
+ console.error(`Error getting metadata for ${className}:`, e);
1327
+ return null;
1328
+ }
1329
+ }
1330
+
1331
+ /**
1332
+ * Generates a SurrealDB query to find instances based on class metadata.
1333
+ */
1334
+ private generateSurrealInstanceQuery(metadata: {
1335
+ requiredPredicates: string[],
1336
+ requiredTriples: Array<{predicate: string, target?: string}>,
1337
+ properties: Map<string, { predicate: string, resolveLanguage?: string }>,
1338
+ collections: Map<string, { predicate: string }>
1339
+ }): string {
1340
+ console.log("generateSurrealInstanceQuery called with requiredTriples:", metadata.requiredTriples);
1341
+
1342
+ if (metadata.requiredTriples.length === 0) {
1343
+ // No required triples - any node with links is an instance
1344
+ const query = `SELECT DISTINCT uri AS base FROM node WHERE count(->link) > 0`;
1345
+ console.log("Generated query (no required triples):", query);
1346
+ return query;
1347
+ }
1348
+
1349
+ // Generate WHERE conditions for each required triple (predicate + optional exact target)
1350
+ const whereConditions = metadata.requiredTriples.map(triple => {
1351
+ if (triple.target) {
1352
+ // Flag: must match both predicate AND exact target value
1353
+ return `count(->link[WHERE predicate = '${triple.predicate}' AND out.uri = '${triple.target}']) > 0`;
1354
+ } else {
1355
+ // Property: just check predicate exists
1356
+ return `count(->link[WHERE predicate = '${triple.predicate}']) > 0`;
1357
+ }
1358
+ }).join(' AND ');
1359
+
1360
+ const query = `SELECT uri AS base FROM node WHERE ${whereConditions}`;
1361
+ console.log("Generated query:", query);
1362
+ return query;
1363
+ }
1364
+
1365
+ /**
1366
+ * Gets a property value using SurrealDB when Prolog fails.
1367
+ * This is used as a fallback in SdnaOnly mode where link data isn't in Prolog.
1368
+ */
1369
+ async getPropertyValueViaSurreal(baseExpression: string, className: string, propertyName: string): Promise<any> {
1370
+ const metadata = await this.getSubjectClassMetadataFromSDNA(className);
1371
+ if (!metadata) {
1372
+ return undefined;
1373
+ }
1374
+
1375
+ const propMeta = metadata.properties.get(propertyName);
1376
+ if (!propMeta) {
1377
+ return undefined;
1378
+ }
1379
+
1380
+ const query = `SELECT out.uri AS value FROM link WHERE in.uri = '${baseExpression}' AND predicate = '${propMeta.predicate}' LIMIT 1`;
1381
+ const result = await this.querySurrealDB(query);
1382
+
1383
+ if (!result || result.length === 0) {
1384
+ return undefined;
1385
+ }
1386
+
1387
+ const value = result[0].value;
1388
+
1389
+ // Handle expression resolution if needed
1390
+ if (propMeta.resolveLanguage && value) {
1391
+ try {
1392
+ const expression = await this.getExpression(value);
1393
+ try {
1394
+ return JSON.parse(expression.data);
1395
+ } catch (e) {
1396
+ return expression.data;
1397
+ }
1398
+ } catch (err) {
1399
+ return value;
1400
+ }
1401
+ }
1402
+
1403
+ return value;
1404
+ }
1405
+
1406
+ /**
1407
+ * Gets collection values using SurrealDB when Prolog fails.
1408
+ * This is used as a fallback in SdnaOnly mode where link data isn't in Prolog.
1409
+ */
1410
+ async getCollectionValuesViaSurreal(baseExpression: string, className: string, collectionName: string): Promise<any[]> {
1411
+ const metadata = await this.getSubjectClassMetadataFromSDNA(className);
1412
+ if (!metadata) {
1413
+ return [];
1414
+ }
1415
+
1416
+ const collMeta = metadata.collections.get(collectionName);
1417
+ if (!collMeta) {
1418
+ return [];
1419
+ }
1420
+
1421
+ const query = `SELECT out.uri AS value, timestamp FROM link WHERE in.uri = '${baseExpression}' AND predicate = '${collMeta.predicate}' ORDER BY timestamp ASC`;
1422
+ const result = await this.querySurrealDB(query);
1423
+
1424
+ if (!result || result.length === 0) {
1425
+ return [];
1426
+ }
1427
+
1428
+ let values = result.map(r => r.value).filter(v => v !== "" && v !== '');
1429
+
1430
+ console.log(`getCollectionValuesViaSurreal: base="${baseExpression}", collection="${collectionName}", predicate="${collMeta.predicate}", found ${values.length} values:`, values);
1431
+
1432
+ // Apply instance filter if present
1433
+ if (collMeta.instanceFilter) {
1434
+ console.log(` Applying instance filter "${collMeta.instanceFilter}" to collection "${collectionName}"`);
1435
+ const filteredValues = [];
1436
+ for (const value of values) {
1437
+ const isInstance = await this.isSubjectInstance(value, collMeta.instanceFilter);
1438
+ console.log(` Checking if "${value}" is instance of "${collMeta.instanceFilter}": ${isInstance}`);
1439
+ if (isInstance) {
1440
+ filteredValues.push(value);
1441
+ }
1442
+ }
1443
+ console.log(` After filtering: ${filteredValues.length} values`);
1444
+ return filteredValues;
1445
+ }
1446
+
1447
+ return values;
1448
+ }
1449
+
1115
1450
  /** Returns all subject instances of the given subject class as proxy objects.
1116
1451
  * @param subjectClass Either a string with the name of the subject class, or an object
1117
1452
  * with the properties of the subject class. In the latter case, all subject classes
@@ -1127,10 +1462,31 @@ export class PerspectiveProxy {
1127
1462
 
1128
1463
  let instances = []
1129
1464
  for(let className of classes) {
1130
- let instanceBaseExpressions = await this.infer(`subject_class("${className}", C), instance(C, X)`)
1131
- let newInstances = await Promise.all(instanceBaseExpressions.map(async x => await this.getSubjectProxy(x.X, className) as unknown as T))
1132
- instances = instances.concat(newInstances)
1465
+ //console.log(`getAllSubjectInstances: Processing class ${className}`);
1466
+ // Query SDNA for metadata, then query SurrealDB for instances
1467
+ const metadata = await this.getSubjectClassMetadataFromSDNA(className);
1468
+ //console.log(`getAllSubjectInstances: Got metadata for ${className}:`, metadata);
1469
+ if (metadata) {
1470
+ const surrealQuery = this.generateSurrealInstanceQuery(metadata);
1471
+ const results = await this.querySurrealDB(surrealQuery);
1472
+ // console.log(`getAllSubjectInstances: SurrealDB returned ${results?.length || 0} results`);
1473
+
1474
+ for (const result of results || []) {
1475
+ //console.log(`getAllSubjectInstances: Creating subject for base ${result.base}`);
1476
+ try {
1477
+ let subject = new Subject(this, result.base, className);
1478
+ await subject.init();
1479
+ instances.push(subject as unknown as T);
1480
+ //console.log(`getAllSubjectInstances: Successfully created subject for ${result.base}`);
1481
+ } catch (e) {
1482
+ //console.warn(`Failed to create subject for ${result.base}:`, e);
1483
+ }
1484
+ }
1485
+ } else {
1486
+ //console.warn(`getAllSubjectInstances: No metadata found for ${className}`);
1487
+ }
1133
1488
  }
1489
+ //console.log(`getAllSubjectInstances: Returning ${instances.length} instances`);
1134
1490
  return instances
1135
1491
  }
1136
1492
 
@@ -1149,8 +1505,23 @@ export class PerspectiveProxy {
1149
1505
 
1150
1506
  let instances = []
1151
1507
  for(let className of classes) {
1152
- instances = await this.infer(`subject_class("${className}", C), instance(C, X)`)
1508
+ //console.log(`getAllSubjectProxies: Processing class ${className}`);
1509
+ // Query SDNA for metadata, then query SurrealDB for instances
1510
+ const metadata = await this.getSubjectClassMetadataFromSDNA(className);
1511
+ //console.log(`getAllSubjectProxies: Got metadata for ${className}`);
1512
+ if (metadata) {
1513
+ const surrealQuery = this.generateSurrealInstanceQuery(metadata);
1514
+ console.log("surrealQuery", surrealQuery);
1515
+ const results = await this.querySurrealDB(surrealQuery);
1516
+ console.log(`getAllSubjectProxies: SurrealDB returned ${results?.length || 0} results`);
1517
+ const newInstance = results.map(r => ({ X: r.base }))
1518
+ console.log("newInstance", newInstance);
1519
+ instances = instances.concat(newInstance);
1520
+ } else {
1521
+ console.warn(`getAllSubjectProxies: No metadata found for ${className}`);
1522
+ }
1153
1523
  }
1524
+ //console.log(`getAllSubjectProxies: Returning ${instances.length} proxies`);
1154
1525
  return instances
1155
1526
  }
1156
1527