[1917] | 1 | |
---|
| 2 | # ---------------------------------------------------------------------- |
---|
| 3 | # COMPONENT: datatable - extracts data from an XML description of a field |
---|
| 4 | # |
---|
| 5 | # This object represents a datatable of data in an XML description of |
---|
| 6 | # simulator output. A datatable is similar to a field, but a field is |
---|
| 7 | # a quantity versus position in device. A datatable is any quantity |
---|
| 8 | # versus any other quantity. This class simplifies the process of |
---|
| 9 | # extracting data vectors that represent the datatable. |
---|
| 10 | # ====================================================================== |
---|
| 11 | # AUTHOR: Michael McLennan, Purdue University |
---|
| 12 | # Copyright (c) 2004-2005 Purdue Research Foundation |
---|
| 13 | # |
---|
| 14 | # See the file "license.terms" for information on usage and |
---|
| 15 | # redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
| 16 | # ====================================================================== |
---|
| 17 | package require Itcl |
---|
| 18 | package require BLT |
---|
| 19 | |
---|
| 20 | namespace eval Rappture { |
---|
| 21 | # forward declaration |
---|
| 22 | } |
---|
| 23 | |
---|
| 24 | itcl::class Rappture::DataTable { |
---|
| 25 | constructor {xmlobj path} { |
---|
[1923] | 26 | # defined below |
---|
[1917] | 27 | } |
---|
| 28 | destructor { |
---|
[1923] | 29 | # defined below |
---|
[1917] | 30 | } |
---|
| 31 | |
---|
| 32 | public method components {{pattern *}} |
---|
| 33 | public method values {} |
---|
| 34 | public method columns {} |
---|
| 35 | public method hints {{key ""}} |
---|
| 36 | |
---|
| 37 | protected method Build {} |
---|
| 38 | private variable _xmlobj "" ; # ref to lib obj with datatable data |
---|
| 39 | private variable _datatable ""; # lib obj representing this datatable |
---|
| 40 | private variable _columns "" |
---|
| 41 | private variable _hints ; # cache of hints stored in XML |
---|
| 42 | private variable _tree "" |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | # ---------------------------------------------------------------------- |
---|
| 46 | # CONSTRUCTOR |
---|
| 47 | # ---------------------------------------------------------------------- |
---|
| 48 | itcl::body Rappture::DataTable::constructor {xmlobj path} { |
---|
| 49 | if {![Rappture::library isvalid $xmlobj]} { |
---|
[1923] | 50 | error "bad value \"$xmlobj\": should be LibraryObj" |
---|
[1917] | 51 | } |
---|
| 52 | set _xmlobj $xmlobj |
---|
| 53 | set _datatable [$xmlobj element -as object $path] |
---|
| 54 | |
---|
| 55 | # build up vectors for various components of the datatable |
---|
| 56 | Build |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | # ---------------------------------------------------------------------- |
---|
| 60 | # DESTRUCTOR |
---|
| 61 | # ---------------------------------------------------------------------- |
---|
| 62 | itcl::body Rappture::DataTable::destructor {} { |
---|
| 63 | itcl::delete object $_datatable |
---|
| 64 | blt::tree destroy $_tree |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | # ---------------------------------------------------------------------- |
---|
| 68 | # USAGE: values |
---|
| 69 | # |
---|
| 70 | # Returns the values for the datatable as a BLT tree. |
---|
| 71 | # ---------------------------------------------------------------------- |
---|
| 72 | itcl::body Rappture::DataTable::values {} { |
---|
| 73 | return $_tree |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | # ---------------------------------------------------------------------- |
---|
| 77 | # USAGE: columns |
---|
| 78 | # |
---|
| 79 | # Returns the columns of the datatable in order. |
---|
| 80 | # ---------------------------------------------------------------------- |
---|
| 81 | itcl::body Rappture::DataTable::columns {} { |
---|
| 82 | return $_columns |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | # ---------------------------------------------------------------------- |
---|
| 86 | # USAGE: hints ?<keyword>? |
---|
| 87 | # |
---|
| 88 | # Returns a list of key/value pairs for various hints about plotting |
---|
| 89 | # this datatable. If a particular <keyword> is specified, then it returns |
---|
| 90 | # the hint for that <keyword>, if it exists. |
---|
| 91 | # ---------------------------------------------------------------------- |
---|
| 92 | itcl::body Rappture::DataTable::hints {{keyword ""}} { |
---|
| 93 | if {![info exists _hints]} { |
---|
[1923] | 94 | foreach {key path} { |
---|
| 95 | group about.group |
---|
| 96 | label about.label |
---|
| 97 | color about.color |
---|
| 98 | style about.style |
---|
| 99 | } { |
---|
| 100 | set str [$_datatable get $path] |
---|
| 101 | if {"" != $str} { |
---|
| 102 | set _hints($key) $str |
---|
| 103 | } |
---|
| 104 | } |
---|
[1917] | 105 | } |
---|
| 106 | set _hints(xmlobj) $_xmlobj |
---|
| 107 | if { $keyword != "" } { |
---|
[1923] | 108 | if { [info exists _hints($keyword)] } { |
---|
| 109 | return $_hints($keyword) |
---|
| 110 | } |
---|
| 111 | return "" |
---|
[1917] | 112 | } |
---|
| 113 | return [array get _hints] |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | # ---------------------------------------------------------------------- |
---|
| 117 | # USAGE: Build |
---|
| 118 | # |
---|
| 119 | # Used internally to build up the vector representation for the |
---|
| 120 | # datatable when the object is first constructed, or whenever the datatable |
---|
| 121 | # data changes. Discards any existing vectors and builds everything |
---|
| 122 | # from scratch. |
---|
| 123 | # ---------------------------------------------------------------------- |
---|
| 124 | itcl::body Rappture::DataTable::Build {} { |
---|
| 125 | # Discard any existing data |
---|
| 126 | if { $_tree != "" } { |
---|
[1923] | 127 | blt::tree destroy $_tree |
---|
| 128 | set _tree "" |
---|
[1917] | 129 | } |
---|
| 130 | set _columns {} |
---|
| 131 | |
---|
| 132 | # Sniff for column information: label, descriptions, and style |
---|
| 133 | foreach cname [$_datatable children -type "column"] { |
---|
[1923] | 134 | set label [$_datatable get "$cname.label"] |
---|
| 135 | set description [$_datatable get "$cname.description"] |
---|
| 136 | set style [$_datatable get "$cname.style"] |
---|
| 137 | lappend _columns $label $description $style |
---|
[1917] | 138 | } |
---|
| 139 | set csvdata [$_datatable get csv] |
---|
| 140 | set _tree [blt::tree create] |
---|
| 141 | if {"" != $csvdata} { |
---|
[1939] | 142 | set d0 [blt::datatable create] |
---|
| 143 | package require blt_datatable_csv |
---|
| 144 | $d0 import csv -data $csvdata |
---|
| 145 | for { set row 1 } { $row <= [$d0 row length] } { incr row } { |
---|
[1923] | 146 | set child [$_tree insert 0] |
---|
| 147 | set c 0 |
---|
[1939] | 148 | set values [$d0 row values $row] |
---|
| 149 | foreach value $values {label description style} $_columns { |
---|
[1923] | 150 | if { $label == "" } { |
---|
| 151 | set label "$c" |
---|
| 152 | } |
---|
| 153 | $_tree set $child $label $value |
---|
| 154 | incr c |
---|
| 155 | } |
---|
| 156 | } |
---|
[1939] | 157 | blt::datatable destroy $d0 |
---|
[1917] | 158 | } |
---|
| 159 | } |
---|