- Main Page
- Namespaces
- Classes
- Files
- Related Pages

# /Users/fpy/titan-1-5-MBARI/Mers/Models/Base/unit-tests/BMHelper.cpp

    00001
    00002 //
    00003 // Copyright (c) 2004           Model-based Embedded and Robotic Systems Group
    00004 // All Rights Reserved          Massachusetts Institute of Technology
    00005 //
    00006 // This software is provided as is. Model-based Embedded and Robotic Systems
    00007 // (MERS) group does not assume any responsibility.
    00008 //
    00009 // Organization:        Model-based Embedded and Robotic Systems Group
    00010 //                                      Massachusetts Institute of Technology
    00011 //
    00013
    00014 #include "BMHelper.h"
    00015 #include <Mers/Models/Base/BaseModel.h>
    00016
    00018
    00019 using namespace tut;
    00020 using Mers::Models::Base::BaseModelVariable;
    00021 using Mers::Models::Base::BaseModelVariableArray;
    00022 using Mers::Models::Base::BaseModelValue;
    00023 using Mers::Models::Base::BaseModelValueArray;
    00024 using Mers::Models::Base::BaseModelDomain;
    00025 using Mers::Models::Base::BaseModelDomainArray;
    00026 using Mers::Models::Base::BaseModelClause;
    00027 using Mers::Models::Base::BaseModelAssignment;
    00028 using Mers::Models::Base::BaseModel;
    00029
    00031
    00032 BMHelper::BMHelper()
    00033 {
    00034
    00035 }
    00036
    00038
    00039 BMHelper::~BMHelper()
    00040 {
    00041
    00042 }
    00043
    00045
    00046 BaseModelDomain *
    00047 BMHelper::loadDomain(const struct BMDomainEntry * domainData,
    00048                                                                                                         unsigned int index)
    00049 {
    00050         ensure("Domain data is non-null", domainData != NULL);
    00051
    00053         bool ownerFlag = true;
    00054
    00055         BaseModelDomain * domain =
    00056                 new BaseModelDomain(domainData->name, ownerFlag, index);
    00057
    00058         char const * const * domainValues = domainData->values;
    00059
    00060         ensure("Domain is non-empty", domainValues != NULL);
    00061
    00062         unsigned int v = 0;
    00063         while(domainValues[v] != NULL) {
    00064                 domain->pushBack(new BaseModelValue(domainValues[v++]));
    00065         }
    00066         return domain;
    00067 }
    00068
    00070
    00071 BaseModelVariable *
    00072 BMHelper::loadVariable(BaseModelDomainArray &domains,
    00073         const struct BMVariableEntry * variableData, unsigned int index)
    00074 {
    00075         ensure("Variable data is non-null", variableData != NULL);
    00076
    00077         ensure("Variable domain index is in range",
    00078                                  variableData->domainIndex < domains.getLength());
    00079
    00080         ensure("Variable name is non-null", variableData->name != NULL);
    00081
    00082         BaseModelVariable * variable =
    00083                 new BaseModelVariable(variableData->name,
    00084                                                                                                         domains[variableData->domainIndex],
    00085                                                                                                         variableData->varType,
    00086                                                                                                         index);
    00087         return variable;
    00088 }
    00089
    00091
    00092 void BMHelper::loadDomainData(unsigned int numDomains,
    00093         const struct BMDomainEntry * domains,
    00094         BaseModelDomainArray &doms)
    00095 {
    00097         for(unsigned int d = 0; d < numDomains; d++) {
    00098                 BaseModelDomain * domain = loadDomain(domains + d, d);
    00099
    00100                 doms.pushBack(domain);
    00101         }
    00102 }
    00103
    00105
    00106 void BMHelper::loadVariableData(unsigned int numVariables,
    00107         const struct BMVariableEntry * variables,
    00108         BaseModelDomainArray &doms, BaseModelVariableArray &vars)
    00109 {
    00110         // Load our Variables
    00111         for(unsigned int v = 0; v < numVariables; v++) {
    00112                 BaseModelVariable * variable = loadVariable(doms, variables + v, v);
    00113
    00114                 vars.pushBack(variable);
    00115         }
    00116 }
    00117
    00119
    00120 void BMHelper::loadModelData(
    00121         unsigned int numDomains, const struct BMDomainEntry * domainData,
    00122         unsigned int numVariables, const struct BMVariableEntry * variableData,
    00123         Mers::Models::Base::BaseModel &model )
    00124 {
    00125         ensure("Domain data is non-null", domainData != NULL);
    00126         ensure("Variable data is non-null", variableData != NULL);
    00127
    00128         // Load our domains.
    00129         for( unsigned int d = 0; d < numDomains; ++d ) {
    00130                 // Create a set of values
    00131                 char const * const * domainValues = domainData[d].values;
    00132
    00133                 // Load our values
    00134                 BaseModelValueArray values( false );
    00135                 unsigned int v = 0;
    00136                 while(domainValues[v] != NULL) {
    00137                         values.pushBack(new BaseModelValue(domainValues[v++]));
    00138                 }
    00139
    00140                 // Add it to the model.
    00141                 model.addDomain( domainData[d].name, values );
    00142         }
    00143
    00144         // Load our variables.
    00145         for( unsigned int v = 0; v < numVariables; ++v ) {
    00146                 ensure( "Variable domain index is in range",
    00147                         variableData[v].domainIndex < model.getDomains()->getLength() );
    00148
    00149                 ensure( "Variable name is non-null", variableData[v].name != NULL );
    00150
    00151                 model.addVariable( variableData[v].name, variableData[v].varType,
    00152                         model.getDomain( variableData[v].domainIndex ) );
    00153         }
    00154 }
    00155
    00157
    00158 void BMHelper::getVariableSubset(unsigned int numSubsetVars,
    00159         const unsigned int * subsetVarData,
    00160         BaseModelVariableArray &allVars, BaseModelVariableArray &subsetVars)
    00161 {
    00162         // Load each variable into the subsetVars.
    00163         for(unsigned int i = 0; i < numSubsetVars; ++i) {
    00164                 // Get the index of the desired variable.
    00165                 unsigned int index = subsetVarData[i];
    00166                 // Make sure its in range (can't be less than zero)
    00167                 ensure("Variable Subset index in range", index < allVars.size());
    00168                 // Add the variable to the subset list.
    00169                 subsetVars.pushBack(allVars[index]);
    00170         }
    00171 }
    00172
    00174
    00175 void BMHelper::loadClause(unsigned int numAssignments,
    00176         const int clauseData[],
    00177         BaseModelVariableArray &variables, BaseModelClause &clause)
    00178 {
    00179         // Load a single clause.
    00180         for(unsigned int a = 0; a < numAssignments; a++)
    00181         {
    00182                 // See if we include this literal - we include it only if it is non-zero.
    00183                 int asgData = clauseData[a];
    00184
    00185                 if(asgData != 0) {
    00186                         // See if this literal is negative.
    00187                         bool negative = asgData < 0;
    00188
    00189                         // Load our value index.
    00190                         unsigned int valIndex;
    00191                         if(negative)
    00192                                 valIndex = (unsigned int)(-asgData - 1);
    00193                         else
    00194                                 valIndex = (unsigned int)(asgData - 1);
    00195
    00196                         // Verify our variable data.
    00197                         ensure("BMHelper::loadClause: "
    00198                                 "Variable Index is within range",
    00199                                 a < variables.getLength());
    00200
    00201                         // Load our variable.
    00202                         BaseModelVariable const * var = variables[a];
    00203
    00204                         // Verify our value data.
    00205                         ensure("BMHelper::loadClause: Variable non-null", var != NULL);
    00206                         ensure("BMHelper::loadClause: Domain non-null",
    00207                                 var->getDomain() != NULL);
    00208                         ensure("BMHelper::loadClause: Value Index is within range",
    00209                                 valIndex < var->getDomain()->getLength());
    00210
    00211                         // Get our value
    00212                         BaseModelValue const * val = var->getValue(valIndex);
    00213
    00214                         // Specify our parity.
    00215                         BaseModelAssignment::OperatorType op;
    00216                         if(negative)
    00217                                 op = BaseModelAssignment::Negative;
    00218                         else
    00219                                 op = BaseModelAssignment::Positive;
    00220
    00221                         // Create an assignment
    00222                         BaseModelAssignment * asg = new BaseModelAssignment(var, val, op);
    00223
    00224                         // Store our assignment
    00225                         clause.pushBack(asg);
    00226                 }
    00227         }
    00228 }
    00229
    00231