VaryCmd.cpp 3.2 KB
Newer Older
gsell's avatar
gsell committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
// ------------------------------------------------------------------------
// $RCSfile: VaryCmd.cpp,v $
// ------------------------------------------------------------------------
// $Revision: 1.1.1.1 $
// ------------------------------------------------------------------------
// Copyright: see Copyright.readme
// ------------------------------------------------------------------------
//
// Class: VaryCmd
//   The class for the OPAL VARY command.
//
// ------------------------------------------------------------------------
//
// $Date: 2000/03/27 09:33:44 $
// $Author: Andreas Adelmann $
//
// ------------------------------------------------------------------------

#include "Match/VaryCmd.h"
#include "Attributes/Attributes.h"
#include "Match/ConcreteVar.h"
#include "Match/Match.h"
#include "Match/MatchLimits.h"
24
#include "Utilities/Options.h"
gsell's avatar
gsell committed
25 26 27 28 29 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
#include <iostream>


// Class VaryCmd
// ------------------------------------------------------------------------

// The attributes of class VaryCmd.
namespace {
    enum {
        NAME,    // the variable reference
        STEP,    // the step size
        LOWER,   // the lower limit, if any
        UPPER,   // the upper limit, if any
        SIZE
    };
}


VaryCmd::VaryCmd():
    Action(SIZE, "VARY",
           "The \"VARY\" sub-command defines a variable to be adjusted.") {
    itsAttr[NAME] = Attributes::makeReference
                    ("NAME", "the variable reference");
    itsAttr[STEP] = Attributes::makeReal
                    ("STEP", "the step size", 0.2);
    itsAttr[LOWER] = Attributes::makeReal
                     ("LOWER", "the lower limit, if any");
    itsAttr[UPPER] = Attributes::makeReal
                     ("UPPER", "the upper limit, if any");
}


57
VaryCmd::VaryCmd(const std::string &name, VaryCmd *parent):
gsell's avatar
gsell committed
58 59 60 61 62 63 64 65
    Action(name, parent)
{}


VaryCmd::~VaryCmd()
{}


66
VaryCmd *VaryCmd::clone(const std::string &name) {
gsell's avatar
gsell committed
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 94 95 96 97
    return new VaryCmd(name, this);
}


void VaryCmd::execute() {
    // Fetch the command attributes.
    double pars[4];
    pars[0] = Attributes::getReal(itsAttr[NAME]);
    pars[1] = Attributes::getReal(itsAttr[STEP]);
    pars[2] = pars[3] = 0.0;

    // Set up the limits flag.
    int limits = NO_LIMIT;
    if(itsAttr[LOWER]) {
        pars[2] = Attributes::getReal(itsAttr[LOWER]);
        if(itsAttr[UPPER]) {
            pars[3] = Attributes::getReal(itsAttr[UPPER]);
            limits = BOTH_LIMITS;
        } else {
            limits = LOWER_LIMIT;
        }
    } else {
        if(itsAttr[UPPER]) {
            pars[3] = Attributes::getReal(itsAttr[UPPER]);
            limits = UPPER_LIMIT;
        } else {
            limits = NO_LIMIT;
        }
    }

    // Make the match variable name.
98
    std::string name(itsAttr[NAME].getImage());
gsell's avatar
gsell committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

    // Test for previously existing match variable;
    // if found, remove it.
    if(Match::block->findVariable(name)) {
        if(Options::warn) {
            std::cerr << "\n### Warning ### "
                      << "Parameter is already variable -- new attributes used.\n"
                      << std::endl;
        }

        Match::block->deleteVariable(name);
    }

    // Add new variable to the table.
    Match::block->addVariable(new ConcreteVar(name, itsAttr[NAME], limits, pars));
}