選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

87 行
4.0KB

  1. <%
  2. '=======================================================================================================================
  3. ' Graph relationships (spec FR-023, section 8.1, section 14, ADR-014): entities and typed, directed, evidence-backed
  4. ' edges between entities, memories and documents.
  5. '
  6. ' Graph_NodeNamespace kind, id namespace of an endpoint in the caller's tenant, "" when it does not exist
  7. ' Graph_NodeVisibleSql side, allowed, activeOnly
  8. ' SQL condition on relationships r: the endpoint ("source"|"target") lives in one
  9. ' of the allowed namespaces (and, with activeOnly, is not a retired memory)
  10. ' Graph_ForgetNode kind, id deletes every edge touching the node and the edges' evidence (inside the
  11. ' caller's transaction; used when a memory or entity is deleted)
  12. ' Graph_IsType / Graph_IsKind validation helpers; relationship types are upper-case words (IS_A, DEPENDS_ON)
  13. '
  14. ' Visibility rule (section 8.1): an edge is shown only when the caller can read the edge's namespace and both
  15. ' endpoints' namespaces. Anything else is left out without a trace, so traversal cannot reveal hidden nodes or edges.
  16. '=======================================================================================================================
  17. Const GRAPH_MAX_DEPTH = 3
  18. Const GRAPH_MAX_NODES = 100
  19. Const GRAPH_MAX_EDGES = 300
  20. Function Graph_IsKind(k)
  21. Graph_IsKind = False
  22. If k = "entity" Or k = "memory" Or k = "document" Then
  23. Graph_IsKind = True
  24. End If
  25. End Function
  26. Function Graph_IsType(t)
  27. Dim re
  28. Set re = New RegExp
  29. re.Pattern = "^[A-Z][A-Z0-9_]{1,39}$"
  30. re.IgnoreCase = False
  31. Graph_IsType = re.Test(t)
  32. End Function
  33. Function Graph_NodeNamespace(kind, id)
  34. Dim rs
  35. Graph_NodeNamespace = ""
  36. If Not Api_IsId(id) Then
  37. Exit Function
  38. End If
  39. Select Case kind
  40. Case "entity"
  41. Set rs = DAL().Query("SELECT namespace_id FROM entities WHERE id = ? AND tenant_id = ?", Array(id, Api_TenantId))
  42. Case "memory"
  43. Set rs = DAL().Query("SELECT namespace_id FROM memories WHERE id = ? AND tenant_id = ?", Array(id, Api_TenantId))
  44. Case "document"
  45. Set rs = DAL().Query("SELECT namespace_id FROM documents WHERE id = ? AND tenant_id = ?", Array(id, Api_TenantId))
  46. Case Else
  47. Exit Function
  48. End Select
  49. If Not rs.EOF Then
  50. Graph_NodeNamespace = "" & rs(0)
  51. End If
  52. rs.Close
  53. End Function
  54. ' allowed: the inline id list from Api_AllowedNamespaceIds("read") (never empty when called)
  55. Function Graph_NodeVisibleSql(side, allowed, activeOnly)
  56. Dim memExtra
  57. memExtra = ""
  58. If activeOnly Then
  59. memExtra = " AND m.lifecycle_status = 'active'"
  60. End If
  61. Graph_NodeVisibleSql = "(CASE r." & side & "_kind" & _
  62. " WHEN 'entity' THEN (SELECT e.namespace_id FROM entities e WHERE e.id = r." & side & "_id AND e.tenant_id = r.tenant_id)" & _
  63. " WHEN 'memory' THEN (SELECT m.namespace_id FROM memories m WHERE m.id = r." & side & "_id AND m.tenant_id = r.tenant_id" & memExtra & ")" & _
  64. " WHEN 'document' THEN (SELECT d.namespace_id FROM documents d WHERE d.id = r." & side & "_id AND d.tenant_id = r.tenant_id)" & _
  65. " END) IN (" & allowed & ")"
  66. End Function
  67. Sub Graph_ForgetNode(kind, id)
  68. DAL().Execute "DELETE FROM evidence_links WHERE owner_kind = 'relationship' AND owner_id IN " & _
  69. "(SELECT id FROM relationships WHERE (source_kind = ? AND source_id = ?) OR (target_kind = ? AND target_id = ?))", Array(kind, id, kind, id)
  70. DAL().Execute "DELETE FROM relationships WHERE (source_kind = ? AND source_id = ?) OR (target_kind = ? AND target_id = ?)", Array(kind, id, kind, id)
  71. End Sub
  72. ' Lower-case, trimmed, inner whitespace collapsed: the default canonical key of an entity name
  73. Function Graph_CanonicalKey(s)
  74. Dim re
  75. Set re = New RegExp
  76. re.Pattern = "\s+"
  77. re.Global = True
  78. Graph_CanonicalKey = LCase(Trim(re.Replace("" & s, " ")))
  79. End Function
  80. %>

Powered by TurnKey Linux.