2008-02-18 20:12:10 -05:00
|
|
|
! Copyright (C) 2008 Alex Chapman
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2008-02-28 21:51:59 -05:00
|
|
|
USING: accessors arrays db db.tuples db.types db.sqlite kernel math new-slots sequences ;
|
2008-02-18 20:12:10 -05:00
|
|
|
IN: semantic-db
|
|
|
|
|
|
|
|
! new semantic-db using Doug Coleman's new db abstraction library
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
TUPLE: arc relation subject object ;
|
|
|
|
|
|
|
|
: <arc> ( relation subject object -- arc )
|
|
|
|
arc construct-empty
|
|
|
|
f <node> over set-delegate
|
|
|
|
swap >>object swap >>subject swap >>relation ;
|
|
|
|
|
|
|
|
arc "arc"
|
|
|
|
{
|
2008-02-28 21:51:59 -05:00
|
|
|
{ "id" "id" INTEGER } ! foreign key to node table?
|
|
|
|
{ "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 -- )
|
|
|
|
dup delegate insert-tuple
|
2008-02-28 21:51:59 -05:00
|
|
|
insert-tuple ;
|
|
|
|
! [ ] [ insert-sql ] make-tuple-statement insert-statement drop ;
|
2008-02-18 20:12:10 -05:00
|
|
|
|
2008-02-28 21:51:59 -05:00
|
|
|
! : insert-arc ( arc -- )
|
|
|
|
! dup primary-key [ update-tuple ] [ insert-arc ] if ;
|
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-02-28 21:51:59 -05:00
|
|
|
{ "context" "type" "relation" "is of type" "semantic-db" "is in 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
|
|
|
|
|
|
|
: create-bootstrap-arcs ( -- )
|
2008-02-28 21:51:59 -05:00
|
|
|
! give everything a type
|
|
|
|
has-type-relation context-type type-type create-arc drop
|
|
|
|
has-type-relation type-type type-type create-arc drop
|
|
|
|
has-type-relation relation-type type-type create-arc drop
|
2008-02-21 04:44:15 -05:00
|
|
|
has-type-relation has-type-relation relation-type create-arc drop
|
|
|
|
has-type-relation semantic-db-context context-type create-arc drop
|
|
|
|
has-type-relation has-context-relation relation-type create-arc drop
|
2008-02-28 21:51:59 -05:00
|
|
|
! give relations a context (semantic-db context)
|
|
|
|
has-context-relation has-type-relation semantic-db-context create-arc drop
|
2008-02-21 04:44:15 -05:00
|
|
|
has-context-relation has-context-relation semantic-db-context create-arc drop ;
|
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
|
|
|
: 1result ( array -- result )
|
|
|
|
#! return the first (and hopefully only) element of the array, or f
|
|
|
|
dup length zero? [ drop f ] [ first ] if ;
|
|
|
|
|
|
|
|
: param ( value key type -- param )
|
|
|
|
rot 3array ;
|