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