AccessViewerSession.C 12.1 KB
Newer Older
hrchilds's avatar
hrchilds committed
1 2
/*****************************************************************************
*
3
* Copyright (c) 2000 - 2014, Lawrence Livermore National Security, LLC
hrchilds's avatar
hrchilds committed
4
* Produced at the Lawrence Livermore National Laboratory
5
* LLNL-CODE-442911
hrchilds's avatar
hrchilds committed
6 7
* All rights reserved.
*
8
* This file is  part of VisIt. For  details, see https://visit.llnl.gov/.  The
hrchilds's avatar
hrchilds committed
9 10 11 12 13 14 15 16 17 18
* full copyright notice is contained in the file COPYRIGHT located at the root
* of the VisIt distribution or at http://www.llnl.gov/visit/copyright.html.
*
* Redistribution  and  use  in  source  and  binary  forms,  with  or  without
* modification, are permitted provided that the following conditions are met:
*
*  - Redistributions of  source code must  retain the above  copyright notice,
*    this list of conditions and the disclaimer below.
*  - Redistributions in binary form must reproduce the above copyright notice,
*    this  list of  conditions  and  the  disclaimer (as noted below)  in  the
19 20 21
*    documentation and/or other materials provided with the distribution.
*  - Neither the name of  the LLNS/LLNL nor the names of  its contributors may
*    be used to endorse or promote products derived from this software without
hrchilds's avatar
hrchilds committed
22 23 24 25 26
*    specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT  HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR  IMPLIED WARRANTIES, INCLUDING,  BUT NOT  LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND  FITNESS FOR A PARTICULAR  PURPOSE
27 28 29
* ARE  DISCLAIMED. IN  NO EVENT  SHALL LAWRENCE  LIVERMORE NATIONAL  SECURITY,
* LLC, THE  U.S.  DEPARTMENT OF  ENERGY  OR  CONTRIBUTORS BE  LIABLE  FOR  ANY
* DIRECT,  INDIRECT,   INCIDENTAL,   SPECIAL,   EXEMPLARY,  OR   CONSEQUENTIAL
hrchilds's avatar
hrchilds committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
* DAMAGES (INCLUDING, BUT NOT  LIMITED TO, PROCUREMENT OF  SUBSTITUTE GOODS OR
* SERVICES; LOSS OF  USE, DATA, OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER
* CAUSED  AND  ON  ANY  THEORY  OF  LIABILITY,  WHETHER  IN  CONTRACT,  STRICT
* LIABILITY, OR TORT  (INCLUDING NEGLIGENCE OR OTHERWISE)  ARISING IN ANY  WAY
* OUT OF THE  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
*****************************************************************************/
#include <AccessViewerSession.h>
#include <DataNode.h>
#include <DebugStream.h>
#include <snprintf.h>

// ****************************************************************************
// Method: AccessViewerSession::AccessViewerSession
//
// Purpose: 
//   Constructor
//
// Programmer: Brad Whitlock
// Creation:   Mon Nov 13 15:43:58 PST 2006
//
// Modifications:
//   
// ****************************************************************************

AccessViewerSession::AccessViewerSession() : ConfigManager()
{
    root = 0;
}

// ****************************************************************************
// Method: AccessViewerSession::~AccessViewerSession
//
// Purpose: 
//   Destructor.
//
// Programmer: Brad Whitlock
// Creation:   Mon Nov 13 15:44:13 PST 2006
//
// Modifications:
//   
// ****************************************************************************

AccessViewerSession::~AccessViewerSession()
{
    delete root;
}

// ****************************************************************************
// Method: AccessViewerSession::ReadConfigFile
//
// Purpose: 
//   Reads the session file and stores it internally.
//
// Arguments:
//   filename : The name of the session file to open.
//
// Returns:    A pointer to the root data node or 0.
//
// Programmer: Brad Whitlock
// Creation:   Mon Nov 13 15:44:26 PST 2006
//
// Modifications:
94 95 96
//   Brad Whitlock, Tue Apr 22 12:19:30 PDT 2008
//   Skip the XML tag.
//
hrchilds's avatar
hrchilds committed
97
// ****************************************************************************
98 99 100 101 102 103 104 105 106 107 108
DataNode *
AccessViewerSession::ReadConfigFile(std::istream& in)
{
    // Skip the XML tag
    FinishTag(in);

    root = new DataNode("root");
    ReadObject(in, root);

    return GetRootNode();
}
hrchilds's avatar
hrchilds committed
109 110 111 112 113 114

DataNode *
AccessViewerSession::ReadConfigFile(const char *filename)
{
    const char *mName = "AccessViewerSession::ReadConfigFile: ";
    DataNode *node = 0;
115
    std::ifstream inf;
hrchilds's avatar
hrchilds committed
116
    // Try and open the file for reading.
hkrishna's avatar
hkrishna committed
117
    inf.open(filename, ios::in); // | ios::trunc); //"rt"
118
    if(inf.is_open() == false)
hrchilds's avatar
hrchilds committed
119 120 121 122 123
    {
        debug1 << mName << "Could not read " << filename << endl;
        return node;
    }

124
    node = ReadConfigFile(inf);
125

126
    inf.close();
hrchilds's avatar
hrchilds committed
127

128
    return node;
hrchilds's avatar
hrchilds committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
}

// ****************************************************************************
// Method: AccessViewerSession::WriteConfigFile
//
// Purpose: 
//   Writes the modified data tree to a session file.
//
// Arguments:
//   filename : The name of the file to write.
//
// Programmer: Brad Whitlock
// Creation:   Mon Nov 13 15:45:25 PST 2006
//
// Modifications:
//   
// ****************************************************************************
146 147 148 149 150 151 152 153 154
bool
AccessViewerSession::WriteConfigFile(std::ostream& out)
{
    // Write the output file to stdout for now.
    out << "<?xml version=\"1.0\"?>\n";
    WriteObject(out, GetRootNode());

    return true;
}
hrchilds's avatar
hrchilds committed
155 156 157 158 159 160 161

bool
AccessViewerSession::WriteConfigFile(const char *filename)
{
    // If we have no root then return.
    if(root == 0)
        return false;
162
    std::ofstream outf;
hrchilds's avatar
hrchilds committed
163
    // Try to open the output file.
164 165
    outf.open(filename, ios::out | ios::trunc);
    if(outf.is_open() == false)
hrchilds's avatar
hrchilds committed
166 167
        return false;

168
    bool res = WriteConfigFile(outf);
hrchilds's avatar
hrchilds committed
169 170

    // close the file
171
    outf.close();
hrchilds's avatar
hrchilds committed
172

173
    return res;
hrchilds's avatar
hrchilds committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187
}

// ****************************************************************************
// Method: AccessViewerSession::GetRootNode
//
// Purpose: 
//   Returns the pointer to the root node.
//
// Returns:    The pointer to the root node.
//
// Programmer: Brad Whitlock
// Creation:   Mon Nov 13 15:45:59 PST 2006
//
// Modifications:
188 189 190
//   Brad Whitlock, Thu Jul 24 17:01:29 PDT 2008
//   Fixed a bug where we could not locate the VIEWER node.
//
hrchilds's avatar
hrchilds committed
191 192 193 194 195 196 197
// ****************************************************************************

DataNode *
AccessViewerSession::GetRootNode() const
{
    DataNode *ret = 0;
    if(root != 0)
198
        ret = root->SearchForNode("VIEWER");
hrchilds's avatar
hrchilds committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
    return ret;
}

// ****************************************************************************
// Method: AccessViewerSession::GetVSNode
//
// Purpose: 
//   Returns the pointer to the ViewerSubject node.
//
// Returns:    The pointer to the ViewerSubject node or 0.
//
// Programmer: Brad Whitlock
// Creation:   Mon Nov 13 15:46:25 PST 2006
//
// Modifications:
//   
// ****************************************************************************

DataNode *
AccessViewerSession::GetVSNode() const
{
    DataNode *ret = 0;
    if(root != 0)
222
        ret = root->SearchForNode("ViewerSubject");
hrchilds's avatar
hrchilds committed
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
    return ret;
}

// ****************************************************************************
// Method: AccessViewerSession::GetSourceMap
//
// Purpose: 
//   Reads the "SourceMap" entry from the session file if it exists.
//
// Arguments:
//   keys   : The source identifiers.
//   values : The source names.
//   uses   : How the sources are used.
//
// Returns:    True on success; False otherwise.
//
// Note:       This method reads the source map from a viewer session file so
//             we can provide UI controls to the user that allow him to change
//             the list of sources being visualized. We also scan the plots
//             in the session file so we can indicate how each source is used.
//
// Programmer: Brad Whitlock
// Creation:   Mon Nov 13 15:46:53 PST 2006
//
// Modifications:
//   
// ****************************************************************************

bool
AccessViewerSession::GetSourceMap(stringVector &keys, stringVector &values,
    std::map<std::string, stringVector> &uses)
{
    const char *mName = "AccessViewerSession::GetSourceMap: ";

    DataNode *vsNode = GetVSNode();
    bool ret = false;
    if(vsNode != 0)
    {
        DataNode *smNode = vsNode->GetNode("SourceMap");
        if(smNode != 0)
        {
            keys.clear();
            values.clear();

            DataNode **children = smNode->GetChildren();
            for(int i = 0; i < smNode->GetNumChildren(); ++i)
            {
                if(children[i]->GetNodeType() == STRING_NODE)
                {
                    keys.push_back(children[i]->GetKey());
                    values.push_back(children[i]->AsString());
                }
            }

            ret = keys.size() > 0;            
        }
279 280 281 282
        else
        {
            debug1 << mName << "Could not find SourceMap node." << endl;
        }
hrchilds's avatar
hrchilds committed
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363

        // NOTE: This section knows a lot about viewer session files, which
        //       means that if the viewer session format changes then this
        //       code must also change.
        //
        // Look through the plots in the session file and determine
        // where each source is used so we can give a little more
        // information to the user.
        DataNode *wmNode = vsNode->GetNode("ViewerWindowManager");
        if(wmNode != 0)
        {
            DataNode *winNode = wmNode->GetNode("Windows");
            if(winNode != 0)
            {
                DataNode **wins = winNode->GetChildren();
                for(int i = 0; i < winNode->GetNumChildren(); ++i)
                {
                    if(wins[i]->GetNodeType() == INTERNAL_NODE &&
                       wins[i]->GetKey() == "ViewerWindow")
                    {
                        DataNode *vpl = wins[i]->GetNode("ViewerPlotList");
                        if(vpl != 0)
                        {
                            char tmp[1000];
                            int ploti = 0;
                            DataNode *plotNode = 0;
                            do
                            {
                                SNPRINTF(tmp, 1000, "plot%02d", ploti++);
                                plotNode = vpl->GetNode(tmp);
                                if(plotNode != 0)
                                {
                                    DataNode *pluginIDNode = 0,
                                             *varNameNode = 0,
                                             *sourceIDNode = 0;
                                    pluginIDNode = plotNode->GetNode("pluginID");
                                    sourceIDNode = plotNode->GetNode("sourceID");
                                    varNameNode = plotNode->GetNode("variableName");

                                    if(sourceIDNode != 0 && 
                                       sourceIDNode->GetNodeType() == STRING_NODE &&
                                       pluginIDNode != 0 && 
                                       pluginIDNode->GetNodeType() == STRING_NODE &&
                                       varNameNode != 0 && 
                                       varNameNode->GetNodeType() == STRING_NODE)
                                    {
                                        std::string source(sourceIDNode->AsString());
                                        std::string varName(varNameNode->AsString());
                                        std::string plotName(pluginIDNode->AsString());

                                        std::string::size_type pos = plotName.rfind("_");
                                        if(pos != std::string::npos)
                                            plotName = plotName.substr(0, pos);

                                        SNPRINTF(tmp, 1000, "Window %d, %s plot of %s",
                                            i+1, plotName.c_str(), varName.c_str());

                                        if(uses.find(source) == uses.end())
                                            uses[source] = stringVector();
                                        uses[source].push_back(std::string(tmp));
                                    }
                                    else
                                    {
                                        debug1 << mName << "pluginID, sourceID, or "
                                        "variableName nodes not located or they "
                                        "were the wrong types." << endl;
                                    }
                                }
                            } while(plotNode != 0);
                        }
                    }
                }
            }
        }

    }

    return ret;
}