Tree Example 1

From neuromechanicwiki
Jump to: navigation, search

This toy example demonstrates the use of nearly every "Tree" function in the Neuromechanic API. It demonstrates how to create, edit, scan, read, and write xml files using Neuromechanic Trees.

In most of the actual functional examples the tree functions are not usually used. Instead, the Tree Class in nmextension.py is used to traverse the trees.

  1. import neuromechanic as nm
  2. import nmextension as nme
  3.  
  4. # tree_gettreebyname accepts "new", "model", "system", or "preferences"
  5. # To create a new tree use "new"
  6. rootnode = nm.tree_gettreebyname('New') 
  7.  
  8. # Make <B> a toplevel element
  9. bnode = nm.tree_createnode(rootnode,nme.NODE_ELEMENT,'B')
  10.  
  11. # Make <C> a child of <B>
  12. cnode = nm.tree_createnode(   bnode,nme.NODE_ELEMENT,'C')
  13.  
  14. # Make an attribute "Whose" in <C>
  15. whosenode = nm.tree_createnode(   cnode,nme.NODE_ATTRIBUTE,'Whose')
  16.  
  17. # Set the attribute "Whose"
  18. nm.tree_setvalue(whosenode,"This starts as a child of B")
  19.  
  20. # Set the value of <C> element values can be up to 2 dimensional numeric lists
  21. # as well as character strings and scalar numerics
  22. nm.tree_setvalue(cnode,[[1,0,0],[0,1,0],[0,0,1]]) 
  23.  
  24. # Create <A> as a sibling of <B>
  25. bparentnode = nm.tree_getparent(bnode)
  26. anode = nm.tree_createnode(bparentnode,nme.NODE_ELEMENT,'A')
  27. # anode = nm.tree_createnode(rootnode,nme.NODE_ELEMENT,'A');  # This would've created <A> in the same spot
  28.  
  29. # Write the tree structure to an xml file
  30. err = nm.tree_writenode(rootnode,'example1.xml')
  31. ''' example1.xml contains:
  32. <*>
  33.   <B>
  34.     <C Whose="This starts as a child of B">
  35. 1 0 0
  36. 0 1 0
  37. 0 0 1
  38.     </C>
  39.   </B>
  40.   <A/>
  41. </*>
  42. '''
  43.  
  44. err = nm.tree_writenodechildren(rootnode,'example2.xml')
  45. ''' example2.xml contains:
  46. <B>
  47.   <C Whose="This starts as a child of B">
  48. 1 0 0
  49. 0 1 0
  50. 0 0 1
  51.   </C>
  52. </B>
  53. <A/>
  54. '''
  55.  
  56. err = nm.tree_writenode(bnode,'example3.xml')
  57. ''' example3.xml contains:
  58. <B>
  59.   <C Whose="This starts as a child of B">
  60. 1 0 0
  61. 0 1 0
  62. 0 0 1
  63.   </C>
  64. </B>
  65. '''
  66.  
  67. # Move <C> to be a child of <A>
  68. nm.tree_adoptnode(cnode,anode)
  69. err = nm.tree_writenodechildren(rootnode,'example4.xml')
  70. ''' example4.xml contains:
  71. <B/>
  72. <A>
  73.   <C Whose="This starts as a child of B">
  74. 1 0 0
  75. 0 1 0
  76. 0 0 1
  77.   </C>
  78. </A>
  79. '''
  80.  
  81. # Check if <C> is "printable"
  82. printable = nm.tree_getprintable(cnode)
  83. if printable==0:
  84.    print('The <C> node is not printable')
  85. else:
  86.    print('The <C> node is printable')
  87.  
  88. # Make <C> unprintable, its still in the tree, just wont show up when printed
  89. nm.tree_setprintable(cnode,0)
  90. err = nm.tree_writenodechildren(rootnode,'example5.xml')
  91. ''' example5.xml contains:
  92. <B/>
  93. <A>
  94. </A>
  95. '''
  96.  
  97. # Delete the tree by destroying the root node
  98. nm.tree_destroynode(rootnode)
  99. # These wont be referenced to anything anymore, you better set them to None
  100. anode = bnode = cnode = whosenode = None
  101.  
  102. # Parse an xml file into memory
  103. rootnode = nm.tree_parsefile('example2.xml')
  104. # Loop through the toplevel elements
  105. for ii in range(nm.tree_getlength(rootnode)):
  106.    # get the iith child element
  107.    p = nm.tree_getchildi(rootnode,ii)
  108.    # get the tag of the iith child
  109.    tag = nm.tree_gettag(p)
  110.    # get its value
  111.    value = nm.tree_getvalue(p)
  112.    print('Element <'+tag+'> has value: '+str(value))
  113.  
  114. # Get all <C> elements that are descendants of the root
  115. cnodes = nm.tree_getlistbytag(rootnode,'C')
  116. # Loop through each <C> element
  117. for ii in range(nm.tree_getlength(cnodes)):
  118.    # get the iith child element
  119.    p = nm.tree_getchildi(cnodes,ii)
  120.    # get the tag of the iith child
  121.    tag = nm.tree_gettag(p)
  122.    # get its value
  123.    value = nm.tree_getvalue(p)
  124.    print('Element <'+tag+'> has value: '+str(value))
  125.    nm.tree_settag(p,'Z')
  126.    print('Element <'+tag+'> has value: '+str(value))
  127.  
  128. # Navigate the Tree to the <C> node by searching by name
  129. anode = nm.tree_getchildbytag(rootnode,'B')
  130. if anode > 0:
  131.    cnode = nm.tree_getchildbytag(   anode,'C')
  132.    if cnode > 0:
  133.       type_C = nm.tree_getnodetype(cnode)
  134.       if type_C == nme.NODE_ELEMENT:
  135.          print('C is an element')
  136.       elif type_C == nme.NODE_ATTRIBUTE:
  137.          print('C is an attribute')
  138.       elif type_C == nme.NODE_COMMENT:
  139.          print('C is a comment')
  140.  
  141.       C_firstchild = nm.tree_getchildi(cnode,0)
  142.       type_C_firstchild = nm.tree_getnodetype(C_firstchild)
  143.       if type_C_firstchild == nme.NODE_ELEMENT:
  144.          print("C's first child is an element")
  145.       elif type_C_firstchild == nme.NODE_ATTRIBUTE:
  146.          print("C's first child is an attribute")
  147.       elif type_C_firstchild == nme.NODE_COMMENT:
  148.          print("C's first child is a comment")