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