#
# -*- coding: utf-8 -*-



#===========================================================================================================
import                                  sys
import                                  imp
import os.path as                       _ospath
u                                       = g.configtool.utilities
_feds                                   = u.feds
inter                                   = u.kpyInterpol.inter

#===========================================================================================================
if __name__ == '__main__':

    #=======================================================================================================
    #   (1)     retrieve grove datasource
    #=======================================================================================================
    groveFed    = _feds.resolve( 'Grove' )
    dsGrove     = groveFed.dsGrove
    graphnames  = list( dsGrove.iterGraphnames() )
    graphnames.sort()
    graphnames  = '\n\t'.join( graphnames )
    print inter( ''
        +   "The grove contains graphs with the following names:\n\t$graphnames"
        )

    #=======================================================================================================
    #   (2)     iterate over graph facts
    #=======================================================================================================
    for graph in dsGrove.getTable( 'graph' ):
        print
        #   print graph
        print inter( ''
            +   "*\tGraph :$graph.name: represents\n"
            +   "\tnamespace '$graph.namespaceurl'\n"
            +   "\tand has $graph.length nodes. It has been constructed from\n"
            +   "\t'$graph.localgraphsource'."
            )
        #===================================================================================================
        #   (3)     iterate over node facts
        #===================================================================================================
        if graph.name == 'abcd12':
            nodename = '@PreferredFlag'
            nodes    = dsGrove.catchTable(
                node = dict(                # select * from node where
                    gauge   = graph.name,   #   graph = 'abcd12' and
                    name    = nodename,     #   name = '@PreferredFlag';
                    )
                )
            print inter( ''
                +   "\n\tThere are $len(nodes) nodes called '$@PreferredFlag' in graph :$graph.name:;\n"
                +   "\tthey are located below the following nodes:"
                )
            nodetable = dsGrove.getTable( 'nodetable' )
            for node in nodes:
                termref = ( graph.name, node.parentTermnr )
                parentnode = dsGrove.nodefactForTermref( termref )
                print inter( '\t\t$parentnode.locator' )








