@@ -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} ;
@@ -183,6 +198,10 @@ const getDiscourseContextResults = async ({
183198
184199 const discourseNode = findDiscourseNode ( targetUid ) ;
185200 if ( ! discourseNode ) return [ ] ;
201+ const useReifiedRelations = getSetting < boolean > (
202+ "use-reified-relations" ,
203+ false ,
204+ ) ;
186205 const nodeType = discourseNode ?. type ;
187206 const nodeTextByType = Object . fromEntries (
188207 nodes . map ( ( { type, text } ) => [ type , text ] ) ,
@@ -214,9 +233,24 @@ const getDiscourseContextResults = async ({
214233 } ) ;
215234
216235 const relationsWithComplement = Array . from ( uniqueRelations . values ( ) ) ;
236+ const queryRelations = useReifiedRelations
237+ ? [
238+ {
239+ r : {
240+ id : "null" ,
241+ complement : "Has Any Relation To" ,
242+ label : "Has Any Relation To" ,
243+ triples : [ ] ,
244+ source : "*" ,
245+ destination : "*" ,
246+ } ,
247+ complement : false ,
248+ } ,
249+ ]
250+ : relationsWithComplement ;
217251
218252 const context = { nodes, relations } ;
219- const queryConfigs = relationsWithComplement . map ( ( relation ) =>
253+ const queryConfigs = queryRelations . map ( ( relation ) =>
220254 buildQueryConfig ( {
221255 args,
222256 targetUid,
@@ -229,12 +263,40 @@ const getDiscourseContextResults = async ({
229263 } ) ,
230264 ) ;
231265
232- const resultsWithRelation = await executeQueries (
266+ let resultsWithRelation = await executeQueries (
233267 queryConfigs ,
234268 targetUid ,
235269 nodeTextByType ,
236270 onResult ,
237271 ) ;
272+ if (
273+ useReifiedRelations &&
274+ resultsWithRelation . length > 0 &&
275+ resultsWithRelation [ 0 ] . results . length > 0
276+ ) {
277+ const byRel : Record < string , Result [ ] > = { } ;
278+ const results = resultsWithRelation [ 0 ] . results ;
279+ resultsWithRelation = [ ] ;
280+ for ( const r of results ) {
281+ const relKey = `${ r . relationUid } -${ r . effectiveSource !== targetUid } ` ;
282+ byRel [ relKey ] = byRel [ relKey ] || [ ] ;
283+ byRel [ relKey ] . push ( r ) ;
284+ }
285+ resultsWithRelation = Object . entries ( byRel ) . map ( ( [ ruid , results ] ) => ( {
286+ relation : {
287+ id : ruid ,
288+ label : ruid . endsWith ( "-false" )
289+ ? uniqueRelations . get ( ruid ) ! . r . label
290+ : uniqueRelations . get ( ruid ) ! . r . complement ,
291+ isComplement : ruid . endsWith ( "-false" ) ,
292+ text : ruid . endsWith ( "-false" )
293+ ? uniqueRelations . get ( ruid ) ! . r . label
294+ : uniqueRelations . get ( ruid ) ! . r . complement ,
295+ target : targetUid ,
296+ } ,
297+ results,
298+ } ) ) ;
299+ }
238300 const groupedResults = Object . fromEntries (
239301 resultsWithRelation . map ( ( r ) => [
240302 r . relation . text ,
0 commit comments