diff options
| author | 2015-10-11 22:13:07 +0200 | |
|---|---|---|
| committer | 2015-10-11 22:13:07 +0200 | |
| commit | 9e9dd15316e80cae916c532d13ddc5ddc5e93697 (patch) | |
| tree | 52f0daa73bcd36d3562cb80847cc4536355448fc | |
| parent | 80ff2a99b45a25695321cc84a30a3fbf3b797d54 (diff) | |
| parent | 1ccb6b4c0454fae8ebc4ebdca9ab74588d3d8707 (diff) | |
| download | OneRoll-9e9dd15316e80cae916c532d13ddc5ddc5e93697.tar.gz OneRoll-9e9dd15316e80cae916c532d13ddc5ddc5e93697.zip | |
Merge branch 'master' of github.com:obiwankennedy/DiceParser
53 files changed, 812 insertions, 223 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index e8b4c5c..9aae367 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,6 +25,7 @@ add_executable( range.cpp booleancondition.cpp validator.cpp + compositevalidator.cpp die.cpp parsingtoolbox.cpp dicealias.cpp @@ -136,7 +136,6 @@ Result: 3.5 Roll 2 dice and add 3 to the sum of those dice. Then the result is used for rolling dice. - ## Arithmetic and Dice It is possible to use arithmetic opearation on dice. Please pay attention that the default operation to translate a @@ -163,7 +162,7 @@ Substract 4 to 6 and then roll two dice. Divide by 2 the result of 1 die. -## Roll two (or more) kind of dice at once. +## Roll two (or more) kinds of dice at once. To make it, you have to separate all dice commands by `;` diff --git a/booleancondition.cpp b/booleancondition.cpp index c5fd7c7..c09166e 100644 --- a/booleancondition.cpp +++ b/booleancondition.cpp @@ -104,7 +104,7 @@ QString BooleanCondition::toString() } return QString("[%1%2]").arg(str).arg(m_value); } -quint8 BooleanCondition::getValidRangeSize(quint64 faces) const +quint64 BooleanCondition::getValidRangeSize(quint64 faces) const { switch (m_operator) { diff --git a/booleancondition.h b/booleancondition.h index cb37b9e..2177106 100644 --- a/booleancondition.h +++ b/booleancondition.h @@ -40,7 +40,7 @@ public: void setValue(qint64); QString toString(); - virtual quint8 getValidRangeSize(quint64 faces) const; + virtual quint64 getValidRangeSize(quint64 faces) const; private: LogicOperator m_operator; diff --git a/compositevalidator.cpp b/compositevalidator.cpp new file mode 100644 index 0000000..cc52fdd --- /dev/null +++ b/compositevalidator.cpp @@ -0,0 +1,118 @@ +/*************************************************************************** +* Copyright (C) 2014 by Renaud Guezennec * +* http://renaudguezennec.homelinux.org/accueil,3.html * +* * +* This file is part of DiceParser * +* * +* DiceParser 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 "compositevalidator.h" + + +CompositeValidator::CompositeValidator() +{ +} +qint64 CompositeValidator::hasValid(Die* b,bool recursive,bool unhighlight) const +{ + + int i = 0; + qint64 sum = 0; + foreach(const Validator* validator, *m_validatorList) + { + qint64 val = validator->hasValid(b,recursive,unhighlight); + if(i==0) + { + sum = val; + } + else + { + switch(m_operators->at(i-1)) + { + case OR: + sum |= val; + break; + case EXCLUSIVE_OR: + sum ^= val;/// @todo may required to be done by hand + break; + case AND: + sum &= val; + break; + } + } + ++i; + } + + return sum; +} + +QString CompositeValidator::toString() +{ + QString str=""; + /*switch (m_operator) + { + case Equal: + str.append("="); + break; + case GreaterThan: + str.append(">"); + break; + case LesserThan: + str.append("<"); + break; + case GreaterOrEqual: + str.append(">="); + break; + case LesserOrEqual: + str.append("<="); + break; + } + return QString("[%1%2]").arg(str).arg(m_value);*/ + return str; +} +quint64 CompositeValidator::getValidRangeSize(quint64 faces) const +{ + quint64 sum =0; + int i = -1; + foreach(Validator* tmp,*m_validatorList) + { + quint64 rel = tmp->getValidRangeSize(faces); + LogicOperation opt; + if(i>=0) + { + opt = m_operators->at(i); + } + if(opt == OR) + { + sum += rel; + } + else if((opt == AND)&&(opt == EXCLUSIVE_OR)) + { + sum = qMax(rel,sum); + } + ++i; + } + + return sum; +} +void CompositeValidator::setOperationList(QVector<LogicOperation>* m) +{ + m_operators = m; +} + +void CompositeValidator::setValidatorList(QList<Validator*>* m) +{ + m_validatorList = m; +} diff --git a/compositevalidator.h b/compositevalidator.h new file mode 100644 index 0000000..789f33f --- /dev/null +++ b/compositevalidator.h @@ -0,0 +1,55 @@ +/*************************************************************************** +* Copyright (C) 2014 by Renaud Guezennec * +* http://renaudguezennec.homelinux.org/accueil,3.html * +* * +* This file is part of DiceParser * +* * +* DiceParser 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 COMPOSITEVALIDATOR_H +#define COMPOSITEVALIDATOR_H + +#include <QString> +#include <QVector> +#include <QList> +#include <Qt> + +#include "validator.h" +/** + * @brief The BooleanCondition class is a Validator class checking validity from logic expression. + * It manages many operators (see : @ref LogicOperator). + */ +class CompositeValidator : public Validator +{ +public: + enum LogicOperation { OR, EXCLUSIVE_OR , AND}; + CompositeValidator(); + + virtual qint64 hasValid(Die* b,bool recursive, bool unhighlight = false) const; + + void setOperationList(QVector<LogicOperation>* m); + void setValidatorList(QList<Validator*>*); + + QString toString(); + + virtual quint64 getValidRangeSize(quint64 faces) const; + +private: + QVector<LogicOperation>* m_operators; + QList<Validator*>* m_validatorList; +}; + +#endif // BOOLEANCONDITION_H diff --git a/diceparser.cpp b/diceparser.cpp index 10ba2d7..144a7d6 100644 --- a/diceparser.cpp +++ b/diceparser.cpp @@ -149,7 +149,6 @@ void DiceParser::insertAlias(DiceAlias* dice, int i) bool DiceParser::parseLine(QString str) { - m_currentTreeHasSeparator = false; m_errorMap.clear(); if(NULL!=m_start) { @@ -576,8 +575,8 @@ bool DiceParser::readDice(QString& str,ExecutionNode* & node) { if(currentOperator==D) { - int num; - int end; + qint64 num; + qint64 end; if(m_parsingToolbox->readNumber(str,num)) { if(num<1) @@ -597,7 +596,7 @@ bool DiceParser::readDice(QString& str,ExecutionNode* & node) else if(m_parsingToolbox->readDiceRange(str,num,end)) { - int face = abs(num - end)+1; + qint64 face = abs(num - end)+1; DiceRollerNode* drNode = new DiceRollerNode(face,num); node = drNode; ExecutionNode* current = drNode; @@ -611,9 +610,16 @@ bool DiceParser::readDice(QString& str,ExecutionNode* & node) else if(currentOperator ==L) { QStringList list; - if(m_parsingToolbox->readList(str,list)) + QList<Range> listRange; + ParsingToolBox::LIST_OPERATOR op = m_parsingToolbox->readListOperator(str); + if(m_parsingToolbox->readList(str,list,listRange)) { ListSetRollNode* lsrNode = new ListSetRollNode(); + lsrNode->setRangeList(listRange); + if(op == ParsingToolBox::UNIQUE) + { + lsrNode->setUnique(true); + } lsrNode->setListValue(list); node = lsrNode; return true; @@ -747,7 +753,6 @@ bool DiceParser::readOperator(QString& str,ExecutionNode* previous) previous->setNextNode(nodeExec); m_currentTreeHasSeparator = true; return true; - } } else @@ -804,7 +809,7 @@ bool DiceParser::readOption(QString& str,ExecutionNode* previous, bool hasDice)/ { case Keep: { - int myNumber=0; + qint64 myNumber=0; bool ascending = m_parsingToolbox->readAscending(str); if(m_parsingToolbox->readNumber(str,myNumber)) { @@ -825,7 +830,7 @@ bool DiceParser::readOption(QString& str,ExecutionNode* previous, bool hasDice)/ break; case KeepAndExplose: { - int myNumber=0; + qint64 myNumber=0; bool ascending = m_parsingToolbox->readAscending(str); if(m_parsingToolbox->readNumber(str,myNumber)) { @@ -864,7 +869,8 @@ bool DiceParser::readOption(QString& str,ExecutionNode* previous, bool hasDice)/ break; case Count: { - Validator* validator = m_parsingToolbox->readValidator(str); + //Validator* validator = m_parsingToolbox->readValidator(str); + Validator* validator = m_parsingToolbox->readCompositeValidator(str); if(NULL!=validator) { /// @todo display warning here. @@ -887,7 +893,8 @@ bool DiceParser::readOption(QString& str,ExecutionNode* previous, bool hasDice)/ case Reroll: case RerollAndAdd: { - Validator* validator = m_parsingToolbox->readValidator(str); + //Validator* validator = m_parsingToolbox->readValidator(str); + Validator* validator = m_parsingToolbox->readCompositeValidator(str); if(NULL!=validator) { /// @todo display warning here. @@ -912,7 +919,7 @@ bool DiceParser::readOption(QString& str,ExecutionNode* previous, bool hasDice)/ break; case Explosing: { - Validator* validator = m_parsingToolbox->readValidator(str); + Validator* validator = m_parsingToolbox->readCompositeValidator(str); if(NULL!=validator) { if(!m_parsingToolbox->isValidValidator(previous,validator)) @@ -967,7 +974,7 @@ QString DiceParser::humanReadableError() bool DiceParser::readOperand(QString& str,ExecutionNode* & node) { - int myNumber=1; + qint64 myNumber=1; if(m_parsingToolbox->readNumber(str,myNumber)) { @@ -983,7 +990,7 @@ void DiceParser::writeDownDotTree(QString filepath) { QString str("digraph ExecutionTree {\n"); m_start->generateDotTree(str); - str.append("}"); + str.append("}\n"); QFile file(filepath); diff --git a/diceparser.h b/diceparser.h index 3fc2cb7..6b9056f 100644 --- a/diceparser.h +++ b/diceparser.h @@ -302,6 +302,7 @@ private: QStringList* m_commandList; QMap<ExecutionNode::ERROR_CODE,QString> m_errorMap; + QMap<ExecutionNode::ERROR_CODE,QString> m_warningMap; ExecutionNode* m_start; diff --git a/diceparser.pri b/diceparser.pri index 22ef326..94c44df 100644 --- a/diceparser.pri +++ b/diceparser.pri @@ -13,6 +13,7 @@ SOURCES += $$PWD/diceparser.cpp \ $$PWD/result/scalarresult.cpp \ $$PWD/parsingtoolbox.cpp \ $$PWD/result/stringresult.cpp \ + $$PWD/compositevalidator.cpp \ $$PWD/dicealias.cpp @@ -27,6 +28,7 @@ HEADERS += \ $$PWD/result/scalarresult.h \ $$PWD/parsingtoolbox.h \ $$PWD/result/stringresult.h \ + $$PWD/compositevalidator.h \ $$PWD/dicealias.h diff --git a/node/countexecutenode.cpp b/node/countexecutenode.cpp index ff3d67b..55b278e 100644 --- a/node/countexecutenode.cpp +++ b/node/countexecutenode.cpp @@ -6,11 +6,11 @@ CountExecuteNode::CountExecuteNode() : m_scalarResult(new ScalarResult()),m_validator(NULL) { - m_result = m_scalarResult; + m_result = m_scalarResult; } void CountExecuteNode::setValidator(Validator* validator) { - m_validator = validator; + m_validator = validator; } CountExecuteNode::~CountExecuteNode() { @@ -23,41 +23,48 @@ CountExecuteNode::~CountExecuteNode() void CountExecuteNode::run(ExecutionNode *previous) { m_previousNode = previous; - if(NULL==previous) - { - return; - } - DiceResult* previous_result = dynamic_cast<DiceResult*>(previous->getResult()); - if(NULL!=previous_result) - { - m_result->setPrevious(previous_result); - QList<Die*> diceList=previous_result->getResultList(); - qint64 sum = 0; - foreach(Die* dice,diceList) - { - sum+=m_validator->hasValid(dice,true,true); - } - m_scalarResult->setValue(sum); - - - if(NULL!=m_nextNode) - { - m_nextNode->run(this); - } - } + if(NULL==previous) + { + return; + } + DiceResult* previous_result = dynamic_cast<DiceResult*>(previous->getResult()); + if(NULL!=previous_result) + { + m_result->setPrevious(previous_result); + QList<Die*> diceList=previous_result->getResultList(); + qint64 sum = 0; + foreach(Die* dice,diceList) + { + sum+=m_validator->hasValid(dice,true,true); + } + m_scalarResult->setValue(sum); + + + if(NULL!=m_nextNode) + { + m_nextNode->run(this); + } + } } -QString CountExecuteNode::toString() const +QString CountExecuteNode::toString(bool withlabel) const { - return "CountExecuteNode"+m_validator->toString(); + if(withlabel) + { + return QString("%1 [label=\"CountExecuteNode %2\"]").arg(m_id).arg(m_validator->toString()); + } + else + { + return m_id; + } } qint64 CountExecuteNode::getPriority() const { - qint64 priority=0; - if(NULL!=m_nextNode) - { - priority = m_nextNode->getPriority(); - } + qint64 priority=0; + if(NULL!=m_nextNode) + { + priority = m_nextNode->getPriority(); + } - return priority; + return priority; } diff --git a/node/countexecutenode.h b/node/countexecutenode.h index 519403b..167ee82 100644 --- a/node/countexecutenode.h +++ b/node/countexecutenode.h @@ -30,7 +30,7 @@ public: * @brief toString * @return */ - virtual QString toString()const; + virtual QString toString(bool withLabel)const; /** * @brief getPriority * @return diff --git a/node/dicerollernode.cpp b/node/dicerollernode.cpp index 06148fc..d8fe49c 100644 --- a/node/dicerollernode.cpp +++ b/node/dicerollernode.cpp @@ -8,30 +8,7 @@ #include <QTime> -//DiceRoller::DiceRoller(QMutex* mutex,DiceResult* diceResult,int faces,int count) -// : m_mutex(mutex),m_sharedDiceResult(diceResult),m_faces(faces),m_diceCount(count) -//{ -//} - -//void DiceRoller::run() -//{ -// for(quint64 i=0; i < m_diceCount ; ++i) -// { -// Die* die = new Die(); -// die->setFaces(m_faces); -// die->roll(); -// m_mutex->lock(); -// m_sharedDiceResult->insertResult(die); -// m_mutex->unlock(); -// } -//} - - - -////////////////////////////////////////////////// -/// \brief DiceRollerNode::DiceRollerNode -////////////////////////////////////////////////// DiceRollerNode::DiceRollerNode(quint64 faces,qint64 offset) : m_faces(faces),m_diceResult(new DiceResult()),m_offset(offset) { @@ -48,6 +25,11 @@ void DiceRollerNode::run(ExecutionNode* previous) m_diceCount = result->getResult(Result::SCALAR).toReal(); m_result->setPrevious(result); + if(m_diceCount == 0) + { + m_errors.insert(NO_DICE_TO_ROLL,QObject::tr("No dice to roll")); + } + for(quint64 i=0; i < m_diceCount ; ++i) { Die* die = new Die(); @@ -61,19 +43,23 @@ void DiceRollerNode::run(ExecutionNode* previous) m_nextNode->run(this); } } - } - - - + } } quint64 DiceRollerNode::getFaces() { return m_faces; } -QString DiceRollerNode::toString() const +QString DiceRollerNode::toString(bool wl) const { - return QString("DiceRollerNode"); + if(wl) + { + return QString("%1 [label=\"DiceRollerNode faces: %2\"]").arg(m_id).arg(m_faces); + } + else + { + return m_id; + } } qint64 DiceRollerNode::getPriority() const { diff --git a/node/dicerollernode.h b/node/dicerollernode.h index 726676e..808676f 100644 --- a/node/dicerollernode.h +++ b/node/dicerollernode.h @@ -2,35 +2,43 @@ #define DICEROLLERNODE_H #include <Qt> -#include <QRunnable> -#include <QMutex> #include "executionnode.h" #include "result/diceresult.h" -//class DiceRoller : public QRunnable -//{ -//public: -// DiceRoller(QMutex* mutex,DiceResult* diceResult,int faces,int count); -// virtual void run (); -//private: -// QMutex* m_mutex; -// DiceResult* m_sharedDiceResult; -// int m_faces; -// quint64 m_diceCount; -//}; /** - * @brief The DiceRollerNode class + * @brief The DiceRollerNode class rolls dice of one kind. */ class DiceRollerNode : public ExecutionNode { public: + /** + * @brief DiceRollerNode builds an instance + * @param faces, number of faces of dices + * @param offset, first value of dice. + */ DiceRollerNode(quint64 faces, qint64 offset = 1); + /** + * @brief run - starts to roll dice. + */ virtual void run(ExecutionNode*); + /** + * @brief getFaces accessor + * @return the face count + */ quint64 getFaces(); - virtual QString toString()const; + /** + * @brief toString + * @param wl + * @return use to generate dot tree; + */ + virtual QString toString(bool wl)const; + /** + * @brief getPriority + * @return priority for dice roll: 4 (higher) + */ virtual qint64 getPriority() const; //private members private: diff --git a/node/executionnode.cpp b/node/executionnode.cpp index dba56fc..abcb7cf 100644 --- a/node/executionnode.cpp +++ b/node/executionnode.cpp @@ -1,13 +1,14 @@ #include "executionnode.h" +#include <QUuid> + ExecutionNode::ExecutionNode() - : m_nextNode(NULL),m_result(NULL),m_previousNode(NULL) + : m_nextNode(NULL),m_result(NULL),m_previousNode(NULL),m_id(QString("\"%1\"").arg(QUuid::createUuid().toString())) { } ExecutionNode::~ExecutionNode() { - if(NULL!=m_result) { delete m_result; @@ -53,19 +54,29 @@ ExecutionNode* ExecutionNode::getPreviousNode() const } void ExecutionNode::generateDotTree(QString& s) { - s.append(toString()); + s.append(toString(true)); + s.append(";\n"); + if(NULL!=m_nextNode) { + s.append(toString(false)); s.append(" -> "); - s.append(m_nextNode->toString()); - s.append(" [label=\"nextNode\"];\n"); + s.append(m_nextNode->toString(false)); + s.append(";\n"); +// s.append(" [label=\"nextNode\"];\n"); m_nextNode->generateDotTree(s); } else { + s.append(toString(false)); s.append(" -> "); - s.append("NULL"); - s.append(" [label=\"nextNode\"];\n"); + s.append("NULL;\n"); + + + s.append(toString(false)); + s.append(" ->"); + s.append(m_result->toString(false)); + s.append(" [label=\"Result\"];\n"); m_result->generateDotTree(s); } diff --git a/node/executionnode.h b/node/executionnode.h index 39d714b..2531ced 100644 --- a/node/executionnode.h +++ b/node/executionnode.h @@ -3,13 +3,14 @@ #include "result/result.h" #include <QDebug> + /** * @brief The ExecutionNode class */ class ExecutionNode { public: - enum ERROR_CODE {NO_ERROR,DIE_RESULT_EXPECTED,BAD_SYNTAXE,ENDLESS_LOOP_ERROR,DIVIDE_BY_ZERO,NOTHING_UNDERSTOOD}; + enum ERROR_CODE {NO_ERROR,DIE_RESULT_EXPECTED,BAD_SYNTAXE,ENDLESS_LOOP_ERROR,DIVIDE_BY_ZERO,NOTHING_UNDERSTOOD,NO_DICE_TO_ROLL,TOO_MANY_DICE}; /** * @brief ExecutionNode */ @@ -46,7 +47,7 @@ public: * @brief toString * @return */ - virtual QString toString()const=0; + virtual QString toString(bool withLabel)const=0; /** * @brief getPriority * @return @@ -85,6 +86,8 @@ protected: * @brief m_errors */ QMap<ExecutionNode::ERROR_CODE,QString> m_errors; + + QString m_id; }; #endif // EXECUTIONNODE_H diff --git a/node/explosedicenode.cpp b/node/explosedicenode.cpp index c0ebfe7..cdbeb6c 100644 --- a/node/explosedicenode.cpp +++ b/node/explosedicenode.cpp @@ -44,9 +44,16 @@ void ExploseDiceNode::setValidator(Validator* val) { m_validator = val; } -QString ExploseDiceNode::toString() const +QString ExploseDiceNode::toString(bool withlabel) const { - return QString("ExploseDiceNode [label=\"ExploseDiceNode %1\"]").arg(m_validator->toString()); + if(withlabel) + { + return QString("%1 [label=\"ExploseDiceNode %2\"]").arg(m_id).arg(m_validator->toString()); + } + else + { + return m_id; + } } qint64 ExploseDiceNode::getPriority() const { diff --git a/node/explosedicenode.h b/node/explosedicenode.h index f5d0f6e..65c0abd 100644 --- a/node/explosedicenode.h +++ b/node/explosedicenode.h @@ -16,7 +16,7 @@ public: virtual ~ExploseDiceNode(); virtual void run(ExecutionNode* previous = NULL); virtual void setValidator(Validator* ); - virtual QString toString()const; + virtual QString toString(bool )const; virtual qint64 getPriority() const; protected: diff --git a/node/helpnode.cpp b/node/helpnode.cpp index dd5f087..6f3916a 100644 --- a/node/helpnode.cpp +++ b/node/helpnode.cpp @@ -34,8 +34,7 @@ void HelpNode::run(ExecutionNode* previous) { if(previous->getResult() == NULL) { - txtResult->setText(toString()); - + txtResult->setText(QObject::tr("Rolisteam Dice Parser:\nFull documentation at: %1").arg(m_path)); } else { @@ -49,9 +48,16 @@ void HelpNode::run(ExecutionNode* previous) m_nextNode->run(this); } } -QString HelpNode::toString() const +QString HelpNode::toString(bool wl) const { - return QObject::tr("Rolisteam Dice Parser:\nFull documentation at: %1 \n").arg(m_path); + if(wl) + { + return QString("%1 [label=\"Rolisteam Dice Parser:\nFull documentation at: %2\"]").arg(m_id).arg(m_path); + } + else + { + return m_id; + } } qint64 HelpNode::getPriority() const diff --git a/node/helpnode.h b/node/helpnode.h index be21b67..ba59f1f 100644 --- a/node/helpnode.h +++ b/node/helpnode.h @@ -45,7 +45,7 @@ public: * @brief toString * @return */ - virtual QString toString()const; + virtual QString toString(bool )const; /** * @brief getPriority * @return diff --git a/node/jumpbackwardnode.cpp b/node/jumpbackwardnode.cpp index be759ce..b0a5c8e 100644 --- a/node/jumpbackwardnode.cpp +++ b/node/jumpbackwardnode.cpp @@ -34,9 +34,16 @@ qint64 JumpBackwardNode::getPriority() const { return 4; } -QString JumpBackwardNode::toString() const +QString JumpBackwardNode::toString(bool wl) const { - return QString("JumpBackwardNode"); + if(wl) + { + return QString("%1 [label=\"JumpBackwardNode\"]").arg(m_id); + } + else + { + return m_id; + } } void JumpBackwardNode::run(ExecutionNode* previous) { diff --git a/node/jumpbackwardnode.h b/node/jumpbackwardnode.h index 469c64a..a89d0d3 100644 --- a/node/jumpbackwardnode.h +++ b/node/jumpbackwardnode.h @@ -43,7 +43,7 @@ public: * @brief toString * @return */ - virtual QString toString() const; + virtual QString toString(bool) const; /** * @brief getPriority * @return diff --git a/node/keepdiceexecnode.cpp b/node/keepdiceexecnode.cpp index a230107..64d53f0 100644 --- a/node/keepdiceexecnode.cpp +++ b/node/keepdiceexecnode.cpp @@ -43,6 +43,10 @@ m_previousNode = previous; { QList<Die*> diceList=previousDiceResult->getResultList(); QList<Die*> diceList2 = diceList.mid(0,m_numberOfDice); + if(m_numberOfDice > diceList.size()) + { + m_errors.insert(TOO_MANY_DICE,QObject::tr(" You ask to keep %1 dice but the result only has %2").arg(m_numberOfDice).arg(diceList.size())); + } foreach(Die* tmp,diceList.mid(m_numberOfDice,-1)) { @@ -60,9 +64,16 @@ void KeepDiceExecNode::setDiceKeepNumber(quint64 n) { m_numberOfDice = n; } -QString KeepDiceExecNode::toString() const +QString KeepDiceExecNode::toString(bool wl) const { - return QString("KeepDiceExecNode [label=\"KeepDiceExecNode %1\"]").arg(m_numberOfDice); + if(wl) + { + return QString("%1 [label=\"KeepDiceExecNode %2\"]").arg(m_id).arg(m_numberOfDice); + } + else + { + return m_id; + } } qint64 KeepDiceExecNode::getPriority() const { diff --git a/node/keepdiceexecnode.h b/node/keepdiceexecnode.h index 01f5028..f86daa5 100644 --- a/node/keepdiceexecnode.h +++ b/node/keepdiceexecnode.h @@ -34,7 +34,7 @@ public: virtual void run(ExecutionNode *previous); virtual void setDiceKeepNumber(quint64 ); - virtual QString toString()const; + virtual QString toString(bool)const; virtual qint64 getPriority() const; private: quint64 m_numberOfDice; diff --git a/node/listaliasnode.cpp b/node/listaliasnode.cpp index 1809eac..9ced186 100644 --- a/node/listaliasnode.cpp +++ b/node/listaliasnode.cpp @@ -59,14 +59,23 @@ QString ListAliasNode::buildList() const } return result; } -QString ListAliasNode::toString() const +QString ListAliasNode::toString(bool wl) const { - QStringList resultList; - foreach(DiceAlias* key, *m_aliasList) - { - resultList << "{" <<key->getCommand() << key->getValue()<< "}"; - } - return QString("ListAliasNode [label=\"ListAliasNode %1\"]").arg(resultList.join(",")); + QStringList resultList; + foreach(DiceAlias* key, *m_aliasList) + { + resultList << "{" <<key->getCommand() << key->getValue()<< "}"; + } + + if(wl) + { + return QString("%1 [label=\"ListAliasNode %2\"]").arg(m_id).arg(resultList.join(",")); + } + else + { + return m_id; + } + } qint64 ListAliasNode::getPriority() const diff --git a/node/listaliasnode.h b/node/listaliasnode.h index e8f8b0f..2f83a1b 100644 --- a/node/listaliasnode.h +++ b/node/listaliasnode.h @@ -42,7 +42,7 @@ public: * @brief toString * @return */ - virtual QString toString() const; + virtual QString toString(bool) const; /** * @brief buildList * @return diff --git a/node/listsetrollnode.cpp b/node/listsetrollnode.cpp index fdc17a5..9f15d36 100644 --- a/node/listsetrollnode.cpp +++ b/node/listsetrollnode.cpp @@ -22,7 +22,7 @@ #include "die.h" ListSetRollNode::ListSetRollNode() - :m_diceResult(new DiceResult()),m_stringResult(new StringResult()) + :m_diceResult(new DiceResult()),m_stringResult(new StringResult()),m_unique(false) { m_result = m_stringResult; } @@ -39,19 +39,21 @@ QStringList ListSetRollNode::getList() { return m_values; } -QString ListSetRollNode::toString() const +QString ListSetRollNode::toString(bool wl) const { - return QString("ListSetRollNode [label=\"ListSetRoll list:%1\"]").arg(m_values.join(',')); + if(wl) + { + return QString("%1 [label=\"ListSetRoll list:%2\"]").arg(m_id).arg(m_values.join(",")); + } + else + { + return m_id; + } + } qint64 ListSetRollNode::getPriority() const { qint64 priority=4; -// if(NULL!=m_nextNode) -// { -// priority = m_nextNode->getPriority(); -// } - - return priority; } void ListSetRollNode::run(ExecutionNode* previous) @@ -68,13 +70,10 @@ void ListSetRollNode::run(ExecutionNode* previous) for(quint64 i=0; i < diceCount ; ++i) { Die* die = new Die(); - die->setFaces(m_values.size()); + computeFacesNumber(die); die->roll(); m_diceResult->insertResult(die); - if(die->getValue()-1<m_values.size()) - { - rollResult << m_values[die->getValue()-1]; - } + getValueFromDie(die,rollResult); } m_stringResult->setText(rollResult.join(",")); if(NULL!=m_nextNode) @@ -83,11 +82,64 @@ void ListSetRollNode::run(ExecutionNode* previous) } } } - - - } void ListSetRollNode::setListValue(QStringList lirs) { m_values = lirs; } +void ListSetRollNode::setUnique(bool u) +{ + m_unique = u; +} +void ListSetRollNode::setRangeList(QList<Range>& ranges) +{ + m_rangeList = ranges; +} +void ListSetRollNode::computeFacesNumber(Die* die) +{ + if(m_rangeList.isEmpty()) + { + die->setFaces(m_values.size()); + } + else + { + Q_ASSERT(m_values.size() == m_rangeList.size()); + qint64 max; + int i=0; + foreach(Range range, m_rangeList) + { + if(((i==0)||(max<range.getEnd()))&&(range.isFullyDefined())) + { + // qDebug()<< range.isFullyDefined() << range.getEnd(); + max= range.getEnd(); + } + ++i; + } + //qDebug() << "set Faces"<<max; + die->setFaces(max); + } + +} +void ListSetRollNode::getValueFromDie(Die* die,QStringList& rollResult) +{ + if(m_rangeList.isEmpty()) + { + if(die->getValue()-1<m_values.size()) + { + rollResult << m_values[die->getValue()-1]; + } + } + else + { + Q_ASSERT(m_values.size() == m_rangeList.size()); + int i=0; + foreach (Range range, m_rangeList) + { + if(range.hasValid(die,false)) + { + rollResult << m_values[i]; + } + ++i; + } + } +} diff --git a/node/listsetrollnode.h b/node/listsetrollnode.h index 3102ade..5f381f5 100644 --- a/node/listsetrollnode.h +++ b/node/listsetrollnode.h @@ -27,6 +27,7 @@ #include "executionnode.h" #include "result/diceresult.h" #include "result/stringresult.h" +#include "range.h" /** * @brief The ListSetRollNode class is dedicated to pick up item from list. */ @@ -36,17 +37,25 @@ public: ListSetRollNode(); virtual ~ListSetRollNode(); virtual void run(ExecutionNode* previous = NULL); - virtual QString toString()const; + virtual QString toString(bool)const; virtual qint64 getPriority() const; QStringList getList(); void setListValue(QStringList); + void setUnique(bool ); + void setRangeList(QList<Range>&); + + +private: + void getValueFromDie(Die* die,QStringList& rollResult); + void computeFacesNumber(Die* die); private: QStringList m_values; DiceResult* m_diceResult; StringResult* m_stringResult; - + bool m_unique; + QList<Range> m_rangeList; }; #endif // LISTSETROLLNODE_H diff --git a/node/numbernode.cpp b/node/numbernode.cpp index c63aadf..e2da54f 100644 --- a/node/numbernode.cpp +++ b/node/numbernode.cpp @@ -44,9 +44,16 @@ void NumberNode::setNumber(qint64 a) m_scalarResult->setValue(a); m_number = a; } -QString NumberNode::toString() const +QString NumberNode::toString(bool withLabel) const { - return QString("NumberNode [label=\"NumberNode %1\"]").arg(m_number); + if(withLabel) + { + return QString("%1 [label=\"NumberNode %2\"]").arg(m_id).arg(m_number); + } + else + { + return m_id; + } } qint64 NumberNode::getPriority() const { diff --git a/node/numbernode.h b/node/numbernode.h index 3c43a51..50d29ad 100644 --- a/node/numbernode.h +++ b/node/numbernode.h @@ -34,7 +34,7 @@ public: NumberNode(); void run(ExecutionNode* previous); void setNumber(qint64); -virtual QString toString()const; + virtual QString toString(bool withLabel)const; virtual qint64 getPriority() const; private: qint64 m_number; diff --git a/node/parenthesesnode.cpp b/node/parenthesesnode.cpp index f81fc1c..f54a976 100644 --- a/node/parenthesesnode.cpp +++ b/node/parenthesesnode.cpp @@ -50,9 +50,16 @@ void ParenthesesNode::run(ExecutionNode* /*previous*/) m_nextNode->run(this); } } -QString ParenthesesNode::toString() const +QString ParenthesesNode::toString(bool b) const { - return "ParenthesesNode"; + if(b) + { + return QString("%1 [label=\"ParenthesesNode\"]").arg(m_id); + } + else + { + return m_id; + } } qint64 ParenthesesNode::getPriority() const { diff --git a/node/parenthesesnode.h b/node/parenthesesnode.h index 999e7bd..fbb1caf 100644 --- a/node/parenthesesnode.h +++ b/node/parenthesesnode.h @@ -34,7 +34,7 @@ public: virtual void run(ExecutionNode* previous = NULL); void setInternelNode(ExecutionNode* node); - virtual QString toString()const; + virtual QString toString(bool)const; virtual qint64 getPriority() const; private: ExecutionNode* m_internalNode; diff --git a/node/rerolldicenode.cpp b/node/rerolldicenode.cpp index 038a3ec..ce8f109 100644 --- a/node/rerolldicenode.cpp +++ b/node/rerolldicenode.cpp @@ -46,9 +46,17 @@ void RerollDiceNode::setValidator(Validator* val) { m_validator = val; } -QString RerollDiceNode::toString() const +QString RerollDiceNode::toString(bool wl) const { - return QString("RerollDiceNode [label=\"RerollDiceNode validatior:%1\"").arg(m_validator->toString()); + if(wl) + { + return QString("%1 [label=\"RerollDiceNode validatior: %2\"]").arg(m_id).arg(m_validator->toString()); + } + else + { + return m_id; + } + //return QString("RerollDiceNode [label=\"RerollDiceNode validatior:%1\""); } void RerollDiceNode::setAddingMode(bool b) { diff --git a/node/rerolldicenode.h b/node/rerolldicenode.h index a97e448..f456bb3 100644 --- a/node/rerolldicenode.h +++ b/node/rerolldicenode.h @@ -39,7 +39,7 @@ public: * @brief toString * @return */ - virtual QString toString()const; + virtual QString toString(bool )const; /** diff --git a/node/scalaroperatornode.cpp b/node/scalaroperatornode.cpp index d532df0..f69cb01 100644 --- a/node/scalaroperatornode.cpp +++ b/node/scalaroperatornode.cpp @@ -138,7 +138,7 @@ qint64 ScalarOperatorNode::multiple(qint64 a,qint64 b) { return a*b; } -QString ScalarOperatorNode::toString() const +QString ScalarOperatorNode::toString(bool wl) const { QString op=""; switch(m_operator) @@ -159,7 +159,14 @@ QString ScalarOperatorNode::toString() const break; } - return QString("ScalarOperatorNode [label=\"ScalarOperatorNode %1\"").arg(op); + if(wl) + { + return QString("%1 [label=\"ScalarOperatorNode %2\"]").arg(m_id).arg(op); + } + else + { + return m_id; + } } qint64 ScalarOperatorNode::getPriority() const { @@ -174,27 +181,31 @@ qint64 ScalarOperatorNode::getPriority() const } void ScalarOperatorNode::generateDotTree(QString& s) { - s.append(toString()); + s.append(toString(true)); + s.append(";\n"); + if(NULL!=m_nextNode) { + s.append(toString(false)); s.append(" -> "); - s.append(m_nextNode->toString()); - s.append(" [label=\"nextNode\"];\n"); + s.append(m_nextNode->toString(false)); + s.append(";\n"); m_nextNode->generateDotTree(s); } else { + s.append(toString(false)); s.append(" -> "); - s.append("NULL"); - s.append(" [label=\"nextNode\"];\n"); + s.append("NULL"); + s.append(" [label=\"nextNode\"];\n"); } QString str; str.append("\n"); if(NULL!=m_internalNode) { - str.append(toString()); + str.append(toString(false)); str.append(" -> "); - str.append(m_internalNode->toString()); + str.append(m_internalNode->toString(false)); str.append(" [label=\"internalNode\"];\n"); m_internalNode->generateDotTree(str); } diff --git a/node/scalaroperatornode.h b/node/scalaroperatornode.h index e43f6f8..ea0f7e1 100644 --- a/node/scalaroperatornode.h +++ b/node/scalaroperatornode.h @@ -42,7 +42,7 @@ public: bool setOperatorChar(QChar c); void setInternalNode(ExecutionNode* node); - virtual QString toString()const; + virtual QString toString(bool wl)const; virtual qint64 getPriority() const; void generateDotTree(QString& s); @@ -60,9 +60,9 @@ private: private: ScalarOperator m_operator; + ScalarResult* m_scalarResult; ExecutionNode* m_internalNode; QMap<QChar,ScalarOperator> m_scalarOperationList; - ScalarResult* m_scalarResult; }; #endif // SCALAROPERATORNODE_H diff --git a/node/sortresult.cpp b/node/sortresult.cpp index 421beb3..d149507 100644 --- a/node/sortresult.cpp +++ b/node/sortresult.cpp @@ -105,9 +105,17 @@ void SortResultNode::setSortAscending(bool asc) { m_ascending = asc; } -QString SortResultNode::toString() const +QString SortResultNode::toString(bool wl) const { - return QString("SortResultNode [label=\"SortResultNode %1\"").arg(m_ascending ? "Ascending":"Descending"); + if(wl) + { + return QString("%1 [label=\"SortResultNode %2\"]").arg(m_id).arg(m_ascending ? "Ascending":"Descending"); + } + else + { + return m_id; + } + } qint64 SortResultNode::getPriority() const { diff --git a/node/sortresult.h b/node/sortresult.h index d505e4b..c4f96dc 100644 --- a/node/sortresult.h +++ b/node/sortresult.h @@ -49,7 +49,7 @@ public: * @brief toString * @return */ - virtual QString toString()const; + virtual QString toString(bool wl)const; /** * @brief getPriority * @return diff --git a/node/startingnode.cpp b/node/startingnode.cpp index 7e13f5d..5f1966e 100644 --- a/node/startingnode.cpp +++ b/node/startingnode.cpp @@ -32,10 +32,19 @@ void StartingNode::run(ExecutionNode*) m_nextNode->run(this); } } -QString StartingNode::toString() const +QString StartingNode::toString(bool withlabel) const { - return "StartingNode [shape=box]"; + if(withlabel) + { + return QString("%1 [label=\"StartingNode\"]").arg(m_id); + } + else + { + return m_id; + } } + + qint64 StartingNode::getPriority() const { qint64 priority=0; diff --git a/node/startingnode.h b/node/startingnode.h index 1edebc9..923e84b 100644 --- a/node/startingnode.h +++ b/node/startingnode.h @@ -41,7 +41,7 @@ public: * @brief toString * @return */ - virtual QString toString()const; + virtual QString toString(bool withlabel)const; /** * @brief getPriority * @return diff --git a/parsingtoolbox.cpp b/parsingtoolbox.cpp index 2d26d79..d511e44 100644 --- a/parsingtoolbox.cpp +++ b/parsingtoolbox.cpp @@ -33,6 +33,12 @@ ParsingToolBox::ParsingToolBox() m_logicOp->insert("<",BooleanCondition::LesserThan); m_logicOp->insert("=",BooleanCondition::Equal); m_logicOp->insert(">",BooleanCondition::GreaterThan); + + + m_logicOperation = new QMap<QString,CompositeValidator::LogicOperation>(); + m_logicOperation->insert("|",CompositeValidator::OR); + m_logicOperation->insert("^",CompositeValidator::EXCLUSIVE_OR); + m_logicOperation->insert("&",CompositeValidator::AND); } ExecutionNode* ParsingToolBox::addSort(ExecutionNode* e,bool b) { @@ -74,28 +80,23 @@ ParsingToolBox::~ParsingToolBox() Validator* ParsingToolBox::readValidator(QString& str) { Validator* returnVal=NULL; - bool expectSquareBrasket=false; - if((str.startsWith("["))) - { - str=str.remove(0,1); - expectSquareBrasket = true; - } + + bool isOk = true; + BooleanCondition::LogicOperator myLogicOp = BooleanCondition::Equal; - //bool hasReadLogicOperator = - readLogicOperator(str,myLogicOp); - int value=0; + bool hasReadLogicOperator = readLogicOperator(str,myLogicOp); + qint64 value=0; if(readNumber(str,value)) { - bool isOk = true; if(str.startsWith("-")) { str=str.remove(0,1); - int end=0; + qint64 end=0; if(readNumber(str,end)) { - if(expectSquareBrasket) + /* if(expectSquareBrasket) { if(str.startsWith("]")) { @@ -106,7 +107,7 @@ Validator* ParsingToolBox::readValidator(QString& str) { isOk=false; } - } + }*/ if(isOk) { str=str.remove(0,1); @@ -118,12 +119,12 @@ Validator* ParsingToolBox::readValidator(QString& str) } else { - if((expectSquareBrasket)&&(str.startsWith("]"))) + /* if((expectSquareBrasket)&&(str.startsWith("]"))) { str=str.remove(0,1); isOk=true; - } - if(isOk) + }*/ + //if(isOk) { BooleanCondition* condition = new BooleanCondition(); condition->setValue(value); @@ -134,14 +135,92 @@ Validator* ParsingToolBox::readValidator(QString& str) } return returnVal; } -bool ParsingToolBox::readNumber(QString& str, int& myNumber) +Validator* ParsingToolBox::readCompositeValidator(QString& str) +{ + bool expectSquareBrasket=false; + if((str.startsWith("["))) + { + str=str.remove(0,1); + expectSquareBrasket = true; + } + + Validator* tmp = readValidator(str); + CompositeValidator::LogicOperation opLogic; + + QVector<CompositeValidator::LogicOperation>* operators = new QVector<CompositeValidator::LogicOperation>(); + QList<Validator*>* validatorList = new QList<Validator*>(); + + while(NULL!=tmp) + { + bool hasOperator = readLogicOperation(str,opLogic); + if( hasOperator ) + { + operators->append(opLogic); + validatorList->append(tmp); + tmp = readValidator(str); + } + else + { + if((expectSquareBrasket)&&(str.startsWith("]"))) + { + str=str.remove(0,1); + //isOk=true; + } + + if(!validatorList->isEmpty()) + { + validatorList->append(tmp); + } + else + { + return tmp; + } + tmp = NULL; + } + + } + if(!validatorList->isEmpty()) + { + CompositeValidator* validator = new CompositeValidator(); + validator->setOperationList(operators); + validator->setValidatorList(validatorList); + return validator; + } + else + { + return NULL; + } +} +bool ParsingToolBox::readLogicOperation(QString& str,CompositeValidator::LogicOperation& op) +{ + QString longKey; + foreach(QString tmp, m_logicOperation->keys()) + { + if(str.startsWith(tmp)) + { + if(longKey.size()<tmp.size()) + { + longKey = tmp; + } + } + } + if(longKey.size()>0) + { + str=str.remove(0,longKey.size()); + op = m_logicOperation->value(longKey); + return true; + } + + return false; +} + +bool ParsingToolBox::readNumber(QString& str, qint64& myNumber) { if(str.isEmpty()) return false; QString number; int i=0; - while(i<str.length() && ((str[i].isNumber()) || ( (i==0) && (str[i]=='-')))) { number+=str[i]; @@ -152,7 +231,7 @@ bool ParsingToolBox::readNumber(QString& str, int& myNumber) return false; bool ok; - myNumber = number.toInt(&ok); + myNumber = number.toLongLong(&ok); if(ok) { str=str.remove(0,number.size()); @@ -180,17 +259,18 @@ bool ParsingToolBox::readCloseParentheses(QString& str) else return false; } -bool ParsingToolBox::readList(QString& str,QStringList& list) +bool ParsingToolBox::readList(QString& str,QStringList& list,QList<Range>& ranges) { if(str.startsWith("[")) { str=str.remove(0,1); - int pos = str.indexOf("]"); + int pos = str.lastIndexOf("]"); if(-1!=pos) { QString liststr = str.left(pos); list = liststr.split(","); str=str.remove(0,pos+1); + readProbability(list,ranges); return true; } } @@ -208,8 +288,6 @@ bool ParsingToolBox::readAscending(QString& str) return true; } return false; - - } bool ParsingToolBox::isValidValidator(ExecutionNode* previous, Validator* val) { @@ -235,7 +313,7 @@ DiceRollerNode* ParsingToolBox::getDiceRollerNode(ExecutionNode* previous) previous = previous->getPreviousNode(); } } -bool ParsingToolBox::readDiceRange(QString& str,int& start, int& end) +bool ParsingToolBox::readDiceRange(QString& str,qint64& start, qint64& end) { bool expectSquareBrasket=false; @@ -272,3 +350,82 @@ bool ParsingToolBox::readDiceRange(QString& str,int& start, int& end) } } +ParsingToolBox::LIST_OPERATOR ParsingToolBox::readListOperator(QString& str) +{ + + if(str.startsWith('u')) + { + return UNIQUE; + } + return NONE; +} +void ParsingToolBox::readProbability(QStringList& str,QList<Range>& ranges) +{ + quint64 totalDistance=0; + quint64 undefDistance = 0; + int undefCount=0; + int maxValue = 0; + int i=0; + int j=0; + //range + foreach(QString line,str) + { + int pos = line.indexOf('['); + if(-1!=pos) + { + QString range = line.right(line.length()-pos); + line = line.left(pos); + str[j]=line; + qint64 start; + qint64 end; + if(readDiceRange(range,start,end)) + { + Range range; + range.setValue(start,end); + ranges.append(range); + totalDistance += end-start+1; + ++i; + } + else + { + Range range; + range.setStart(start); + ranges.append(range); + ++undefCount; + undefDistance +=start; + } + if((end>maxValue)||(i==1)) + { + maxValue = end; + } + } + ++j; + + } + + qint64 totalDistPourcent = totalDistance * undefDistance / (100-undefDistance); + + if(totalDistPourcent<undefCount) + { + totalDistPourcent = undefCount; + } + + for(int i = 0; i< ranges.size(); ++i) + { + Range tmp = ranges.at(i); + if(!tmp.isFullyDefined()) + { + int dist = tmp.getStart(); + tmp.setStart(maxValue+1); + maxValue+=1; + double truc = undefDistance*1.0/dist; + + tmp.setEnd(maxValue+(truc*totalDistPourcent)); + maxValue = maxValue+(truc*totalDistPourcent); + ranges[i]=tmp; + } + } + + + +} diff --git a/parsingtoolbox.h b/parsingtoolbox.h index 5d38919..3d37f7b 100644 --- a/parsingtoolbox.h +++ b/parsingtoolbox.h @@ -27,6 +27,7 @@ #include "node/executionnode.h" #include "node/dicerollernode.h" #include "booleancondition.h" +#include "compositevalidator.h" #include "range.h" /** @@ -36,6 +37,7 @@ class ParsingToolBox { public: + enum LIST_OPERATOR {NONE,UNIQUE}; /** * @brief ParsingToolBox */ @@ -70,7 +72,12 @@ public: * @return */ Validator* readValidator(QString& str); - + /** + * @brief readCompositeValidator + * @param str + * @return + */ + Validator* readCompositeValidator(QString& str); /** * @brief readNumber read number in the given str and remove from the string the read character. @@ -78,7 +85,7 @@ public: * @param myNumber reference to the found number * @return true, succeed to read number, false otherwise. */ - bool readNumber(QString& str, int& myNumber); + bool readNumber(QString& str, qint64& myNumber); /** @@ -100,7 +107,7 @@ public: * @param list * @return */ - bool readList(QString& str,QStringList& list); + bool readList(QString& str,QStringList& list, QList<Range>& ranges); /** * @brief isValidValidator * @param previous @@ -122,10 +129,21 @@ public: * @param end * @return */ - bool readDiceRange(QString& str,int& start, int& end); + bool readDiceRange(QString& str,qint64& start, qint64& end); + /** + * @brief readListOperator + * @param str + * @return + */ + LIST_OPERATOR readListOperator(QString& str); + + void readProbability(QStringList& str,QList<Range>& ranges); + + bool readLogicOperation(QString& str,CompositeValidator::LogicOperation& op); private: QMap<QString,BooleanCondition::LogicOperator>* m_logicOp; + QMap<QString,CompositeValidator::LogicOperation>* m_logicOperation; }; #endif // PARSINGTOOLBOX_H @@ -22,6 +22,7 @@ #include "range.h" Range::Range() + : m_hasEnd(false),m_hasStart(false) { @@ -30,6 +31,9 @@ void Range::setValue(qint64 s,qint64 e) { m_start = s; m_end=e; + + m_hasEnd = true; + m_hasStart = true; } qint64 Range::hasValid(Die* m,bool recursive, bool unhighlight) const @@ -59,8 +63,31 @@ QString Range::toString() { return QString("[%1-%2]").arg(m_start).arg(m_end); } -quint8 Range::getValidRangeSize(quint64 faces) const +quint64 Range::getValidRangeSize(quint64 faces) const { Q_UNUSED(faces); return m_end-m_start; } +void Range::setStart(qint64 start) +{ + m_start = start; + m_hasStart = true; +} +void Range::setEnd(qint64 end) +{ + m_end = end; + m_hasEnd = true; +} + +bool Range::isFullyDefined() +{ + return (m_hasEnd & m_hasStart); +} +qint64 Range::getStart() const +{ + return m_start; +} +qint64 Range::getEnd() const +{ + return m_end; +} @@ -33,15 +33,22 @@ class Range : public Validator public: Range(); void setValue(qint64,qint64); - + void setStart(qint64); + void setEnd(qint64); virtual qint64 hasValid(Die* b,bool recursive,bool unlight = false) const; virtual QString toString(); - virtual quint8 getValidRangeSize(quint64 faces) const; + virtual quint64 getValidRangeSize(quint64 faces) const; + + bool isFullyDefined(); + qint64 getStart() const; + qint64 getEnd() const; private: qint64 m_start; qint64 m_end; + bool m_hasEnd; + bool m_hasStart; }; #endif // RANGE_H diff --git a/result/diceresult.cpp b/result/diceresult.cpp index 95122f1..ceb77b8 100644 --- a/result/diceresult.cpp +++ b/result/diceresult.cpp @@ -54,7 +54,6 @@ QVariant DiceResult::getResult(RESULT_TYPE type) break; case DICE_LIST: { - return QVariant(); break; } @@ -82,12 +81,19 @@ qreal DiceResult::getScalarResult() return 0; } -QString DiceResult::toString() +QString DiceResult::toString(bool wl) { QStringList scalarSum; foreach(Die* die,m_diceValues) { scalarSum << QString::number(die->getValue()); } - return QString("DiceResult_Value_%1_dice_%2").arg(getScalarResult()).arg(scalarSum.join('_')); + if(wl) + { + return QString("%3 [label=\"DiceResult Value %1 dice %2\"]").arg(getScalarResult()).arg(scalarSum.join('_')).arg(m_id); + } + else + { + return m_id; + } } diff --git a/result/diceresult.h b/result/diceresult.h index 07378d1..84a4621 100644 --- a/result/diceresult.h +++ b/result/diceresult.h @@ -65,7 +65,7 @@ public: * @brief toString * @return */ - virtual QString toString(); + virtual QString toString(bool wl); private: qreal getScalarResult(); diff --git a/result/result.cpp b/result/result.cpp index 161cfea..7b6633c 100644 --- a/result/result.cpp +++ b/result/result.cpp @@ -20,9 +20,10 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include "result.h" +#include <QUuid> Result::Result() - : m_previous(NULL),m_resultTypes(STRING) + : m_previous(NULL),m_id(QString("\"%1\"").arg(QUuid::createUuid().toString())) { } @@ -47,22 +48,23 @@ bool Result::hasResultOfType(RESULT_TYPE type) const } void Result::generateDotTree(QString& s) { - s.append(toString()); + s.append(toString(true)); + s.append(";\n"); + if(NULL!=m_previous) { + s.append(toString(false)); s.append(" -> "); - s.append(m_previous->toString()); - s.append(" [label=\"previousResult\"];\n"); + s.append(m_previous->toString(true)); + s.append("\n"); m_previous->generateDotTree(s); } { + s.append(toString(false)); s.append(" -> "); - s.append("NULL"); + s.append("NULL"); s.append(" [label=\"previousResult\"];\n"); } } -/*QString Result::toString() -{ - return QString(); -}*/ + diff --git a/result/result.h b/result/result.h index 3d635bc..3f16535 100644 --- a/result/result.h +++ b/result/result.h @@ -77,9 +77,10 @@ public: * @brief toString * @return */ - virtual QString toString() = 0; + virtual QString toString(bool wl) = 0; protected: int m_resultTypes;/// @brief + QString m_id; private: Result* m_previous;/// @brief diff --git a/result/scalarresult.cpp b/result/scalarresult.cpp index a998608..9168938 100644 --- a/result/scalarresult.cpp +++ b/result/scalarresult.cpp @@ -36,7 +36,14 @@ QVariant ScalarResult::getResult(Result::RESULT_TYPE type) return m_value; } -QString ScalarResult::toString() +QString ScalarResult::toString(bool wl) { - return QString("ScalarResult_Value_%1").arg(m_value); + if(wl) + { + return QString("%2 [label=\"ScalarResult %1\"]").arg(m_value).arg(m_id); + } + else + { + return m_id; + } } diff --git a/result/scalarresult.h b/result/scalarresult.h index 8a0fa11..f441c9b 100644 --- a/result/scalarresult.h +++ b/result/scalarresult.h @@ -49,7 +49,7 @@ public: * @brief toString * @return */ - virtual QString toString(); + virtual QString toString(bool); private: qreal m_value; diff --git a/result/stringresult.cpp b/result/stringresult.cpp index 474ae23..2dff0ac 100644 --- a/result/stringresult.cpp +++ b/result/stringresult.cpp @@ -32,9 +32,16 @@ QVariant StringResult::getResult(RESULT_TYPE type) return QVariant(); } -QString StringResult::toString() -{ - return QString("StringResult_value_%1").arg(getText().replace(" ","_")); +QString StringResult::toString(bool wl) +{ + if(wl) + { + return QString("%2 [label=\"StringResult_value_%1\"]").arg(getText().replace(" ","_")).arg(m_id); + } + else + { + return m_id; + } } void StringResult::setHighLight(bool b) { diff --git a/result/stringresult.h b/result/stringresult.h index 6819aaa..cdd7de2 100644 --- a/result/stringresult.h +++ b/result/stringresult.h @@ -36,7 +36,7 @@ public: * @brief toString * @return */ - virtual QString toString(); + virtual QString toString(bool); virtual void setHighLight(bool ); virtual bool hasHighLight() const; diff --git a/validator.cpp b/validator.cpp index 19c2bfb..d532fdb 100644 --- a/validator.cpp +++ b/validator.cpp @@ -24,3 +24,7 @@ Validator::Validator() { } +Validator::~Validator() +{ + +} diff --git a/validator.h b/validator.h index 6dd8292..4728065 100644 --- a/validator.h +++ b/validator.h @@ -37,6 +37,10 @@ public: */ Validator(); /** + * @brief ~Validator + */ + virtual ~Validator(); + /** * @brief hasValid * @param b * @param recursive @@ -54,7 +58,7 @@ public: * @param faces * @return */ - virtual quint8 getValidRangeSize(quint64 faces) const = 0 ; + virtual quint64 getValidRangeSize(quint64 faces) const = 0 ; }; #endif // VALIDATOR_H |