Skip to content

Commit

Permalink
Merge pull request #30 from arturo-lang/add-support-for-reverse-edge-…
Browse files Browse the repository at this point in the history
…filters

Add support for reverse edge filters
  • Loading branch information
drkameleon committed Nov 2, 2023
2 parents 5215fdc + d30c9a7 commit 1ea11ba
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<p align="center"><img width="90%" align="center" src="https://raw.githubusercontent.com/arturo-lang/grafito/master/ui-screenshot.png"/></p>

---

<!--ts-->

* [At A Glance](#at-a-glance)
Expand Down
79 changes: 78 additions & 1 deletion grafito.art
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ Grafito: #[
Palette: ""
]

;
; Define custom objects
;--------------------------
define :edgeFilter [direction, content][]

;----------------------------------------------

graph: function [
dbpath :string :null
body :block
Expand Down Expand Up @@ -90,6 +97,8 @@ graph: function [
hasEdgeFilter: read "sql/filters/hasEdge.sql"
hasPropertyFilter: read "sql/filters/hasProperty.sql"
edgeWithTargetFilter: read "sql/filters/edgeWithTarget.sql"
edgeWithSourceFilter: read "sql/filters/edgeWithSource.sql"
edgeWithAnyFilter: read "sql/filters/edgeWithAny.sql"
propertyWithValueFilter: read "sql/filters/propertyWithValue.sql"

; Global SQL pragmas
Expand Down Expand Up @@ -724,6 +733,22 @@ graph: function [
nd\tag = to :string ls
]

;
; Helpers
; for edge filters
;--------------------------
edgeFilterRight: function [cnt][
to :edgeFilter @[1, cnt]
]

edgeFilterLeft: function [cnt][
to :edgeFilter @[2, cnt]
]

edgeFilterAny: function [cnt][
to :edgeFilter @[3, cnt]
]

;
; Fetch all results for tag
; with given properties
Expand Down Expand Up @@ -796,13 +821,61 @@ graph: function [
'qedgevals ++ @[k, v\id]
continue
]
if is? :edgeFilter v [
; it's a directed edge filter
if dictionary? v\content [
(v\direction = 1)? [
'edgeFilters ++ edgeWithTargetFilter
'qedgevals ++ @[k, v\content\id]
][
(v\direction = 2)? [
'edgeFilters ++ edgeWithSourceFilter
'qedgevals ++ @[k, v\content\id]
][
(v\direction = 3)? [
'edgeFilters ++ edgeWithAnyFilter
'qedgevals ++ @[k, v\content\id, v\content\id]
][
print "Shouldn't have reached here!"
]
]
]
]
if all? @[
block? v\content
0 < size v\content
:dictionary = type first v\content
][
orCriteria: new []
loop v\content [edgef][
(v\direction = 1)? [
'orCriteria ++ edgeWithTargetFilter
'qedgevals ++ @[k, edgef\id]
][
(v\direction = 2)? [
'orCriteria ++ edgeWithSourceFilter
'qedgevals ++ @[k, edgef\id]
][
(v\direction = 3)? [
'orCriteria ++ edgeWithAnyFilter
'qedgevals ++ @[k, edgef\id, edgef\id]
][
print "Shouldn't have reached here!"
]
]
]
]
'edgeFilters ++ "(" ++ (join.with:" OR " orCriteria) ++ ")"
]
continue
]
if block? v [
(and? 0 < size v
:dictionary = type first v)? [
; it's an array of edge filters
orCriteria: new []
loop v [edgef][
'orCriteria ++ {!sql (edges.tag=? AND edges.target=?)}
'orCriteria ++ edgeWithTargetFilter
'qedgevals ++ @[k, edgef\id]
]
'edgeFilters ++ "(" ++ (join.with:" OR " orCriteria) ++ ")"
Expand Down Expand Up @@ -1110,6 +1183,10 @@ graph: function [
alias.infix {<~} 'reverseLink
alias.infix {<~>} 'reciprocalLink

alias {|>} 'edgeFilterRight
alias {<|} 'edgeFilterLeft
alias {<|>} 'edgeFilterAny

; open the database
printDebug ~"DB = |cleanpath|"
db: open dbPath
Expand Down
1 change: 1 addition & 0 deletions sql/filters/edgeWithAny.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(edges.tag=? AND (edges.target=? OR edges.source=?))
1 change: 1 addition & 0 deletions sql/filters/edgeWithSource.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(edges.tag=? AND edges.source=?)
2 changes: 1 addition & 1 deletion sql/procs/fetchNodes.withEdges.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
SELECT nodes.id, nodes.tag, nodes.properties
FROM nodes
INNER JOIN edges
ON edges.source=nodes.id
ON edges.source=nodes.id OR edges.target=nodes.id
WHERE nodes.tag=? |propies|
GROUP BY nodes.id
HAVING COUNT(
Expand Down

0 comments on commit 1ea11ba

Please sign in to comment.