2008-02-18 20:12:10 -05:00
|
|
|
! Copyright (C) 2008 Alex Chapman
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2008-03-05 20:37:51 -05:00
|
|
|
USING: accessors arrays continuations db db.tuples db.types db.sqlite kernel math math.parser new-slots sequences ;
|
2008-02-18 20:12:10 -05:00
|
|
|
IN: semantic-db
|
|
|
|
|
|
|
|
TUPLE: node id content ;
|
|
|
|
: <node> ( content -- node )
|
|
|
|
node construct-empty swap >>content ;
|
|
|
|
|
|
|
|
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-02-21 04:44:15 -05:00
|
|
|
: create-node ( content -- id )
|
2008-02-28 21:51:59 -05:00
|
|
|
<node> dup insert-tuple id>> ;
|
2008-02-18 20:12:10 -05:00
|
|
|
|
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
|
|
|
|
|
|
|
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 ;
|
|
|
|
|
|
|
|
: insert-arc ( arc -- )
|
2008-03-05 20:37:51 -05:00
|
|
|
f <node> dup insert-tuple id>> >>id insert-tuple ;
|
2008-02-18 20:12:10 -05:00
|
|
|
|
|
|
|
: delete-arc ( arc -- )
|
|
|
|
dup delete-tuple delegate delete-tuple ;
|
|
|
|
|
2008-02-21 04:44:15 -05:00
|
|
|
: create-arc ( relation subject object -- id )
|
2008-02-28 21:51:59 -05:00
|
|
|
<arc> dup insert-arc id>> ;
|
2008-02-18 20:12:10 -05:00
|
|
|
|
|
|
|
: create-bootstrap-nodes ( -- )
|
2008-03-05 20:37:51 -05:00
|
|
|
{ "context" "type" "relation" "has type" "semantic-db" "has context" }
|
2008-02-21 04:44:15 -05:00
|
|
|
[ create-node drop ] each ;
|
2008-02-18 20:12:10 -05:00
|
|
|
|
2008-02-28 21:51:59 -05:00
|
|
|
! TODO: maybe put these in a 'special nodes' table
|
2008-02-18 20:12:10 -05:00
|
|
|
: context-type 1 ; inline
|
2008-02-28 21:51:59 -05:00
|
|
|
: type-type 2 ; inline
|
|
|
|
: relation-type 3 ; inline
|
|
|
|
: has-type-relation 4 ; inline
|
|
|
|
: semantic-db-context 5 ; inline
|
|
|
|
: has-context-relation 6 ; inline
|
2008-02-18 20:12:10 -05:00
|
|
|
|
2008-03-05 20:37:51 -05:00
|
|
|
: has-semantic-db-context ( id -- )
|
|
|
|
has-context-relation swap semantic-db-context create-arc drop ;
|
|
|
|
|
|
|
|
: has-type-in-semantic-db ( subject type -- )
|
|
|
|
has-type-relation -rot create-arc drop ;
|
|
|
|
|
2008-02-18 20:12:10 -05:00
|
|
|
: create-bootstrap-arcs ( -- )
|
2008-02-28 21:51:59 -05:00
|
|
|
! give everything a type
|
2008-03-05 20:37:51 -05:00
|
|
|
context-type type-type has-type-in-semantic-db
|
|
|
|
type-type type-type has-type-in-semantic-db
|
|
|
|
relation-type type-type has-type-in-semantic-db
|
|
|
|
has-type-relation relation-type has-type-in-semantic-db
|
|
|
|
semantic-db-context context-type has-type-in-semantic-db
|
|
|
|
has-context-relation relation-type has-type-in-semantic-db
|
|
|
|
! give relations and types the semantic-db context
|
|
|
|
context-type has-semantic-db-context
|
|
|
|
type-type has-semantic-db-context
|
|
|
|
relation-type has-semantic-db-context
|
|
|
|
has-type-relation has-semantic-db-context
|
|
|
|
has-context-relation has-semantic-db-context ;
|
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* ;
|