@@ -7,6 +7,9 @@ import getDiscourseRelations, {
77 DiscourseRelation ,
88} from "./getDiscourseRelations" ;
99import { Selection } from "./types" ;
10+ import { getSetting } from "./extensionSettings" ;
11+ import { ANY_RELATION_REGEX } from "./deriveDiscourseNodeAttribute" ;
12+ import { use } from "cytoscape" ;
1013
1114const resultCache : Record < string , Awaited < ReturnType < typeof fireQuery > > > = { } ;
1215const CACHE_TIMEOUT = 1000 * 60 * 5 ;
@@ -57,6 +60,18 @@ const buildSelections = ({
5760 text : `node:${ conditionUid } -Anchor` ,
5861 } ) ;
5962 }
63+ if ( ANY_RELATION_REGEX . test ( r . label ) ) {
64+ selections . push ( {
65+ uid : window . roamAlphaAPI . util . generateUID ( ) ,
66+ label : "relationUid" ,
67+ text : "hasSchema" ,
68+ } ) ;
69+ selections . push ( {
70+ uid : window . roamAlphaAPI . util . generateUID ( ) ,
71+ label : "effectiveSource" ,
72+ text : "effectiveSource" ,
73+ } ) ;
74+ }
6075
6176 return selections ;
6277} ;
@@ -186,6 +201,10 @@ const getDiscourseContextResults = async ({
186201
187202 const discourseNode = findDiscourseNode ( { uid : targetUid } ) ;
188203 if ( ! discourseNode ) return [ ] ;
204+ const useReifiedRelations = getSetting < boolean > (
205+ "use-reified-relations" ,
206+ false ,
207+ ) ;
189208 const nodeType = discourseNode ?. type ;
190209 const nodeTextByType = Object . fromEntries (
191210 nodes . map ( ( { type, text } ) => [ type , text ] ) ,
@@ -217,9 +236,24 @@ const getDiscourseContextResults = async ({
217236 } ) ;
218237
219238 const relationsWithComplement = Array . from ( uniqueRelations . values ( ) ) ;
239+ const queryRelations = useReifiedRelations
240+ ? [
241+ {
242+ r : {
243+ id : "null" ,
244+ complement : "Has Any Relation To" ,
245+ label : "Has Any Relation To" ,
246+ triples : [ ] ,
247+ source : "*" ,
248+ destination : "*" ,
249+ } ,
250+ complement : false ,
251+ } ,
252+ ]
253+ : relationsWithComplement ;
220254
221255 const context = { nodes, relations } ;
222- const queryConfigs = relationsWithComplement . map ( ( relation ) =>
256+ const queryConfigs = queryRelations . map ( ( relation ) =>
223257 buildQueryConfig ( {
224258 args,
225259 targetUid,
@@ -232,12 +266,40 @@ const getDiscourseContextResults = async ({
232266 } ) ,
233267 ) ;
234268
235- const resultsWithRelation = await executeQueries (
269+ let resultsWithRelation = await executeQueries (
236270 queryConfigs ,
237271 targetUid ,
238272 nodeTextByType ,
239273 onResult ,
240274 ) ;
275+ if (
276+ useReifiedRelations &&
277+ resultsWithRelation . length > 0 &&
278+ resultsWithRelation [ 0 ] . results . length > 0
279+ ) {
280+ const byRel : Record < string , Result [ ] > = { } ;
281+ const results = resultsWithRelation [ 0 ] . results ;
282+ resultsWithRelation = [ ] ;
283+ for ( const r of results ) {
284+ const relKey = `${ r . relationUid } -${ r . effectiveSource !== targetUid } ` ;
285+ byRel [ relKey ] = byRel [ relKey ] || [ ] ;
286+ byRel [ relKey ] . push ( r ) ;
287+ }
288+ resultsWithRelation = Object . entries ( byRel ) . map ( ( [ ruid , results ] ) => ( {
289+ relation : {
290+ id : ruid ,
291+ label : ruid . endsWith ( "-false" )
292+ ? uniqueRelations . get ( ruid ) ! . r . label
293+ : uniqueRelations . get ( ruid ) ! . r . complement ,
294+ isComplement : ruid . endsWith ( "-false" ) ,
295+ text : ruid . endsWith ( "-false" )
296+ ? uniqueRelations . get ( ruid ) ! . r . label
297+ : uniqueRelations . get ( ruid ) ! . r . complement ,
298+ target : targetUid ,
299+ } ,
300+ results,
301+ } ) ) ;
302+ }
241303 const groupedResults = Object . fromEntries (
242304 resultsWithRelation . map ( ( r ) => [
243305 r . relation . text ,
0 commit comments