newly added. Not for productive used yetsvn diff | grep Index:
This commit is contained in:
0
src/tests/spirit/.kdbgrc.spirit_fcn_test
Executable file
0
src/tests/spirit/.kdbgrc.spirit_fcn_test
Executable file
620
src/tests/spirit/PFunction.cpp
Normal file
620
src/tests/spirit/PFunction.cpp
Normal file
@@ -0,0 +1,620 @@
|
||||
/***************************************************************************
|
||||
|
||||
PFunction.cpp
|
||||
|
||||
Author: Andreas Suter
|
||||
e-mail: andreas.suter@psi.ch
|
||||
|
||||
$Id$
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2007 by Andreas Suter *
|
||||
* andreas.suter@psi.c *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
|
||||
#include<cmath>
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
#include "PFunction.h"
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// Constructor
|
||||
//--------------------------------------------------------------------------
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
* info is an abstract syntax tree (AST) generate by the spirit parse library
|
||||
* (see http://spirit.sourceforge.net/distrib/spirit_1_8_5/libs/spirit/doc/trees.html).
|
||||
* It contains a single parsed msr-function in an ascii representation.
|
||||
* Here it takes the from
|
||||
* assignment (root node)
|
||||
* |_ 'FUNx'
|
||||
* |_ '='
|
||||
* |_ expression
|
||||
* |_ ...
|
||||
*
|
||||
* Since it would be inefficient to evaluate this AST directly it is transferred to
|
||||
* a more efficient tree fFuncs here in the constructor.
|
||||
*
|
||||
* \param info AST parse tree holding a single parsed msr-function in an ascii representation
|
||||
* \param param the parameter vector
|
||||
* \param map the map vector
|
||||
*/
|
||||
PFunction::PFunction(tree_parse_info<> info, vector<double> param, vector<int> map)
|
||||
{
|
||||
cout << endl << "in PFunction ...";
|
||||
|
||||
fInfo = info;
|
||||
fParam = param;
|
||||
fMap = map;
|
||||
|
||||
// init class variables
|
||||
fValid = true;
|
||||
fFuncNo = -1;
|
||||
|
||||
// check parameter and map range
|
||||
if (!CheckParameterAndMapRange()) {
|
||||
fValid = false;
|
||||
return;
|
||||
}
|
||||
|
||||
// generate function evaluation tree
|
||||
if (!GenerateFuncEvalTree()) {
|
||||
fValid = false;
|
||||
return;
|
||||
}
|
||||
|
||||
EvaluateTree(info);
|
||||
|
||||
cout << endl << "fFuncString: '" << fFuncString << "'";
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// Destructor
|
||||
//--------------------------------------------------------------------------
|
||||
/**
|
||||
* <p>
|
||||
*/
|
||||
PFunction::~PFunction()
|
||||
{
|
||||
// cout << endl << "in ~PFunction ...";
|
||||
fParam.clear();
|
||||
fMap.clear();
|
||||
|
||||
CleanupFuncEvalTree();
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// CheckParameterRange (protected)
|
||||
//-------------------------------------------------------------
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
*/
|
||||
bool PFunction::CheckParameterAndMapRange()
|
||||
{
|
||||
return CheckParameterAndMapInTree(fInfo.trees.begin());
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// CheckParameterAndMapInTree (protected)
|
||||
//-------------------------------------------------------------
|
||||
/**
|
||||
* <p> walk through the tree and check if the parameter found
|
||||
* are within a valid range given by the size of fParam.
|
||||
*
|
||||
* \param i
|
||||
*/
|
||||
bool PFunction::CheckParameterAndMapInTree(iter_t const& i)
|
||||
{
|
||||
bool success = true;
|
||||
int value;
|
||||
|
||||
if (i->value.id() == PFunctionGrammar::funLabelID) {
|
||||
assert(i->children.size() == 0);
|
||||
SetFuncNo(i);
|
||||
} else if (i->value.id() == PFunctionGrammar::parameterID) {
|
||||
assert(i->children.size() == 0);
|
||||
string str(i->value.begin(), i->value.end());
|
||||
cout << endl << "parameterID: value = " << str << endl;
|
||||
sscanf(str.c_str(), "PAR%d", &value);
|
||||
//cout << endl << ">> value = " << value << ", fParam.size() = " << fParam.size();
|
||||
if (value > (int)fParam.size()) { // parameter number found > number of parameters
|
||||
cout << endl << "**ERROR**: found parameter " << str << " with only " << fParam.size() << " parameters present?!?";
|
||||
fValid = false;
|
||||
}
|
||||
} else if (i->value.id() == PFunctionGrammar::mapID) {
|
||||
assert(i->children.size() == 0);
|
||||
string str(i->value.begin(), i->value.end());
|
||||
cout << endl << "mapID: value = " << str << endl;
|
||||
sscanf(str.c_str(), "MAP%d", &value);
|
||||
//cout << endl << ">> value = " << value << ", fParam.size() = " << fParam.size();
|
||||
if (value > (int)fMap.size()) { // parameter number found > number of parameters
|
||||
cout << endl << "**ERROR**: found map " << str << " with only " << fMap.size() << " maps present?!?";
|
||||
fValid = false;
|
||||
}
|
||||
} else if (i->value.id() == PFunctionGrammar::functionID) {
|
||||
cout << endl << "children = " << i->children.size() << endl;
|
||||
assert(i->children.size() == 4);
|
||||
// i: 'funcName', '(', 'expression', ')'
|
||||
success = CheckParameterAndMapInTree(i->children.begin()+2); // thats the real stuff
|
||||
} else if (i->value.id() == PFunctionGrammar::factorID) {
|
||||
// i: real | parameter | map | function | expression
|
||||
assert(i->children.size() == 1);
|
||||
success = CheckParameterAndMapInTree(i->children.begin());
|
||||
} else if (i->value.id() == PFunctionGrammar::termID) {
|
||||
// '*'/'/' i: lhs, rhs
|
||||
assert(i->children.size() == 2);
|
||||
success = CheckParameterAndMapInTree(i->children.begin());
|
||||
success = CheckParameterAndMapInTree(i->children.begin()+1);
|
||||
} else if (i->value.id() == PFunctionGrammar::expressionID) {
|
||||
// '+'/'-' i: lhs, rhs
|
||||
assert(i->children.size() == 2);
|
||||
success = CheckParameterAndMapInTree(i->children.begin());
|
||||
success = CheckParameterAndMapInTree(i->children.begin()+1);
|
||||
} else if (i->value.id() == PFunctionGrammar::assignmentID) {
|
||||
// i: 'FUNx', '=', 'expression'
|
||||
assert(i->children.size() == 3);
|
||||
success = CheckParameterAndMapInTree(i->children.begin()); // FUNx
|
||||
success = CheckParameterAndMapInTree(i->children.begin()+2); // this is the real stuff
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// SetFuncNo (protected)
|
||||
//-------------------------------------------------------------
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
* \param i
|
||||
*/
|
||||
bool PFunction::SetFuncNo(iter_t const& i)
|
||||
{
|
||||
int funNo = -1;
|
||||
int status;
|
||||
bool success = true;
|
||||
|
||||
// get string from tree
|
||||
string str(i->value.begin(), i->value.end());
|
||||
|
||||
// extract function number from string
|
||||
status = sscanf(str.c_str(), "FUN%d", &funNo);
|
||||
//cout << endl << "SetFuncNo: status = " << status << ", funNo = " << funNo;
|
||||
if (status == 1) { // found 1 int
|
||||
fFuncNo = funNo;
|
||||
} else { // wrong string
|
||||
success = false;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// GenerateFuncEvalTree (protected)
|
||||
//-------------------------------------------------------------
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
*/
|
||||
bool PFunction::GenerateFuncEvalTree()
|
||||
{
|
||||
FillFuncEvalTree(fInfo.trees.begin(), fFunc);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// FillFuncEvalTree (protected)
|
||||
//-------------------------------------------------------------
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
*/
|
||||
void PFunction::FillFuncEvalTree(iter_t const& i, PFuncTreeNode &node)
|
||||
{
|
||||
double dvalue;
|
||||
int ivalue;
|
||||
int status;
|
||||
string str;
|
||||
PFuncTreeNode child;
|
||||
|
||||
if (i->value.id() == PFunctionGrammar::realID) { // handle number
|
||||
str = string(i->value.begin(), i->value.end()); // get string
|
||||
status = sscanf(str.c_str(), "%lf", &dvalue); // convert string to double
|
||||
node.fID = PFunctionGrammar::realID; // keep the ID
|
||||
node.fDvalue = dvalue; // keep the value
|
||||
// cout << endl << ">> realID: value = " << dvalue;
|
||||
} else if (i->value.id() == PFunctionGrammar::parameterID) { // handle parameter number
|
||||
str = string(i->value.begin(), i->value.end()); // get string
|
||||
status = sscanf(str.c_str(), "PAR%d", &ivalue); // convert string to parameter number
|
||||
node.fID = PFunctionGrammar::parameterID; // keep the ID
|
||||
node.fIvalue = ivalue; // keep the value
|
||||
// cout << endl << ">> parameterID: value = " << ivalue;
|
||||
} else if (i->value.id() == PFunctionGrammar::mapID) { // handle map number
|
||||
str = string(i->value.begin(), i->value.end()); // get string
|
||||
status = sscanf(str.c_str(), "MAP%d", &ivalue); // convert string to map number
|
||||
node.fID = PFunctionGrammar::mapID; // keep the ID
|
||||
node.fIvalue = ivalue; // keep the value
|
||||
// cout << endl << ">> mapID: value = " << ivalue;
|
||||
} else if (i->value.id() == PFunctionGrammar::functionID) { // handle function like cos ...
|
||||
// keep the id
|
||||
node.fID = PFunctionGrammar::functionID;
|
||||
// keep function tag
|
||||
// i: 'funcName', '(', 'expression', ')'
|
||||
iter_t it = i->children.begin();
|
||||
str = string(it->value.begin(), it->value.end()); // get string
|
||||
// cout << endl << ">> functionID: value = " << str;
|
||||
if (!strcmp(str.c_str(), "COS"))
|
||||
node.fFunctionTag = FUN_COS;
|
||||
else if (!strcmp(str.c_str(), "SIN"))
|
||||
node.fFunctionTag = FUN_SIN;
|
||||
else if (!strcmp(str.c_str(), "TAN"))
|
||||
node.fFunctionTag = FUN_TAN;
|
||||
else if (!strcmp(str.c_str(), "COSH"))
|
||||
node.fFunctionTag = FUN_COSH;
|
||||
else if (!strcmp(str.c_str(), "SINH"))
|
||||
node.fFunctionTag = FUN_SINH;
|
||||
else if (!strcmp(str.c_str(), "TANH"))
|
||||
node.fFunctionTag = FUN_TANH;
|
||||
else if (!strcmp(str.c_str(), "ACOS"))
|
||||
node.fFunctionTag = FUN_ACOS;
|
||||
else if (!strcmp(str.c_str(), "ASIN"))
|
||||
node.fFunctionTag = FUN_ASIN;
|
||||
else if (!strcmp(str.c_str(), "ATAN"))
|
||||
node.fFunctionTag = FUN_ATAN;
|
||||
else if (!strcmp(str.c_str(), "ACOSH"))
|
||||
node.fFunctionTag = FUN_ACOSH;
|
||||
else if (!strcmp(str.c_str(), "ASINH"))
|
||||
node.fFunctionTag = FUN_ASINH;
|
||||
else if (!strcmp(str.c_str(), "ATANH"))
|
||||
node.fFunctionTag = FUN_ATANH;
|
||||
else if (!strcmp(str.c_str(), "LOG"))
|
||||
node.fFunctionTag = FUN_LOG;
|
||||
else if (!strcmp(str.c_str(), "LN"))
|
||||
node.fFunctionTag = FUN_LN;
|
||||
else if (!strcmp(str.c_str(), "EXP"))
|
||||
node.fFunctionTag = FUN_EXP;
|
||||
else {
|
||||
cout << endl << "**PANIC ERROR**: function " << str << " doesn't exist, but you never should have reached this point!";
|
||||
assert(0);
|
||||
}
|
||||
// add node
|
||||
node.children.push_back(child);
|
||||
// i: 'funcName', '(', 'expression', ')'
|
||||
FillFuncEvalTree(i->children.begin()+2, node.children[0]);
|
||||
} else if (i->value.id() == PFunctionGrammar::factorID) {
|
||||
// cout << endl << ">> factorID";
|
||||
// keep the id
|
||||
node.fID = PFunctionGrammar::factorID;
|
||||
// add child lhs
|
||||
node.children.push_back(child);
|
||||
FillFuncEvalTree(i->children.begin(), node.children[0]);
|
||||
} else if (i->value.id() == PFunctionGrammar::termID) {
|
||||
// keep the id
|
||||
node.fID = PFunctionGrammar::termID;
|
||||
// keep operator tag
|
||||
if (*i->value.begin() == '*')
|
||||
node.fOperatorTag = OP_MUL;
|
||||
else
|
||||
node.fOperatorTag = OP_DIV;
|
||||
/*
|
||||
if (node.fOperatorTag == OP_MUL)
|
||||
cout << endl << ">> termID: value = *";
|
||||
else
|
||||
cout << endl << ">> termID: value = /";
|
||||
*/
|
||||
// add child lhs
|
||||
node.children.push_back(child);
|
||||
FillFuncEvalTree(i->children.begin(), node.children[0]);
|
||||
// add child rhs
|
||||
node.children.push_back(child);
|
||||
FillFuncEvalTree(i->children.begin()+1, node.children[1]);
|
||||
} else if (i->value.id() == PFunctionGrammar::expressionID) { // handle expression
|
||||
// keep the id
|
||||
node.fID = PFunctionGrammar::expressionID;
|
||||
// keep operator tag
|
||||
if (*i->value.begin() == '+')
|
||||
node.fOperatorTag = OP_ADD;
|
||||
else
|
||||
node.fOperatorTag = OP_SUB;
|
||||
/*
|
||||
if (node.fOperatorTag == OP_ADD)
|
||||
cout << endl << ">> expressionID: value = +";
|
||||
else
|
||||
cout << endl << ">> expressionID: value = -";
|
||||
*/
|
||||
// add child lhs
|
||||
node.children.push_back(child);
|
||||
FillFuncEvalTree(i->children.begin(), node.children[0]);
|
||||
// add child rhs
|
||||
node.children.push_back(child);
|
||||
FillFuncEvalTree(i->children.begin()+1, node.children[1]);
|
||||
} else if (i->value.id() == PFunctionGrammar::assignmentID) {
|
||||
// nothing to be done except to pass the next element in the ast
|
||||
// i: 'funx', '=', 'expression'
|
||||
FillFuncEvalTree(i->children.begin()+2, node);
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// Eval (public)
|
||||
//-------------------------------------------------------------
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
*/
|
||||
double PFunction::Eval()
|
||||
{
|
||||
return EvalNode(fFunc);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// EvalNode (protected)
|
||||
//-------------------------------------------------------------
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
* \param node
|
||||
*/
|
||||
double PFunction::EvalNode(PFuncTreeNode &node)
|
||||
{
|
||||
if (node.fID == PFunctionGrammar::realID) {
|
||||
return node.fDvalue;
|
||||
} else if (node.fID == PFunctionGrammar::parameterID) {
|
||||
return fParam[node.fIvalue-1];
|
||||
} else if (node.fID == PFunctionGrammar::mapID) {
|
||||
return fParam[fMap[node.fIvalue-1]-1];
|
||||
} else if (node.fID == PFunctionGrammar::functionID) {
|
||||
if (node.fFunctionTag == FUN_COS) {
|
||||
return cos(EvalNode(node.children[0]));
|
||||
} else if (node.fFunctionTag == FUN_SIN) {
|
||||
return sin(EvalNode(node.children[0]));
|
||||
} else if (node.fFunctionTag == FUN_TAN) {
|
||||
return tan(EvalNode(node.children[0]));
|
||||
} else if (node.fFunctionTag == FUN_COSH) {
|
||||
return cosh(EvalNode(node.children[0]));
|
||||
} else if (node.fFunctionTag == FUN_SINH) {
|
||||
return sinh(EvalNode(node.children[0]));
|
||||
} else if (node.fFunctionTag == FUN_TANH) {
|
||||
return tanh(EvalNode(node.children[0]));
|
||||
} else if (node.fFunctionTag == FUN_ACOS) {
|
||||
return acos(EvalNode(node.children[0]));
|
||||
} else if (node.fFunctionTag == FUN_ASIN) {
|
||||
return asin(EvalNode(node.children[0]));
|
||||
} else if (node.fFunctionTag == FUN_ATAN) {
|
||||
return atan(EvalNode(node.children[0]));
|
||||
} else if (node.fFunctionTag == FUN_ACOSH) {
|
||||
return acosh(EvalNode(node.children[0]));
|
||||
} else if (node.fFunctionTag == FUN_ASINH) {
|
||||
return asinh(EvalNode(node.children[0]));
|
||||
} else if (node.fFunctionTag == FUN_ATANH) {
|
||||
return atanh(EvalNode(node.children[0]));
|
||||
} else if (node.fFunctionTag == FUN_LOG) {
|
||||
return log(EvalNode(node.children[0]))/log(10);
|
||||
} else if (node.fFunctionTag == FUN_LN) {
|
||||
return log(EvalNode(node.children[0]));
|
||||
} else if (node.fFunctionTag == FUN_EXP) {
|
||||
return exp(EvalNode(node.children[0]));
|
||||
} else {
|
||||
cout << endl << "**PANIC ERROR**: PFunction::EvalNode: node.fID == PFunctionGrammar::functionID: you never should have reached this point!" << endl;
|
||||
assert(0);
|
||||
}
|
||||
} else if (node.fID == PFunctionGrammar::factorID) {
|
||||
return EvalNode(node.children[0]);
|
||||
} else if (node.fID == PFunctionGrammar::termID) {
|
||||
if (node.fOperatorTag == OP_MUL) {
|
||||
return EvalNode(node.children[0]) * EvalNode(node.children[1]);
|
||||
} else {
|
||||
double denominator = EvalNode(node.children[1]);
|
||||
if (denominator == 0.0) {
|
||||
cout << endl << "**PANIC ERROR**: PFunction::EvalNode: division by 0.0" << endl;
|
||||
assert(0);
|
||||
}
|
||||
return EvalNode(node.children[0]) / denominator;
|
||||
}
|
||||
} else if (node.fID == PFunctionGrammar::expressionID) {
|
||||
if (node.fOperatorTag == OP_ADD) {
|
||||
return EvalNode(node.children[0]) + EvalNode(node.children[1]);
|
||||
} else {
|
||||
return EvalNode(node.children[0]) - EvalNode(node.children[1]);
|
||||
}
|
||||
} else {
|
||||
cout << endl << "**PANIC ERROR**: PFunction::EvalNode: you never should have reached this point!" << endl;
|
||||
assert(0);
|
||||
}
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// CleanupFuncEvalTree (protected)
|
||||
//-------------------------------------------------------------
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
*/
|
||||
void PFunction::CleanupFuncEvalTree()
|
||||
{
|
||||
// clean up all children
|
||||
CleanupNode(fFunc);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// CleanupNode (protected)
|
||||
//-------------------------------------------------------------
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
* \param node
|
||||
*/
|
||||
void PFunction::CleanupNode(PFuncTreeNode &node)
|
||||
{
|
||||
if (node.children.size() != 0) {
|
||||
for (unsigned int i=0; i<node.children.size(); i++) {
|
||||
CleanupNode(node.children[i]);
|
||||
}
|
||||
node.children.clear();
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// EvaluateTree (protected)
|
||||
//-------------------------------------------------------------
|
||||
long PFunction::EvaluateTree(tree_parse_info<> info)
|
||||
{
|
||||
return EvalTreeExpression(info.trees.begin());
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// EvalTreeExpression (protected)
|
||||
//-------------------------------------------------------------
|
||||
long PFunction::EvalTreeExpression(iter_t const& i)
|
||||
{
|
||||
static int counter = 0;
|
||||
static int termOp = 0;
|
||||
|
||||
cout << endl << counter <<": in EvalExpression. i->value = '" <<
|
||||
string(i->value.begin(), i->value.end()) <<
|
||||
"' i->children.size() = " << i->children.size() << endl;
|
||||
|
||||
if (i->value.id() == PFunctionGrammar::realID) {
|
||||
assert(i->children.size() == 0);
|
||||
cout << endl << "realID: children = " << i->children.size();
|
||||
cout << endl << "realID: " << string(i->value.begin(), i->value.end());
|
||||
cout << endl << "-----";
|
||||
if (*i->value.begin() == '-')
|
||||
fFuncString += "(";
|
||||
fFuncString += string(i->value.begin(), i->value.end());
|
||||
if (*i->value.begin() == '-')
|
||||
fFuncString += ")";
|
||||
} else if (i->value.id() == PFunctionGrammar::funLabelID) {
|
||||
assert(i->children.size() == 0);
|
||||
//SetFuncNo(i);
|
||||
cout << endl << "funLabelID: children = " << i->children.size();
|
||||
cout << endl << "funLabelID: value = " << string(i->value.begin(), i->value.end());
|
||||
cout << endl << "-----";
|
||||
fFuncString += string(i->value.begin(), i->value.end()); // funx
|
||||
} else if (i->value.id() == PFunctionGrammar::parameterID) {
|
||||
assert(i->children.size() == 0);
|
||||
cout << endl << "parameterID: children = " << i->children.size();
|
||||
cout << endl << "parameterID: value = " << string(i->value.begin(), i->value.end());
|
||||
cout << endl << "-----";
|
||||
fFuncString += string(i->value.begin(), i->value.end());
|
||||
} else if (i->value.id() == PFunctionGrammar::mapID) {
|
||||
assert(i->children.size() == 0);
|
||||
cout << endl << "mapID: children = " << i->children.size();
|
||||
cout << endl << "mapID: value = " << string(i->value.begin(), i->value.end());
|
||||
cout << endl << "-----";
|
||||
fFuncString += string(i->value.begin(), i->value.end());
|
||||
} else if (i->value.id() == PFunctionGrammar::functionID) {
|
||||
assert(i->children.size() == 4);
|
||||
cout << endl << "functionID: children = " << i->children.size();
|
||||
iter_t it = i->children.begin();
|
||||
cout << endl << "functionID: value = " << string(it->value.begin(), it->value.end());
|
||||
cout << endl << "-----";
|
||||
// funcName, '(', expression, ')'
|
||||
counter++;
|
||||
fFuncString += string(it->value.begin(), it->value.end());
|
||||
if (termOp == 0)
|
||||
fFuncString += "(";
|
||||
EvalTreeExpression(i->children.begin()+2); // the real stuff
|
||||
if (termOp == 0)
|
||||
fFuncString += ")";
|
||||
counter--;
|
||||
} else if (i->value.id() == PFunctionGrammar::factorID) {
|
||||
cout << endl << "factorID: children = " << i->children.size();
|
||||
counter++;
|
||||
return EvalTreeExpression(i->children.begin());
|
||||
counter--;
|
||||
} else if (i->value.id() == PFunctionGrammar::termID) {
|
||||
cout << endl << "termID: children = " << i->children.size();
|
||||
if (*i->value.begin() == '*') {
|
||||
cout << endl << "termID: '*'";
|
||||
assert(i->children.size() == 2);
|
||||
counter++;
|
||||
termOp++;
|
||||
EvalTreeExpression(i->children.begin());
|
||||
fFuncString += " * ";
|
||||
EvalTreeExpression(i->children.begin()+1);
|
||||
termOp--;
|
||||
counter--;
|
||||
} else if (*i->value.begin() == '/') {
|
||||
cout << endl << "termID: '/'";
|
||||
assert(i->children.size() == 2);
|
||||
counter++;
|
||||
termOp++;
|
||||
EvalTreeExpression(i->children.begin());
|
||||
fFuncString += " / ";
|
||||
EvalTreeExpression(i->children.begin()+1);
|
||||
termOp--;
|
||||
counter--;
|
||||
} else {
|
||||
assert(0);
|
||||
}
|
||||
} else if (i->value.id() == PFunctionGrammar::expressionID) {
|
||||
cout << endl << "expressionID: children = " << i->children.size();
|
||||
if (*i->value.begin() == '+') {
|
||||
cout << endl << "expressionID: '+'";
|
||||
assert(i->children.size() == 2);
|
||||
counter++;
|
||||
if (termOp > 0)
|
||||
fFuncString += "(";
|
||||
EvalTreeExpression(i->children.begin());
|
||||
fFuncString += " + ";
|
||||
EvalTreeExpression(i->children.begin()+1);
|
||||
if (termOp > 0)
|
||||
fFuncString += ")";
|
||||
counter--;
|
||||
} else if (*i->value.begin() == '-') {
|
||||
cout << endl << "expressionID: '-'";
|
||||
assert(i->children.size() == 2);
|
||||
counter++;
|
||||
if (termOp > 0)
|
||||
fFuncString += "(";
|
||||
EvalTreeExpression(i->children.begin());
|
||||
fFuncString += " - ";
|
||||
EvalTreeExpression(i->children.begin()+1);
|
||||
if (termOp > 0)
|
||||
fFuncString += ")";
|
||||
counter--;
|
||||
} else {
|
||||
assert(0);
|
||||
}
|
||||
} else if (i->value.id() == PFunctionGrammar::assignmentID) {
|
||||
cout << endl << "assignmentID: children = " << i->children.size();
|
||||
assert(i->children.size() == 3);
|
||||
counter++;
|
||||
EvalTreeExpression(i->children.begin());
|
||||
EvalTreeExpression(i->children.begin()+1); // this is the string "="
|
||||
EvalTreeExpression(i->children.begin()+2); // this is the real stuff
|
||||
counter--;
|
||||
} else if (*i->value.begin() == '=') {
|
||||
cout << endl << "'=' in assignment";
|
||||
fFuncString += " = ";
|
||||
} else {
|
||||
assert(0);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
111
src/tests/spirit/PFunction.h
Normal file
111
src/tests/spirit/PFunction.h
Normal file
@@ -0,0 +1,111 @@
|
||||
/***************************************************************************
|
||||
|
||||
PFunction.h
|
||||
|
||||
Author: Andreas Suter
|
||||
e-mail: andreas.suter@psi.ch
|
||||
|
||||
$Id$
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2007 by Andreas Suter *
|
||||
* andreas.suter@psi.c *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef _PFUNCTION_H_
|
||||
#define _PFUNCTION_H_
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
#include <boost/spirit/tree/ast.hpp>
|
||||
using namespace boost::spirit;
|
||||
|
||||
#include "PFunctionGrammar.h"
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
#define OP_ADD 0
|
||||
#define OP_SUB 1
|
||||
#define OP_MUL 2
|
||||
#define OP_DIV 3
|
||||
|
||||
#define FUN_COS 0
|
||||
#define FUN_SIN 1
|
||||
#define FUN_TAN 2
|
||||
#define FUN_COSH 3
|
||||
#define FUN_SINH 4
|
||||
#define FUN_TANH 5
|
||||
#define FUN_ACOS 6
|
||||
#define FUN_ASIN 7
|
||||
#define FUN_ATAN 8
|
||||
#define FUN_ACOSH 9
|
||||
#define FUN_ASINH 10
|
||||
#define FUN_ATANH 11
|
||||
#define FUN_LOG 12
|
||||
#define FUN_LN 13
|
||||
#define FUN_EXP 14
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
typedef struct func_tree_node {
|
||||
int fID; ///< tag showing what tree element this is
|
||||
int fOperatorTag; ///< tag for '+', '-', '*', '/'
|
||||
int fFunctionTag; ///< tag got "cos", "sin", ...
|
||||
int fIvalue; ///< for parameter numbers and maps
|
||||
double fDvalue; ///< for numbers
|
||||
vector<func_tree_node> children; ///< holding sub-tree
|
||||
} PFuncTreeNode;
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
class PFunction {
|
||||
public:
|
||||
PFunction(tree_parse_info<> info, vector<double> param, vector<int> map);
|
||||
virtual ~PFunction();
|
||||
|
||||
virtual bool IsValid() { return fValid; }
|
||||
virtual int GetFuncNo() { return fFuncNo; }
|
||||
virtual double Eval();
|
||||
|
||||
protected:
|
||||
virtual bool CheckParameterAndMapRange();
|
||||
virtual bool CheckParameterAndMapInTree(iter_t const& i);
|
||||
virtual bool SetFuncNo(iter_t const& i);
|
||||
|
||||
virtual bool GenerateFuncEvalTree();
|
||||
virtual void FillFuncEvalTree(iter_t const& i, PFuncTreeNode &node);
|
||||
virtual double EvalNode(PFuncTreeNode &node);
|
||||
virtual void CleanupFuncEvalTree();
|
||||
virtual void CleanupNode(PFuncTreeNode &node);
|
||||
|
||||
virtual long EvaluateTree(tree_parse_info<> info);
|
||||
virtual long EvalTreeExpression(iter_t const& i);
|
||||
|
||||
private:
|
||||
tree_parse_info<> fInfo;
|
||||
vector<double> fParam;
|
||||
vector<int> fMap;
|
||||
PFuncTreeNode fFunc;
|
||||
|
||||
bool fValid; ///< flag showing if the function is valid
|
||||
int fFuncNo; ///< function number, i.e. FUNx with x the function number
|
||||
|
||||
string fFuncString;
|
||||
};
|
||||
|
||||
#endif // _PFUNCTION_H_
|
||||
142
src/tests/spirit/PFunctionGrammar.h
Normal file
142
src/tests/spirit/PFunctionGrammar.h
Normal file
@@ -0,0 +1,142 @@
|
||||
/***************************************************************************
|
||||
|
||||
PFunctionGrammer.h
|
||||
|
||||
Author: Andreas Suter
|
||||
e-mail: andreas.suter@psi.ch
|
||||
|
||||
$Id$
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2007 by Andreas Suter *
|
||||
* andreas.suter@psi.c *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef _PFUNCTIONGRAMMAR_H_
|
||||
#define _PFUNCTIONGRAMMAR_H_
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
//#define BOOST_SPIRIT_DEBUG
|
||||
|
||||
#include <boost/spirit/core.hpp>
|
||||
#include <boost/spirit/tree/ast.hpp>
|
||||
using namespace boost::spirit;
|
||||
|
||||
typedef char const* iterator_t;
|
||||
typedef tree_match<iterator_t> parse_tree_match_t;
|
||||
typedef parse_tree_match_t::tree_iterator iter_t;
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
/**
|
||||
*
|
||||
*/
|
||||
struct PFunctionGrammar : public grammar<PFunctionGrammar>
|
||||
{
|
||||
static const int realID = 1;
|
||||
static const int funLabelID = 2;
|
||||
static const int parameterID = 3;
|
||||
static const int mapID = 4;
|
||||
static const int functionID = 5;
|
||||
static const int factorID = 6;
|
||||
static const int termID = 7;
|
||||
static const int expressionID = 8;
|
||||
static const int assignmentID = 9;
|
||||
|
||||
template <typename ScannerT>
|
||||
struct definition
|
||||
{
|
||||
definition(PFunctionGrammar const& /*self*/)
|
||||
{
|
||||
// Start grammar definition
|
||||
real = leaf_node_d[ real_p ];
|
||||
|
||||
fun_label = leaf_node_d[ ( lexeme_d[ "FUN" >> +digit_p ] ) ];
|
||||
|
||||
parameter = leaf_node_d[ ( lexeme_d[ "PAR" >> +digit_p ] ) ];
|
||||
|
||||
map = leaf_node_d[ ( lexeme_d[ "MAP" >> +digit_p ] ) ];
|
||||
|
||||
function = str_p("COS") >> ch_p('(') >> expression >> ch_p(')')
|
||||
| str_p("SIN") >> ch_p('(') >> expression >> ch_p(')')
|
||||
| str_p("TAN") >> ch_p('(') >> expression >> ch_p(')')
|
||||
| str_p("COSH") >> ch_p('(') >> expression >> ch_p(')')
|
||||
| str_p("SINH") >> ch_p('(') >> expression >> ch_p(')')
|
||||
| str_p("TANH") >> ch_p('(') >> expression >> ch_p(')')
|
||||
| str_p("ACOS") >> ch_p('(') >> expression >> ch_p(')')
|
||||
| str_p("ASIN") >> ch_p('(') >> expression >> ch_p(')')
|
||||
| str_p("ATAN") >> ch_p('(') >> expression >> ch_p(')')
|
||||
| str_p("ACOSH") >> ch_p('(') >> expression >> ch_p(')')
|
||||
| str_p("ASINH") >> ch_p('(') >> expression >> ch_p(')')
|
||||
| str_p("ATANH") >> ch_p('(') >> expression >> ch_p(')')
|
||||
| str_p("LOG") >> ch_p('(') >> expression >> ch_p(')')
|
||||
| str_p("LN") >> ch_p('(') >> expression >> ch_p(')')
|
||||
| str_p("EXP") >> ch_p('(') >> expression >> ch_p(')')
|
||||
;
|
||||
|
||||
factor = real
|
||||
| parameter
|
||||
| map
|
||||
| function
|
||||
| inner_node_d[ch_p('(') >> expression >> ch_p(')')]
|
||||
;
|
||||
|
||||
term = factor >>
|
||||
*( (root_node_d[ch_p('*')] >> factor)
|
||||
| (root_node_d[ch_p('/')] >> factor)
|
||||
);
|
||||
|
||||
expression = term >>
|
||||
*( (root_node_d[ch_p('+')] >> term)
|
||||
| (root_node_d[ch_p('-')] >> term)
|
||||
);
|
||||
|
||||
assignment = (fun_label >> ch_p('=') >> expression);
|
||||
// End grammar definition
|
||||
|
||||
// turn on the debugging info.
|
||||
BOOST_SPIRIT_DEBUG_RULE(real);
|
||||
BOOST_SPIRIT_DEBUG_RULE(fun_label);
|
||||
BOOST_SPIRIT_DEBUG_RULE(parameter);
|
||||
BOOST_SPIRIT_DEBUG_RULE(map);
|
||||
BOOST_SPIRIT_DEBUG_RULE(function);
|
||||
BOOST_SPIRIT_DEBUG_RULE(factor);
|
||||
BOOST_SPIRIT_DEBUG_RULE(term);
|
||||
BOOST_SPIRIT_DEBUG_RULE(expression);
|
||||
BOOST_SPIRIT_DEBUG_RULE(assignment);
|
||||
}
|
||||
|
||||
rule<ScannerT, parser_context<>, parser_tag<assignmentID> > assignment;
|
||||
rule<ScannerT, parser_context<>, parser_tag<expressionID> > expression;
|
||||
rule<ScannerT, parser_context<>, parser_tag<termID> > term;
|
||||
rule<ScannerT, parser_context<>, parser_tag<factorID> > factor;
|
||||
rule<ScannerT, parser_context<>, parser_tag<functionID> > function;
|
||||
rule<ScannerT, parser_context<>, parser_tag<mapID> > map;
|
||||
rule<ScannerT, parser_context<>, parser_tag<parameterID> > parameter;
|
||||
rule<ScannerT, parser_context<>, parser_tag<funLabelID> > fun_label;
|
||||
rule<ScannerT, parser_context<>, parser_tag<realID> > real;
|
||||
|
||||
rule<ScannerT, parser_context<>, parser_tag<assignmentID> > const&
|
||||
start() const { return assignment; }
|
||||
};
|
||||
};
|
||||
|
||||
#endif // _PFUNCTIONGRAMMAR_H_
|
||||
417
src/tests/spirit/PFunctionHandler.cpp
Normal file
417
src/tests/spirit/PFunctionHandler.cpp
Normal file
@@ -0,0 +1,417 @@
|
||||
/***************************************************************************
|
||||
|
||||
PFunctionHandler.cpp
|
||||
|
||||
Author: Andreas Suter
|
||||
e-mail: andreas.suter@psi.ch
|
||||
|
||||
$Id$
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2007 by Andreas Suter *
|
||||
* andreas.suter@psi.c *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
|
||||
#include <string>
|
||||
#include <cassert>
|
||||
|
||||
#include <qfile.h>
|
||||
#include <qtextstream.h>
|
||||
#include <qstring.h>
|
||||
|
||||
#include "PFunctionHandler.h"
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// Constructor
|
||||
//-------------------------------------------------------------
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
* \param fln
|
||||
*/
|
||||
PFunctionHandler::PFunctionHandler(char *fln)
|
||||
{
|
||||
fValid = true;
|
||||
fFileName = QString(fln);
|
||||
|
||||
cout << endl << "in PFunctionHandler(char *fln)";
|
||||
cout << endl << "fFileName = " << fFileName.latin1();
|
||||
|
||||
fValid = ReadFile();
|
||||
if (fValid)
|
||||
fValid = MapsAreValid();
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// Constructor
|
||||
//-------------------------------------------------------------
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
* \param lines
|
||||
*/
|
||||
PFunctionHandler::PFunctionHandler(vector<QString> lines)
|
||||
{
|
||||
fValid = true;
|
||||
fFileName = "";
|
||||
|
||||
cout << endl << "in PFunctionHandler(vector<QString> lines)";
|
||||
|
||||
if (lines.size() == 0) {
|
||||
fValid = false;
|
||||
return;
|
||||
}
|
||||
|
||||
// analyze input
|
||||
bool done = false;
|
||||
int status;
|
||||
int val[10];
|
||||
double dval[10];
|
||||
bool inFcnBlock = false;
|
||||
for (unsigned int i=0; i<lines.size(); i++) {
|
||||
if (lines[i].startsWith("#")) // comment hence ignore
|
||||
continue;
|
||||
lines[i] = lines[i].upper();
|
||||
if (lines[i].startsWith("PAR")) {
|
||||
cout << endl << "this is a parameter line ...";
|
||||
status = sscanf(lines[i].latin1(), "PAR %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf",
|
||||
&dval[0], &dval[1], &dval[2], &dval[3], &dval[4],
|
||||
&dval[5], &dval[6], &dval[7], &dval[8], &dval[9]);
|
||||
if (status < 0) {
|
||||
done = true;
|
||||
fValid = false;
|
||||
cout << endl << "invalid PAR line, sorry ...";
|
||||
} else { // fill map
|
||||
cout << endl << "PAR line, status = " << status;
|
||||
for (int i=0; i<status; i++)
|
||||
fParam.push_back(dval[i]);
|
||||
}
|
||||
} else if (lines[i].startsWith("MAP")) {
|
||||
cout << endl << "this is a map line ...";
|
||||
status = sscanf(lines[i].latin1(), "MAP %d %d %d %d %d %d %d %d %d %d",
|
||||
&val[0], &val[1], &val[2], &val[3], &val[4],
|
||||
&val[5], &val[6], &val[7], &val[8], &val[9]);
|
||||
if (status < 0) {
|
||||
done = true;
|
||||
fValid = false;
|
||||
cout << endl << "invalid MAP line, sorry ...";
|
||||
} else { // fill map
|
||||
cout << endl << "MAP line, status = " << status;
|
||||
for (int i=0; i<status; i++)
|
||||
fMap.push_back(val[i]);
|
||||
}
|
||||
} else if (lines[i].startsWith("FUNCTIONS")) {
|
||||
cout << endl << "the functions block start ...";
|
||||
inFcnBlock = true;
|
||||
} else if (lines[i].startsWith("END")) {
|
||||
cout << endl << "end tag found; rest will be ignored";
|
||||
done = true;
|
||||
} else if (inFcnBlock) {
|
||||
fLines.push_back(lines[i]);
|
||||
}
|
||||
}
|
||||
|
||||
// check if all blocks are given
|
||||
if ((fMap.size() == 0) || (fParam.size() == 0) || (fLines.size() == 0)) {
|
||||
if (fMap.size() == 0)
|
||||
cout << endl << "MAP block is missing ...";
|
||||
if (fParam.size() == 0)
|
||||
cout << endl << "PAR block is missing ...";
|
||||
if (fLines.size() == 0)
|
||||
cout << endl << "FUNCTION block is missing ...";
|
||||
fValid = false;
|
||||
}
|
||||
|
||||
fValid = MapsAreValid();
|
||||
|
||||
if (fValid) {
|
||||
cout << endl << "Functions: ";
|
||||
for (unsigned int i=0; i<fLines.size(); i++)
|
||||
cout << endl << fLines[i].latin1();
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// Destructor
|
||||
//-------------------------------------------------------------
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
*/
|
||||
PFunctionHandler::~PFunctionHandler()
|
||||
{
|
||||
cout << endl << "in ~PFunctionHandler()" << endl << endl;
|
||||
fParam.clear();
|
||||
fMap.clear();
|
||||
fLines.clear();
|
||||
|
||||
fFuncs.clear();
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// DoParse (public)
|
||||
//-------------------------------------------------------------
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
*/
|
||||
bool PFunctionHandler::DoParse()
|
||||
{
|
||||
cout << endl << "in PFunctionHandler::DoParse() ...";
|
||||
|
||||
bool success = true;
|
||||
PFunctionGrammar function;
|
||||
|
||||
for (unsigned int i=0; i<fLines.size(); i++) {
|
||||
cout << endl << "fLines[" << i << "] = '" << fLines[i].latin1() << "'";
|
||||
|
||||
tree_parse_info<> info = ast_parse(fLines[i].latin1(), function, space_p);
|
||||
|
||||
if (info.full) {
|
||||
cout << endl << "parse successfull ..." << endl;
|
||||
PFunction func(info, fParam, fMap);
|
||||
fFuncs.push_back(func);
|
||||
} else {
|
||||
cout << endl << "parse failed ... (" << i << ")" << endl;
|
||||
success = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// check that all functions are valid. It could be that parsing was fine but
|
||||
// the parameter index, or map index was out of range
|
||||
if (success) {
|
||||
for (unsigned int i=0; i<fFuncs.size(); i++) {
|
||||
if (!fFuncs[i].IsValid()) {
|
||||
cout << endl << "**ERROR**: function fun" << fFuncs[i].GetFuncNo();
|
||||
cout << " has a problem with either parameter or map out of range!";
|
||||
success = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// check that the function numbers are unique
|
||||
if (success) {
|
||||
for (unsigned int i=0; i<fFuncs.size(); i++) {
|
||||
for (unsigned int j=i+1; j<fFuncs.size(); j++) {
|
||||
if (fFuncs[i].GetFuncNo() == fFuncs[j].GetFuncNo()) {
|
||||
cout << endl << "**ERROR**: function number " << fFuncs[i].GetFuncNo();
|
||||
cout << " is at least twice present! Fix this first.";
|
||||
success = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (success) {
|
||||
for (unsigned int i=0; i<fFuncs.size(); i++)
|
||||
cout << endl << "func number = " << fFuncs[i].GetFuncNo();
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// Eval (public)
|
||||
//-------------------------------------------------------------
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
* \param i
|
||||
*/
|
||||
double PFunctionHandler::Eval(int i)
|
||||
{
|
||||
if (GetFuncIndex(i) == -1) {
|
||||
cout << endl << "**ERROR**: Couldn't find FUN" << i << " for evaluation";
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
cout << endl << "PFunctionHandler::Eval: GetFuncIndex("<<i<<") = " << GetFuncIndex(i);
|
||||
cout << endl;
|
||||
|
||||
return fFuncs[GetFuncIndex(i)].Eval();
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// GetFuncNo (public)
|
||||
//-------------------------------------------------------------
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
* \param i
|
||||
*/
|
||||
unsigned int PFunctionHandler::GetFuncNo(unsigned int i)
|
||||
{
|
||||
if (i > fFuncs.size())
|
||||
return -1;
|
||||
|
||||
return fFuncs[i].GetFuncNo();
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// ReadFile (private)
|
||||
//-------------------------------------------------------------
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
*/
|
||||
bool PFunctionHandler::ReadFile()
|
||||
{
|
||||
cout << endl << "in ~PFunctionHandler::ReadFile()";
|
||||
|
||||
if (fFileName.isEmpty()) {
|
||||
cout << endl << "PFunctionHandler::ReadFile(): **ERROR**";
|
||||
cout << endl << " no file name given :-(. Will quit";
|
||||
return false;
|
||||
}
|
||||
|
||||
QFile f(fFileName);
|
||||
if (!f.exists()) {
|
||||
cout << endl << "PFunctionHandler::ReadFile(): **ERROR**";
|
||||
cout << endl << " File '" << fFileName.latin1() << "' does not exist.";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!f.open(IO_ReadOnly)) {
|
||||
cout << endl << "PFunctionHandler::ReadFile(): **ERROR**";
|
||||
cout << endl << " File '" << fFileName.latin1() << "' couldn't being opened.";
|
||||
return false;
|
||||
}
|
||||
|
||||
QTextStream stream(&f);
|
||||
QString line;
|
||||
bool done = false;
|
||||
bool success = true;
|
||||
int status;
|
||||
int val[10];
|
||||
double dval[10];
|
||||
bool inFcnBlock = false;
|
||||
while ( !stream.atEnd() && !done) {
|
||||
line = stream.readLine(); // line of text excluding '\n'
|
||||
if (line.startsWith("#")) // comment hence ignore
|
||||
continue;
|
||||
line = line.upper();
|
||||
if (line.startsWith("PAR")) {
|
||||
cout << endl << "this is a parameter line ...";
|
||||
status = sscanf(line.latin1(), "PAR %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf",
|
||||
&dval[0], &dval[1], &dval[2], &dval[3], &dval[4],
|
||||
&dval[5], &dval[6], &dval[7], &dval[8], &dval[9]);
|
||||
if (status < 0) {
|
||||
done = true;
|
||||
success = false;
|
||||
cout << endl << "invalid PAR line, sorry ...";
|
||||
} else { // fill map
|
||||
cout << endl << "PAR line, status = " << status;
|
||||
for (int i=0; i<status; i++)
|
||||
fParam.push_back(dval[i]);
|
||||
}
|
||||
} else if (line.startsWith("MAP")) {
|
||||
cout << endl << "this is a map line ...";
|
||||
status = sscanf(line.latin1(), "MAP %d %d %d %d %d %d %d %d %d %d",
|
||||
&val[0], &val[1], &val[2], &val[3], &val[4],
|
||||
&val[5], &val[6], &val[7], &val[8], &val[9]);
|
||||
if (status < 0) {
|
||||
done = true;
|
||||
success = false;
|
||||
cout << endl << "invalid MAP line, sorry ...";
|
||||
} else { // fill map
|
||||
cout << endl << "MAP line, status = " << status;
|
||||
for (int i=0; i<status; i++)
|
||||
fMap.push_back(val[i]);
|
||||
}
|
||||
} else if (line.startsWith("FUNCTIONS")) {
|
||||
cout << endl << "the functions block start ...";
|
||||
inFcnBlock = true;
|
||||
} else if (line.startsWith("END")) {
|
||||
cout << endl << "end tag found; rest will be ignored";
|
||||
done = true;
|
||||
} else if (inFcnBlock) {
|
||||
fLines.push_back(line);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
f.close();
|
||||
|
||||
// check if all blocks are given
|
||||
if ((fMap.size() == 0) || (fParam.size() == 0) || (fLines.size() == 0)) {
|
||||
if (fMap.size() == 0)
|
||||
cout << endl << "MAP block is missing ...";
|
||||
if (fParam.size() == 0)
|
||||
cout << endl << "PAR block is missing ...";
|
||||
if (fLines.size() == 0)
|
||||
cout << endl << "FUNCTION block is missing ...";
|
||||
success = false;
|
||||
}
|
||||
|
||||
if (success) {
|
||||
cout << endl << "Functions: ";
|
||||
for (unsigned int i=0; i<fLines.size(); i++)
|
||||
cout << endl << fLines[i].latin1();
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// MapsAreValid (private)
|
||||
//-------------------------------------------------------------
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
*/
|
||||
bool PFunctionHandler::MapsAreValid()
|
||||
{
|
||||
bool success = true;
|
||||
|
||||
int maxParam = fParam.size();
|
||||
for (unsigned int i=0; i<fMap.size(); i++)
|
||||
if (fMap[i] > maxParam)
|
||||
success = false;
|
||||
|
||||
if (!success)
|
||||
cout << endl << "invalid MAP found ...";
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// GetFuncIndex (private)
|
||||
//-------------------------------------------------------------
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
* \param funcNo
|
||||
*/
|
||||
int PFunctionHandler::GetFuncIndex(int funcNo)
|
||||
{
|
||||
int index = -1;
|
||||
|
||||
for (unsigned int i=0; i<fFuncs.size(); i++) {
|
||||
if (fFuncs[i].GetFuncNo() == funcNo) {
|
||||
index = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
86
src/tests/spirit/PFunctionHandler.h
Normal file
86
src/tests/spirit/PFunctionHandler.h
Normal file
@@ -0,0 +1,86 @@
|
||||
/***************************************************************************
|
||||
|
||||
PFunctionHandler.h
|
||||
|
||||
Author: Andreas Suter
|
||||
e-mail: andreas.suter@psi.ch
|
||||
|
||||
$Id$
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2007 by Andreas Suter *
|
||||
* andreas.suter@psi.c *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef _PFUNCTIONHANDLER_H_
|
||||
#define _PFUNCTIONHANDLER_H_
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
#include <qstring.h>
|
||||
|
||||
#include "PFunctionGrammar.h"
|
||||
#include "PFunction.h"
|
||||
|
||||
class PFunctionHandler
|
||||
{
|
||||
public:
|
||||
PFunctionHandler(char *fln);
|
||||
PFunctionHandler(vector<QString> lines);
|
||||
virtual ~PFunctionHandler();
|
||||
|
||||
virtual bool IsValid() { return fValid; }
|
||||
virtual bool DoParse();
|
||||
virtual double Eval(int i);
|
||||
virtual unsigned int GetFuncNo(unsigned int i);
|
||||
virtual unsigned int GetNoOfFuncs() { return fFuncs.size(); }
|
||||
|
||||
private:
|
||||
bool fValid;
|
||||
|
||||
QString fFileName;
|
||||
|
||||
vector<double> fParam;
|
||||
vector<int> fMap;
|
||||
vector<QString> fLines;
|
||||
|
||||
vector<PFunction> fFuncs;
|
||||
|
||||
virtual bool ReadFile();
|
||||
virtual bool MapsAreValid();
|
||||
virtual int GetFuncIndex(int funcNo);
|
||||
};
|
||||
|
||||
// cint dictionary stuff --------------------------------------
|
||||
#ifdef __CINT__
|
||||
|
||||
#pragma link off all globals;
|
||||
#pragma link off all classes;
|
||||
#pragma link off all functions;
|
||||
|
||||
#pragma link C++ class PFunctionHandler+;
|
||||
|
||||
#endif // end __CINT__
|
||||
//-------------------------------------------------------------
|
||||
|
||||
#endif // _PFUNCTIONHANDLER_H_
|
||||
|
||||
28
src/tests/spirit/fcnInput.txt
Normal file
28
src/tests/spirit/fcnInput.txt
Normal file
@@ -0,0 +1,28 @@
|
||||
#----------------------------------------------
|
||||
# function input file
|
||||
#----------------------------------------------
|
||||
#
|
||||
# '#' are comment lines
|
||||
#
|
||||
# The file has the following structure:
|
||||
#
|
||||
# PAR <par1> <par2> ... <parN>
|
||||
# MAP <map1> <map2> ... <mapM>
|
||||
# FUNCTIONS
|
||||
# fun1 = <function>
|
||||
# fun2 = <function>
|
||||
# funX = <function>
|
||||
# END
|
||||
#----------------------------------------------
|
||||
PAR 1.0 2.1 3.5 -0.87 0.87
|
||||
MAP 2 1 4 5
|
||||
FUNCTIONS
|
||||
fun0 = sin(par3/(par1+map2))
|
||||
#fun0 = par1 + map3 * cos(cos(par2 - map1))
|
||||
#fun8 = log(sin(par1)) + exp(-1.0*map2)
|
||||
#fun1 = par1 + map1 * (0.01355+par1*(2.1 - (-2.3 / 3.4)))
|
||||
#fun2 = par1 * par2 - map3
|
||||
#fun3 = -3.2 + (par2-par1)/(map2+map3)
|
||||
#fun7 = 1.2
|
||||
END
|
||||
#----------------------------------------------
|
||||
BIN
src/tests/spirit/spirit.pdf
Normal file
BIN
src/tests/spirit/spirit.pdf
Normal file
Binary file not shown.
100
src/tests/spirit/spirit_fcn_test.cpp
Normal file
100
src/tests/spirit/spirit_fcn_test.cpp
Normal file
@@ -0,0 +1,100 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
#include "PFunctionHandler.h"
|
||||
|
||||
//-----------------------------------------------------
|
||||
void syntax()
|
||||
{
|
||||
cout << endl << "spirit_fcn_test [--file <filename>] | [--help]";
|
||||
cout << endl << " without arguments: interactive mode";
|
||||
cout << endl << " --file <filename>: function block etc. from file";
|
||||
cout << endl << " --help: this help";
|
||||
cout << endl << endl;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------
|
||||
void handle_input(vector<QString> &lines)
|
||||
{
|
||||
cout << endl << "will handle input ...";
|
||||
cout << endl << "you should provide a PAR, a MAP, and a FUNCTION block";
|
||||
cout << endl << " Map block:";
|
||||
cout << endl << " MAP <map1> <map2> ... <mapM>";
|
||||
cout << endl << " Parameter block:";
|
||||
cout << endl << " PAR <par1> <par2> ... <parN>";
|
||||
cout << endl << " Function Block:";
|
||||
cout << endl << " FUNCTION";
|
||||
cout << endl << " fun1 = <function1>";
|
||||
cout << endl << " fun2 = <function2>";
|
||||
cout << endl << " ...";
|
||||
cout << endl << " funX = <functionX>";
|
||||
cout << endl << " END";
|
||||
cout << endl << "to get out of the input handle type '.q'";
|
||||
cout << endl;
|
||||
bool done = false;
|
||||
char str[128];
|
||||
do {
|
||||
cout << ">> ";
|
||||
cin.getline(str, sizeof(str));
|
||||
if (!strcmp(str, ".q"))
|
||||
done = true;
|
||||
else
|
||||
lines.push_back(str);
|
||||
} while (!done);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
bool inputFile = false;
|
||||
|
||||
if (argc > 3) {
|
||||
syntax();
|
||||
return 0;
|
||||
} else if (argc == 2) {
|
||||
syntax();
|
||||
return 0;
|
||||
} else if (argc == 3) {
|
||||
if (strcmp(argv[1], "--file")) {
|
||||
syntax();
|
||||
return 0;
|
||||
} else {
|
||||
inputFile = true;
|
||||
}
|
||||
}
|
||||
|
||||
PFunctionHandler *fcnHandler = 0;
|
||||
|
||||
if (inputFile) {
|
||||
fcnHandler = new PFunctionHandler(argv[2]);
|
||||
} else {
|
||||
vector<QString> lines;
|
||||
handle_input(lines);
|
||||
cout << endl << "lines.size() = " << lines.size();
|
||||
fcnHandler = new PFunctionHandler(lines);
|
||||
}
|
||||
|
||||
if (fcnHandler == 0) {
|
||||
cout << endl << "Couldn't invoke function handler, sorry ..." << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool go_on = fcnHandler->IsValid();
|
||||
|
||||
if (go_on) {
|
||||
cout << endl << "will do the parsing ...";
|
||||
if (fcnHandler->DoParse()) {
|
||||
cout << endl << "will do the evaluation ...";
|
||||
for (unsigned int i=0; i<fcnHandler->GetNoOfFuncs(); i++)
|
||||
cout << endl << "FUN" << fcnHandler->GetFuncNo(i) << " = " << fcnHandler->Eval(fcnHandler->GetFuncNo(i));
|
||||
}
|
||||
}
|
||||
|
||||
// clean up
|
||||
if (fcnHandler) {
|
||||
delete fcnHandler;
|
||||
fcnHandler = 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
24
src/tests/spirit/spirit_fcn_test.pro
Normal file
24
src/tests/spirit/spirit_fcn_test.pro
Normal file
@@ -0,0 +1,24 @@
|
||||
#------------------------------------------------------
|
||||
# spirit_fcn_test.pro
|
||||
# qmake file for spirit_fcn_test
|
||||
#
|
||||
# Andreas Suter, 2007/12/10
|
||||
#
|
||||
# $Id$
|
||||
#
|
||||
#------------------------------------------------------
|
||||
|
||||
MAKEFILE = Makefile
|
||||
|
||||
CONFIG += warn_on debug
|
||||
|
||||
HEADERS = PFunctionGrammar.h \
|
||||
PFunction.h \
|
||||
PFunctionHandler.h
|
||||
|
||||
SOURCES = spirit_fcn_test.cpp \
|
||||
PFunction.cpp \
|
||||
PFunctionHandler.cpp
|
||||
|
||||
TARGET=spirit_fcn_test
|
||||
|
||||
Reference in New Issue
Block a user