void CalculatorDlg::update(TableState* o) { TableState::EventType ev_type = o->GetEventType(); if (ev_type == TableState::empty ) return; InitTargetChoice(true); UpdateQuickParserTable(); ValidateExpression(); }
void CalculatorDlg::ConnectToProject(Project* project_) { LOG_MSG("In CalculatorDlg::ConnectToProject"); project = project_; table_state = project->GetTableState(); table_state->registerObserver(this); table_int = project->GetTableInt(); InitTargetChoice(true); select_lbl_s_txt->Enable(true); UpdateQuickParserTable(); ValidateExpression(); }
void CalculatorDlg::DisconnectFromProject() { LOG_MSG("In CalculatorDlg::DisconnectFromProject"); project = 0; if (table_state) table_state->removeObserver(this); table_state = 0; table_int = 0; InitTargetChoice(false); select_lbl_s_txt->Enable(false); select_btn->Enable(false); msg_s_txt->SetLabelText(""); UpdateQuickParserTable(); ValidateExpression(); }
void CalculatorDlg::AssignOrSelect(bool assign) { bool select = !assign; if (!all_init || !project) return; ValidateExpression(); if (!expr_valid) { msg_s_txt->SetLabelText("Error: invalid expression"); Refresh(); return; } // we will now do a full evaluation and will print out // preview values. full_parser_table.clear(); std::set<wxString>::iterator it; for (it = active_ident_set.begin(); it!=active_ident_set.end(); ++it) { int col = table_int->FindColId(*it); if (col < 0) continue; GdaFVSmtPtr val(new GdaFlexValue()); table_int->GetColData(col, (*val)); full_parser_table[*it] = val; size_t val_obs = (*val).GetObs(); LOG(val_obs); } GdaParser parser; WeightsManInterface* wmi = 0; if (project && project->GetWManInt()) wmi = project->GetWManInt(); bool parser_success = parser.eval(tokens, &full_parser_table, wmi); if (!parser_success) { wxString s(parser.GetErrorMsg()); msg_s_txt->SetLabelText(s); Refresh(); return; } GdaFVSmtPtr v = parser.GetEvalVal(); int targ_col = -1; size_t targ_tms = -1; if (assign) { // Ensure dimensions are compatiable. targ_col = table_int->FindColId(target_choice->GetStringSelection()); if (targ_col < 0) { msg_s_txt->SetLabelText("Error: Target choice not found"); Refresh(); return; } targ_tms = table_int->GetColTimeSteps(targ_col); } std::valarray<double>& V = (*v).GetValArrayRef(); size_t V_tms = (*v).GetTms(); size_t V_obs = (*v).GetObs(); if (assign && targ_tms < V_tms) { msg_s_txt->SetLabelText("Error: Target has too few time periods."); Refresh(); return; } size_t V_sz = V.size(); double V_first = V[0]; LOG(V_tms); LOG(V_obs); LOG(V_first); size_t obs = table_int->GetNumberRows(); if (assign) { std::vector<double> t_vec(obs); std::vector<bool> undefined(obs); // MMM must consider case of [1 2 3 4 5 ... tms] if (V_obs == 1 && V_tms == 1) { double val = (*v).GetDouble(); bool undef = false; if (!Gda::IsFinite(val)) { val = 0; undef = true; } for (size_t i=0; i<obs; ++i) { t_vec[i] = val; undefined[i] = undef; } } else if (V_tms == 1) { // fill t_vec from V. for (size_t i=0; i<obs; ++i) { double val = V[i]; if (Gda::IsFinite(val)) { t_vec[i] = val; undefined[i] = false; } else { t_vec[i] = 0; undefined[i] = true; } } } for (size_t t=0; t<targ_tms; ++t) { if (V_tms > 1) { // fill t_vec from V for each time period. //std::slice sl(t,obs,Vtms); for (size_t i=0; i<obs; ++i) { double val = V[t+i*V_tms]; if (Gda::IsFinite(val)) { t_vec[i] = val; undefined[i] = false; } else { t_vec[i] = 0; undefined[i] = true; } } } table_int->SetColData(targ_col, t, t_vec); table_int->SetColUndefined(targ_col, t, undefined); } wxString s("Success. First obs. and time value = "); s << (*v).GetDouble(); msg_s_txt->SetLabelText(s); } else { int t=0; wxString t_str = ""; if (V_tms > 1) { // will use current time period t = project->GetTimeState()->GetCurrTime(); t_str = project->GetTimeState()->GetCurrTimeString(); } int num_obs_sel = 0; vector<bool> selected(obs); if (V_obs == 1) { bool val = (bool) V[t]; for (size_t i=0; i<obs; ++i) { selected[i] = val; } if (val) num_obs_sel = obs; } else { // fill t_vec from V for time period t. for (size_t i=0; i<obs; ++i) { selected[i] = (bool) V[t+i*V_tms]; if (selected[i]) ++num_obs_sel; } } HighlightState& hs = *project->GetHighlightState(); std::vector<bool>& h = hs.GetHighlight(); bool selection_changed = false; for (size_t i=0; i<obs; i++) { bool sel = selected[i]; if (sel && !h[i]) { h[i] = true; selection_changed = true; } else if (!sel && h[i]) { h[i] = false; selection_changed = true; } } hs.SetEventType(HLStateInt::delta); hs.notifyObservers(); wxString s; s << num_obs_sel << " observation" << (num_obs_sel != 1 ? "s" : ""); s << " selected"; if (V_tms == 1) { s << "."; } else { s << " for time period " << t_str << " of result."; } msg_s_txt->SetLabelText(s); } Refresh(); }
void CalculatorDlg::OnExprUpdate(wxCommandEvent& e) { ValidateExpression(); }
double CalculateExpression(const std::string& i_string, size_t& o_errorPos) { std::vector<ExpressionItem*> items; size_t maxNestingLevel = 0; size_t parseResult = ParseExpression(i_string, items, maxNestingLevel); if (parseResult != kDefaultErrorPos) { o_errorPos = parseResult; return 0.0; } size_t validateResult = ValidateExpression(items); if (validateResult != kDefaultErrorPos) { o_errorPos = validateResult; return 0.0; } size_t nestingLevel = maxNestingLevel + 1; while (true) { nestingLevel--; size_t loc = 0; while (loc < items.size()) { while (loc < items.size() && (items[loc]->NestingLevel() != nestingLevel)) { loc++; } if (loc >= items.size()) { break; } size_t startLoc = loc; while (loc < items.size() && (items[loc]->NestingLevel() == nestingLevel)) { loc++; } size_t endLoc = loc; if (startLoc != endLoc) { double value = CalculateSequence(items, startLoc, endLoc); for (size_t i = startLoc; i < endLoc; ++i) { delete items[i]; } items.erase(items.begin() + startLoc, items.begin() + (endLoc - 1)); Operand* operand = new Operand(value, nestingLevel - 1, 0); items[startLoc] = operand; } } if (nestingLevel == 0) { break; } } assert(items.size() == 1); Operand* operand = dynamic_cast<Operand*>(items[0]); double result = operand->GetValue(); delete operand; return result; }