| 
									
										
										
										
											2008-02-18 20:12:10 -05:00
										 |  |  | ! Copyright (C) 2008 Alex Chapman | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2008-03-20 16:30:59 -04:00
										 |  |  | USING: accessors arrays continuations db db.tuples db.types db.sqlite kernel math math.parser sequences ;
 | 
					
						
							| 
									
										
										
										
											2008-02-18 20:12:10 -05:00
										 |  |  | IN: semantic-db | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TUPLE: node id content ;
 | 
					
						
							|  |  |  | : <node> ( content -- node )
 | 
					
						
							|  |  |  |     node construct-empty swap >>content ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-06 07:54:16 -05:00
										 |  |  | : <id-node> ( id -- node )
 | 
					
						
							|  |  |  |     node construct-empty swap >>id ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-18 20:12:10 -05:00
										 |  |  | node "node" | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2008-02-28 21:51:59 -05:00
										 |  |  |     { "id" "id" +native-id+ +autoincrement+ } | 
					
						
							| 
									
										
										
										
											2008-02-18 20:12:10 -05:00
										 |  |  |     { "content" "content" TEXT } | 
					
						
							|  |  |  | } define-persistent | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : create-node-table ( -- )
 | 
					
						
							|  |  |  |     node create-table ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-06 07:54:16 -05:00
										 |  |  | : delete-node ( node-id -- )
 | 
					
						
							|  |  |  |     <id-node> delete-tuple ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : create-node* ( str -- node-id )
 | 
					
						
							| 
									
										
										
										
											2008-02-28 21:51:59 -05:00
										 |  |  |     <node> dup insert-tuple id>> ;
 | 
					
						
							| 
									
										
										
										
											2008-02-18 20:12:10 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-06 07:54:16 -05:00
										 |  |  | : create-node ( str -- )
 | 
					
						
							|  |  |  |     create-node* drop ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : node-content ( id -- str )
 | 
					
						
							|  |  |  |     f <node> swap >>id select-tuple content>> ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-05 20:37:51 -05:00
										 |  |  | TUPLE: arc id relation subject object ;
 | 
					
						
							| 
									
										
										
										
											2008-02-18 20:12:10 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : <arc> ( relation subject object -- arc )
 | 
					
						
							| 
									
										
										
										
											2008-03-05 20:37:51 -05:00
										 |  |  |     arc construct-empty swap >>object swap >>subject swap >>relation ;
 | 
					
						
							| 
									
										
										
										
											2008-02-18 20:12:10 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-06 07:54:16 -05:00
										 |  |  | : <id-arc> ( id -- arc )
 | 
					
						
							|  |  |  |     arc construct-empty swap >>id ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : insert-arc ( arc -- )
 | 
					
						
							|  |  |  |     f <node> dup insert-tuple id>> >>id insert-tuple ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : delete-arc ( arc-id -- )
 | 
					
						
							|  |  |  |     dup delete-node <id-arc> delete-tuple ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : create-arc* ( relation subject object -- arc-id )
 | 
					
						
							|  |  |  |     <arc> dup insert-arc id>> ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : create-arc ( relation subject object -- )
 | 
					
						
							|  |  |  |     create-arc* drop ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-18 20:12:10 -05:00
										 |  |  | arc "arc" | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2008-03-05 20:37:51 -05:00
										 |  |  |     { "id" "id" INTEGER +assigned-id+ } ! foreign key to node table? | 
					
						
							| 
									
										
										
										
											2008-02-28 21:51:59 -05:00
										 |  |  |     { "relation" "relation" INTEGER +not-null+ } | 
					
						
							|  |  |  |     { "subject" "subject" INTEGER +not-null+ } | 
					
						
							|  |  |  |     { "object" "object" INTEGER +not-null+ } | 
					
						
							| 
									
										
										
										
											2008-02-18 20:12:10 -05:00
										 |  |  | } define-persistent | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : create-arc-table ( -- )
 | 
					
						
							|  |  |  |     arc create-table ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : create-bootstrap-nodes ( -- )
 | 
					
						
							| 
									
										
										
										
											2008-03-06 07:54:16 -05:00
										 |  |  |     "semantic-db" create-node | 
					
						
							|  |  |  |     "has context" create-node ;
 | 
					
						
							| 
									
										
										
										
											2008-03-05 20:37:51 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-06 07:54:16 -05:00
										 |  |  | : semantic-db-context 1 ;
 | 
					
						
							|  |  |  | : has-context-relation 2 ;
 | 
					
						
							| 
									
										
										
										
											2008-03-05 20:37:51 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-18 20:12:10 -05:00
										 |  |  | : create-bootstrap-arcs ( -- )
 | 
					
						
							| 
									
										
										
										
											2008-03-06 07:54:16 -05:00
										 |  |  |     has-context-relation has-context-relation semantic-db-context create-arc ;     | 
					
						
							| 
									
										
										
										
											2008-02-18 20:12:10 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : init-semantic-db ( -- )
 | 
					
						
							|  |  |  |     create-node-table create-arc-table create-bootstrap-nodes create-bootstrap-arcs ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-28 21:51:59 -05:00
										 |  |  | : param ( value key type -- param )
 | 
					
						
							| 
									
										
										
										
											2008-03-05 20:37:51 -05:00
										 |  |  |     swapd 3array ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : single-int-results ( bindings sql -- array )
 | 
					
						
							|  |  |  |     f f <simple-statement> [ do-bound-query ] with-disposal | 
					
						
							|  |  |  |     [ first string>number ] map ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : ensure2 ( x y quot1 quot2 -- z )
 | 
					
						
							|  |  |  |     #! quot1 ( x y -- z/f ) finds an existing z | 
					
						
							|  |  |  |     #! quot2 ( x y -- z ) creates a new z if quot1 returns f | 
					
						
							|  |  |  |     >r >r 2dup r> call [ 2nip ] r> if* ;
 | 
					
						
							| 
									
										
										
										
											2008-03-10 20:44:03 -04:00
										 |  |  | 
 |