Commit 97733713 authored by Jeannie Backer's avatar Jeannie Backer
Browse files

Added isisminer program. Updated PvlFlatMap. Fixes #2262

git-svn-id: http://subversion.wr.usgs.gov/repos/prog/isis3/trunk@6592 41f8697f-d340-4b68-9986-7bafba869bb8
parent 15e263dd
Loading
Loading
Loading
Loading
+222 −0
Original line number Diff line number Diff line
/**                                                                       
 * @file                                                                  
 * $Revision: 6513 $
 * $Date: 2016-01-14 16:04:44 -0700 (Thu, 14 Jan 2016) $
 * $Id: AssetSidebarStrategy.cpp 6513 2016-01-14 23:04:44Z kbecker@GS.DOI.NET $
 * 
 *   Unless noted otherwise, the portions of Isis written by the USGS are 
 *   public domain. See individual third-party library and package descriptions 
 *   for intellectual property information, user agreements, and related  
 *   information.                                                         
 *                                                                        
 *   Although Isis has been used by the USGS, no warranty, expressed or   
 *   implied, is made by the USGS as to the accuracy and functioning of such 
 *   software and related material nor shall the fact of distribution     
 *   constitute any such warranty, and no responsibility is assumed by the
 *   USGS in connection therewith.                                        
 *                                                                        
 *   For additional information, launch                                   
 *   $ISISROOT/doc//documents/Disclaimers/Disclaimers.html                
 *   in a browser or see the Privacy & Disclaimers page on the Isis website,
 *   http://isis.astrogeology.usgs.gov, and the USGS privacy and disclaimers on
 *   http://www.usgs.gov/privacy.html.                                    
 */ 
#include "AssetSidebarStrategy.h"

// boost library
#include <boost/foreach.hpp>

// other ISIS
#include "Application.h"
#include "PvlFlatMap.h"
#include "PvlObject.h"
#include "Resource.h"

using namespace std;

namespace Isis {

  /** 
   * Default constructor
   * 
   * Note: This method is not called by isisminer
   * or any of it's support classes so it is not covered
   * by appTests.  It is not possible to get full test
   * coverage unless isisminer implementation changes
   * or this class is moved to an objs directory with a unitTest.
   */  
  AssetSidebarStrategy::AssetSidebarStrategy() : SidebarStrategy(), 
                        m_asset(), m_create(false), m_removeEmpties(true),
                        m_source(FromCopy) {
    setName("AssetSidebar");
    setType("AssetSidebar");
  }
  

  /** 
   * @brief Constructor loads from a Strategy object AssetSidebar definition
   * 
   * This constructor loads and retains processing parameters from the AssetSidebar
   * Strategy object definition as (typically) read from the configuration file.
   * 
   * @author 2012-07-15 Kris Becker
   * 
   * @param definition AssetSidebar Strategy PVL object definition
   * @param globals Global Resource of keywords
   */  
  AssetSidebarStrategy::AssetSidebarStrategy(const PvlObject &definition, 
                                             const ResourceList &globals) : 
                                             SidebarStrategy(definition, globals), 
                                             m_asset(), m_create(false),
                                             m_removeEmpties(true),
                                             m_source(FromCopy) {
  
    PvlFlatMap parms(getDefinitionMap());
    m_asset         = parms.get("Asset");
    m_create        = ("create" == parms.get("Operation", "process").toLower());
    m_removeEmpties = toBool(parms.get("ClearOnEmpty", "true"));

    QString source = parms.get("CreateSource", "copy").toLower();
    if ( "clone" == source ) { m_source = FromClone; }
    else if ( "none" == source ) { m_source = FromNone; }
    else { m_source = FromCopy; }

    return; 
  }
  
  
  /**
   * Destructor
   */
  AssetSidebarStrategy::~AssetSidebarStrategy() { }



  /** Remove existing asset if create option is invoked */
  int AssetSidebarStrategy::preRunProcess(ResourceList &resources, 
                                     const ResourceList &globals) {

    //  Count current active ones whilst incrementing the counter
    BOOST_FOREACH ( SharedResource resource, resources ) {
      if ( !resource->isDiscarded() ) { 
        processed(); 
        // If creating a an existing one of the complete resource list by
        // default. Either a copy or a clone.
        if ( true == m_create ) {
          if ( FromCopy == m_source ) {
            QVariant asset(QVariant::fromValue(copyList(resources))); 
            resource->addAsset(m_asset, asset);
          }
          else if ( FromClone == m_source ) {
            QVariant asset(QVariant::fromValue(cloneList(resources))); 
            resource->addAsset(m_asset, asset);
          }
          else {
            resource->removeAsset( m_asset );
          }
        }
      }
    }

    return (totalProcessed());
  }


  /** 
   * @brief Applies a strategy to assets of active resources.
   * 
   * This method applies another strategy to active resources' assets.
   * 
   * When the Operation keyword has a value of "create", this method will not try to obtain
   * any assets contained in active resources. This is in order to create new assets to 
   * add to active resources. If this operation is not "create", then this method will
   * attempt to obtain assets that are contained in active resources, apply the strategy to
   * these assets, then add the processed assets to the active resources.
   * 
   * @author 2012-07-15 Kris Becker
   * 
   * @param strategy The strategy to apply to assets
   * @param resources The active resources
   * @param globals Global Resource of keywords
   * @return int The total number of assets processed in the strategy
   */  
  int AssetSidebarStrategy::apply(SharedStrategy &strategy, 
                                  ResourceList &resources, 
                                  const ResourceList &globals) { 
    int nassets = 0;
    BOOST_FOREACH ( SharedResource resource, resources ) {
      if ( resource->isActive() ) {
        ResourceList assetList;

        // Obtain assets from the current resource if they exist
        if ( resource->hasAsset(m_asset) ) {
          QVariant asset = resource->asset(m_asset);
          // Note: this conditional will not evaluate as False
          // ResourceList is declared as a Q_METATYPE (QVariant) in Resource.h
          // Thus, the QVariant asset will be converted to ResourceList, another QVariant
          if ( asset.canConvert<ResourceList>() ) {
            assetList = asset.value<ResourceList>();
          }
        }
        
        //  Apply the strategy to the list of assets
        nassets += strategy->apply( assetList, getGlobals(resource, globals) ); 
        
        //  Add the list to the asset
        QVariant v_asset(QVariant::fromValue(assetList));
        resource->addAsset(m_asset, v_asset);
      }
    }
  
    return (nassets);
  }

/**
 * @brief Post run processing after execution of all strategies 
 *  
 * This method will be invoked by SidebarStrategy after all the strategies have 
 * been ran. This implementation will check for the existance of the named 
 * asset and determine if empty lists exists. If the user requests, removed 
 * these assets. 
 *  
 * Note this will only operate on active Resources. 
 *  
 * The active status of the Resources is unaltered (users can use the 
 * ResourceManager to do that). 
 * 
 * @author 2015-09-15 Kris Becker
 * 
 * @param resources List of Resources to check for assets
 * @param globals   Global parameter pool
 * 
 * @return int Number of empty Asset resource lists found and removed
 */
  int AssetSidebarStrategy::postRunProcess(ResourceList &resources, 
                                          const ResourceList &globals) {

    // Set up incoming asset handling conditions
    int nremoved = 0;
    if ( m_removeEmpties ) {
      BOOST_FOREACH ( SharedResource resource, resources ) {
        if ( resource->isActive() ) {
          if ( resource->hasAsset(m_asset) ) {
            QVariant asset = resource->asset(m_asset);
            // Note: this conditional will not evaluate as False
            // ResourceList is declared as a Q_METATYPE (QVariant) in Resource.h
            // Thus, the QVariant asset will be converted to ResourceList, another QVariant
            if ( asset.canConvert<ResourceList>() ) {
              ResourceList assetList = asset.value<ResourceList>();
              if ( assetList.size() == 0) {
                resource->removeAsset(m_asset);
                nremoved++;
              }
            }
          }
        }
      }
    }
    return ( SidebarStrategy::postRunProcess(resources, globals) );
  }


}  //namespace Isis
+115 −0
Original line number Diff line number Diff line
#ifndef AssetSidebarStrategy_h
#define AssetSidebarStrategy_h
/**
 * @file                                                                  
 * $Revision: 6513 $ 
 * $Date: 2016-01-14 16:04:44 -0700 (Thu, 14 Jan 2016) $ 
 * $Id: AssetSidebarStrategy.h 6513 2016-01-14 23:04:44Z kbecker@GS.DOI.NET $
 *
 *   Unless noted otherwise, the portions of Isis written by the USGS are
 *   public domain. See individual third-party library and package descriptions
 *   for intellectual property information, user agreements, and related
 *   information.
 *
 *   Although Isis has been used by the USGS, no warranty, expressed or
 *   implied, is made by the USGS as to the accuracy and functioning of such
 *   software and related material nor shall the fact of distribution
 *   constitute any such warranty, and no responsibility is assumed by the
 *   USGS in connection therewith.
 *
 *   For additional information, launch
 *   $ISISROOT/doc//documents/Disclaimers/Disclaimers.html
 *   in a browser or see the Privacy &amp; Disclaimers page on the Isis website,
 *   http://isis.astrogeology.usgs.gov, and the USGS privacy and disclaimers on
 *   http://www.usgs.gov/privacy.html.
 */

// parent class
#include "SidebarStrategy.h"

// Qt library
#include <QString>

// for ResourceList, SharedResource, and SharedStrategy typedefs
#include "Resource.h"
#include "Strategy.h"

namespace Isis {
  class PvlObject;

  /**
   * @brief AssetSidebarStrategy - allows assets to be processed with most strategies.
   * 
   * This strategy gives users the ability to process assets with most other isisminer 
   * strategies. 
   * 
   * If the user gives the Operation keyword a value of "create", then this strategy will create
   * new assets to add to the active resources. In other words, the specified strategy will not be 
   * applied to any existing assets contained in the active resources. Therefore, this operation
   * would be useful with strategies that create/obtain resources, such as PvlReader. 
   * The created assets would then added to the active resources.
   * 
   * If the Operation keyword does not exist or has a value other than "create", then this 
   * AssetSidebar strategy will apply the provided strategy to the assets contained in the active
   * resources and add the processed assets to the active resources.
   *  
   * @code
   * Object = Strategy
   * Name        = TestCreateAsset
   * Type        = AssetSidebar
   * Asset       = AssetA
   * Operation   = create
   * Description = "Adds AssetA (assets read from test.csv) to the active resources"
   *  Object = IsisMiner
   *    Object = Strategy
   *      Name = ReadAssetTest
   *      Type = CsvReader
   *      CsvFile = "test.csv"
   *      HasHeader = True
   *      SkipLines = 0
   *      IgnoreComments = False
   *      Delimiter = ", "
   *    EndObject
   *  EndObject
   * EndObject
   * @endcode
   *  
   * @author 2012-07-15 Kris Becker
   * @internal
   *   @history 2012-07-15 Kris Becker - Original version.
   *   @history 2015-03-18 Jeannie Backer - Brought class files closer to ISIS coding standards.
   *   @history 2015-05-01 Ian Humphrey - Updated documentation.
   *   @history 2015-05-08 Kris Becker - Modify constructor to take a global
   *                          resources list; modified apply() method to accept
   *                          a global resource list.
   *   @history 2015-09-15 Kris Becker - Reworked how the create option is
   *                         implemented.
   */
  class AssetSidebarStrategy : public SidebarStrategy {
  
    public:
      AssetSidebarStrategy();
      AssetSidebarStrategy(const PvlObject &definition, const ResourceList &globals);
      virtual ~AssetSidebarStrategy();
  
    protected:
      virtual int preRunProcess(ResourceList &resources, const ResourceList &globals);

      virtual int apply(SharedStrategy &strategy, ResourceList &resources,
                        const ResourceList &globals);

      
      virtual int postRunProcess(ResourceList &resources, const ResourceList &globals);
  
    private:
      enum CreateSource{ FromNone, FromCopy, FromClone };
      QString      m_asset;         //!< Name (identifier) of the asset to process
      bool         m_create;        //!< Is the opertatio to create an asset?
      bool         m_removeEmpties; //!< Remove asset if empty after all have been processed
      CreateSource m_source;        //!< Source of Asset list creation

  };

} // Namespace Isis

#endif
+384 −0

File added.

Preview size limit exceeded, changes collapsed.

+181 −0
Original line number Diff line number Diff line
#ifndef CalculatorStrategy_h
#define CalculatorStrategy_h
/**
 * @file                                                                  
 * $Revision: 6513 $ 
 * $Date: 2016-01-14 16:04:44 -0700 (Thu, 14 Jan 2016) $ 
 * $Id: CalculatorStrategy.h 6513 2016-01-14 23:04:44Z kbecker@GS.DOI.NET $
 *
 *   Unless noted otherwise, the portions of Isis written by the USGS are
 *   public domain. See individual third-party library and package descriptions
 *   for intellectual property information, user agreements, and related
 *   information.
 *
 *   Although Isis has been used by the USGS, no warranty, expressed or
 *   implied, is made by the USGS as to the accuracy and functioning of such
 *   software and related material nor shall the fact of distribution
 *   constitute any such warranty, and no responsibility is assumed by the
 *   USGS in connection therewith.
 *
 *   For additional information, launch
 *   $ISISROOT/doc//documents/Disclaimers/Disclaimers.html
 *   in a browser or see the Privacy &amp; Disclaimers page on the Isis website,
 *   http://isis.astrogeology.usgs.gov, and the USGS privacy and disclaimers on
 *   http://www.usgs.gov/privacy.html.
 */

// parent class
#include "Strategy.h"
// contains CalculatorVariablePool parent 
#include "InlineCalculator.h"

// Qt library
#include <QString>
#include <QSharedPointer>
#include <QVector>

// SharedResource typedef
#include "PvlFlatMap.h"
#include "Resource.h"

namespace Isis {

  class PvlObject;
  struct Equation;

  /**
   * @brief CalculatorStrategy - provides inline calculations
   *
   * This strategy provides user with the ability to write new keywords to
   * each Resource based on an equation with keyword names as variables.
   *
   * Here is an example of a Calculator Strategy object definition:
   *
   * @code
   * Object = Strategy
   *   Name = Trigonometry
   *   Type = Calculator
   *   Description = "Calculate trigonometric functions of angle."
   *   Group = Initializers
   *     Sine    = 0
   *     Cosine  = 0
   *     Tangent = 0
   *   EndGroup
   *   Group = Equations
   *     Sine    = "sin(angle)"
   *     Cosine  = "cos(angle)"
   *     Tangent = "tan(angle)"
   *   EndGroup
   * EndObject
   * @endcode
   *  
   * @author 2012-07-15 Kris Becker
   *
   * @internal
   *   @history 2012-07-15 Kris Becker - Original version.
   *   @history 2015-03-18 Jeannie Backer - Brought class files closer to ISIS coding standards.
   *   @history 2015-03-24 Jeffrey Covington - Documented class and methods.
   *   @history 2015-03-31 Jeffrey Covington - Added the Equations group to support multiple
   *                                           equations per Strategy.
   *   @history 2015-05-08 Kris Becker - Modify constructor to take a global
   *                          resources list; modified apply() method to accept
   *                          a global resource list.
   *   @history 2015-06-03 Kris Becker - Modified the calculate() method to use
   *                          a list of resources as the variable pool to
   *                          support use of global parameters. This also
   *                          required changes to ResourceCalculatorVariablePool
   *                          to accept a ResourceList in addition to a
   *                          Resource.
   *   @history 2015-10-12 Kris Becker - Enhanced the initializer options to i
   *                          include argument subsititution.
   *  
   *              
   */
  class CalculatorStrategy : public Strategy {
  
    public:
      CalculatorStrategy();
      CalculatorStrategy(const PvlObject &definition, const ResourceList &globals);
      virtual ~CalculatorStrategy();
  
      virtual int apply(SharedResource &resource, const ResourceList &globals);

      double  result() const;
  
    protected:
      int    initialize(SharedResource &resource, const ResourceList &globals);
      double calculate(SharedResource &resource);
      double calculate(ResourceList &resources);
  
      int    initialize(PvlFlatMap &pvl);
      double calculate(PvlFlatMap &pvl);
  
    private:
      QList<Equation>                   m_equations;    //!< Stores the equations
      QSharedPointer<PvlFlatMap>        m_initializers; //!< Stores the initializers
      QStringList                       m_initArgs;     //!< Initializer arguments
      QSharedPointer<InlineCalculator>  m_calculator;   /**!< The current calculator to use in
                                                              calculations */
      double                            m_result;       //!< The result of the calculation
  };


  /**
   * @brief Provides Resource wrapper for variables to Calculator class 
   *  
   * This small class provides the interface to the InlineCalculator class to 
   * lookup and provide variables in equations from Resources. You can choose to 
   * provide only one resource or a resource list. 
   * 
   * @author 2013-02-02 Kris Becker 
   *  
   * @internal 
   *   @history 2015-06-03 Kris Becker - Modified to accept a ResourceList
   *                          in addition to a Resource. Now traverses the list
   *                          of Resources to find the variable.
   */
  class ResourceCalculatorVariablePool : public CalculatorVariablePool {
    public:
      ResourceCalculatorVariablePool(SharedResource &resource);
      ResourceCalculatorVariablePool(ResourceList &resources);
      virtual ~ResourceCalculatorVariablePool(); 
      virtual bool exists(const QString &variable) const;
      virtual QVector<double> value(const QString &variable, 
                                    const int &index = 0) const;
    private:
      ResourceList   m_resources; //!< A reference to the resource list
  };


  /**
   * @brief Provides PvlFlatMap wrapper for variables to Calculator class 
   *  
   * This small class provides the interface to the InlineCalculator class to 
   * lookup and provide variables in equations from Pvl sources.
   * 
   * @author 2013-02-19 Kris Becker 
   */
  class PvlFlatMapCalculatorVariablePool : public CalculatorVariablePool {
    public:
      PvlFlatMapCalculatorVariablePool(PvlFlatMap &pvl);
      virtual ~PvlFlatMapCalculatorVariablePool();
      virtual bool exists(const QString &variable) const;
      virtual QVector<double> value(const QString &variable, 
                                    const int &index = 0) const;

    private:
      PvlFlatMap &m_pvl; //!< 
  };

  /**
   * Stores relevant information about each equation.
   */
  struct Equation {
    QString equation;                            //!< Stores the string of the equation
    QString store;                               /**!< The keyword to store the result of the
                                                       equation in */
    QSharedPointer<InlineCalculator> calculator; //!< The calculator initialized from the equation 
  };
} // Namespace Isis

#endif
+174 −0

File added.

Preview size limit exceeded, changes collapsed.

Loading