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 db.tuples hashtables kernel
|
2008-03-13 00:41:51 -04:00
|
|
|
semantic-db semantic-db.relations sequences sequences.deep ;
|
2008-02-18 20:12:10 -05:00
|
|
|
IN: semantic-db.hierarchy
|
|
|
|
|
2008-02-21 04:44:15 -05:00
|
|
|
TUPLE: tree id children ;
|
|
|
|
C: <tree> tree
|
2008-02-18 20:12:10 -05:00
|
|
|
|
2008-02-21 04:44:15 -05:00
|
|
|
: has-parent-relation ( -- relation-id )
|
2008-03-06 07:54:16 -05:00
|
|
|
"has parent" relation-id ;
|
|
|
|
|
|
|
|
: parent-child* ( parent child -- arc-id )
|
|
|
|
has-parent-relation spin create-arc* ;
|
|
|
|
|
|
|
|
: parent-child ( parent child -- )
|
|
|
|
parent-child* drop ;
|
2008-03-05 20:37:51 -05:00
|
|
|
|
2008-03-06 07:54:16 -05:00
|
|
|
: un-parent-child ( parent child -- )
|
2008-03-07 18:07:11 -05:00
|
|
|
has-parent-relation spin <arc> select-tuples [ id>> delete-arc ] each ;
|
2008-03-05 20:37:51 -05:00
|
|
|
|
2008-03-06 07:54:16 -05:00
|
|
|
: child-arcs ( node-id -- child-arcs )
|
|
|
|
has-parent-relation f rot <arc> select-tuples ;
|
2008-03-05 20:37:51 -05:00
|
|
|
|
2008-03-06 07:54:16 -05:00
|
|
|
: children ( node-id -- children )
|
|
|
|
child-arcs [ subject>> ] map ;
|
2008-03-05 20:37:51 -05:00
|
|
|
|
2008-03-06 07:54:16 -05:00
|
|
|
: parent-arcs ( node-id -- parent-arcs )
|
|
|
|
has-parent-relation swap f <arc> select-tuples ;
|
2008-02-21 04:44:15 -05:00
|
|
|
|
2008-03-06 07:54:16 -05:00
|
|
|
: parents ( node-id -- parents )
|
|
|
|
parent-arcs [ object>> ] map ;
|
2008-02-21 04:44:15 -05:00
|
|
|
|
|
|
|
: get-node-hierarchy ( node-id -- tree )
|
2008-03-06 07:54:16 -05:00
|
|
|
dup children [ get-node-hierarchy ] map <tree> ;
|
|
|
|
|
|
|
|
: (get-root-nodes) ( node-id -- root-nodes/node-id )
|
|
|
|
dup parents dup empty? [
|
|
|
|
drop
|
|
|
|
] [
|
|
|
|
nip [ (get-root-nodes) ] map
|
|
|
|
] if ;
|
|
|
|
|
|
|
|
: get-root-nodes ( node-id -- root-nodes )
|
2008-03-13 00:22:16 -04:00
|
|
|
(get-root-nodes) flatten prune ;
|