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


"""

Demonstration script to illaustrate ways of accessing dsGrove.


"""




#-----------------------------------------------------------------------------------------------------------
#   Enable globals:
try:
    g
except NameError:
    import os.path as _p; import inspect as _i
    pathToScript = _p.pardir, _p.pardir, 'biocase', 'lib', 'biocase', 'fundamentals.py'
    execfile( _p.join( _p.dirname( _i.getfile( lambda:None ) ), *pathToScript ) )



#-----------------------------------------------------------------------------------------------------------
#   Get data source federator and data source:
groveFed    = g.configtool.utilities.feds.resolve( 'Grove', readonly = True )
dsGrove     = groveFed.dsGrove

#-----------------------------------------------------------------------------------------------------------
#   Iterate some facts in dsGrove -- don't get confused with fields named 'gauge', what they really
#   contain is graphnames (i.e. they don't know ':abcd12:', only 'abcd12' etc.):
for fact in dsGrove.catchTable( node = dict( gauge = 'bion' ) ): print fact
print
for fact in dsGrove.catchTable( graph = None ): print fact
print
for fact in dsGrove.catchTable( property = dict( gauge = 'bion' ) ): print fact
print


#-----------------------------------------------------------------------------------------------------------
#   Build a cache for **much** (approx. 100 times) faster access:
ancestorsCache = dsGrove.getPropertiesForAllNodes( 'abcd12', 'ancestors' )

for termnr in range( dsGrove.getGraphlength( 'bion' ) ):
    ancestorTermnrs = ancestorsCache[ str( termnr ) ]
    print ancestorTermnrs

#-----------------------------------------------------------------------------------------------------------
#   Resolve tuples or strings that contain a ':xxx:' notation and a numerical or textual reference
#   to a node:
print
print dsGrove.nodefactForOntonym(   ':abcd12:2'     )
print dsGrove.nodefactForOntonym( ( ':abcd12:', '2' ) )
print dsGrove.nodefactForOntonym( ( ':abcd12:',  2  ) )
print dsGrove.nodefactForOntonym(   ':abcd12:/DataSets/DataSet/DatasetDerivations' )
print dsGrove.nodefactForOntonym( ( ':abcd12:', '/DataSets/DataSet/DatasetDerivations' ) )
nodefact = dsGrove.nodefactForOntonym(   ':abcd12:2'     )
print
print nodefact.gauge
print nodefact.name
print nodefact.locator
print nodefact.ontonym
print nodefact.parentTermnr
print nodefact.termnr
print nodefact.level
print nodefact.isLeaf
#-----------------------------------------------------------------------------------------------------------
#   If what you have is exactly a graphname and termnr you can pass so-called termrefs:
print
print dsGrove.nodefactForTermref( (  'abcd12',  2   ) )
print dsGrove.nodefactForTermref( (  'abcd12', '2'  ) )

print
print g.cfg.prefixesByNamespaces[ 'http=//www.tdwg.org/schemas/abcd/1.2' ]
print g.cfg.namespacesByPrefix[ 'abcd12' ]


groveFed.close()




