NMGenerator::Type type(const nvar& v){ switch(v.type()){ case nvar::Function:{ NMGenerator::Type t = NMGenerator::type(v); if(!(t & NMGenerator::Supported) || t == NMGenerator::Requested){ return t; } size_t size = v.size(); for(size_t i = 0; i < size; ++i){ NMGenerator::Type ti = type(v[i]); if(!(ti & NMGenerator::Supported)){ return ti; } else if(ti == NMGenerator::Requested){ t = ti; } } return t; } case nvar::Symbol: return o_->Get(v).isNone() ? NMGenerator::Requested : NMGenerator::Supported; } return NMGenerator::Supported; }
bool NConcept::isInteger(const nvar& v, bool allowNegative){ if(v.hasVector()){ for(const nvar& vi : v){ if(vi == undef){ return false; } if(!vi.isInteger()){ return false; } if(!allowNegative && vi < 0){ return false; } } return true; } if(v == undef){ return false; } if(v.isInteger()){ if(allowNegative){ return true; } return v >= 0; } return false; }
void setStringMapAttribute_(const nstr& key, const nvar& value){ nvec values; if(value.isString()){ values.push_back(value); } else{ if(!value.hasVector()){ error_(key, value); } for(size_t i = 0; i < value.size(); ++i){ if(!value[i].isString()){ error_(key, value); } values.push_back(value[i]); } } nvar& r = attrs_(key); for(const nstr& k : values){ r(k) = true; } }
nvar product(const nvar& v){ nvar ret = multiplier_; for(size_t i = 0; i < v.size(); ++i){ ret *= v[i]; } return ret; }
NFunc handle(const nvar& v, uint32_t flags){ NMGenerator::Type t = type(v); if(t == NMGenerator::Requested || (t & NMGenerator::Supported && flags & NObject::Delegated)){ v.setFunc(_runFunc); return _runFunc; } return o_->NObject::handle(v, flags); }
void setStringAttribute_(const nstr& key, const nvar& value){ nstr str; if(value.isString()){ str = value; } else{ if(!value.hasVector()){ error_(key, value); } for(size_t i = 0; i < value.size(); ++i){ if(!value[i].isString()){ error_(key, value); } } str = nstr::join(value.vec(), "; "); } attrs_(key) = str; }
double match(const nvar& v, bool full){ //cout << "attrs_ is: " << attrs_ << endl; //cout << "v is: " << v << endl; if(attrs_["in"] != v["in"]){ //cout << "f1" << endl; return -1; } if(attrs_["out"] && !v["out"]){ //cout << "a is: " << attrs_.out << endl; //cout << "b is: " << v.out << endl; //cout << "f2" << endl; return -1; } if(attrs_.hasKey("vec")){ if(!v.hasKey("vec") && attrs_["vec"] != undef){ return -1; } } else if(v.hasKey("vec")){ return -1; } if(attrs_.hasKey("set")){ if(!v.hasKey("set") && attrs_["set"] != undef){ return -1; } } else if(v.hasKey("set")){ return -1; } if(full){ if(attrs_.hasKey("takeCloneOf")){ if(!v.hasKey("cloneOf")){ //cout << "f5" << endl; return -1; } if(attrs_["takeCloneOf"] != v["cloneOf"]){ //cout << "f6" << endl; return -1; } } } else{ return 1; } // no age bias for right now return 1; if(v["age"] > 100){ return -1; } double m = pow((100.0 - v["age"])/50.0, 2.0); //cout << "m is: " << m << endl; return m; }
void error_(const nstr& key, const nvar& value){ throw NError("NConcept: invalid attribute for key '" + key + "' value: " + value.toStr()); }
void validate(const nvar& v){ if(!NConcept::isNumeric(v, false)){ NERROR("invalid value: " + v.toStr()); } }
void NMParser_::translate(nvar& n){ if(n.isSymbol()){ SymbolMap::const_iterator itr = _symbolMap.find(n.str()); SymbolKey key = itr->second; switch(key){ case SKEY_True: n = true; break; case SKEY_False: n = false; break; case SKEY_E: n.str() = "Eu"; break; default: break; } return; } size_t size = n.size(); FunctionMap::const_iterator itr = _functionMap.find({n.str(), size}); if(itr == _functionMap.end()){ itr = _functionMap.find({n.str(), -1}); } if(itr == _functionMap.end()){ error(n, "unrecognized function: " + n); return; } FunctionKey key = itr->second; switch(key){ case FKEY_Set_2: case FKEY_Mod_2: case FKEY_Not_1: case FKEY_And_2: case FKEY_Or_2: case FKEY_Exp_1: case FKEY_Sqrt_1: case FKEY_Abs_1: case FKEY_Log_1: case FKEY_Cos_1: case FKEY_Sin_1: case FKEY_Sinh_1: case FKEY_Tan_1: case FKEY_Tanh_1: case FKEY_Integrate_2: case FKEY_GCD_n: case FKEY_LCM_n: case FKEY_Factorial_1: return; case FKEY_Times_n: if(size < 2){ break; } n.str() = "Mul"; n.foldRight(); return; case FKEY_Plus_n: if(size < 2){ break; } n.str() = "Add"; n.foldRight(); return; case FKEY_Power_2: n.str() = "Pow"; return; case FKEY_Rational_2: n = nrat(n[0], n[1]); return; case FKEY_Less_2: n.str() = "LT"; return; case FKEY_Greater_2: n.str() = "GT"; return; case FKEY_LessEqual_2: n.str() = "LE"; return; case FKEY_GreaterEqual_2: n.str() = "GE"; return; case FKEY_Equal_2: n.str() = "EQ"; return; case FKEY_Unequal_2: n.str() = "NE"; return; case FKEY_Increment_1: n.str() = "PostInc"; return; case FKEY_PreIncrement_1: n.str() = "Inc"; return; case FKEY_Decrement_1: n.str() = "PostDec"; return; case FKEY_PreDecrement_1: n.str() = "Dec"; return; case FKEY_ArcCos_1: n.str() = "Acos"; return; case FKEY_Cosh_1: n.str() = "Cosh"; return; case FKEY_ArcSin_1: n.str() = "Asin"; return; case FKEY_ArcTan_1: n.str() = "Atan"; return; case FKEY_D_2: n.str() = "Derivative"; return; case FKEY_List_n:{ size_t size = n.size(); nvar v; for(size_t i = 0; i < size; ++i){ v << move(n[i]); } n = move(v); return; } } error(n, "unrecognized function: " + n); }