// RoutineDesinView.cpp : implementation of the CRoutineDesinView class
//
#include "stdafx.h"
#include "tcl80.h"
#include "RoutineDesinDoc.h"
#include "RoutineDesinView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CRoutineDesinView
/////////////////////////////////////////////////////////////////////////////
// CRoutineDesinView construction/destruction
CRoutineDesinView::CRoutineDesinView()
{
MapList = new CVarList;
CStringList & PSStringList = GetDocument()->PSStringList;
POSITION pos = PSStringList.GetHeadPosition();
while (pos != NULL)
{
CString Key;
Key = PSStringList.GetAt(pos);
m_PSList.AddString(Key);
PSStringList.GetNext(pos);
}
/*
CPSAndSpecList & pPSAndSpecList = GetDocument()->PSAndSpecList;
CPSAndSpec * pPSAndSpec;
POSITION pos = pPSAndSpecList.GetStartPosition();
while (pos != NULL)
{
CString Key;
pPSAndSpecList.GetNextAssoc(pos, Key, pPSAndSpec);
m_PSList.AddString(Key);
}
*/
Tree = new CObList0;
DerivedList = new CObList;
m_SolList.ShowWindow(SW_HIDE);
CButton *Btn = (CButton *)GetDlgItem(IDC_NEXTSOLUTION);
CButton *Btn1 = (CButton *)GetDlgItem(IDC_PREVSOULTION);
CButton *Btn2 = (CButton *)GetDlgItem(IDC_MARKSOLL);
Btn->ShowWindow(SW_HIDE);
Btn1->ShowWindow(SW_HIDE);
Btn2->ShowWindow(SW_HIDE);
CListBox *List1 = (CListBox *)GetDlgItem(IDC_LIST1);
CListBox *List2 = (CListBox *)GetDlgItem(IDC_LIST2);
CListBox *List3 = (CListBox *)GetDlgItem(IDD_LIST3);
CListBox *List4 = (CListBox *)GetDlgItem(IDD_LIST4);
List1->ShowWindow(SW_HIDE);
List2->ShowWindow(SW_HIDE);
List3->ShowWindow(SW_HIDE);
List4->ShowWindow(SW_HIDE);
CButton *Btn11 = (CButton *)GetDlgItem(IDC_PREVSOL);
CButton *Btn12 = (CButton *)GetDlgItem(IDC_NEXTSOL);
Btn11->ShowWindow(SW_HIDE);
Btn12->ShowWindow(SW_HIDE);
m_Tree1.ShowWindow(SW_HIDE);
}
CRoutineDesinView::~CRoutineDesinView()
{
}
/////////////////////////////////////////////////////////////////////////////
// CRoutineDesinView printing
/////////////////////////////////////////////////////////////////////////////
// CRoutineDesinView diagnostics
/////////////////////////////////////////////////////////////////////////////
// CRoutineDesinView message handlers
void CRoutineDesinView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
}
void CRoutineDesinView::OnSelchangePS()
{
CString PSKey;
if (m_PSList.GetCurSel() >=0)
m_PSList.GetText(m_PSList.GetCurSel(),PSKey);
else
return;
CPSAndSpecList & pPSAndSpecList = GetDocument()->PSAndSpecList;
CPSAndSpec * pPSAndSpec;
pPSAndSpecList.Lookup(PSKey,pPSAndSpec);
m_Tree.DeleteAllItems();
UpdateView(pPSAndSpec->TopSpec);
CurrentPS = PSKey ;
}
void CRoutineDesinView::UpdateView(CString str,HTREEITEM hItem)
{
CObList1 & pCollectionList = GetDocument()->CollectionList;
POSITION pos = pCollectionList.GetHeadPosition();
HTREEITEM hItemSon = m_Tree.InsertItem(str,hItem);
CAgent * pAgent = new CAgent;
while (pos != NULL)
{
CString Key;
pAgent = (CAgent *)pCollectionList.GetAt(pos);
if (pAgent->Parent == str)
UpdateView(pAgent->Name,hItemSon);
pCollectionList.GetNext(pos);
}
}
void CRoutineDesinView::OnAddAgent()
{
HTREEITEM hItem = m_Tree.GetSelectedItem();
if (hItem == NULL) return;
CString m_AgentName;
CString ParentType1;
CString ParentType = GetType(hItem);
if (ParentType == "Step")
{
MessageBox("Can not Add Agent to Step ....");
return;
}
if ( ParentType == "Plan")
{
CAddUAgent AddAgentDlg;
if (AddAgentDlg.DoModal() != IDOK) return;
if (AddAgentDlg.m_AgentName.IsEmpty()) return;
m_AgentName = AddAgentDlg.m_AgentName;
ParentType1 = AddAgentDlg.m_Type;
if (ParentType1.IsEmpty())
{
MessageBox("Unkown Agent Type...");
return;
}
}
else
{
CAddAgent AddAgentDlg;
if (AddAgentDlg.DoModal() != IDOK) return;
if (AddAgentDlg.m_AgentName.IsEmpty()) return;
m_AgentName = AddAgentDlg.m_AgentName;
}
CString AgentParentName = AgentGetName(hItem);
CObList1 & pCollectionList = GetDocument()->CollectionList;
CAgent * pAgent = new CAgent;
pAgent = pCollectionList.Lookup(m_AgentName);
if (pAgent)
{
MessageBox("Agent Aleady Exist....");
return;
}
// CString DetailName = m_AgentName + "("+str+")" ;
HTREEITEM hItemSon = m_Tree.InsertItem(m_AgentName,hItem);
// MessageBox(ParentType);
pAgent = new CAgent;
pAgent->Name = m_AgentName;
pAgent->Parent = AgentParentName;
if (ParentType == "Specialist")
{
pAgent->AgentType = "Plan";
CPlanList & pPlanList = GetDocument()->PlanList;
CPlan * pPlan = new CPlan;
pPlan->Name = pAgent->Name;
pPlan->Parent = pAgent->Parent;
pPlanList[pAgent->Name] = pPlan;
pCollectionList.AddTail(pAgent);
m_Tree.Expand(hItem,TVE_EXPAND);
return;
}
if (ParentType == "Plan")
{
pAgent->AgentType = ParentType1;
pCollectionList.AddTail(pAgent);
m_Tree.Expand(hItem,TVE_EXPAND);
return;
}
if (ParentType == "Task")
{
pAgent->AgentType = "Step";
CStepList & pStepList = GetDocument()->StepList;
CStep * pStep = new CStep;
pStep->Name = pAgent->Name;
pStep->Parent = pAgent->Parent;
pStepList[pAgent->Name] = pStep;
pCollectionList.AddTail(pAgent);
m_Tree.Expand(hItem,TVE_EXPAND);
return;
}
}
CString CRoutineDesinView::AgentGetName(HTREEITEM hItem)
{
char buf[256];
TV_ITEM tvi;
tvi.hItem = hItem;
tvi.mask = TVIF_TEXT;
tvi.pszText = buf;
tvi.cchTextMax = 256;
m_Tree.GetItem (&tvi);
CString str = tvi.pszText;
return str;
}
CString CRoutineDesinView::GetType(HTREEITEM hItem)
{
char buf[256];
TV_ITEM tvi;
tvi.hItem = hItem;
tvi.mask = TVIF_TEXT;
tvi.pszText = buf;
tvi.cchTextMax = 256;
m_Tree.GetItem (&tvi);
CString str = tvi.pszText;
return GetType(str);
}
CString CRoutineDesinView::GetType(CString str)
{
CObList1 & pCollectionList = GetDocument()->CollectionList;
CAgent * pAgent = new CAgent;
pAgent = pCollectionList.Lookup(str);
if (pAgent)
return pAgent->AgentType;
else return "";
}
void CRoutineDesinView::OnRightclickTree(NMHDR* pNMHDR, LRESULT* pResult)
{
HTREEITEM hItem = m_Tree.GetSelectedItem();
if (hItem == NULL) return;
RECT * rect = new RECT;
m_Tree.GetItemRect(hItem,rect,-1);
CMenu menu;
CMenu *submenu = new CMenu;
// load the menu
menu.LoadMenu(IDR_MAINFRAME);
// get the popup menu
submenu = menu.GetSubMenu(3);
// convert to screen coordinates
CPoint point;
point.x=rect->right+21;
point.y=rect->bottom+91;
ClientToScreen(&point);
// post the menu
submenu->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,
point.x, point.y, AfxGetApp()->m_pMainWnd,NULL);
*pResult = 0;
}
void CRoutineDesinView::OnUpdateAgent()
{
HTREEITEM hItem = m_Tree.GetSelectedItem();
if (hItem == NULL) return;
CObList1 & pCollectionList = GetDocument()->CollectionList;
CStepList & pStepList = GetDocument()->StepList;
CString AgentType = GetType(hItem);
if (AgentType == "Plan")
{
CPlanDlg PlanDlg;
CPlanList & pPlanList = GetDocument()->PlanList;
CVarList & pVarList = GetDocument()->VarList;
CPlan *pPlan = new CPlan;
pPlan = pPlanList[AgentGetName(hItem)];
PlanDlg.GetAllData(pCollectionList,pStepList,AgentGetName(hItem),pPlan,pVarList);
if (PlanDlg.DoModal()==IDOK)
{
HTREEITEM RootItem = m_Tree.GetRootItem();
CString str = AgentGetName(RootItem);
m_Tree.DeleteAllItems();
UpdateView(str);
m_Tree.Expand(RootItem,TVE_EXPAND);
}
return;
}
if (AgentType == "Task")
{
CTaskDlg TaskDlg;
TaskDlg.GetAllData(pCollectionList,pStepList,AgentGetName(hItem));
if (TaskDlg.DoModal()==IDOK)
{
HTREEITEM RootItem = m_Tree.GetRootItem();
CString str = AgentGetName(RootItem);
m_Tree.DeleteAllItems();
UpdateView(str);
m_Tree.Expand(RootItem,TVE_EXPAND);
}
return;
}
if (AgentType == "Step")
{
CStepBrowse StepBrowseDlg;
CVarList & pVarList = GetDocument()->VarList;
StepBrowseDlg.GetAllData(pStepList,pVarList,AgentGetName(hItem));
if (StepBrowseDlg.DoModal()==IDOK)
{
}
}
}
void CRoutineDesinView::OnBrowseDatabase()
{
CDatabaseDlg DatabaseDlg;
CObList1 & pCollectionList = GetDocument()->CollectionList;
CVarList & pVarList = GetDocument()->VarList;
DatabaseDlg.GetAllData(pCollectionList,pVarList);
DatabaseDlg.DoModal();
}
void CRoutineDesinView::OnRunPS()
{
if (AfxMessageBox("Run Single Design",MB_YESNO)== IDYES)
Single = TRUE;
else
Single = FALSE;
Back = FALSE;
if (m_PSList.GetCurSel() < 0)
return;
CurrentOrAllPS = 1;
Back = FALSE;
CPSAndSpec * pPSAndSpec;
if (CurrentPS=="Fertilization")
RunPS("Irrigation PS");
CPSAndSpecList & pPSAndSpecList = GetDocument()->PSAndSpecList;
pPSAndSpecList.Lookup(CurrentPS,pPSAndSpec);
if (Single == TRUE)
{
if (!SpecialistHandler(pPSAndSpec->TopSpec))
MessageBox("Design failed....");
}else
{
CNode *Node1 = new CNode;
Node1->AttributeName = "Construct Of " + CurrentPS;
Node1->Value = "";
hItemSon000 = m_Tree1.InsertItem(Node1->AttributeName,1,1);
WTree.SetAt(hItemSon000,Node1);
Array *A1= new Array;
A1->Add( hItemSon000);
if (!MSpecialistHandler(pPSAndSpec->TopSpec,A1))
MessageBox("Design failed....");
GetSolution(hItemSon000);
CHItem * hItem = new CHItem;
hItem->hItem= hItemSon000;
CString Int ;
CurrentSol = 0;
Int.Format("%d-%d",0,1);
// IntToTree.SetAt((CString)Int,(CHItem)hItem);
IntToTree[Int] = hItem;
CListBox *List1 = (CListBox *)GetDlgItem(IDC_LIST1);
List1->AddString(Node1->AttributeName );
}
}
BOOL CRoutineDesinView::SpecialistHandler(CString Spec)
{
if (!ConstrainHandler(Spec,0))
return FALSE;
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))
if (ConstrainHandler(Spec,1))
return TRUE;
RankingPlanList->GetNext(pos);
}
return FALSE;
}
BOOL CRoutineDesinView::ConstrainHandler(CString AgentName,int InitOrFinal )
{
return TRUE;
}
CStringList* CRoutineDesinView::GetRankingPlanList(CString Spec)
{
CStringList* PerfectList = new CStringList;
CStringList* RoleoutList = new CStringList;
CStringList* SuitableList = new CStringList;
CStringList* EmptyList = new CStringList;
CAgent * pAgent = new CAgent;
CPlanList & pPlanList = GetDocument()->PlanList;
CObList1 & pCollectionList = GetDocument()->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 = new CPlan;
pPlan = pPlanList[pAgent->Name];
CSponsor *pSponsor = new CSponsor;
pSponsor = (CSponsor *) pPlan->pSponsor;
int RowNo = pSponsor->Rows.GetSize();
for (int i =0; i < RowNo; i++)
{
CRow * row = new CRow;
BOOL Success;
row = (CRow *) pSponsor->Rows.GetAt(i);
int ColNo = row->Columns.GetSize();
for (int j =0; j < ColNo; j++)
{
CColumn *column = new CColumn;
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 CRoutineDesinView::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;
if (GetType(PlanItem) == "Constraint")
if (!ConstrainHandler(PlanItem)) return FALSE;
PlanItemList->GetNext(pos);
}
return TRUE;
}
CStringList * CRoutineDesinView::GetAllItems(CString Plan)
{
CStringList* ItemList = new CStringList;
CObList1 & pCollectionList = GetDocument()->CollectionList;
POSITION pos = pCollectionList.GetHeadPosition();
while (pos != NULL)
{
CString Key;
CAgent * pAgent = new CAgent;
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;
}
BOOL CRoutineDesinView::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 CRoutineDesinView::StepHandler(CString Step)
{
if (!ConstrainHandler(Step))
return FALSE;
CStepList & pStepList = GetDocument()->StepList;
CStep * pStep = new CStep;
pStep = pStepList[Step];
CEvaluationAgent *pEvaluationAgent = new CEvaluationAgent;
pEvaluationAgent = (CEvaluationAgent *) pStep->pEvaluationAgent;
int RowNo = pEvaluationAgent->Rows.GetSize();
for (int i =0; i < RowNo; i++)
{
CRow * row = new CRow;
BOOL Success = TRUE;
row = (CRow *) pEvaluationAgent->Rows.GetAt(i);
int ColNo = row->Columns.GetSize();
for (int j =0; j < ColNo; j++)
{
CColumn *column = new CColumn;
column = (CColumn *) row->Columns.GetAt(j);
Success = EvaluateColumn(column,pStep->AttributeName,Step);
if (Back)
{
SolveBack();
return TRUE;
}
if (!Success) break;
}
if (Success)
{
CVarList & pVarList = GetDocument()->VarList;
CAttribute * pAttribute = new CAttribute;
pAttribute = pVarList[pStep->AttributeName];
if (pAttribute->ClassType == "RDNumericalVar")
pAttribute->Value = SolveExp(row);
else
pAttribute->Value = row->Result;
pAttribute->HasNoInput = FALSE;
CNodeOfStack * Node = new CNodeOfStack;
Node->AttributeName = pAttribute->Name;
Node->Type = "Inference";
Stack.AddHead(Node);
return TRUE;
}
}
return TRUE;
}
BOOL CRoutineDesinView::EvaluateColumn(CColumn * column,CString AttributeName,CString strAgent)
{
if (column->Operator == "?") return TRUE;
CVarList & pVarList = GetDocument()->VarList;
CAttribute * pAttribute = new CAttribute;
pAttribute = 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;
}
void CRoutineDesinView::ShowItsDialog(CAttribute * pAttribute,CString strAgent)
{
if (pAttribute->ClassType == "RDYesNoVar")
{
CYesNoQ YesNoDlg;
YesNoDlg.GetAllData(pAttribute,strAgent);
if (YesNoDlg.DoModal() == IDCANCEL) return;
CNodeOfStack * Node = new CNodeOfStack;
Node->AttributeName = pAttribute->Name;
Node->Type = "User";
Stack.AddHead(Node);
if (YesNoDlg.Back)
{
Back = TRUE;
return;
}
pAttribute->Value = YesNoDlg.Value;
pAttribute->HasNoInput = FALSE;
return;
}
if (pAttribute->ClassType == "RDOneOfVar")
{
COneOfQ OneOfVar;
OneOfVar.GetAllData(pAttribute,strAgent);
if (OneOfVar.DoModal() == IDCANCEL) return;
CNodeOfStack * Node = new CNodeOfStack;
Node->AttributeName = pAttribute->Name;
Node->Type = "User";
Stack.AddHead(Node);
if (OneOfVar.Back)
{
Back = TRUE;
return;
}
pAttribute->Value = OneOfVar.Value;
pAttribute->HasNoInput = FALSE;
return;
}
if (pAttribute->ClassType == "RDStringVar")
{
CStringDlg StringDlg;
StringDlg.GetAllData("",pAttribute,2);
if (StringDlg.DoModal() == IDCANCEL) return;
CNodeOfStack * Node = new CNodeOfStack;
Node->AttributeName = pAttribute->Name;
Node->Type = "User";
Stack.AddHead(Node);
if (StringDlg.Back)
{
Back = TRUE;
return;
}
pAttribute->Value = StringDlg.m_Value;
pAttribute->HasNoInput = FALSE;
return;
}
if (pAttribute->ClassType == "RDNumericalVar")
{
CStringDlg StringDlg;
StringDlg.GetAllData("",pAttribute,3);
if (StringDlg.DoModal() == IDCANCEL) return;
CNodeOfStack * Node = new CNodeOfStack;
Node->AttributeName = pAttribute->Name;
Node->Type = "User";
Stack.AddHead(Node);
if (StringDlg.Back)
{
Back = TRUE;
return;
}
pAttribute->Value = StringDlg.m_Value;
pAttribute->HasNoInput = FALSE;
return;
}
}
void CRoutineDesinView::OnRunAllPS()
{
if (AfxMessageBox("Run Single Deign",MB_YESNO)== IDYES)
Single = TRUE;
else
Single = FALSE;
Back = FALSE;
CPSAndSpec * pPSAndSpec;
CurrentOrAllPS = 0;
Back = FALSE;
CPSAndSpecList & pPSAndSpecList = GetDocument()->PSAndSpecList;
/* new */
CNode *Node1 = new CNode;
Node1->AttributeName = "Construct Of All PS's " ;
Node1->Value = "";
hItemSon000 = m_Tree1.InsertItem(Node1->AttributeName,1,1);
WTree.SetAt(hItemSon000,Node1);
Array *A1= new Array;
A1->Add( hItemSon000);
/*end new */
POSITION pos = pPSAndSpecList.GetStartPosition();
while (pos != NULL)
{
CString Key;
pPSAndSpecList.GetNextAssoc(pos, Key, pPSAndSpec);
if (Single == TRUE)
{
if (!SpecialistHandler(pPSAndSpec->TopSpec))
MessageBox("Design of " + pPSAndSpec->TopSpec + " failed....");
}else
{
//Array * List = new Array; /// Add All List Of all Attribute
A1=MSpecialistHandler(pPSAndSpec->TopSpec,A1);
// MessageBox("Design of " + pPSAndSpec->TopSpec + " failed....");
/* new 1*/
GetSolution(hItemSon000);
CHItem * hItem = new CHItem;
hItem->hItem= hItemSon000;
CString Int ;
CurrentSol = 0;
Int.Format("%d-%d",0,1);
// IntToTree.SetAt((CString)Int,(CHItem)hItem);
IntToTree[Int] = hItem;
CListBox *List1 = (CListBox *)GetDlgItem(IDC_LIST1);
List1->AddString(Node1->AttributeName );
/*end new 1*/
}
}
}
CString CRoutineDesinView::SolveExp(CRow * row)
{
CAttribute * pAttribute = new CAttribute;
CString in = row->Result;
CVarList & pVarList = GetDocument()->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);
// if (Back)
// {
// SolveBack();
// return;
// }
}
double * ret = new double;
long dd = m_tcl.TclExprDouble(in,ret);
CString out;
out.Format("%.2f",*ret);
delete ret;
return out;
}
int CRoutineDesinView::Precedence(CString str)
{
if ( str == "-" || str == "+" ) return 0;
if ( str == "/" || str == "*" ) return 1;
if ( str == ")" ) return 2;
return 0;
}
double CRoutineDesinView::execute(double a, CString op, double b)
{
if (op == "-") return a-b;
if (op == "+") return a+b;
if (op == "/") return a/b;
if (op == "*") return a*b;
return 0;
}
void CRoutineDesinView::OnRunAgent()
{
if (AfxMessageBox("Run Single Deign",MB_YESNO)== IDYES)
Single = TRUE;
else
Single = FALSE;
CurrentOrAllPS = 2;
Back = FALSE;
HTREEITEM hItem = m_Tree.GetSelectedItem();
if (hItem == NULL) return;
// GetAgent Name
char buf[256];
TV_ITEM tvi;
tvi.hItem = hItem;
tvi.mask = TVIF_TEXT;
tvi.pszText = buf;
tvi.cchTextMax = 256;
m_Tree.GetItem (&tvi);
CString m_AgentName = tvi.pszText;
CString AgentType = GetType(hItem);
if (AgentType == "Step")
{
if (Single)
StepHandler(m_AgentName);
}
if (AgentType == "Task")
TaskHandler(m_AgentName);
if (AgentType == "Plan")
PlanHandler(m_AgentName);
if (AgentType == "Specialist")
SpecialistHandler(m_AgentName);
}
void CRoutineDesinView::SolveBack()
{
Back = FALSE;
POSITION pos = Stack.GetHeadPosition();
CNodeOfStack * Node = new CNodeOfStack;
int Count = 0;
while (pos != NULL)
{
Node = (CNodeOfStack *)Stack.RemoveHead();
CVarList & pVarList = GetDocument()->VarList;
CAttribute * pAttribute ;
pAttribute = pVarList[Node->AttributeName];
pAttribute->Value = "";
pAttribute->HasNoInput = TRUE;
if (Node->Type == "User")
{
if (Count >= 1) break;
Count ++;
}
if (Stack.GetCount() == 0) break;
Stack.GetNext(pos);
}
// Stack.RemoveAll();
if (CurrentOrAllPS == 1)
OnRunPS();
if (CurrentOrAllPS == 0)
OnRunAllPS();
if (CurrentOrAllPS == 2)
OnRunAgent();
}
void CRoutineDesinView::OnInputAttributes()
{
CVarList & pVarList = GetDocument()->VarList;
CShowOutput Showdlg;
Showdlg.GetAllData(pVarList,TRUE);
Showdlg.DoModal();
return;
if (Showdlg.Update)
{
POSITION pos = Stack.GetHeadPosition();
CNodeOfStack * Node = new CNodeOfStack;
int Count = 0;
while (pos != NULL)
{
if (Node->Type == "User")
{
Stack.GetNext(pos);
continue;
}
Node = (CNodeOfStack *)Stack.RemoveHead();
CVarList & pVarList = GetDocument()->VarList;
CAttribute * pAttribute = new CAttribute;
pAttribute = pVarList[Node->AttributeName];
pAttribute->Value = "";
pAttribute->HasNoInput = TRUE;
Stack.GetNext(pos);
}
if (CurrentOrAllPS == 1)
OnRunPS();
if (CurrentOrAllPS == 0)
OnRunAllPS();
if (CurrentOrAllPS == 2)
OnRunAgent();
}
}
void CRoutineDesinView::OnResults()
{
/* CVarList & pVarList = GetDocument()->VarList;
COutputDlg OutputDlg;
OutputDlg.GetAllData(GetDocument()->TaskPSList,GetDocument()->PSAndSpecList,pVarList,Tree);
OutputDlg.DoModal();
*/
CVarList & pVarList = GetDocument()->VarList;
CShowOutput Showdlg;
Showdlg.GetAllData(pVarList,FALSE);
Showdlg.DoModal();
return;
if (Showdlg.Update)
{
POSITION pos = Stack.GetHeadPosition();
CNodeOfStack * Node = new CNodeOfStack;
int Count = 0;
while (pos != NULL)
{
if (Node->Type == "User")
{
Stack.GetNext(pos);
continue;
}
Node = (CNodeOfStack *)Stack.RemoveHead();
CVarList & pVarList = GetDocument()->VarList;
CAttribute * pAttribute = new CAttribute;
pAttribute = pVarList[Node->AttributeName];
pAttribute->Value = "";
pAttribute->HasNoInput = TRUE;
Stack.GetNext(pos);
}
if (CurrentOrAllPS == 1)
OnRunPS();
if (CurrentOrAllPS == 0)
OnRunAllPS();
if (CurrentOrAllPS == 2)
OnRunAgent();
}
}
Array* CRoutineDesinView::mStepHandler(CString Step,Array * List)
{
if (!ConstrainHandler(Step))
return FALSE;
CStepList & pStepList = GetDocument()->StepList;
CStep * pStep = new CStep;
Array* ReturnedList = new Array;
if (List == NULL)
List = new Array;
CString Out;
ReturnedList = new Array;
pStep = pStepList[Step];
CAttribute * pAttribute = new CAttribute;
CVarList & pVarList = GetDocument()->VarList;
pAttribute = pVarList[pStep->AttributeName];
CEvaluationAgent *pEvaluationAgent = new CEvaluationAgent;
pEvaluationAgent = (CEvaluationAgent *) pStep->pEvaluationAgent;
int RowNo = pEvaluationAgent->Rows.GetSize();
for (int i =0; i < RowNo; i++)
{
CRow * row = new CRow;
BOOL Success = TRUE;
row = (CRow *) pEvaluationAgent->Rows.GetAt(i);
int ColNo = row->Columns.GetSize();
for (int j =0; j < ColNo; j++)
{
CColumn *column = new CColumn;
column = (CColumn *) row->Columns.GetAt(j);
// i do not know why this statment for, but it is necessary
if (column->Attribute.IsEmpty()) continue;
Success = mEvaluateColumn(column,pStep->AttributeName,Step);
/*
15/4/2000
if (Success)
{
if (pAttribute->ClassType == "RDNumericalVar")
Out = SolveExp(row);
}
*/
if (!Success) break;
}
// 28/8/99
if (Success && ColNo == 0 && pAttribute->ClassType == "RDNumericalVar")
Out = SolveExp(row);
if (Success)
{
CNode * Node1 = new CNode;
Node1->AttributeName = pStep->AttributeName;
//AddInTree(Node1);
if (pAttribute->ClassType == "RDNumericalVar")
{
Out = SolveExp(row);
pAttribute->Value = Out;
}
else
pAttribute->Value = row->Result;
Node1->Value = pAttribute->Value;
pAttribute->Values.AddTail(pAttribute->Value);
pAttribute->HasNoInput = FALSE;
int Size = List->GetSize();
for (int i =0 ; i< Size;i++)
{
HTREEITEM hItemSon ;
hItemSon = List->GetAt(i);
if (ValidNode(hItemSon,row)) // make sure that the value that gets from row is valid with the wole path
{
if (pAttribute->output)
{
hItemSon = m_Tree1.InsertItem(Node1->AttributeName+" = "+Node1->Value,1,2,hItemSon);
WTree.SetAt(hItemSon,Node1);
ReturnedList->Add(hItemSon);
}
}
}
}
}
if (ReturnedList->GetSize()==0)
return List;
return ReturnedList;
}
BOOL CRoutineDesinView::mEvaluateColumn(CColumn * column, CString AttributeName, CString strAgent)
{
if (column->Operator == "?") return TRUE;
CVarList & pVarList = GetDocument()->VarList;
CAttribute * pAttribute = new CAttribute;
pAttribute = pVarList[column->Attribute];
if (pAttribute->HasNoInput)
ShowItsDialog(pAttribute,strAgent);
if (column->Operator == "= ")
if (pAttribute->Value == column->Value) return TRUE;
else
{
POSITION pos = pAttribute->Values.GetHeadPosition();
while (pos != NULL)
{
CString Val = pAttribute->Values.GetAt(pos);
if (Val == column->Value)
return TRUE;
pAttribute->Values.GetNext(pos);
}
return FALSE;
}
if (column->Operator == "~=")
if (pAttribute->Value != column->Value) return TRUE;
else
{
POSITION pos = pAttribute->Values.GetHeadPosition();
while (pos != NULL)
{
CString Val = pAttribute->Values.GetAt(pos);
if (Val != column->Value)
return TRUE;
pAttribute->Values.GetNext(pos);
}
return FALSE;
}
if (column->Operator == "< ")
if (pAttribute->Value < column->Value) return TRUE;
else
{
POSITION pos = pAttribute->Values.GetHeadPosition();
while (pos != NULL)
{
CString Val = pAttribute->Values.GetAt(pos);
if (Val < column->Value)
return TRUE;
pAttribute->Values.GetNext(pos);
}
return FALSE;
}
if (column->Operator == "> ")
if (pAttribute->Value > column->Value) return TRUE;
else
{
POSITION pos = pAttribute->Values.GetHeadPosition();
while (pos != NULL)
{
CString Val = pAttribute->Values.GetAt(pos);
if (Val > column->Value)
return TRUE;
pAttribute->Values.GetNext(pos);
}
return FALSE;
}
if (column->Operator == ">=")
if (pAttribute->Value >= column->Value) return TRUE;
else
{
POSITION pos = pAttribute->Values.GetHeadPosition();
while (pos != NULL)
{
CString Val = pAttribute->Values.GetAt(pos);
if (Val >= column->Value)
return TRUE;
pAttribute->Values.GetNext(pos);
}
return FALSE;
}
if (column->Operator == "<=")
if (pAttribute->Value <= column->Value) return TRUE;
else
{
POSITION pos = pAttribute->Values.GetHeadPosition();
while (pos != NULL)
{
CString Val = pAttribute->Values.GetAt(pos);
if (Val <= column->Value)
return TRUE;
pAttribute->Values.GetNext(pos);
}
return FALSE;
}
return TRUE;
}
void CRoutineDesinView::OnPsAndOp()
{
COutputBrowser OutputBrowser;
OutputBrowser.GetAllData(GetDocument());
OutputBrowser.DoModal();
}
Array* CRoutineDesinView::MSpecialistHandler(CString Spec, Array * List)
{
if (!ConstrainHandler(Spec,0))
return NULL;
Array* TempList = new Array;
Array* ReturnedList = new Array;
CStringList * RankingPlanList ;
RankingPlanList = GetRankingPlanList(Spec);
if (RankingPlanList == NULL) return FALSE;
if (RankingPlanList->IsEmpty())
{
MessageBox("Design failed....");
return NULL;
}
POSITION pos = RankingPlanList->GetHeadPosition();
while (pos != NULL)
{
CString Plan = RankingPlanList->GetAt(pos);
TempList = MPlanHandler(Plan, List);
// if templist == list then fail
for (int i =0 ; i< TempList->GetSize();i++)
{
HTREEITEM hItemSon ;
hItemSon = TempList->GetAt(i);
ReturnedList->Add(hItemSon);
}
// if fail call error handler "to be continue"
ConstrainHandler(Spec,1);
RankingPlanList->GetNext(pos);
}
return ReturnedList;
}
Array* CRoutineDesinView::MPlanHandler(CString Plan, Array * List)
{
CStringList* PlanItemList ;
PlanItemList = GetAllItems(Plan);
Array* ReturnedList = new Array;
POSITION pos = PlanItemList->GetHeadPosition();
while (pos != NULL)
{
CString PlanItem = PlanItemList->GetAt(pos);
if (GetType(PlanItem) == "Task")
{
ReturnedList = MTaskHandler(PlanItem,List);
if (ReturnedList == NULL)
return List;
List = ReturnedList;
}else
{
if (GetType(PlanItem) == "Specialist")
ReturnedList = MSpecialistHandler(PlanItem,List);
if (ReturnedList == NULL)
return List;
List = ReturnedList;
}
if (GetType(PlanItem) == "Constraint")
if (!ConstrainHandler(PlanItem))
return List;
PlanItemList->GetNext(pos);
}
return ReturnedList;
}
Array* CRoutineDesinView::MTaskHandler(CString Task, Array * List)
{
CStringList *StepList ;
StepList = GetAllItems(Task);
Array* ReturnedList = new Array;
POSITION pos = StepList->GetHeadPosition();
while (pos != NULL)
{
CString Step = StepList->GetAt(pos);
ReturnedList= mStepHandler(Step,List);
List = ReturnedList;
//if (List = NULL) return FALSE;
StepList->GetNext(pos);
}
return List;
}
BOOL CRoutineDesinView::ValidNode(HTREEITEM hItemSon,CRow * row)
{
int ColNo = row->Columns.GetSize();
for (int j =0; j < ColNo; j++)
{
CColumn *column = new CColumn;
column = (CColumn *) row->Columns.GetAt(j);
// i do not know why this statment for, but it is necessary
if (column->Attribute.IsEmpty()) continue;
while (hItemSon !=NULL)
{
CNode * Node1 = new CNode;
WTree.Lookup(hItemSon,Node1);
if (Node1->AttributeName == column->Attribute &&
Node1->Value != column->Value)
return FALSE;
hItemSon = m_Tree1.GetParentItem(hItemSon);
}
}
return TRUE;
}
void CRoutineDesinView::OnMarksoll()
{
m_SolList.ResetContent();
POSITION pos = SolutionStack.GetHeadPosition();
while (pos )
{
CHItem * hItem ;
hItem = (CHItem *)SolutionStack.GetAt(pos);
HTREEITEM hItemSon = hItem->hItem;
CNode * Node1 = new CNode;
WTree.Lookup(hItemSon,Node1);
m_SolList.AddString(Node1->AttributeName +" = " + Node1->Value);
SolutionStack.GetNext(pos);
}
/*
HTREEITEM hItemSon1= m_Tree1.GetChildItem(hItemSon);
CNode * Node1 = new CNode;
WTree.Lookup(hItemSon1,Node1);
CString str1 = Node1->AttributeName +" = " + Node1->Value;
AfxMessageBox(str1);
hItemSon1 = m_Tree1.GetNextSiblingItem(hItemSon1);
WTree.Lookup(hItemSon1,Node1);
str1 = Node1->AttributeName +" = " + Node1->Value;
AfxMessageBox(str1);
hItemSon1 = m_Tree1.GetNextSiblingItem(hItemSon1);
WTree.Lookup(hItemSon1,Node1);
str1 = Node1->AttributeName +" = " + Node1->Value;
AfxMessageBox(str1);
if (hItemSon == NULL) return;
*/
}
void CRoutineDesinView::GetSolution(HTREEITEM hItem)
{
HTREEITEM hItemSon;
while ((hItemSon=m_Tree1.GetChildItem(hItem)) != NULL)
{
CHItem * hItem1 = new CHItem;
hItem = hItem1->hItem = hItemSon;
SolutionStack.AddHead((CHItem *)hItem1);
}
}
void CRoutineDesinView::OnNextSolution()
{
OnMarksoll();
CHItem * hItem1 = new CHItem;
if (SolutionStack.GetCount()==0)
{
GetSolution(hItemSon000);
return;
}
while ((hItem1 =(CHItem *)SolutionStack.RemoveHead()) != NULL)
{
CHItem * hItem = new CHItem;
hItem->hItem = m_Tree1.GetNextSiblingItem(hItem1->hItem);
if (hItem->hItem !=NULL)
{
SolutionStack.AddHead((CHItem *)hItem);
GetSolution(hItem->hItem);
return;
}
if (SolutionStack.GetCount()==0)
{
AfxMessageBox("There is no another solution .... ");
GetSolution(hItemSon000);
return;
}
}
}
void CRoutineDesinView::OnPrevSoultion()
{
OnMarksoll();
CHItem * hItem1 = new CHItem;
if (SolutionStack.GetCount()==0)
{
GetSolution(hItemSon000);
return;
}
while ((hItem1 =(CHItem *)SolutionStack.RemoveHead()) != NULL)
{
CHItem * hItem = new CHItem;
hItem->hItem = m_Tree1.GetPrevSiblingItem(hItem1->hItem);
if (hItem->hItem !=NULL)
{
SolutionStack.AddHead((CHItem *)hItem);
GetSolution(hItem->hItem);
return;
}
if (SolutionStack.GetCount()==0)
{
AfxMessageBox("There is no another solution .... ");
GetSolution(hItemSon000);
return;
}
}
}
void CRoutineDesinView::OnShowTree()
{
m_Tree1.ShowWindow(SW_SHOW);
CButton *Btn = (CButton *)GetDlgItem(IDC_NEXTSOLUTION);
CButton *Btn1 = (CButton *)GetDlgItem(IDC_PREVSOULTION);
CButton *Btn2 = (CButton *)GetDlgItem(IDC_MARKSOLL);
Btn->ShowWindow(SW_HIDE);
Btn1->ShowWindow(SW_HIDE);
Btn2->ShowWindow(SW_HIDE);
m_SolList.ShowWindow(SW_HIDE);
CListBox *List1 = (CListBox *)GetDlgItem(IDC_LIST1);
CListBox *List2 = (CListBox *)GetDlgItem(IDC_LIST2);
CListBox *List3 = (CListBox *)GetDlgItem(IDD_LIST3);
CListBox *List4 = (CListBox *)GetDlgItem(IDD_LIST4);
List1->ShowWindow(SW_HIDE);
List2->ShowWindow(SW_HIDE);
List3->ShowWindow(SW_HIDE);
List4->ShowWindow(SW_HIDE);
CButton *Btn11 = (CButton *)GetDlgItem(IDC_PREVSOL);
CButton *Btn12 = (CButton *)GetDlgItem(IDC_NEXTSOL);
Btn11->ShowWindow(SW_HIDE);
Btn12->ShowWindow(SW_HIDE);
}
void CRoutineDesinView::OnShowTable()
{
m_Tree1.ShowWindow(SW_HIDE);
CButton *Btn = (CButton *)GetDlgItem(IDC_NEXTSOLUTION);
CButton *Btn1 = (CButton *)GetDlgItem(IDC_PREVSOULTION);
CButton *Btn2 = (CButton *)GetDlgItem(IDC_MARKSOLL);
Btn->ShowWindow(SW_HIDE);
Btn1->ShowWindow(SW_HIDE);
Btn2->ShowWindow(SW_HIDE);
CListBox *List1 = (CListBox *)GetDlgItem(IDC_LIST1);
CListBox *List2 = (CListBox *)GetDlgItem(IDC_LIST2);
CListBox *List3 = (CListBox *)GetDlgItem(IDD_LIST3);
CListBox *List4 = (CListBox *)GetDlgItem(IDD_LIST4);
List1->ShowWindow(SW_SHOW);
List2->ShowWindow(SW_SHOW);
List3->ShowWindow(SW_SHOW);
List4->ShowWindow(SW_SHOW);
m_SolList.ShowWindow(SW_HIDE);
CButton *Btn11 = (CButton *)GetDlgItem(IDC_PREVSOL);
CButton *Btn12 = (CButton *)GetDlgItem(IDC_NEXTSOL);
Btn11->ShowWindow(SW_SHOW);
Btn12->ShowWindow(SW_SHOW);
}
void CRoutineDesinView::OnShowDialog()
{
m_Tree1.ShowWindow(SW_HIDE);
CButton *Btn = (CButton *)GetDlgItem(IDC_NEXTSOLUTION);
CButton *Btn1 = (CButton *)GetDlgItem(IDC_PREVSOULTION);
CButton *Btn2 = (CButton *)GetDlgItem(IDC_MARKSOLL);
Btn->ShowWindow(SW_SHOW);
Btn1->ShowWindow(SW_SHOW);
Btn2->ShowWindow(SW_SHOW);
m_SolList.ShowWindow(SW_SHOW);
CListBox *List1 = (CListBox *)GetDlgItem(IDC_LIST1);
CListBox *List2 = (CListBox *)GetDlgItem(IDC_LIST2);
CListBox *List3 = (CListBox *)GetDlgItem(IDD_LIST3);
CListBox *List4 = (CListBox *)GetDlgItem(IDD_LIST4);
List1->ShowWindow(SW_HIDE);
List2->ShowWindow(SW_HIDE);
List3->ShowWindow(SW_HIDE);
List4->ShowWindow(SW_HIDE);
CButton *Btn11 = (CButton *)GetDlgItem(IDC_PREVSOL);
CButton *Btn12 = (CButton *)GetDlgItem(IDC_NEXTSOL);
Btn11->ShowWindow(SW_HIDE);
Btn12->ShowWindow(SW_HIDE);
}
void CRoutineDesinView::OnSelchangeList1()
{
CListBox *List1 = (CListBox *)GetDlgItem(IDC_LIST1);
CListBox *List2 = (CListBox *)GetDlgItem(IDC_LIST2);
List2->ResetContent();
CListBox *List3 = (CListBox *)GetDlgItem(IDD_LIST3);
List3->ResetContent();
CListBox *List4 = (CListBox *)GetDlgItem(IDD_LIST4);
List4->ResetContent();
CString Int ;
Int.Format("%d-%d",List1->GetCurSel(),CurrentSol+1);
CHItem * hItem = new CHItem;
// if (!IntToTree.Lookup(Int,(CHItem *)hItem) ) return;
hItem=(CHItem *)IntToTree[Int];
HTREEITEM hItemChild = m_Tree1.GetChildItem(hItem->hItem);
int x =0;
while (hItemChild != NULL)
{
CNode *Node1 = new CNode;
CHItem * hItem1 = new CHItem;
hItem1->hItem = hItemChild;
WTree.Lookup(hItemChild,Node1);
Int.Format("%d-%d", x++,CurrentSol+2);
IntToTree.SetAt(Int,hItem1);
List2->AddString(Node1->AttributeName + " = " + Node1->Value);
hItemChild = m_Tree1.GetNextSiblingItem(hItemChild);
}
}
void CRoutineDesinView::OnSelchangeList2()
{
CListBox *List2 = (CListBox *)GetDlgItem(IDC_LIST2);
CListBox *List3 = (CListBox *)GetDlgItem(IDD_LIST3);
List3->ResetContent();
CString Int ;
Int.Format("%d-%d",List2->GetCurSel(),CurrentSol+2);
CHItem * hItem = new CHItem;
// if (!IntToTree.Lookup(Int,(CHItem *)hItem) ) return;
hItem=(CHItem *)IntToTree[Int];
HTREEITEM hItemChild = m_Tree1.GetChildItem(hItem->hItem);
int x =0;
while (hItemChild != NULL)
{
CNode *Node1 = new CNode;
CHItem * hItem1 = new CHItem;
hItem1->hItem = hItemChild;
WTree.Lookup(hItemChild,Node1);
Int.Format("%d-%d", x++,CurrentSol+3);
IntToTree.SetAt(Int,hItem1);
List3->AddString(Node1->AttributeName + " = " + Node1->Value);
hItemChild = m_Tree1.GetNextSiblingItem(hItemChild);
}
}
/*
void CRoutineDesinView::OnSelchangeList4()
{
CListBox *List3 = (CListBox *)GetDlgItem(IDD_LIST3);
CListBox *List4 = (CListBox *)GetDlgItem(IDD_LIST4);
List3->ResetContent();
CString Int ;
int xx = List4->GetCurSel();
Int.Format("%d-%d",List4->GetCurSel(),3);
CHItem * hItem = new CHItem;
// if (!IntToTree.Lookup(Int,(CHItem *)hItem) ) return;
hItem=(CHItem *)IntToTree[Int];
HTREEITEM hItemChild = m_Tree1.GetChildItem(hItem->hItem);
int x =0;
while (hItemChild != NULL)
{
CNode *Node1 = new CNode;
CHItem * hItem1 = new CHItem;
hItem1->hItem = hItemChild;
WTree.Lookup(hItemChild,Node1);
Int.Format("%d-%d", x++,5);
IntToTree.SetAt(Int,hItem1);
List3->AddString(Node1->AttributeName + " = " + Node1->Value);
hItemChild = m_Tree1.GetNextSiblingItem(hItemChild);
}
}
*/
void CRoutineDesinView::OnSelchangeList3()
{
CListBox *List3 = (CListBox *)GetDlgItem(IDD_LIST3);
CListBox *List4 = (CListBox *)GetDlgItem(IDD_LIST4);
List4->ResetContent();
CString Int ;
int xx = List3->GetCurSel();
Int.Format("%d-%d",List3->GetCurSel(),CurrentSol+3);
CHItem * hItem = new CHItem;
// if (!IntToTree.Lookup(Int,(CHItem *)hItem) ) return;
hItem=(CHItem *)IntToTree[Int];
HTREEITEM hItemChild = m_Tree1.GetChildItem(hItem->hItem);
int x =0;
while (hItemChild != NULL)
{
CNode *Node1 = new CNode;
CHItem * hItem1 = new CHItem;
hItem1->hItem = hItemChild;
WTree.Lookup(hItemChild,Node1);
Int.Format("%d-%d", x++,CurrentSol+4);
IntToTree.SetAt(Int,hItem1);
List4->AddString(Node1->AttributeName + " = " + Node1->Value);
hItemChild = m_Tree1.GetNextSiblingItem(hItemChild);
}
}
void CRoutineDesinView::OnNextsol()
{
CurrentSol = CurrentSol + 1;
OnSelchangeList1();
}
void CRoutineDesinView::OnPrevsol()
{
if (CurrentSol <= 0) return;
CurrentSol = CurrentSol - 1;
OnSelchangeList1();
}
void CRoutineDesinView::OnDeleteAgent()
{
HTREEITEM hItem = m_Tree.GetSelectedItem();
if (hItem == NULL) return;
CString m_AgentName;
m_AgentName = AgentGetName(hItem);
CString ParentType1;
CObList1 & pCollectionList = GetDocument()->CollectionList;
CString ParentType = GetType(hItem);
if (AfxMessageBox("Are you sure of Deletetion.....",MB_YESNO)== IDNO) return;
pCollectionList.RemoveAt(pCollectionList.Find(pCollectionList.Lookup(m_AgentName)));
m_Tree1.DeleteItem(hItem);
CStepList & pStepList = GetDocument()->StepList;
CPlanList & pPlanList = GetDocument()->PlanList;
BOOL x = pStepList.RemoveKey(m_AgentName);
x = pPlanList.RemoveKey(m_AgentName);
MessageBox("Step Deleted.....");
}
void CRoutineDesinView::OnNewCase()
{
CCaseDlg dlg;
int x = dlg.DoModal();
if ( x != 1 ) return;
CurCase = dlg.m_Name;
CVarList & pVarList = GetDocument()->VarList;
POSITION pos = pVarList.GetStartPosition();
while (pos != NULL)
{
CString Key;
CAttribute * pAttribute = new CAttribute;
pVarList.GetNextAssoc(pos, Key, pAttribute);
if (pAttribute == NULL) continue;
if (pAttribute->HasNoInput == FALSE && pAttribute->db == TRUE ) continue;
pAttribute->HasNoInput = TRUE;
pAttribute->Value = "";
pAttribute->Values.RemoveAll();
}
m_Tree1.DeleteAllItems();
WTree.RemoveAll();
CListBox *List1 = (CListBox *)GetDlgItem(IDC_LIST1);
List1->ResetContent();
CListBox *List2 = (CListBox *)GetDlgItem(IDC_LIST2);
List2->ResetContent();
CListBox *List3 = (CListBox *)GetDlgItem(IDD_LIST3);
List3->ResetContent();
CListBox *List4 = (CListBox *)GetDlgItem(IDD_LIST4);
List4->ResetContent();
}
void CRoutineDesinView::OnSaveCase()
{
CCase * Ca = new CCase;
Ca->Case = CurCase;
CVarList & pVarList = GetDocument()->VarList;
CCaseList & CaseList = GetDocument()->CaseList;
POSITION pos = pVarList.GetStartPosition();
while (pos != NULL)
{
CString Key;
CAttribute * pAttribute = new CAttribute;
pVarList.GetNextAssoc(pos, Key, pAttribute);
Ca->VarList.SetAt(pAttribute->Name, pAttribute);
}
CaseList.SetAt(Ca->Case, Ca);
}
void CRoutineDesinView::OnLoadCase()
{
CLoadCase Dlg;
CCaseList & pCaseList = GetDocument()->CaseList;
CNode * node = new CNode;
Dlg.GetAllData(pCaseList,node);
int x = Dlg.DoModal();
if ( x != 1 ) return;
CurCase = node->Value;
CCase * Ca = new CCase;
CCaseList & CaseList = GetDocument()->CaseList;
CVarList & pVarList = GetDocument()->VarList;
if (CaseList.Lookup(CurCase,Ca)== FALSE) return;
POSITION pos = Ca->VarList.GetStartPosition();
while (pos != NULL)
{
CString Key;
CAttribute * pAttribute = new CAttribute;
Ca->VarList.GetNextAssoc(pos, Key, pAttribute);
pVarList[pAttribute->Name] = pAttribute;
}
m_Tree1.DeleteAllItems();
WTree.RemoveAll();
CListBox *List1 = (CListBox *)GetDlgItem(IDC_LIST1);
List1->ResetContent();
CListBox *List2 = (CListBox *)GetDlgItem(IDC_LIST2);
List2->ResetContent();
CListBox *List3 = (CListBox *)GetDlgItem(IDD_LIST3);
List3->ResetContent();
CListBox *List4 = (CListBox *)GetDlgItem(IDD_LIST4);
List4->ResetContent();
}
void CRoutineDesinView::OnFarm()
{
// ChgCol("sprinklerValid","SprinklerValid");
findAtt("N_Analysis");
// findAtt("K_Base");
CFarmDlg Dlg;
CVarList & pVarList = GetDocument()->VarList;
Dlg.GetAllData(pVarList);
Dlg.DoModal();
}
void CRoutineDesinView::OnFert()
{
CFertDlg Dlg;
CVarList & pVarList = GetDocument()->VarList;
Dlg.GetAllData(pVarList);
Dlg.DoModal();
}
void CRoutineDesinView::OnSoil()
{
CSoilAndWater Dlg;
CVarList & pVarList = GetDocument()->VarList;
Dlg.GetAllData(pVarList);
Dlg.DoModal();
}
void CRoutineDesinView::OnEqu()
{
CEquipDataDlg Dlg;
CVarList & pVarList = GetDocument()->VarList;
Dlg.GetAllData(pVarList);
Dlg.DoModal();
}
void CRoutineDesinView::OnDocumentor()
{
CFileDialog cfdlg(FALSE,"*.html","*.html",OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"HTML Files (*.html)");
cfdlg.DoModal();
CString str = cfdlg.GetPathName();
CStdioFile OutFile(str,CFile::modeCreate | CFile::modeWrite );
//
str = "
Design of wheat";
OutFile.WriteString(str);
str = "Design Document of Neper Wheat
";
OutFile.WriteString(str);
CPSAndSpecList & pPSAndSpecList = GetDocument()->PSAndSpecList;
CPSAndSpec * pPSAndSpec;
POSITION pos = pPSAndSpecList.GetStartPosition();
while (pos != NULL)
{
CString Key;
pPSAndSpecList.GetNextAssoc(pos, Key, pPSAndSpec);
str = "Problem Solver:" + pPSAndSpec->PS+"
";
OutFile.WriteString(str);
str = "Top Specialist :" + pPSAndSpec->TopSpec +"
";
OutFile.WriteString(str);
str = GetChildOfSpec("",pPSAndSpec->TopSpec);
OutFile.WriteString(str);
}
OutFile.WriteString("");
OutFile.Close();
}
CString CRoutineDesinView::GetChildOfSpec(CString str1,CString Spec)
{
CAgent * pAgent = new CAgent;
CString Returned=str1;
CPlanList & pPlanList = GetDocument()->PlanList;
CObList1 & pCollectionList = GetDocument()->CollectionList;
POSITION pos = pCollectionList.GetHeadPosition();
int RowNo;
while (pos != NULL)
{
CString Key;
pAgent = (CAgent *)pCollectionList.GetAt(pos);
if (pAgent->Parent != Spec)
{
pCollectionList.GetNext(pos);
continue;
}
Returned = Returned + "Plan:" + pAgent->Name +"
";
CPlan *pPlan = new CPlan;
pPlan = pPlanList[pAgent->Name];
CSponsor *pSponsor = new CSponsor;
pSponsor = (CSponsor *) pPlan->pSponsor;
RowNo = pSponsor->Rows.GetSize();
for (int i =0; i < RowNo; i++)
{
CRow * row = new CRow;
row = (CRow *) pSponsor->Rows.GetAt(i);
int ColNo = row->Columns.GetSize();
if (i==0)
{
Returned = Returned + "Sponsor
";
Returned = Returned + " Row Number | ";
for (int j =0; j < ColNo; j++)
{
CColumn *column = new CColumn;
column = (CColumn *) row->Columns.GetAt(j);
Returned = Returned + ""+column->Attribute+" | ";
}
Returned = Returned + "Results |
";
}
CString s;
s.Format("%d",i+1);
Returned = Returned + ""+s+" | ";
for (int j =0; j < ColNo; j++)
{
CColumn *column = new CColumn;
column = (CColumn *) row->Columns.GetAt(j);
Returned = Returned + ""+ column->Operator+column->Value+ " | ";
}
Returned = Returned + ""+row->Result+" |
";
}
if(RowNo>0)
Returned = Returned + "
";
CStringList* PlanItemList ;
PlanItemList = GetAllItems(pAgent->Name);
POSITION pos1 = PlanItemList->GetHeadPosition();
while (pos1 != NULL)
{
CString PlanItem = PlanItemList->GetAt(pos1);
if (GetType(PlanItem) == "Task")
{
Returned = Returned + "Task:" + PlanItem +"
";
Returned = GetChildOfTask(Returned,PlanItem);
}
if (GetType(PlanItem) == "Specialist")
{
Returned = Returned + "Specialist:" + PlanItem +"
";
Returned = GetChildOfSpec(Returned,PlanItem);
}
PlanItemList->GetNext(pos1);
}
pCollectionList.GetNext(pos);
}
return Returned;
}
CString CRoutineDesinView::GetChildOfTask(CString str, CString Task)
{
CString Returned=str;
CStringList *StepList ;
StepList = GetAllItems(Task);
POSITION pos = StepList->GetHeadPosition();
while (pos != NULL)
{
CString Step = StepList->GetAt(pos);
Returned = Returned + "Step:" + Step +"
";
CStepList & pStepList = GetDocument()->StepList;
CStep * pStep = new CStep;
pStep = pStepList[Step];
Returned = Returned + "Value Holder: " + pStep->AttributeName +"
";
CEvaluationAgent *pEvaluationAgent = new CEvaluationAgent;
pEvaluationAgent = (CEvaluationAgent *) pStep->pEvaluationAgent;
int RowNo = pEvaluationAgent->Rows.GetSize();
for (int i =0; i < RowNo; i++)
{
CRow * row = new CRow;
row = (CRow *) pEvaluationAgent->Rows.GetAt(i);
int ColNo = row->Columns.GetSize();
if (i==0)
{
Returned = Returned + " Row Number | ";
for (int j =0; j < ColNo; j++)
{
CColumn *column = new CColumn;
column = (CColumn *) row->Columns.GetAt(j);
Returned = Returned + ""+column->Attribute+" | ";
}
Returned = Returned + "Results |
";
}
CString s;
s.Format("%d",i+1);
Returned = Returned + ""+s+" | ";
for (int j =0; j < ColNo; j++)
{
CColumn *column = new CColumn;
column = (CColumn *) row->Columns.GetAt(j);
Returned = Returned + ""+ column->Operator+column->Value+ " | ";
}
Returned = Returned + ""+row->Result+" |
";
}
Returned = Returned + "
";
StepList->GetNext(pos);
}
return Returned;
}
void CRoutineDesinView::OnADDFirstAGENT()
{
HTREEITEM hItem = m_Tree.GetSelectedItem();
if (hItem == NULL) return;
CString m_AgentName;
CString ParentType1;
CString ParentType = GetType(hItem);
if (ParentType == "Step")
{
MessageBox("Can not Add Agent to Step ....");
return;
}
if ( ParentType == "Plan")
{
CAddUAgent AddAgentDlg;
if (AddAgentDlg.DoModal() != IDOK) return;
if (AddAgentDlg.m_AgentName.IsEmpty()) return;
m_AgentName = AddAgentDlg.m_AgentName;
ParentType1 = AddAgentDlg.m_Type;
if (ParentType1.IsEmpty())
{
MessageBox("Unkown Agent Type...");
return;
}
}
else
{
CAddAgent AddAgentDlg;
if (AddAgentDlg.DoModal() != IDOK) return;
if (AddAgentDlg.m_AgentName.IsEmpty()) return;
m_AgentName = AddAgentDlg.m_AgentName;
}
CString AgentParentName = AgentGetName(hItem);
CObList1 & pCollectionList = GetDocument()->CollectionList;
CAgent * pAgent = new CAgent;
pAgent = pCollectionList.Lookup(m_AgentName);
if (pAgent)
{
MessageBox("Agent Aleady Exist....");
return;
}
// CString DetailName = m_AgentName + "("+str+")" ;
HTREEITEM hItemSon = m_Tree.InsertItem(m_AgentName,hItem,TVI_FIRST);
// MessageBox(ParentType);
pAgent = new CAgent;
pAgent->Name = m_AgentName;
pAgent->Parent = AgentParentName;
if (ParentType == "Specialist")
{
pAgent->AgentType = "Plan";
CPlanList & pPlanList = GetDocument()->PlanList;
CPlan * pPlan = new CPlan;
pPlan->Name = pAgent->Name;
pPlan->Parent = pAgent->Parent;
pPlanList[pAgent->Name] = pPlan;
pCollectionList.AddHead(pAgent);
m_Tree.Expand(hItem,TVE_EXPAND);
return;
}
if (ParentType == "Plan")
{
pAgent->AgentType = ParentType1;
pCollectionList.AddHead(pAgent);
m_Tree.Expand(hItem,TVE_EXPAND);
return;
}
if (ParentType == "Task")
{
pAgent->AgentType = "Step";
CStepList & pStepList = GetDocument()->StepList;
CStep * pStep = new CStep;
pStep->Name = pAgent->Name;
pStep->Parent = pAgent->Parent;
pStepList[pAgent->Name] = pStep;
pCollectionList.AddHead(pAgent);
m_Tree.Expand(hItem,TVE_EXPAND);
return;
}
}
void CRoutineDesinView::ChgCol(CString str1, CString str2)
{
CStepList & pStepList = GetDocument()->StepList;
POSITION pos = pStepList.GetStartPosition();
while (pos != NULL)
{
CStep * pStep;
CString Key;
pStepList.GetNextAssoc(pos, Key, pStep);
CEvaluationAgent *pEvaluationAgent;
pEvaluationAgent = (CEvaluationAgent *) pStep->pEvaluationAgent;
int RowNo = pEvaluationAgent->Rows.GetSize();
for (int i =0; i < RowNo; i++)
{
CRow * row;
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);
if(column->Attribute == str1)
column->Attribute = str2;
}
}
}
}
void CRoutineDesinView::RunPS(CString PSStr)
{
Back = FALSE;
CPSAndSpec * pPSAndSpec;
CPSAndSpecList & pPSAndSpecList = GetDocument()->PSAndSpecList;
pPSAndSpecList.Lookup(PSStr,pPSAndSpec);
if (Single == TRUE)
{
if (!SpecialistHandler(pPSAndSpec->TopSpec))
MessageBox("Design failed....");
}else
{
CNode *Node1 = new CNode;
Node1->AttributeName = "Construct Of " + PSStr;
Node1->Value = "";
hItemSon000 = m_Tree1.InsertItem(Node1->AttributeName,1,1);
WTree.SetAt(hItemSon000,Node1);
Array *A1= new Array;
A1->Add( hItemSon000);
if (!MSpecialistHandler(pPSAndSpec->TopSpec,A1))
MessageBox("Design failed....");
GetSolution(hItemSon000);
CHItem * hItem = new CHItem;
hItem->hItem= hItemSon000;
CString Int ;
CurrentSol = 0;
Int.Format("%d-%d",0,1);
// IntToTree.SetAt((CString)Int,(CHItem)hItem);
IntToTree[Int] = hItem;
CListBox *List1 = (CListBox *)GetDlgItem(IDC_LIST1);
List1->AddString(Node1->AttributeName );
}
}
void CRoutineDesinView::OnLoadfromNeper()
{
CFileDialog cfdlg(TRUE,"*.txt","*.txt",OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"Text Files (*.txt)");
cfdlg.DoModal();
CString str = cfdlg.GetPathName();
CStdioFile InFile(str,CFile::modeRead );
InFile.ReadString(str);
int Len = str.GetLength();
int i = 0;
while (iVarList;
CAttribute * pAttribute;
if (pVarList.Lookup(var,pAttribute)&& value != "no input")
{
pAttribute->Value = value;
pAttribute->HasNoInput= FALSE;
}
}
}
void CRoutineDesinView::OnLOADCaseN()
{
CFileDialog cfdlg(TRUE,"*.Case","*.Case",OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"Neper Case Files (*.Case)");
cfdlg.DoModal();
CString str = cfdlg.GetPathName();
CStdioFile InFile(str,CFile::modeRead );
while(InFile.ReadString(str))
{
int Len = str.GetLength();
int i = 0;
while (iVarList;
CAttribute * pAttribute;
if ((pVarList.Lookup(var,pAttribute) || MapList->Lookup(var,pAttribute))&&pAttribute->input == 1
&& value != "no input")
{
pAttribute->Value = value;
pAttribute->HasNoInput= FALSE;
}
}
}
}
void CRoutineDesinView::OnMap()
{
CFileDialog cfdlg(TRUE,"*.Map","*.Map",OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"Map Files (*.Map)");
cfdlg.DoModal();
CString str = cfdlg.GetPathName();
CStdioFile InFile(str,CFile::modeRead );
CVarList & pVarList = GetDocument()->VarList;
CAttribute * pAttribute;
MapList = new CVarList;
while(InFile.ReadString(str))
{
CString var,mvar;
int pos = str.Find(":");
if (pos < 0)continue;
for(int i = 0; i< pos;i++)
mvar.Insert(i,str[i]);
for( i = pos+1; i< str.GetLength();i++)
var.Insert(i+1-pos,str[i]);
if (pVarList.Lookup(var,pAttribute))
MapList->SetAt(mvar,pAttribute);
/* POSITION pos1 = pVarList.GetStartPosition();
while (pos1 != NULL)
{
CString Key;
pVarList.GetNextAssoc(pos1, Key, pAttribute);
if (Key == var)
MapList->SetAt(mvar,pAttribute);
}*/
}
}
void CRoutineDesinView::findAtt(CString str1)
{
CStepList & pStepList = GetDocument()->StepList;
POSITION pos = pStepList.GetStartPosition();
while (pos != NULL)
{
CStep * pStep;
CString Key;
pStepList.GetNextAssoc(pos, Key, pStep);
if(pStep->AttributeName == str1)
{
AfxMessageBox("row "+Key);
continue;
}
CEvaluationAgent *pEvaluationAgent;
pEvaluationAgent = (CEvaluationAgent *) pStep->pEvaluationAgent;
int RowNo = pEvaluationAgent->Rows.GetSize();
for (int i =0; i < RowNo; i++)
{
CRow * row;
BOOL found = FALSE;
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);
if(column->Attribute == str1)
{
AfxMessageBox(Key);
found = TRUE;
break;
}
}
if (found) break;
}
}
}
void CRoutineDesinView::OnCompareCase()
{
CFileDialog cfdlg(FALSE,"*.xls","*.xls",OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"Out Files (*.xls)");
cfdlg.DoModal();
CString str = cfdlg.GetPathName();
CStdioFile OutFile(str,CFile::modeCreate | CFile::modeWrite );
CFileDialog cfdlg1(TRUE,"*.In","*.In",OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"In Files (*.In)");
cfdlg1.DoModal();
CString str1 = cfdlg1.GetPathName();
CStdioFile InFile(str1,CFile::modeRead );
while (InFile.ReadString(str))
{
int Len = str.GetLength();
int i = 0;
while (iVarList;
CAttribute * pAttribute;
if (pVarList.Lookup(var,pAttribute)&& value != "no input")
{
pAttribute->Value = value;
pAttribute->HasNoInput= FALSE;
}
*/
CAttribute * pAttribute;
if(!MapList->Lookup(var,pAttribute))continue;
OnMarksoll();
POSITION pos = SolutionStack.GetHeadPosition();
while (pos )
{
CHItem * hItem ;
hItem = (CHItem *)SolutionStack.GetAt(pos);
HTREEITEM hItemSon = hItem->hItem;
CNode * Node1 = new CNode;
WTree.Lookup(hItemSon,Node1);
if (Node1->AttributeName == pAttribute->Name)
{
Node1->Value.Replace("\n"," ");
Node1->Value.Replace("\t"," ");
value.Replace("\n"," ");
value.Replace("\t"," ");
OutFile.WriteString(Node1->AttributeName+"\t"+Node1->Value+"\t"+value+"\n");
break;
}
SolutionStack.GetNext(pos);
}
}
}
OutFile.Close();
}
void CRoutineDesinView::OnShowOperations()
{
COutputDlg OutputDlg;
OutputDlg.GetAllData1(GetDocument(),this);
OutputDlg.DoModal();
}
void CRoutineDesinView::OnDeleteAgentIC()
{
OnDeleteAgent();
}
void CRoutineDesinView::OnAddAgentIC()
{
OnAddAgent();
}
void CRoutineDesinView::OnUpdateAgentIC()
{
OnUpdateAgent();
}
void CRoutineDesinView::OnRunAgentIC()
{
OnRunAgent();
}
void CRoutineDesinView::OnHelp()
{
}
void CRoutineDesinView::OnBrowseDoc()
{
OnDocumentor();
}