2013-05-06 12:13:49 +02:00
|
|
|
/*
|
|
|
|
|
Copyright 2013 Statoil ASA.
|
|
|
|
|
|
|
|
|
|
This file is part of the Open Porous Media project (OPM).
|
|
|
|
|
|
|
|
|
|
OPM 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 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
OPM 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 OPM. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
#ifndef PARSER_ITEM_H
|
|
|
|
|
#define PARSER_ITEM_H
|
|
|
|
|
|
|
|
|
|
#include <string>
|
2013-05-07 21:55:49 +02:00
|
|
|
#include <sstream>
|
2013-05-06 12:13:49 +02:00
|
|
|
#include <iostream>
|
2013-08-23 15:39:08 +02:00
|
|
|
#include <deque>
|
2013-05-13 16:41:09 +02:00
|
|
|
|
2013-12-02 13:07:01 +01:00
|
|
|
#include <memory>
|
2013-05-13 16:41:09 +02:00
|
|
|
#include <boost/lexical_cast.hpp>
|
2013-05-06 12:13:49 +02:00
|
|
|
|
2013-07-31 16:15:22 +02:00
|
|
|
#include <opm/json/JsonObject.hpp>
|
|
|
|
|
|
2013-05-09 12:17:00 +02:00
|
|
|
#include <opm/parser/eclipse/Parser/ParserEnums.hpp>
|
2013-05-13 16:41:09 +02:00
|
|
|
#include <opm/parser/eclipse/RawDeck/RawRecord.hpp>
|
2013-05-24 10:09:59 +02:00
|
|
|
#include <opm/parser/eclipse/Deck/DeckItem.hpp>
|
2014-08-21 14:41:49 +02:00
|
|
|
#include <opm/parser/eclipse/RawDeck/StarToken.hpp>
|
2013-05-06 12:13:49 +02:00
|
|
|
|
|
|
|
|
namespace Opm {
|
2014-12-08 16:34:28 +01:00
|
|
|
|
2013-12-09 20:19:29 +01:00
|
|
|
class ParserItem {
|
|
|
|
|
public:
|
|
|
|
|
ParserItem(const std::string& itemName);
|
|
|
|
|
ParserItem(const std::string& itemName, ParserItemSizeEnum sizeType);
|
2013-12-17 15:56:12 +01:00
|
|
|
explicit ParserItem(const Json::JsonObject& jsonConfig);
|
2013-12-11 13:24:27 +01:00
|
|
|
|
|
|
|
|
virtual void push_backDimension(const std::string& dimension);
|
2013-12-10 17:34:52 +01:00
|
|
|
virtual const std::string& getDimension(size_t index) const;
|
2013-12-14 10:11:59 +01:00
|
|
|
virtual DeckItemPtr scan(RawRecordPtr rawRecord) const = 0;
|
2013-12-09 21:12:27 +01:00
|
|
|
virtual bool hasDimension() const;
|
2013-12-10 17:34:52 +01:00
|
|
|
virtual size_t numDimensions() const;
|
2013-12-09 20:19:29 +01:00
|
|
|
const std::string& name() const;
|
|
|
|
|
ParserItemSizeEnum sizeType() const;
|
2014-01-10 10:57:35 +01:00
|
|
|
std::string getDescription() const;
|
2014-04-10 22:29:18 +02:00
|
|
|
bool scalar() const;
|
2014-01-10 10:57:35 +01:00
|
|
|
void setDescription(std::string helpText);
|
2014-01-09 13:33:32 +01:00
|
|
|
|
2014-09-13 11:02:23 +02:00
|
|
|
virtual void inlineNew(std::ostream& /* os */) const = 0;
|
|
|
|
|
|
2013-12-09 20:19:29 +01:00
|
|
|
virtual ~ParserItem() {
|
|
|
|
|
}
|
2014-09-13 11:02:23 +02:00
|
|
|
|
|
|
|
|
virtual bool equal(const ParserItem& other) const = 0;
|
|
|
|
|
|
2013-12-09 20:19:29 +01:00
|
|
|
protected:
|
2014-09-13 11:02:23 +02:00
|
|
|
template <class T>
|
|
|
|
|
bool parserRawItemEqual(const ParserItem &other) const {
|
|
|
|
|
const T * lhs = dynamic_cast<const T*>(this);
|
|
|
|
|
const T * rhs = dynamic_cast<const T*>(&other);
|
|
|
|
|
if (!lhs || !rhs)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (lhs->name() != rhs->name())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (lhs->getDescription() != rhs->getDescription())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (lhs->sizeType() != rhs->sizeType())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (lhs->m_defaultSet != rhs->m_defaultSet)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// we only care that the default value is equal if it was
|
|
|
|
|
// specified...
|
|
|
|
|
if (lhs->m_defaultSet && lhs->getDefault() != rhs->getDefault())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-09 20:19:29 +01:00
|
|
|
bool m_defaultSet;
|
2013-05-13 16:41:09 +02:00
|
|
|
|
2013-12-09 20:19:29 +01:00
|
|
|
private:
|
|
|
|
|
std::string m_name;
|
|
|
|
|
ParserItemSizeEnum m_sizeType;
|
2014-01-10 10:57:35 +01:00
|
|
|
std::string m_description;
|
2013-12-09 20:19:29 +01:00
|
|
|
};
|
2013-05-24 10:09:59 +02:00
|
|
|
|
2013-12-09 20:19:29 +01:00
|
|
|
typedef std::shared_ptr<const ParserItem> ParserItemConstPtr;
|
|
|
|
|
typedef std::shared_ptr<ParserItem> ParserItemPtr;
|
2013-10-16 09:59:32 +02:00
|
|
|
|
2014-08-21 14:41:49 +02:00
|
|
|
/// Scans the rawRecords data according to the ParserItems definition.
|
|
|
|
|
/// returns a DeckItem object.
|
|
|
|
|
/// NOTE: data are popped from the rawRecords deque!
|
2014-09-11 16:23:17 +02:00
|
|
|
template<typename ParserItemType , typename DeckItemType , typename ValueType>
|
2014-08-21 14:41:49 +02:00
|
|
|
DeckItemPtr ParserItemScan(const ParserItemType * self , RawRecordPtr rawRecord) {
|
|
|
|
|
std::shared_ptr<DeckItemType> deckItem = std::make_shared<DeckItemType>( self->name() , self->scalar() );
|
2014-12-08 16:34:28 +01:00
|
|
|
|
2014-09-11 16:23:17 +02:00
|
|
|
if (self->sizeType() == ALL) {
|
2014-08-21 14:41:49 +02:00
|
|
|
while (rawRecord->size() > 0) {
|
|
|
|
|
std::string token = rawRecord->pop_front();
|
2014-09-11 13:05:32 +02:00
|
|
|
|
|
|
|
|
std::string countString;
|
|
|
|
|
std::string valueString;
|
|
|
|
|
if (isStarToken(token, countString, valueString)) {
|
2014-09-18 14:47:05 +02:00
|
|
|
StarToken st(token, countString, valueString);
|
2014-09-11 16:23:17 +02:00
|
|
|
ValueType value;
|
|
|
|
|
|
2014-08-25 10:45:44 +02:00
|
|
|
if (st.hasValue()) {
|
2014-09-18 14:47:05 +02:00
|
|
|
value = readValueToken<ValueType>(st.valueString());
|
2014-09-11 13:05:32 +02:00
|
|
|
deckItem->push_backMultiple( value , st.count());
|
2014-08-25 10:45:44 +02:00
|
|
|
} else {
|
2014-09-12 12:23:37 +02:00
|
|
|
value = self->getDefault();
|
2014-09-11 13:05:32 +02:00
|
|
|
for (size_t i=0; i < st.count(); i++)
|
2014-09-12 12:23:37 +02:00
|
|
|
deckItem->push_backDefault( value );
|
2014-08-25 10:45:44 +02:00
|
|
|
}
|
2014-08-21 14:41:49 +02:00
|
|
|
} else {
|
2014-09-11 16:23:17 +02:00
|
|
|
ValueType value = readValueToken<ValueType>(token);
|
2014-08-21 14:41:49 +02:00
|
|
|
deckItem->push_back(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2014-12-19 19:02:53 +01:00
|
|
|
if (rawRecord->size() == 0)
|
|
|
|
|
// if the record was ended prematurely, use the default value for the item...
|
|
|
|
|
deckItem->push_backDefault( self->getDefault() );
|
|
|
|
|
else {
|
2014-09-11 16:23:17 +02:00
|
|
|
// The '*' should be interpreted as a repetition indicator, but it must
|
|
|
|
|
// be preceeded by an integer...
|
2014-08-21 14:41:49 +02:00
|
|
|
std::string token = rawRecord->pop_front();
|
2014-09-11 13:05:32 +02:00
|
|
|
std::string countString;
|
|
|
|
|
std::string valueString;
|
|
|
|
|
if (isStarToken(token, countString, valueString)) {
|
2014-09-18 14:47:05 +02:00
|
|
|
StarToken st(token, countString, valueString);
|
2014-09-11 16:23:17 +02:00
|
|
|
|
2014-12-19 19:02:53 +01:00
|
|
|
if (!st.hasValue())
|
|
|
|
|
deckItem->push_backDefault( self->getDefault() );
|
|
|
|
|
else
|
2014-09-18 14:47:05 +02:00
|
|
|
deckItem->push_back(readValueToken<ValueType>(st.valueString()));
|
2014-09-11 16:23:17 +02:00
|
|
|
|
|
|
|
|
// replace the first occurence of "N*FOO" by a sequence of N-1 times
|
|
|
|
|
// "1*FOO". this is slightly hacky, but it makes it work if the
|
|
|
|
|
// number of defaults pass item boundaries...
|
|
|
|
|
std::string singleRepetition;
|
|
|
|
|
if (st.hasValue())
|
2014-09-11 13:05:32 +02:00
|
|
|
singleRepetition = st.valueString();
|
2014-09-11 16:23:17 +02:00
|
|
|
else
|
|
|
|
|
singleRepetition = "1*";
|
|
|
|
|
|
2014-09-11 13:05:32 +02:00
|
|
|
for (size_t i=0; i < st.count() - 1; i++)
|
2014-09-11 16:23:17 +02:00
|
|
|
rawRecord->push_front(singleRepetition);
|
2014-08-21 14:41:49 +02:00
|
|
|
} else {
|
2014-09-11 16:23:17 +02:00
|
|
|
ValueType value = readValueToken<ValueType>(token);
|
2014-08-21 14:41:49 +02:00
|
|
|
deckItem->push_back(value);
|
|
|
|
|
}
|
2014-08-25 10:45:44 +02:00
|
|
|
}
|
2014-08-21 14:41:49 +02:00
|
|
|
}
|
|
|
|
|
return deckItem;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-08 16:34:28 +01:00
|
|
|
|
2014-08-21 14:41:49 +02:00
|
|
|
|
2013-05-08 14:31:20 +02:00
|
|
|
}
|
2013-05-06 12:13:49 +02:00
|
|
|
|
2013-05-08 14:31:20 +02:00
|
|
|
#endif
|
2013-05-07 21:55:49 +02:00
|
|
|
|