git-svn-id: svn://db.shs.com.ru/libs@357 a8b55f48-bf90-11e4-a774-851b48703e85

This commit is contained in:
2018-02-13 13:59:11 +00:00
parent a9a05517a9
commit 540b358a08
26 changed files with 218 additions and 260 deletions

View File

@@ -1,6 +1,8 @@
project(cd_utils)
cmake_minimum_required(VERSION 2.6)
cmake_policy(SET CMP0017 OLD)
if (POLICY CMP0017)
cmake_policy(SET CMP0017 NEW)
endif()
if (NOT LIBPROJECT)
find_package(PIP REQUIRED)
option(LIB "System install" 1)

View File

@@ -91,13 +91,13 @@ CDCore::~CDCore() {
void CDCore::k_write(PIIODevice * d) {
k_.write(d, PIString());
k_.write(d, CDType::cdK, PIString());
}
void CDCore::k_read(PIIODevice * d) {
PIConfig conf(d, PIIODevice::ReadOnly);
k_.read(&(conf.rootEntry()));
k_.read(&(conf.rootEntry()), CDType::cdK);
initRoot(k_);
K_ChangedGlobal();
/*PIVector<PIIODevice * > ds = connection.allDevices();

View File

@@ -8,6 +8,8 @@ using namespace CDUtils;
//int cdtype_debug_cnt = 1;
const int cd_x_history_max_size = 4000;
CDType::CDType() {
index_ = -1;
value_d = 0.;
@@ -49,9 +51,28 @@ CDType::CDType(int i, const PIString & n, const PIString & t, const PIString & v
}
CDType &CDType::operator =(double x) {
value_d = x;
if (mode_ == X_All_Avg) {
avg_h << x;
double val = 0;
if (avg_h.size_s() >= avg_size) {
for (int i=0; i<avg_h.size_s(); i++) {
val += avg_h[i];
}
val /= avg_h.size();
avg_h.pop_front();
}
if (history.size() < cd_x_history_max_size)
history << val;
}
return *this;
}
PIString CDType::type() const {
if (type_.trimmed().isEmpty()) return "f";
// piCout << "type =" << type_.trimmed() << ";" << type_ << "#";
// piCout << "type =" << type_.trimmed() << ";" << type_ << "#";
return type_;
}
@@ -206,7 +227,7 @@ PIVariantTypes::Enum CDType::parseEnumComment(PIString c) {
int CDSection::count(bool recursive) const {
int ret = k.size_s();
int ret = cd.size_s();
if (recursive) {
PIMap<int, CDSection>::const_iterator i;
for (i = s.begin(); i != s.end(); ++i)
@@ -224,7 +245,7 @@ int CDSection::sectionsCount() const {
PIStringList CDSection::index_names() const {
PIStringList ret;
PIMap<int, CDType>::const_iterator i;
for (i = k.begin(); i != k.end(); ++i)
for (i = cd.begin(); i != cd.end(); ++i)
ret << i->second.name();
return ret;
}
@@ -275,13 +296,13 @@ CDType & CDSection::getByName(const PIString & name_) {
int dv = 0;
if (isd) dv = np.back().toInt();
//piCout << np.back() << isd << dv;
for (it = cs->k.begin(); it != cs->k.end(); ++it) {
for (it = cs->cd.begin(); it != cs->cd.end(); ++it) {
bool f = false;
if (isd) f = (dv == it.key());
else f = (np.back() == it.value().name());
//piCout << "k..." << it.key() << it.value().name() << f;
if (f)
return cs->k[it.key()];
return cs->cd[it.key()];
}
return null;
}
@@ -297,13 +318,15 @@ CDType & CDSection::getByPath(const PIDeque<int> & path_) {
}
void CDSection::write(PIIODevice * d, const PIString & prefix) {
void CDSection::write(PIIODevice * d, CDType::cdT cdt, const PIString & prefix) {
if (!d) return;
if (k.isEmpty() && s.isEmpty()) return;
if (cd.isEmpty() && s.isEmpty()) return;
// piCout << "[CDSection] write start";
PIString l;
if (prefix.isEmpty()) l = "[k]";
else l = "[" + prefix + ".k]";
PIStringList cdtl;
cdtl << "null" << "k" << "x" << "c";
if (prefix.isEmpty()) l = "[" + cdtl[cdt] + "]";
else l = "[" + prefix + "." + cdtl[cdt] + "]";
l += "\n";
d->write(l.toUTF8());
l = "name = " + name + " \n";
@@ -311,20 +334,38 @@ void CDSection::write(PIIODevice * d, const PIString & prefix) {
l = "alias = " + alias + " \n";
d->write(l.toUTF8());
PIMap<int, CDType>::iterator i;
for (i = k.begin(); i != k.end(); ++i) {
for (i = cd.begin(); i != cd.end(); ++i) {
CDType & ck(i.value());
l.clear(); l << ck.index() << ".f = " << ck.formula() << " #s " << ck.comment() << " \n";
d->write(l.toUTF8());
l.clear(); l << ck.index() << ".v = " << ck.value() << " #" << ck.type() << " " << ck.name() << " \n";
d->write(l.toUTF8());
if (!ck.enumValues().enum_list.isEmpty()) {
l.clear(); l << ck.index() << ".ev = {";
//PIVector<CDType::Enumerator> el = ck.enumValues();
piForeachC (PIVariantTypes::Enumerator & e, ck.enumValues().enum_list)
l << e.value << " - " << e.name << ", ";
l.cutRight(2);
l << "} \n";
if (ck.cd_type() != cdt) continue;
switch (cdt) {
case CDType::cdNull: break;
case CDType::cdK:
l.clear(); l << ck.index() << ".f = " << ck.formula() << " #s " << ck.comment() << " \n";
d->write(l.toUTF8());
l.clear(); l << ck.index() << ".v = " << ck.value() << " #" << ck.type() << " " << ck.name() << " \n";
d->write(l.toUTF8());
if (!ck.enumValues().enum_list.isEmpty()) {
l.clear(); l << ck.index() << ".ev = {";
//PIVector<CDType::Enumerator> el = ck.enumValues();
piForeachC (PIVariantTypes::Enumerator & e, ck.enumValues().enum_list)
l << e.value << " - " << e.name << ", ";
l.cutRight(2);
l << "} \n";
d->write(l.toUTF8());
}
break;
case CDType::cdX:
l.clear(); l << ck.index() << ".name = " << ck.name() << " #s " << ck.comment() << " \n";
d->write(l.toUTF8());
l.clear(); l << ck.index() << ".mode = " << ck.xmode() << " #e (0 - cur, 1 - all_avg) " << "\n";
d->write(l.toUTF8());
l.clear(); l << ck.index() << ".avg = " << ck.avg() << " #n " << "\n";
d->write(l.toUTF8());
break;
case CDType::cdC:
l.clear(); l << ck.index() << ".name = " << ck.name() << " #s " << ck.comment() << " \n";
d->write(l.toUTF8());
break;
}
}
if (!s.isEmpty()) {
@@ -332,7 +373,7 @@ void CDSection::write(PIIODevice * d, const PIString & prefix) {
else l = prefix + ".s";
PIMap<int, CDSection>::iterator j;
for (j = s.begin(); j != s.end(); ++j) {
j.value().write(d, l + "." + PIString::fromNumber(j.key()));
j.value().write(d, cdt, l + "." + PIString::fromNumber(j.key()));
}
}
if (prefix.isEmpty()) {
@@ -343,34 +384,51 @@ void CDSection::write(PIIODevice * d, const PIString & prefix) {
}
void CDSection::read(const void * ep) {
void CDSection::read(const void * ep, CDType::cdT cdt) {
// piCout << "[CDSection] read start";
k.clear();
PIStringList cdtl;
cdtl << "null" << "k" << "x" << "c";
cd.clear();
s.clear();
PIConfig::Entry & e(*(PIConfig::Entry*)ep);
name = e.getValue("k.name").value();
alias = e.getValue("k.alias").value();
PIConfig::Entry & kl = e.getValue("k");
name = e.getValue(cdtl[cdt] + ".name").value();
alias = e.getValue(cdtl[cdt] + ".alias").value();
PIConfig::Entry & kl = e.getValue(cdtl[cdt]);
for (int i = 0; i < kl.childCount(); ++i) {
const PIConfig::Entry * ke(kl.child(i));
const PIConfig::Entry * e(kl.child(i));
bool ok = false;
int kid = ke->name().toInt(-1, &ok);
int id = e->name().toInt(-1, &ok);
// piCout << "[read]" << ke->name() << ke->value() << ok;
// PIString n = ke->getValue("v").comment();
// PIString t = n.takeLeft(1);
if (ok) {
CDType ck = CDType(kid, ke->getValue("v").comment(), ke->getValue("v").type(), ke->getValue("v").value(), ke->getValue("f").value(), ke->getValue("f").comment(), CDType::cdK);
PIString ev = ke->getValue("ev", "");
if (!ev.isEmpty())
ck.enum_values = ck.parseEnumComment(ev);
k[kid] = ck;
CDType c;
PIString ev;
switch (cdt) {
case CDType::cdNull: break;
case CDType::cdK:
c = CDType(id, e->getValue("v").comment(), e->getValue("v").type(), e->getValue("v").value(), e->getValue("f").value(), e->getValue("f").comment(), cdt);
ev = e->getValue("ev", "");
if (!ev.isEmpty())
c.enum_values = c.parseEnumComment(ev);
break;
case CDType::cdX:
c = CDType(id, e->getValue("name").value(), PIString(), PIString(), PIString() , e->getValue("name").comment(), cdt);
c.setXMode((CDType::XMode)e->getValue("mode").value().toInt());
c.setAvg((CDType::XMode)e->getValue("avg").value().toInt());
break;
case CDType::cdC:
c = CDType(id, e->getValue("name").value(), PIString(), PIString(), PIString() , e->getValue("name").comment(), cdt);
break;
}
cd[id] = c;
}
}
PIConfig::Entry & sl = e.getValue("s");
for (int i = 0; i < sl.childCount(); ++i) {
const PIConfig::Entry * se(sl.child(i));
int sid = se->name().toInt();
s[sid].read(se);
s[sid].read(se, cdt);
}
// piCout << "[CDSection] read end";
}
@@ -388,26 +446,26 @@ void CDSection::update(CDSection & v, UpdateModeFlags mode) {
PIMap<PIString, PIString> prev_k_f_bn;
PIMap<int, CDType>::iterator i;
if (mode[SaveByIndex]) {
for (i = k.begin(); i != k.end(); ++i)
for (i = cd.begin(); i != cd.end(); ++i)
prev_k_f_bi[i.key()] = i.value().formula();
}
if (mode[SaveByName]) {
for (i = k.begin(); i != k.end(); ++i)
for (i = cd.begin(); i != cd.end(); ++i)
prev_k_f_bn[i.value().name_] = i.value().formula();
}
if (!mode[Merge])
k.clear();
for (i = v.k.begin(); i != v.k.end(); ++i) {
cd.clear();
for (i = v.cd.begin(); i != v.cd.end(); ++i) {
int id = i.key();
PIString n = i.value().name();
k[id] = i.value();
cd[id] = i.value();
if (mode[SaveByIndex]) {
if (prev_k_f_bi.contains(id))
k[id].setFormula(prev_k_f_bi[id]);
cd[id].setFormula(prev_k_f_bi[id]);
}
if (mode[SaveByName]) {
if (prev_k_f_bn.contains(n))
k[id].setFormula(prev_k_f_bn[n]);
cd[id].setFormula(prev_k_f_bn[n]);
}
}
@@ -479,12 +537,12 @@ void CDSection::update(CDSection & v, UpdateModeFlags mode) {
bool CDSection::isSameStructure(CDSection & v) {
PIMap<PIString, int> k_ids;
PIMap<int, CDType>::iterator i;
for (i = k.begin(); i != k.end(); ++i)
for (i = cd.begin(); i != cd.end(); ++i)
k_ids[i.value().name()] = i.key();
for (i = v.k.begin(); i != v.k.end(); ++i) {
for (i = v.cd.begin(); i != v.cd.end(); ++i) {
if (!k_ids.contains(i.value().name())) continue;
//piCout << i.key() << k[i.key()].name() << i.value().name();
if (k[k_ids[i.value().name()]].index() != i.key())
if (cd[k_ids[i.value().name()]].index() != i.key())
return false;
}
PIMap<int, CDSection>::iterator j;
@@ -499,7 +557,7 @@ bool CDSection::isSameStructure(CDSection & v) {
void CDSection::prepareCalculate() {
PIMap<int, CDType>::iterator i;
for (i = k.begin(); i != k.end(); ++i) {
for (i = cd.begin(); i != cd.end(); ++i) {
i.value().parent = this;
i.value().calculated = false;
}
@@ -511,7 +569,7 @@ void CDSection::prepareCalculate() {
void CDSection::calculateRecursive(PIEvaluator * e) {
PIMap<int, CDType>::iterator i;
for (i = k.begin(); i != k.end(); ++i)
for (i = cd.begin(); i != cd.end(); ++i)
i.value().calculate(e);
PIMap<int, CDSection>::iterator j;
for (j = s.begin(); j != s.end(); ++j)
@@ -522,7 +580,7 @@ void CDSection::calculateRecursive(PIEvaluator * e) {
void CDSection::makePath(PIDeque<int> p) {
PIDeque<int> tp;
PIMap<int, CDType>::iterator i;
for (i = k.begin(); i != k.end(); ++i) {
for (i = cd.begin(); i != cd.end(); ++i) {
tp = p;
tp << i.key();
i.value().path_ = tp;
@@ -540,7 +598,7 @@ void CDSection::makePath(PIDeque<int> p) {
PIVector<CDType * > CDSection::children(bool recursive) const {
PIVector<CDType * > ret;
PIMap<int, CDType>::const_iterator i;
for (i = k.begin(); i != k.end(); ++i)
for (i = cd.begin(); i != cd.end(); ++i)
ret << const_cast<CDType * >(&(i.value()));
if (!recursive) return ret;
PIMap<int, CDSection>::const_iterator j;
@@ -553,14 +611,9 @@ PIVector<CDType * > CDSection::children(bool recursive) const {
PIVariantTypes::Enum CDSection::enumValues() const {
PIVariantTypes::Enum ret;
PIMap<int, CDType>::const_iterator i;
for (i = k.begin(); i != k.end(); ++i)
for (i = cd.begin(); i != cd.end(); ++i)
ret << PIVariantTypes::Enumerator(i.key(), i.value().name());
return ret;
}
XType::XType(const CDType & cdt) {
}

View File

@@ -27,8 +27,12 @@ class CDType {
friend class CDSection;
public:
enum cdT {cdNull, cdK, cdX, cdC};
enum XMode {X_Current, X_All_Avg};
CDType();
CDType(int i, const PIString & n, const PIString & t, const PIString & v, const PIString & f, const PIString & c, cdT cd_t);
CDType & operator =(double x);
int index() const {return index_;}
PIString name() const {return name_;}
PIString type() const;
@@ -47,6 +51,10 @@ public:
void setEnumValues(const PIVariantTypes::Enum & ev) {enum_values = ev;}
const PIString & errorString() const {return error_;}
PIDeque<int> path() const {return path_;}
void setXMode(XMode mode) {mode_ = mode;}
void setAvg(int avg) {avg_size = avg;}
XMode xmode() const {return mode_;}
int avg() const {return avg_size;}
protected:
bool calculate(PIEvaluator * e, PIVector<const CDType * > stack = PIVector<const CDType * >());
@@ -61,23 +69,8 @@ protected:
double value_d;
int value_i;
bool value_b, calculated;
};
class XType : public CDType {
friend class XSection;
public:
enum XMode {X_Current, X_All_Avg};
XType(const CDType & cdt = CDType());
void setXMode(XMode mode) {mode_ = mode;}
void setAvg(int avg) {avg_size = avg;}
double currentValue() const {return last_val;}
private:
PIVector<double> history;
double last_val;
PIVector <double> avg_h;
int avg_size;
XMode mode_;
};
@@ -92,10 +85,10 @@ public:
CDSection() {}
bool test(int v) {return k.value(v).toBool();}
bool test(int v) {return cd.value(v).toBool();}
// CDType & operator [](int v) {if (!k.contains(v)) k[v].index_ = v; return k[v];}
CDType & operator [](int v) {return k[v];}
const CDType operator [](int v) const {return k[v];}
CDType & operator [](int v) {return cd[v];}
const CDType operator [](int v) const {return cd[v];}
CDType & operator [](const PIString & name_) {return getByName(name_);}
const CDType operator [](const PIString & name_) const {return const_cast<CDSection*>(this)->getByName(name_);}
CDType & operator [](const PIDeque<int> & path_) {return getByPath(path_);}
@@ -103,10 +96,10 @@ public:
CDSection & section(int v) {return s[v];}
const CDSection section(int v) const {return s[v];}
bool isEmpty() const {return k.isEmpty() && s.isEmpty();}
bool isEmpty() const {return cd.isEmpty() && s.isEmpty();}
int count(bool recursive = true) const;
int sectionsCount() const;
PIVector<int> indexes() const {return k.keys();}
PIVector<int> indexes() const {return cd.keys();}
PIStringList index_names() const;
void calculate();
void makePath(PIDeque<int> p = PIDeque<int>());
@@ -118,26 +111,26 @@ public:
protected:
CDSection(PIMap<int, CDType> k_, PIMap<int, CDSection> s_) {
k = k_;
cd = k_;
s = s_;
}
CDType & getByName(const PIString & name_);
CDType & getByPath(const PIDeque<int> & path_);
void write(PIIODevice * d, const PIString & prefix = PIString());
void read(const void * ep);
void write(PIIODevice * d, CDType::cdT cdt, const PIString & prefix = PIString());
void read(const void * ep, CDType::cdT cdt);
void update(CDSection & v, UpdateModeFlags mode = SaveByName);
bool isSameStructure(CDSection & v);
void prepareCalculate();
void calculateRecursive(PIEvaluator * e);
PIMap<int, CDType> k;
PIMap<int, CDType> cd;
PIMap<int, CDSection> s;
CDType null;
};
}
inline PICout operator <<(PICout s, const CDUtils::CDType & v) {
s.space();
s.setControl(0, true);
@@ -153,15 +146,4 @@ inline PICout operator <<(PICout s, const CDUtils::CDType & v) {
}
inline PICout operator <<(PICout s, const CDUtils::XType & v) {
s.space();
s.setControl(0, true);
switch (v.cd_type()) {
case CDUtils::CDType::cdX : s << "X["; break;
default : s << "Null["; break;
}
s << v.name() << "(" << v.index() << ")] = " << v.currentValue();
s.restoreControl();
return s;
}
#endif // CDUTILS_TYPES_H