// RulePage.cpp : implementation file // #include "stdafx.h" #include "KDate.h" #include "KBEditorsDoc.h" #include "KBEditors.h" #include "RulePage.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CRulePage property page IMPLEMENT_DYNCREATE(CRulePage, CPropertyPage) CRulePage::CRulePage() : CPropertyPage(CRulePage::IDD) { //{{AFX_DATA_INIT(CRulePage) m_Str = _T(""); //}}AFX_DATA_INIT } CRulePage::~CRulePage() { } void CRulePage::DoDataExchange(CDataExchange* pDX) { CPropertyPage::DoDataExchange(pDX); //{{AFX_DATA_MAP(CRulePage) DDX_Control(pDX, IDC_RULEID, m_RuleList); DDX_Control(pDX, IDC_CLUSTERCOMBO, m_Cluster); DDX_Control(pDX, IDC_CPTTREE, m_CptTree); DDX_Text(pDX, IDC_CONDITIONS, m_Str); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CRulePage, CPropertyPage) //{{AFX_MSG_MAP(CRulePage) ON_NOTIFY(NM_DBLCLK, IDC_CPTTREE, OnDblclkCpttree) ON_BN_CLICKED(IDC_ADDACTION, OnAddaction) ON_BN_CLICKED(IDC_SAVERULE, OnSaverule) ON_BN_CLICKED(IDC_NEWRULE, OnNewrule) ON_BN_CLICKED(IDC_FIND, OnFind) ON_BN_CLICKED(IDC_FUNCTION, OnFunction) ON_CBN_SELCHANGE(IDC_CLUSTERCOMBO, OnSelchangeClustercombo) ON_BN_CLICKED(IDC_DELETERULE, OnDeleteRule) ON_CBN_SELCHANGE(IDC_RULEID, OnSelchangeRuleid) ON_BN_CLICKED(IDC_DELCLUSTER, OnDelCluster) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CRulePage message handlers void CRulePage::SeeDoc(CKBEditorsDoc*Doc) { m_Doc = Doc; } BOOL CRulePage::OnSetActive() { return CPropertyPage::OnSetActive(); } void CRulePage::DrawTree() { m_CptTree.DeleteAllItems(); CString str ; POSITION pos = m_Doc->m_CptList.GetStartPosition(); while (pos !=NULL) { m_Doc->m_CptList.GetNextAssoc(pos,str,m_Doc->Cpt); if (m_Doc->Cpt->m_PropList.GetCount() > 0) { HTREEITEM TreeItem = m_CptTree.InsertItem(str,TVI_ROOT,TVI_SORT); POSITION pos1 =m_Doc->Cpt->m_PropList.GetStartPosition(); while (pos1 !=NULL) { CString str1; m_Doc->Cpt->m_PropList.GetNextAssoc(pos1,str1,m_Doc->Prop); HTREEITEM TreeItem1 = m_CptTree.InsertItem(str1,TreeItem); POSITION pos2 = m_Doc->Prop->m_LegalValueList.GetStartPosition(); while (pos2 !=NULL) { CString str2; CString str3; m_Doc->Prop->m_LegalValueList.GetNextAssoc(pos2,str2,str3); m_CptTree.InsertItem(str2,TreeItem1); } } } } FillCluster(); OnSelchangeClustercombo(); } void CRulePage::OnDblclkCpttree(NMHDR* pNMHDR, LRESULT* pResult) { HTREEITEM TreeItem = m_CptTree.GetSelectedItem(); CString str1 = m_CptTree.GetItemText(TreeItem); HTREEITEM TreeItem1 = m_CptTree.GetParentItem(TreeItem); if (TreeItem1==NULL)return; CString str2 = m_CptTree.GetItemText(TreeItem1); CString str; CString m_str; GetDlgItemText(IDC_CONDITIONS,m_str); HTREEITEM TreeItem2 = m_CptTree.GetParentItem(TreeItem1); if (TreeItem2==NULL) { str = "@" + str2 +"."+str1; str = m_str + str; SetDlgItemText(IDC_CONDITIONS,str); return; } CString str3 = m_CptTree.GetItemText(TreeItem2); //str1 = str1.Mid(1,str1.GetLength()-1); //str1 = str1.SpanExcluding("'"); str1 = "\"" + str1 + "\""; str = "@" + str3 +"."+str2 +"=="+str1; str = m_str + str; SetDlgItemText(IDC_CONDITIONS,str); *pResult = 0; } void CRulePage::OnAddaction() { HTREEITEM TreeItem = m_CptTree.GetSelectedItem(); CString str1 = m_CptTree.GetItemText(TreeItem); HTREEITEM TreeItem1 = m_CptTree.GetParentItem(TreeItem); if (TreeItem1==NULL)return; CString str2 = m_CptTree.GetItemText(TreeItem1); CString str; CString m_str; GetDlgItemText(IDC_ACTIONS,m_str); HTREEITEM TreeItem2 = m_CptTree.GetParentItem(TreeItem1); if (TreeItem2==NULL) { str = "@" + str2 +"."+str1; str = m_str + str; SetDlgItemText(IDC_ACTIONS,str); return; } CString str3 = m_CptTree.GetItemText(TreeItem2); str1 = "\"" + str1 + "\""; str = "@" + str3 +"."+str2 +"="+str1; str = m_str + str; SetDlgItemText(IDC_ACTIONS,str); } void CRulePage::OnSaverule() { CRule *Rule = new CRule; CString str,str1; GetDlgItemText(IDC_CONDITIONS,str); if( !ValidateCondition(str,str1)) { AfxMessageBox("Condition is not valid"); AfxMessageBox(str1); // return; } Rule->SetCondition(str); GetDlgItemText(IDC_ACTIONS,str); if( !ValidateAction(str,str1)) { AfxMessageBox("Action is not valid"); // AfxMessageBox(str1); // return; } Rule->SetAction(str); int x = GetDlgItemInt(IDC_PRIORITY); Rule->Priority=x; GetDlgItemText(IDC_RULEID,str); Rule->RuleID = str; GetDlgItemText(IDC_CLUSTERCOMBO,str); if (!m_Doc->m_ClusterList.Lookup(str,m_Doc->Cluster)) { m_Doc->Cluster = new CCluster; m_Doc->Cluster->ClusterID= str; m_Cluster.AddString(str); } m_Doc->Cluster->RuleList.SetAt(Rule->RuleID,Rule); m_Doc->m_ClusterList.SetAt(m_Doc->Cluster->ClusterID,m_Doc->Cluster); m_RuleList.AddString(Rule->RuleID); SetDlgItemText(IDC_RULEID,""); m_Doc->OnSaveDocument(m_Doc->AppName); } void CRulePage::OnNewrule() { CString str=""; SetDlgItemText(IDC_RULEID,str); SetDlgItemText(IDC_ACTIONS,str); SetDlgItemText(IDC_CONDITIONS,str); int x = 1; SetDlgItemInt(IDC_PRIORITY,x); } void CRulePage::OnFind() { CString str; GetDlgItemText(IDC_CLUSTERCOMBO,str); if (!m_Doc->m_ClusterList.Lookup(str,m_Doc->Cluster)) return; int Sel = m_RuleList.GetCurSel(); if (Sel < 0 ) return; m_RuleList.GetLBText(Sel,str); if (!m_Doc->Cluster->RuleList.Lookup(str,m_Doc->Rule)) return; SetDlgItemText(IDC_ACTIONS,m_Doc->Rule->GetAction()); SetDlgItemText(IDC_CONDITIONS,m_Doc->Rule->GetCondition()); SetDlgItemInt(IDC_PRIORITY,m_Doc->Rule->Priority); } void CRulePage::FillCluster() { POSITION pos =m_Doc->m_ClusterList.GetStartPosition(); while (pos) { CString str ; m_Doc->m_ClusterList.GetNextAssoc(pos,str,m_Doc->Cluster); m_Cluster.AddString(str); } // m_Cluster.SetCurSel(0); } BOOL CRulePage::ValidateCondition(CString strCond,CString& retStr) { strCond.TrimLeft();strCond.TrimRight(); if(strCond == "true") return TRUE; int x; strCond = RemoveSpace(strCond); x=strCond.Find("@"); CString strConc; while(x>=0) { int pos=x+1; CString strCpt; CString strProp; while(strCond[pos] !='.') { strCpt = strCpt + strCond[pos++]; } pos++; while(pos < strCond.GetLength() && strCond[pos] != '=' && strCond[pos] !='>' && strCond[pos] !='+' && strCond[pos] !='-' && strCond[pos] !='<' && strCond[pos] !=' ' &&strCond[pos] !='*' &&strCond[pos] !='/' && strCond[pos] !='&' && strCond[pos] !='|' && strCond[pos] !='!' && strCond[pos] !=')' ) { strProp = strProp + strCond[pos++]; } strConc = "@"+strCpt + "."+ strProp; if (!m_Doc->m_CptList.Lookup(strCpt,m_Doc->Cpt)) { AfxMessageBox(strCpt + " Is Not Found..."); return FALSE; } if (!m_Doc->Cpt->m_PropList.Lookup(strProp,m_Doc->Prop)) { AfxMessageBox("Property " + strProp + " Is Not Found in Concept" + strCpt ); return FALSE; } if (m_Doc->Prop->Type == "real" || m_Doc->Prop->Type == "integer" || m_Doc->Prop->Type == "date") strCond.Replace(strConc,"0"); else strCond.Replace(strConc,"\"X\""); x=strCond.Find("@"); if (x==0)break; } CRule * Rule = new CRule; strCond = ReplaceNow(strCond); strCond = ValidateDateFun(strCond); strCond = Rule->ReplaceAbsDate(strCond); // long * ret=new long; // return !m_tcl.TclExprBoolean(strCond,ret); /* long r = m_tcl.TclEval(strCond); r = m_tcl.TclExprString(strCond); retStr = strCond; //long r = m_tcl.TclExprBoolean(strCond,ret); delete ret; */ BOOL r = Rule->CEval(strCond); delete Rule; return !r; } BOOL CRulePage::ValidateAction(CString strAction,CString& retStr) { if (strAction.IsEmpty()) return TRUE; strAction.TrimRight();strAction.TrimLeft(); char LastCh = strAction[strAction.GetLength()-1]; if (LastCh !=';') return FALSE; strAction.Delete(strAction.GetLength()-1,1); strAction.Replace(";","&&"); strAction.Replace("=","=="); if(strAction.Find("=") == -1) return FALSE; return ValidateCondition(strAction,retStr); } void CRulePage::OnFunction() { // TODO: Add your control notification handler code here } void CRulePage::OnSelchangeClustercombo() { m_RuleList.ResetContent(); int Sel = m_Cluster.GetCurSel(); if (Sel < 0 ) return; CString str; m_Cluster.GetLBText(Sel,str); m_Doc->m_ClusterList.Lookup(str,m_Doc->Cluster); POSITION pos = m_Doc->Cluster->RuleList.GetStartPosition(); while (pos !=NULL) { m_Doc->Cluster->RuleList.GetNextAssoc(pos,str,m_Doc->Rule); m_RuleList.AddString(m_Doc->Rule->RuleID); } } void CRulePage::OnDeleteRule() { int Sel = m_Cluster.GetCurSel(); if (Sel < 0 ) return; CString str; m_Cluster.GetLBText(Sel,str); m_Doc->m_ClusterList.Lookup(str,m_Doc->Cluster); CString str1; GetDlgItemText(IDC_RULEID,str1); CRule * Rule; m_Doc->Cluster->RuleList.Lookup(str1,Rule); m_Doc->Cluster->RuleList.RemoveKey(str1); delete Rule; OnSelchangeClustercombo(); } void CRulePage::OnSelchangeRuleid() { OnFind(); } CString CRulePage::ValidateDateFun(CString str) { int x; str.MakeLower(); for (int i=1;i<4;i++) { CString strFind; switch(i) { case 1: strFind = "month"; break; case 2: strFind = "year"; break; case 3: strFind = "day"; break; } x=str.Find(strFind); CString strConc; while(x>=0) { int pos=x+strFind.GetLength(); CString strDate; while(str[pos] !=')') strDate = strDate + str[pos++]; strDate = strDate + str[pos]; // if (ValidateDate( strDate)) { str.SetAt(x,'0'); str.Delete(x+1,pos-x); }//else break; x=str.Find(strFind); if (x==-1) break; } } return str; } BOOL CRulePage::ValidateDate(CString str) { CKDate KDate(str); return KDate.s0(); } CString CRulePage::ReplaceNow(CString str) { CTime t = CTime::GetCurrentTime(); CString Temp; str.MakeLower(); Temp.Format("#%d/%d/%d#",t.GetDay(),t.GetMonth(),t.GetYear()); str.Replace("now",Temp); return str; } //DEL CString CRulePage::ReplaceAbsDate(CString str) //DEL { //DEL int x; //DEL x=str.Find("#"); //DEL while(x>=0) //DEL { //DEL CString strDate,strDays; //DEL int pos=x+1; //DEL while(pos < str.GetLength() && str[pos] !='#' ) //DEL strDate = strDate + str[pos++]; //DEL strDate = "#" +strDate+"#"; //DEL CTime tAbs(1965,1,1); //DEL CTime t = StrToDate(strDate,0); //DEL CTimeSpan ts=t-tAbs; //DEL strDays.Format("%d",ts.GetDays()); //DEL str.Replace(strDate,strDays); //DEL x=str.Find("#"); //DEL if (x==-1) break; //DEL } //DEL return str; //DEL } //DEL CTime CRulePage::StrToDate(CString str, long Expr) //DEL { //DEL int pos=1; //DEL CString Temp = ""; //DEL //DEL long * ret = new long; //DEL while(str[pos] !='/') //DEL Temp = Temp + str[pos++]; //DEL m_tcl.TclExprLong(Temp,ret); //DEL long day =*ret; //DEL Temp = ""; //DEL pos+=1; //DEL while(str[pos] !='/') //DEL Temp = Temp + str[pos++]; //DEL m_tcl.TclExprLong(Temp,ret); //DEL long month =*ret; //DEL Temp = ""; //DEL pos+=1; //DEL while(str[pos] !='#') //DEL Temp = Temp + str[pos++]; //DEL m_tcl.TclExprLong(Temp,ret); //DEL long year =*ret; //DEL if (year < 50 ) year +=2000; //DEL else //DEL if (year < 100 ) year +=1900; //DEL CTime t(year,month,day,0,0,0); //DEL CTimeSpan ts(Expr,0,0,0); //DEL t = t+ts; //DEL delete ret; //DEL return t; //DEL } CString CRulePage::RemoveSpace(CString strCond) { CString Temp; Temp.Empty(); for(int i = 0;im_ClusterList.Lookup(str1,m_Doc->Cluster); POSITION pos = m_Doc->Cluster->RuleList.GetStartPosition(); while (pos !=NULL) { CRule * Rule1; CString str; m_Doc->Cluster->RuleList.GetNextAssoc(pos,str,Rule1); delete Rule1; } m_Doc->Cluster->RuleList.RemoveAll(); m_Doc->m_ClusterList.RemoveKey(str1); m_Cluster.DeleteString(Sel); m_Cluster.SetWindowText(""); m_RuleList.ResetContent(); }