factor/extra/semantic-db/hierarchy/hierarchy.factor

46 lines
1.2 KiB
Factor
Raw Normal View History

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
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 )
"has parent" relation-id ;
: parent-child* ( parent child -- arc-id )
has-parent-relation spin create-arc* ;
: parent-child ( parent child -- )
parent-child* drop ;
: un-parent-child ( parent child -- )
2008-03-07 18:07:11 -05:00
has-parent-relation spin <arc> select-tuples [ id>> delete-arc ] each ;
: child-arcs ( node-id -- child-arcs )
has-parent-relation f rot <arc> select-tuples ;
: children ( node-id -- children )
child-arcs [ subject>> ] map ;
: parent-arcs ( node-id -- parent-arcs )
has-parent-relation swap f <arc> select-tuples ;
2008-02-21 04:44:15 -05:00
: parents ( node-id -- parents )
parent-arcs [ object>> ] map ;
2008-02-21 04:44:15 -05:00
: get-node-hierarchy ( node-id -- tree )
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 )
(get-root-nodes) flatten prune ;