// RD.cpp : Implementation of CRD #include "stdafx.h" #include "RDCom.h" #include "RoutineDesinDoc.h" #include "RD.h" #include "tcl.h" ///////////////////////////////////////////////////////////////////////////// // CRD STDMETHODIMP CRD::OnStartPage (IUnknown* pUnk) { if(!pUnk) return E_POINTER; CComPtr spContext; HRESULT hr; // Get the IScriptingContext Interface hr = pUnk->QueryInterface(IID_IScriptingContext, (void **)&spContext); if(FAILED(hr)) return hr; // Get Request Object Pointer hr = spContext->get_Request(&m_piRequest); if(FAILED(hr)) { spContext.Release(); return hr; } // Get Response Object Pointer hr = spContext->get_Response(&m_piResponse); if(FAILED(hr)) { m_piRequest.Release(); return hr; } // Get Server Object Pointer hr = spContext->get_Server(&m_piServer); if(FAILED(hr)) { m_piRequest.Release(); m_piResponse.Release(); return hr; } // Get Session Object Pointer hr = spContext->get_Session(&m_piSession); if(FAILED(hr)) { m_piRequest.Release(); m_piResponse.Release(); m_piServer.Release(); return hr; } // Get Application Object Pointer hr = spContext->get_Application(&m_piApplication); if(FAILED(hr)) { m_piRequest.Release(); m_piResponse.Release(); m_piServer.Release(); m_piSession.Release(); return hr; } m_bOnStartPageCalled = TRUE; return S_OK; } STDMETHODIMP CRD::OnEndPage () { m_bOnStartPageCalled = FALSE; // Release all interfaces m_piRequest.Release(); m_piResponse.Release(); m_piServer.Release(); m_piSession.Release(); m_piApplication.Release(); return S_OK; } STDMETHODIMP CRD::OpenRDDoc(BSTR str) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CString ss(str); pDoc = new CRoutineDesinDoc; BOOL result = pDoc->OnOpenDocument(ss); return S_OK; } STDMETHODIMP CRD::RunSinglePS(BSTR PS, BSTR *out) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CString CurrentPS(PS); if (CurrentPS=="Fertilization") RunPS("Irrigation PS"); RunPS(CurrentPS); return S_OK; } BOOL CRD::SpecialistHandler(CString Spec) { CStringList * RankingPlanList ; RankingPlanList = GetRankingPlanList(Spec); if (RankingPlanList == NULL) return FALSE; if (RankingPlanList->IsEmpty()) { return FALSE; } POSITION pos = RankingPlanList->GetHeadPosition(); while (pos != NULL) { CString Plan = RankingPlanList->GetAt(pos); if (PlanHandler(Plan)) RankingPlanList->GetNext(pos); } return FALSE; } CStringList* CRD::GetRankingPlanList(CString Spec) { CStringList* PerfectList = new CStringList; CStringList* RoleoutList = new CStringList; CStringList* SuitableList = new CStringList; CStringList* EmptyList = new CStringList; CAgent * pAgent; CPlanList & pPlanList = pDoc->PlanList; CObList1 & pCollectionList = pDoc->CollectionList; POSITION pos = pCollectionList.GetHeadPosition(); while (pos != NULL) { CString Key; pAgent = (CAgent *)pCollectionList.GetAt(pos); if (pAgent->Parent != Spec) { pCollectionList.GetNext(pos); continue; } CPlan *pPlan; pPlan = (CPlan *)pPlanList[pAgent->Name]; CSponsor *pSponsor; pSponsor = (CSponsor *) pPlan->pSponsor; int RowNo = pSponsor->Rows.GetSize(); for (int i =0; i < RowNo; i++) { CRow * row; BOOL Success; row = (CRow *) pSponsor->Rows.GetAt(i); int ColNo = row->Columns.GetSize(); for (int j =0; j < ColNo; j++) { CColumn *column; column = (CColumn *) row->Columns.GetAt(j); Success = EvaluateColumn(column,column->Attribute,pAgent->Name); if (!Success) break; } if (Success) { if (row->Result == "Perfect") PerfectList->AddTail(pAgent->Name); if (row->Result.IsEmpty()) EmptyList->AddTail(pAgent->Name); if (row->Result == "Roleout") RoleoutList->AddTail(pAgent->Name); if (row->Result == "Suitable") SuitableList->AddTail(pAgent->Name); break; } } pCollectionList.GetNext(pos); } if (!PerfectList->IsEmpty()) return PerfectList; if (!SuitableList->IsEmpty()) return SuitableList; if (!EmptyList->IsEmpty()) return EmptyList; if (!RoleoutList->IsEmpty()) return RoleoutList; return NULL; } BOOL CRD::EvaluateColumn(CColumn *column, CString AttributeName, CString strAgent) { if (column->Operator == "?") return TRUE; CVarList & pVarList = pDoc->VarList; CAttribute * pAttribute; pAttribute = (CAttribute * )pVarList[column->Attribute]; /*if (pAttribute->HasNoInput) ShowItsDialog(pAttribute,strAgent); */ if (column->Operator == "= ") if (pAttribute->Value == column->Value) return TRUE; else return FALSE; if (column->Operator == "~=") if (pAttribute->Value != column->Value) return TRUE; else return FALSE; if (column->Operator == "< ") if (pAttribute->Value < column->Value) return TRUE; else return FALSE; if (column->Operator == "> ") if (pAttribute->Value > column->Value) return TRUE; else return FALSE; if (column->Operator == ">=") if (pAttribute->Value >= column->Value) return TRUE; else return FALSE; if (column->Operator == "<=") if (pAttribute->Value <= column->Value) return TRUE; else return FALSE; return TRUE; } BOOL CRD::PlanHandler(CString Plan) { CStringList* PlanItemList ; PlanItemList = GetAllItems(Plan); POSITION pos = PlanItemList->GetHeadPosition(); while (pos != NULL) { CString PlanItem = PlanItemList->GetAt(pos); if (GetType(PlanItem) == "Task") if (!TaskHandler(PlanItem)) return FALSE; if (GetType(PlanItem) == "Specialist") if (!SpecialistHandler(PlanItem)) return FALSE; PlanItemList->GetNext(pos); } return TRUE; } CStringList* CRD::GetAllItems(CString Plan) { CStringList* ItemList = new CStringList; CObList1 & pCollectionList = pDoc->CollectionList; POSITION pos = pCollectionList.GetHeadPosition(); while (pos != NULL) { CString Key; CAgent * pAgent; pAgent = (CAgent *)pCollectionList.GetAt(pos ); if (pAgent->Parent != Plan) { pCollectionList.GetNext(pos); continue; } // ItemList->AddHead(pAgent->Name); ItemList->AddTail(pAgent->Name); pCollectionList.GetNext(pos); } return ItemList; } CString CRD::GetType(CString str) { CObList1 & pCollectionList = pDoc->CollectionList; CAgent * pAgent; pAgent = (CAgent * )pCollectionList.Lookup(str); if (pAgent) return pAgent->AgentType; else return ""; } BOOL CRD::TaskHandler(CString Task) { CStringList *StepList ; StepList = GetAllItems(Task); POSITION pos = StepList->GetHeadPosition(); while (pos != NULL) { BOOL Res; CString Step = StepList->GetAt(pos); Res = StepHandler(Step); if (!Res) return FALSE; StepList->GetNext(pos); } return TRUE; } BOOL CRD::StepHandler(CString Step) { CStepList & pStepList = pDoc->StepList; CStep * pStep; pStep = (CStep * )pStepList[Step]; CEvaluationAgent *pEvaluationAgent; pEvaluationAgent = (CEvaluationAgent *) pStep->pEvaluationAgent; int RowNo = pEvaluationAgent->Rows.GetSize(); for (int i =0; i < RowNo; i++) { CRow * row; BOOL Success = TRUE; row = (CRow *) pEvaluationAgent->Rows.GetAt(i); int ColNo = row->Columns.GetSize(); for (int j =0; j < ColNo; j++) { CColumn *column; column = (CColumn *) row->Columns.GetAt(j); Success = EvaluateColumn(column,pStep->AttributeName,Step); if (!Success) break; } if (Success) { CVarList & pVarList = pDoc->VarList; CAttribute * pAttribute; pAttribute =(CAttribute *) pVarList[pStep->AttributeName]; if (pAttribute->ClassType == "RDNumericalVar") pAttribute->Value = SolveExp(row); else pAttribute->Value = row->Result; pAttribute->HasNoInput = FALSE; return TRUE; } } return TRUE; } CString CRD::SolveExp(CRow *row) { CAttribute * pAttribute; CString in = row->Result; CVarList & pVarList = pDoc->VarList; POSITION pos = pVarList.GetStartPosition(); while (pos != NULL) { CString Key; pVarList.GetNextAssoc(pos, Key, pAttribute); if (pAttribute == NULL) continue; if (pAttribute->ClassType != "RDNumericalVar") continue; int x = in.Find(pAttribute->Name); if (x ==-1)continue; //if (pAttribute->HasNoInput) // ShowItsDialog(pAttribute,"Exp."); in.Replace(pAttribute->Name,pAttribute->Value); } double * ret = new double; double dd = CEvalExp(in); CString out; out.Format("%.2f",dd); delete ret; return out; } void CRD::RunPS(CString PS) { CPSAndSpec * pPSAndSpec; CPSAndSpecList & pPSAndSpecList = pDoc->PSAndSpecList; if (pPSAndSpecList.Lookup(PS,pPSAndSpec)) SpecialistHandler(pPSAndSpec->TopSpec); } double CRD::CEvalExp(CString str) { double * ptr = new double; Tcl_Interp *interp = new Tcl_Interp; interp = Tcl_CreateInterp(); Tcl_ExprDouble(interp,(char*)(LPCTSTR)str,ptr); double x = *ptr; Tcl_DeleteInterp(interp); delete ptr; return x; } STDMETHODIMP CRD::SetValToAttribute(BSTR strAtt, BSTR strVal) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CString ssVal(strVal); CString ssAtt(strAtt); CVarList & pVarList = pDoc->VarList; CAttribute * pAttribute; if (pVarList.Lookup(ssAtt,pAttribute)) { pAttribute->Value = ssVal; pAttribute->HasNoInput= FALSE; } return S_OK; } STDMETHODIMP CRD::GetOutputAtt(BSTR *strOut) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CAttribute * pAttribute; CVarList & pVarList = pDoc->VarList; POSITION pos = pVarList.GetStartPosition(); CString sOut=""; while (pos != NULL) { CString Key; pVarList.GetNextAssoc(pos, Key, pAttribute); if (pAttribute == NULL || !pAttribute->output || pAttribute->HasNoInput) continue; sOut = sOut + "Value + "\" />"; } sOut= sOut + ""; CComBSTR bstr; bstr.Append(sOut.AllocSysString()); *strOut = bstr.Detach(); return S_OK; } STDMETHODIMP CRD::GetLegal(BSTR AttStr, BSTR *Vals) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CAttribute * pAttribute; CVarList & pVarList = pDoc->VarList; CString str(AttStr); pVarList.Lookup(str,pAttribute); CString sOut=""; POSITION pos = pAttribute->LegalValue.GetHeadPosition(); while (pos != NULL) { CString Key = pAttribute->LegalValue.GetAt(pos); sOut = sOut + "" + Key + ""; pAttribute->LegalValue.GetNext(pos); } sOut= sOut + ""; CComBSTR bstr; bstr.Append(sOut.AllocSysString()); *Vals = bstr.Detach(); return S_OK; } STDMETHODIMP CRD::GetType(BSTR AttStr, BSTR *strType) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CAttribute * pAttribute; CVarList & pVarList = pDoc->VarList; CString str(AttStr); pVarList.Lookup(str,pAttribute); CString sOut = pAttribute->ClassType; CComBSTR bstr; bstr.Append(sOut.AllocSysString()); *strType = bstr.Detach(); return S_OK; } STDMETHODIMP CRD::IsInWM(BSTR AttStr, BSTR valStr ,BSTR *outStr) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CAttribute * pAttribute; CVarList & pVarList = pDoc->VarList; CString str(AttStr); CString strValue(valStr); pVarList.Lookup(str,pAttribute); CString sOut; if (pAttribute->HasNoInput || strValue != pAttribute->Value ) sOut = "FALSE"; else sOut = "TRUE"; CComBSTR bstr; bstr.Append(sOut.AllocSysString()); *outStr = bstr.Detach(); return S_OK; } STDMETHODIMP CRD::GetValue(BSTR AttStr, BSTR *Vals) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CAttribute * pAttribute; CVarList & pVarList = pDoc->VarList; CString str(AttStr); pVarList.Lookup(str,pAttribute); CString sOut; if (pAttribute->HasNoInput) sOut = ""; else sOut = pAttribute->Value; CComBSTR bstr; bstr.Append(sOut.AllocSysString()); *Vals = bstr.Detach(); return S_OK; } STDMETHODIMP CRD::GetAllAtt(BSTR *strOut) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CAttribute * pAttribute; CVarList & pVarList = pDoc->VarList; POSITION pos = pVarList.GetStartPosition(); CString sOut=""; while (pos != NULL) { CString Key; pVarList.GetNextAssoc(pos, Key, pAttribute); // if (pAttribute == NULL || pAttribute->HasNoInput) continue; sOut = sOut + "Value + "\" />"; } sOut= sOut + ""; CComBSTR bstr; bstr.Append(sOut.AllocSysString()); *strOut = bstr.Detach(); return S_OK; } STDMETHODIMP CRD::RestAllAttribute() { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CAttribute * pAttribute; CVarList & pVarList = pDoc->VarList; POSITION pos = pVarList.GetStartPosition(); while (pos != NULL) { CString Key; pVarList.GetNextAssoc(pos, Key, pAttribute); //if (pAttribute->output==1){ pAttribute->HasNoInput = TRUE; pAttribute->Value = ""; //} } return S_OK; } void CRD::GetReqAtt(CString PS) { ReqAtt = ""; CPSAndSpec * pPSAndSpec; CPSAndSpecList & pPSAndSpecList = pDoc->PSAndSpecList; if (pPSAndSpecList.Lookup(PS,pPSAndSpec)) GetReqAttFromSpec(pPSAndSpec->TopSpec); ReqAtt += ""; } BOOL CRD::GetReqAttFromSpec(CString Spec) { CStringList * RankingPlanList ; RankingPlanList = GetRankingPlanList(Spec); if (RankingPlanList == NULL) return FALSE; if (RankingPlanList->IsEmpty()) { return FALSE; } POSITION pos = RankingPlanList->GetHeadPosition(); while (pos != NULL) { CString Plan = RankingPlanList->GetAt(pos); if (GetReqAttFromPlan(Plan)) RankingPlanList->GetNext(pos); } return TRUE; } BOOL CRD::GetReqAttFromPlan(CString Plan) { CStringList* PlanItemList ; PlanItemList = GetAllItems(Plan); POSITION pos = PlanItemList->GetHeadPosition(); while (pos != NULL) { CString PlanItem = PlanItemList->GetAt(pos); if (GetType(PlanItem) == "Task"){ if (!GetReqAttFromTask(PlanItem)) return FALSE; } if (GetType(PlanItem) == "Specialist"){ if (!GetReqAttFromSpec(PlanItem)) return FALSE; } PlanItemList->GetNext(pos); } return TRUE; } BOOL CRD::GetReqAttFromTask(CString Task) { CStringList *StepList ; StepList = GetAllItems(Task); POSITION pos = StepList->GetHeadPosition(); while (pos != NULL) { BOOL Res; CString Step = StepList->GetAt(pos); Res = GetReqAttFromStep(Step); if (!Res) return FALSE; StepList->GetNext(pos); } return TRUE; } BOOL CRD::GetReqAttFromStep(CString Step) { CStepList & pStepList = pDoc->StepList; CStep * pStep ; pStep =(CStep *) pStepList[Step]; CVarList & pVarList = pDoc->VarList; CAttribute * pAttribute; CEvaluationAgent *pEvaluationAgent; pEvaluationAgent = (CEvaluationAgent *) pStep->pEvaluationAgent; int RowNo = pEvaluationAgent->Rows.GetSize(); CString OutStr; for (int i =0; i < RowNo; i++) { CRow * row; OutStr.Empty(); BOOL Success = TRUE; row = (CRow *) pEvaluationAgent->Rows.GetAt(i); int ColNo = row->Columns.GetSize(); for (int j =0; j < ColNo; j++) { CColumn *column; column = (CColumn *) row->Columns.GetAt(j); Success = EvaluateColumn(column,pStep->AttributeName,Step); // begin Test /* if (!Success){ OutStr =""; break; } */ // end Test pAttribute = pVarList[column->Attribute]; if (pAttribute->HasNoInput ){ if(pAttribute->input && ReqAtt.Find(pAttribute->Name)<0) OutStr += "Name + "\"/>" ; } else{ OutStr.Empty(); // break; } if (!Success && pAttribute->HasNoInput) Success = TRUE; if (!pAttribute && !Success && !pAttribute->HasNoInput) break; //} } ReqAtt += OutStr; if (Success) { pAttribute = (CAttribute *)pVarList[pStep->AttributeName]; if (pAttribute->ClassType == "RDNumericalVar"){ CString in = row->Result; POSITION pos = pVarList.GetStartPosition(); while (pos != NULL) { CString Key; pVarList.GetNextAssoc(pos, Key, pAttribute); if (pAttribute == NULL) continue; if (pAttribute->ClassType != "RDNumericalVar") continue; int x = in.Find(pAttribute->Name); if (x ==-1)continue; if (pAttribute->HasNoInput && pAttribute->input && ReqAtt.Find(pAttribute->Name)<0) ReqAtt += "Name + "\"/>" ; } } } } return TRUE; } STDMETHODIMP CRD::GetReqAttPS(BSTR strPS, BSTR *strOut) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CString PS(strPS); GetReqAtt(PS); CString sOut = ReqAtt; CComBSTR bstr; bstr.Append(sOut.AllocSysString()); *strOut = bstr.Detach(); return S_OK; }