1 ! Copyright (C) 2008 Alex Chapman
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays continuations db db.tuples db.types db.sqlite kernel math math.parser sequences ;
6 TUPLE: node id content ;
7 : <node> ( content -- node )
8 node new swap >>content ;
10 : <id-node> ( id -- node )
15 { "id" "id" +native-id+ +autoincrement+ }
16 { "content" "content" TEXT }
19 : create-node-table ( -- )
22 : delete-node ( node-id -- )
23 <id-node> delete-tuple ;
25 : create-node* ( str -- node-id )
26 <node> dup insert-tuple id>> ;
28 : create-node ( str -- )
31 : node-content ( id -- str )
32 f <node> swap >>id select-tuple content>> ;
34 TUPLE: arc id relation subject object ;
36 : <arc> ( relation subject object -- arc )
37 arc new swap >>object swap >>subject swap >>relation ;
39 : <id-arc> ( id -- arc )
42 : insert-arc ( arc -- )
43 f <node> dup insert-tuple id>> >>id insert-tuple ;
45 : delete-arc ( arc-id -- )
46 dup delete-node <id-arc> delete-tuple ;
48 : create-arc* ( relation subject object -- arc-id )
49 <arc> dup insert-arc id>> ;
51 : create-arc ( relation subject object -- )
56 { "id" "id" INTEGER +assigned-id+ } ! foreign key to node table?
57 { "relation" "relation" INTEGER +not-null+ }
58 { "subject" "subject" INTEGER +not-null+ }
59 { "object" "object" INTEGER +not-null+ }
62 : create-arc-table ( -- )
65 : create-bootstrap-nodes ( -- )
66 "semantic-db" create-node
67 "has context" create-node ;
69 : semantic-db-context 1 ;
70 : has-context-relation 2 ;
72 : create-bootstrap-arcs ( -- )
73 has-context-relation has-context-relation semantic-db-context create-arc ;
75 : init-semantic-db ( -- )
76 create-node-table create-arc-table create-bootstrap-nodes create-bootstrap-arcs ;
78 : param ( value key type -- param )
79 swapd <sqlite-low-level-binding> ;
81 : single-int-results ( bindings sql -- array )
82 f f <simple-statement> [ do-bound-query ] with-disposal
83 [ first string>number ] map ;
85 : ensure2 ( x y quot1 quot2 -- z )
86 #! quot1 ( x y -- z/f ) finds an existing z
87 #! quot2 ( x y -- z ) creates a new z if quot1 returns f
88 >r >r 2dup r> call [ 2nip ] r> if* ;