// KBEditorsDoc.cpp : implementation of the CKBEditorsDoc class // #include "stdafx.h" #include "tcl.h" #include #include "General.h" //#include "NominalDlg.h" //#include "NumberDlg.h" //#include "DateDlg.h" /****************************************/ IMPLEMENT_SERIAL(CKBDB, CObject, VERSIONABLE_SCHEMA | 1 ) CKBDB::CKBDB() { } CKBDB::CKBDB(CString strCpt,CString strProp, CString strTable, CString strField) { Cpt = strCpt ; Prop = strProp ; Table = strTable ; Field = strField ; } void CKBDB::GetCptPropTblFld(CString& c,CString& p, CString& t, CString& f) { c = Cpt ; p = Prop ; t = Table ; f = Field ; } void CKBDB ::Serialize(CArchive& ar) { if (ar.IsStoring()){ ar << Cpt; ar << Prop; ar << Table; ar << Field; } else{ int nShema=ar.GetObjectSchema(); switch(nShema){ case 0: case 1: ar >> Cpt; ar >> Prop; ar >> Table; ar >> Field; break; default: AfxMessageBox ("Error: Invalid CKBDB Format"); return; } } } IMPLEMENT_SERIAL(CTblKeys, CObject, VERSIONABLE_SCHEMA | 1 ) CTblKeys::CTblKeys() { } CTblKeys:: CTblKeys(CStringArray& Keys,CStringArray& KeysType) { for(int i=0;i> ServiceName; ar >> TriggerCondition; ar >> Input; ar >> InputType; ar >> Output; break; default: // AfxMessageBox ("Error: Invalid Service Format"); return; } } } IMPLEMENT_SERIAL(CCoordinator, CObject, VERSIONABLE_SCHEMA | 1 ) CCoordinator::CCoordinator() { } CCoordinator::CCoordinator(CKBEditorsView* m_View,CKBEditorsDoc* m_Doc) { pView = m_View; pDoc = m_Doc; } CCoordinator::~CCoordinator() { } void CCoordinator::Start() { // Display General Dlg to get the common Value } void CCoordinator::Serialize(CArchive& ar) { if (ar.IsStoring()) { } else { } } /****************************************/ /*IMPLEMENT_SERIAL(CRow, CObject, 0) CRow::CRow() { } CRow::~CRow() { } void CRow::Serialize(CArchive& ar) { if (ar.IsStoring()) { ar << Result; } else { ar >> Result; } Columns.Serialize(ar); }*/ IMPLEMENT_SERIAL(CCell, CObject, VERSIONABLE_SCHEMA | 1) CCell::CCell() { } CCell::~CCell() { } void CCell::Serialize(CArchive& ar) { if (ar.IsStoring()) { ar << Operator; ar << Attribute; ar << Value; } else { int nShema=ar.GetObjectSchema(); switch(nShema){ case 0: case 1: ar >> Operator; ar >> Attribute; ar >> Value; break; default: // AfxMessageBox ("Error: Invalid Table Cell Format"); return; } } } CTable::CTable() { MaxRows = 1; } CTable::~CTable() { } IMPLEMENT_SERIAL(CTable, CObject, VERSIONABLE_SCHEMA | 1 ) void CTable::Serialize(CArchive& ar) { if (ar.IsStoring()) { ar << MaxRows; //ar << Result; Schema.Serialize(ar); Body.Serialize(ar); ResultList.Serialize(ar); } else { int nShema=ar.GetObjectSchema(); switch(nShema){ case 0: case 1: ar >> MaxRows; //ar >> Result; Schema.Serialize(ar); Body.Serialize(ar); ResultList.Serialize(ar); break; default: // AfxMessageBox ("Error: Invalid Table Format"); return; } } } void CTable::Play(CWMStructure * m_WM,CCptList * pCptList) { int MaxCols = Schema.GetSize(); CRule *r = new CRule; for (int i=1;i<=MaxRows;i++){ CString strRow; for(int j=0;j<=MaxCols;j++){ CString Index; Index.Format("%d-%d",i,j); CCell *Cell; if(Body.Lookup(Index,Cell)){ CString ret= GetSeparator(Cell->Attribute,pCptList); strRow += "@"+Cell->Attribute + Cell->Operator + ret + Cell->Value + ret +" && "; } } strRow +="1==1"; r->SetCondition(strRow); CString sstr,s; ResultList.Lookup(i,sstr); ResultList.Lookup(0,s); sstr.Delete(0,3); CString ret= GetSeparator(s,pCptList); sstr = "@"+s+"="+ret+sstr+ret+";"; r->SetAction(sstr); // AfxMessageBox(r->Condition); // AfxMessageBox(r->Action); r->Run(m_WM,pCptList); CString type = r->GetType(s,pCptList); if ( type !="Multi-Value" ){ delete r; return; } } delete r; } CString CTable::GetSeparator(CString Att, CCptList *pCptList) { CRule *r = new CRule; CString type = r->GetType(Att,pCptList); delete r; if (type=="Integer" || type=="Real") return " "; if (type=="date") return "#"; if (type=="nominal" || type=="Multi-Vale") return "\""; return "\""; } void CTable::SetCell(int Row,int Col,CString OPVal) { CString Index; Index.Format("%d-%d",Row,Col); CCell *Cell = new CCell; CString OP; int index=OPVal.Find('-'); OP = OPVal.Left(index); Cell->Value = OPVal.Right(OPVal.GetLength() - index - 1); Cell->Operator = OP; Cell->Attribute = Schema[Col]; Body.SetAt(Index,Cell); } CCell * CTable::GetCell(int Row,int Col) { CString Index; Index.Format("%d-%d",Row,Col); CCell *Cell; if (Body.Lookup(Index,Cell)) return Cell; Cell = new CCell; return Cell; } //#include "KBEditorsView.h" CImageOb::CImageOb() { } CImageOb::~CImageOb() { } IMPLEMENT_SERIAL(CImageOb, CObject, VERSIONABLE_SCHEMA | 1 ) void CImageOb::Serialize(CArchive& ar) { if (ar.IsStoring()) { m_ImageList.Serialize(ar); } else { int nShema=ar.GetObjectSchema(); switch(nShema){ case 0: case 1: m_ImageList.Serialize(ar); break; default: // AfxMessageBox ("Error: Invalid ImageOb Format"); return; } } } IMPLEMENT_SERIAL(CValueList, CObject,VERSIONABLE_SCHEMA | 1 ) CValueList::CValueList() { } CValueList::~CValueList() { } void CValueList::Serialize(CArchive& ar) { if (ar.IsStoring()) { m_CValueList.Serialize(ar); } else { int nShema=ar.GetObjectSchema(); switch(nShema){ case 0: case 1: m_CValueList.Serialize(ar); break; default: // AfxMessageBox ("Error: Invalid ValueList Format"); return; } } } IMPLEMENT_SERIAL(CValue, CObject, VERSIONABLE_SCHEMA | 1) CValue::CValue() { } CValue::~CValue() { } void CValue::Serialize(CArchive& ar) { if (ar.IsStoring()) { ar << V; } else { int nShema=ar.GetObjectSchema(); switch(nShema){ case 0: case 1: ar >> V; break; default: // AfxMessageBox ("Error: Invalid Value Format"); return; } } } IMPLEMENT_SERIAL(CCluster, CObject, VERSIONABLE_SCHEMA | 1 ) CCluster::CCluster() { } CCluster::~CCluster() { } void CCluster::Serialize(CArchive& ar) { if (ar.IsStoring()) { ar << ClusterID; Input.Serialize(ar); Output.Serialize(ar); RuleList.Serialize(ar); } else { int nShema=ar.GetObjectSchema(); switch(nShema){ case 0: case 1: ar >> ClusterID; Input.Serialize(ar); Output.Serialize(ar); RuleList.Serialize(ar); break; default: // AfxMessageBox ("Error: Invalid Cluster Format"); return; } } } IMPLEMENT_SERIAL(CRule, CObject, VERSIONABLE_SCHEMA | 1 ) CRule::CRule() { } CRule::~CRule() { } void CRule::Serialize(CArchive& ar) { if (ar.IsStoring()) { ar << RuleID; ar << Condition; ar << Action; ar << Priority; } else { int nShema=ar.GetObjectSchema(); switch(nShema){ case 0: case 1: ar >> RuleID; ar >> Condition; ar >> Action; ar >> Priority; break; default: //AfxMessageBox ("Error: Invalid Rule Format"); return; } } } IMPLEMENT_SERIAL(CInference, CObject, VERSIONABLE_SCHEMA | 1 ) CInference::CInference() { } CInference::~CInference() { } void CInference::Serialize(CArchive& ar) { if (ar.IsStoring()) { ar << InferenceID; StaticRoleList.Serialize(ar); } else { int nShema=ar.GetObjectSchema(); switch(nShema){ case 0: case 1: ar >> InferenceID; StaticRoleList.Serialize(ar); break; default: // AfxMessageBox ("Error: Invalid Inference Format"); return; } } } IMPLEMENT_SERIAL(CConcept, CObject, VERSIONABLE_SCHEMA | 1) CConcept::CConcept() { } CConcept::~CConcept() { } void CConcept::Serialize(CArchive& ar) { if (ar.IsStoring()){ ar << NameL; ar << SupperNID; ar << nID; ar << NameA; ar << DescriptionA; ar << DescriptionL; m_PropList.Serialize(ar); } else { int nShema=ar.GetObjectSchema (); switch(nShema){ case 0: case 1: ar >> NameL; ar >> SupperNID; ar >> nID; ar >> NameA; ar >> DescriptionA; ar >> DescriptionL; m_PropList.Serialize(ar); break; default: // AfxMessageBox ("Error: Invalid Concept Format"); return; } } } IMPLEMENT_SERIAL(CProperty, CObject, VERSIONABLE_SCHEMA | 1 ) CProperty::CProperty() { } CProperty::~CProperty() { } void CProperty::Serialize(CArchive& ar) { if (ar.IsStoring()) { ar << NameL; ar << NameA; ar << PromptA; ar << PromptL; ar << Type; ar << UL; ar << LL; ar << Default; ar << nID; ar << Necessary; ar << ControlID; m_LegalValueList.Serialize(ar); m_SearchOrderList.Serialize(ar); } else { int nShema=ar.GetObjectSchema (); switch(nShema){ case 0: case 1: ar >> NameL; ar >> NameA; ar >> PromptA; ar >> PromptL; ar >> Type; ar >> UL; ar >> LL; ar >> Default; ar >> nID; ar >> Necessary; ar >> ControlID; m_LegalValueList.Serialize(ar); m_SearchOrderList.Serialize(ar); break; default: // AfxMessageBox ("Error: Invalid Property Format"); return; } } } BOOL CRule::Run(CWMStructure * m_WM,CCptList*pCptList) { //Helly Begin 24_3_2001 //if(Condition.IsEmpty()){ Condition.TrimLeft();Condition.TrimRight(); if(Condition== "true"){ AddActionToWM(Action, m_WM,pCptList); return TRUE; } //Helly End 24_3_2001 CString strEval = Substitute(RemoveSpace(Condition),m_WM,pCptList); if ( CEval(strEval)) { AddActionToWM(Action, m_WM,pCptList); return TRUE; } return FALSE; } CString CRule::RemoveSpace(CString strCond) { CString Temp; Temp.Empty(); BOOL quote = FALSE; for(int i = 0;i=0) { int pos=x+1; CString strCpt; CString strProp; while(str[pos] !='.') { strCpt = strCpt + str[pos++]; } pos++; while(pos < str.GetLength() && str[pos] != '=' && str[pos] !='>' && str[pos] !='<' && str[pos] !='+' &&str[pos] !='-' && str[pos] !='%' && str[pos] !='&' && str[pos] !='|' &&str[pos] !='*' &&str[pos] !='/' && str[pos] !=')' && str[pos] !='!' ) { strProp = strProp + str[pos++]; } strConc = "@"+strCpt + "."+ strProp; strConc1 = "$"+strCpt + "."+ strProp; CString strCptProp=strCpt + "-"+ strProp; CValue *Value = new CValue; CValueList * Arr ; CString strType = GetType(strCpt,strProp,pCptList); if (!m_WM->Lookup(strCptProp,Arr)) { CString defStr = GetDefaultValue(strCpt,strProp,pCptList); if (defStr.IsEmpty()) { str.Replace(strConc,strConc1); } else {//replace string with defstr if (strType == "integer" || strType == "real"|| strType == "date") str.Replace(strConc,defStr); else str.Replace(strConc,"\"" +defStr+"\""); SetToWM(strCpt,strProp,defStr,m_WM,pCptList); // add the defstr into wm } }else { if (strType == "Multi-Value") { int pos1 = pos; CString strCptPropVal = strConc; CString strVal1; while(str[pos1] !='\"') { strCptPropVal = strCptPropVal + str[pos1++]; } strCptPropVal = strCptPropVal + str[pos1++]; while(str[pos1] !='\"') { strCptPropVal = strCptPropVal + str[pos1]; strVal1 = strVal1+str[pos1++]; } strCptPropVal = strCptPropVal + str[pos1]; BOOL found = FALSE; for ( int i = 0;i< Arr->m_CValueList.GetSize();i++) { Value = (CValue*)Arr->m_CValueList.GetAt(i); if(Value->V==strVal1) { int x = strCptPropVal.Find("!="); if(x ==-1) str.Replace(strCptPropVal,"1==1"); else str.Replace(strCptPropVal,"1>1"); found = TRUE; break; } } if (!found) if (strCptPropVal.Find("!") >0) str.Replace(strCptPropVal,"1==1"); else str.Replace(strCptPropVal,"1>1"); } else { Value = (CValue*)Arr->m_CValueList.GetAt(0);//Helly what about multi-value if (strType == "integer" || strType == "real"|| strType == "date") { str.Replace(strConc,Value->V); } else { CString str1 = "\"" +Value->V +"\""; str.Replace(strConc,str1); } } } x=str.Find("@"); if (x==0)break; } str = AskingCheck(str,m_WM,pCptList); str = EvalDate(str); return str; } CString CRule::GetType(CString strCpt, CString strProp, CCptList * pCptList) { CConcept * Cpt; CProperty * Prop; if(!pCptList->Lookup(strCpt,Cpt)) return ""; if(!Cpt->m_PropList.Lookup(strProp,Prop)) return ""; return Prop->Type; } CString CRule::GetType(CString str, CCptList *pCptList) { CString ssCpt = str.SpanExcluding("."); ssCpt.TrimLeft();ssCpt.TrimRight(); str.MakeReverse(); CString ssProp = str.SpanExcluding("."); ssProp.MakeReverse(); ssProp.TrimLeft();ssProp.TrimRight(); return (GetType(ssCpt,ssProp,pCptList)); } CString CRule::EvalDate(CString str) { str = ReplaceNow(str); str = EvalDateFun(str); str = ReplaceAbsDate(str); return str; } CString CRule::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; } CString CRule::EvalDateFun(CString str) { int x,posR; 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; CString strDate1,strOld; while(str[pos] !=')') strDate = strDate + str[pos++]; strDate = strDate + str[pos]; posR = strDate.Find("#"); while(strDate[++posR] !='#') strDate1 = strDate1 + strDate[posR]; strDate1 = "#" +strDate1+"#"; strOld = strDate; strDate.Replace(strDate1,"0"); int ret; int Rep; // pView->m_Tcl.TclExprLong(strDate,ret); ret = atoi((LPCTSTR)strDate); CTime t = StrToDate(strDate1,ret); switch(i) { case 1: Rep = t.GetMonth(); break; case 2: Rep = t.GetYear(); break; case 3: Rep = t.GetDay(); break; } //str.Delete(x,strFind.GetLength()); CString temp;temp.Format("%d",Rep); str.Replace(strFind+strOld,temp); //Yehia maybe this statment replaced to be ==> str.Replace("\""+strOld+"\"",temp); x=str.Find(strFind); if (x==-1) break; } } return str; } CString CRule::ReplaceAbsDate(CString str) { int x; x=str.Find("#"); while(x>=0) { CString strDate,strDays; int pos=x+1; while(pos < str.GetLength() && str[pos] !='#' ) strDate = strDate + str[pos++]; strDate = "#" +strDate+"#"; CTime tAbs(1970,1,1); CTime t = StrToDate(strDate,0); CTimeSpan ts=t-tAbs; strDays.Format("%d",ts.GetDays()+1); str.Replace("\""+strDate+"\"",strDays); str.Replace(strDate,strDays); x=str.Find("#"); if (x==-1) break; } return str; } CTime CRule::StrToDate(CString str,int Expr) { int pos=1; CString Temp = ""; int day, month, year ; while(str[pos] !='/') Temp = Temp + str[pos++]; day = atoi((LPCTSTR)Temp); Temp = ""; pos+=1; while(str[pos] !='/') Temp = Temp + str[pos++]; month = atoi((LPCTSTR)Temp); Temp = ""; pos+=1; while(str[pos] !='#') Temp = Temp + str[pos++]; year = atoi((LPCTSTR)Temp); if (year < 50 ) year +=2000; else if (year < 100 ) year +=1900; CTime t(year,month,day,0,0,0); CTimeSpan ts(Expr,0,0,0); t = t+ts; return t; } void CRule::AddActionToWM(CString strAction, CWMStructure * m_WM,CCptList * pCptList) { int x=strAction.Find("@"); CString strConc; while(x>=0) { int pos=x+1; CString strCpt; CString strProp; while(strAction[pos] !='.') { strCpt = strCpt + strAction[pos++]; } pos++; while(pos < strAction.GetLength() && strAction[pos] != '=' && strAction[pos] != ' ' ) { strProp = strProp + strAction[pos++]; } strConc = "@"+strCpt + "."+ strProp; strAction.Delete(strAction.Find(strConc),strConc.GetLength()); strAction.TrimRight();strAction.TrimLeft(); CString strVal = strAction.SpanExcluding(";"); strVal.TrimRight();strVal.TrimLeft(); strVal.Delete(strVal.Find("="),1); // delete = strVal.TrimLeft();strVal.TrimRight(); strAction.Delete(0,strAction.Find(";")+1); // delete ";" strAction.TrimRight();strAction.TrimLeft(); CConcept * Cpt; CProperty * Prop; CString strCptProp = strCpt+"-"+strProp; if(!pCptList->Lookup(strCpt,Cpt))return; if(!Cpt->m_PropList.Lookup(strProp,Prop))return; if (Prop->Type == "nominal" || Prop->Type == "string" ||Prop->Type == "Multi-Value") { strVal.Delete(0,1); strVal = strVal.SpanExcluding("\""); }else { if (strVal.FindOneOf("()+-/*@")!=-1) strVal = EvalFunction(strVal,m_WM); } if (Prop->Type == "date" ) { // Helly 14_3_2001 Begin // added becaus action may be = now only if(strVal == "now") strVal = ReplaceNow(strVal); // else{ // //Helly 14_3_2001 End int xdays = atoi(strVal); strVal.Empty(); CTime tAbs(1970,1,1); CTimeSpan ts(xdays,0,0,0); tAbs = tAbs + ts; strVal.Format("#%d/%d/%d#",tAbs.GetDay(),tAbs.GetMonth(),tAbs.GetYear()); } } CValue *Value = new CValue; CValueList * Arr= new CValueList; Value->V = strVal; if (Prop->Type == "Multi-Value") { if(!m_WM->Lookup(strCptProp,Arr)) { // Arr= new CValueList; Arr->m_CValueList.SetAtGrow(0,Value); } else { int found =0; for (int i=0; im_CValueList.GetSize();i++) { Value=(CValue*)Arr->m_CValueList.GetAt(i); if (Value->V == strVal) { found =1; break; } } if (found == 0) { Value = new CValue; Value->V = strVal; Arr->m_CValueList.SetAtGrow(Arr->m_CValueList.GetSize(),Value); } } } else { Arr->m_CValueList.SetAtGrow(0,Value); } m_WM->SetAt(strCptProp,Arr); x=strAction.Find("@"); if (x==-1)break; } } CString CRule::EvalFunction(CString strExpr,CWMStructure * m_WM) { int x=strExpr.Find("@"); CString strConc; while(x>=0) { int pos=x+1; CString strCpt; CString strProp; while(strExpr[pos] !='.') { strCpt = strCpt + strExpr[pos++]; } pos++; // int term = strExpr.FindOneOf("(,)+-/*"); // instead of this statment the for loop because of using functions // so i need to begin search from pos not from 0 int term = strExpr.GetLength(); //helly "now - @plant.SedlingDate" does not get the property so term must be init to strExpr.GetLength() for(int i = pos; i < strExpr.GetLength();i++) { if (strExpr[i]== '(' || strExpr[i]== ',' || strExpr[i]== ')' || strExpr[i]== '-' || strExpr[i]== '+' || strExpr[i]== '/' || strExpr[i]== '*') { term = i; break; } } while(pos < term && pos < strExpr.GetLength() && strExpr[pos] != '=' && strExpr[pos] != ' ' ) { strProp = strProp + strExpr[pos++]; } strConc = "@"+strCpt + "."+ strProp; CString strCptProp = strCpt+"-"+strProp; CValue *Value = new CValue; CValueList * Arr= new CValueList; m_WM->Lookup(strCptProp,Arr); Value=(CValue*)Arr->m_CValueList.GetAt(0); strExpr.Replace(strConc,Value->V); x=strExpr.Find("@"); if (x==-1)break; } strExpr=EvalDate(strExpr); double ret; ret = CEvalExp(strExpr); strExpr.Format("%-.2f",(float)ret); return strExpr; } CString CRule::GetDefaultValue(CString strCpt, CString strProp,CCptList*pCptList) { CConcept * Cpt; CProperty * Prop; if (!pCptList->Lookup(strCpt,Cpt)) return ""; if (!Cpt->m_PropList.Lookup(strProp,Prop)) return ""; return Prop->Default; } CString CRule::GetCondition(){return Condition;} CString CRule::GetAction(){return Action;} void CRule::SetCondition(CString Cond){Condition = Cond;} void CRule::SetAction(CString Act){Action = Act;} void CCluster::RunRule(CWMStructure * m_WM,CString strRule,CCptList*pCptList) { CRule * Rule; if (!RuleList.Lookup(strRule,Rule)) return; Rule->Run(m_WM,pCptList); } BOOL CCluster::RunRule(CWMStructure * m_WM,CCptList*pCptList) { return Rule->Run(m_WM,pCptList); } void CCluster::Run( CWMStructure *m_WM,CCptList*pCptList) { CString str; POSITION pos = RuleList.GetStartPosition(); while (pos !=NULL) { RuleList.GetNextAssoc(pos,str,Rule); //if (RunRule(m_WM,pCptList)) return; // run non Exhustive RunRule(m_WM,pCptList); //Exhustive } } void CCluster::RunByPriority(CWMStructure *m_WM,CCptList*pCptList) { CString str; CObList rList; POSITION pos = RuleList.GetStartPosition(); while (pos !=NULL) { RuleList.GetNextAssoc(pos,str,Rule); if (rList.GetCount() == 0) rList.AddHead((CRule*)Rule); else { POSITION pos1 = rList.GetHeadPosition(); BOOL Add = FALSE; while (pos1 !=NULL) { CRule * Rule = (CRule *) rList.GetAt(pos1); if (Rule->Priority <= Rule->Priority ) { rList.InsertBefore(pos1,(CRule*)Rule); Add = TRUE; break; } rList.GetNext( pos1 ); } if (!Add) rList.AddTail((CRule*)Rule); } } POSITION pos11 = rList.GetHeadPosition(); while (pos11 !=NULL) { CRule * Rule1 = (CRule *) rList.GetAt(pos11); if (Rule1->Run(m_WM,pCptList)) return; // Rule1->Run(Doc,m_WM)); To run exhaustive rules comment the above line and uncomment this line rList.GetNext( pos11 ); } } void CRule::SetToWM(CString strCpt, CString strProp, CString strVal,CWMStructure * m_WM,CCptList*pCptList) { CValue *Value; CValueList * Arr; CString Type= GetType(strCpt,strProp,pCptList); if (Type == "Multi-Value") { if(!m_WM->Lookup(strCpt+"-"+strProp,Arr)) { Value = new CValue; Value->V = strVal; Arr= new CValueList; Arr->m_CValueList.SetAtGrow(0,Value); m_WM->SetAt(strCpt+"-"+strProp,Arr); } else{ int found =0; for (int i=0; im_CValueList.GetSize();i++){ Value=(CValue*)Arr->m_CValueList.GetAt(i); if (Value->V == strVal){ found =1; break; } } if (found == 0){ Value = new CValue; Value->V = strVal; Arr->m_CValueList.SetAtGrow(Arr->m_CValueList.GetSize(),Value); m_WM->SetAt(strCpt+"-"+strProp,Arr); } } } else{ Value = new CValue; Arr= new CValueList; Value->V = strVal; Arr->m_CValueList.SetAtGrow(0,Value); m_WM->SetAt(strCpt+"-"+strProp,Arr); } } CString CRule:: GetValue(CString Cpt, CString Prop, CWMStructure * m_WM) { CValueList * Arr; CValue *Value; CString Temp; Temp.Empty(); if(m_WM->Lookup(Cpt+ "-" + Prop,Arr)){ Value=(CValue*)Arr->m_CValueList.GetAt(0); Temp = Value->V; } return Temp; } CString CRule:: GetValue(CString str, CWMStructure * m_WM) { CValueList * Arr; CValue *Value; CString Temp; Temp.Empty(); if(m_WM->Lookup(str,Arr)){ Value=(CValue*)Arr->m_CValueList.GetAt(0); Temp = Value->V; } return Temp; } void CRule::GetValue(CString Cpt, CString Prop, CStringList &List,CWMStructure * m_WM,CCptList*pCptList) { CValueList * Arr; CValue *Value; if(m_WM->Lookup(Cpt+ "-" + Prop,Arr)) { for ( int i = 0;i < Arr->m_CValueList.GetSize(); i++) { Value=(CValue*)Arr->m_CValueList.GetAt(i); List.AddTail( Value->V); } }else { CString str = GetDefaultValue(Cpt,Prop,pCptList); if (!str.IsEmpty()) { List.AddTail( str); } } } CString CRule::AskingCheck(CString strEval,CWMStructure * m_WM,CCptList*pCptList) { if(strEval.Find("$")<0) return strEval ; CStringArray Statments; CString statment,str = strEval; int i = 0; while(i=0) Statments.SetAtGrow(Statments.GetSize(),statment); statment.Empty(); } for(i = 0;i1"); // replace the rest with true expresion for(int j = i+1;j1"); else str.Replace(Statments[j],"1==1"); } // end of replacment if (CEval(str)!= TRUE) // return false { str = strEval; // replace one statement with true expresion str.Replace(Statments[i],"1==1"); // replace the rest with true expresion for(j = i+1;j1"); } return strEval; } CString CRule::AskUser(CString str,CWMStructure * m_WM,CCptList*pCptList) { /* CConcept * Cpt; CProperty * Prop; int x=str.Find("$"); CString strConc,str1; while(x>=0) { int pos=x+1; CString strCpt; CString strProp; while(str[pos] !='.') { strCpt = strCpt + str[pos++]; } pos++; while(pos < str.GetLength() && str[pos] != '=' && str[pos] !='>' && str[pos] !='<' && str[pos] !='+' &&str[pos] !='-' && str[pos] !='%' && str[pos] !='&' && str[pos] !='|' &&str[pos] !='*' &&str[pos] !='/' && str[pos] !=')' && str[pos] !='!' ) { strProp = strProp + str[pos++]; } strConc = "$"+strCpt + "."+ strProp; str1 = strCpt + "-"+ strProp; CString strType = GetType(strCpt,strProp,pCptList); pCptList->Lookup(strCpt,Cpt); Cpt->m_PropList.Lookup(strProp,Prop); CValue *Value = new CValue; CValueList * Arr ; if (!m_WM->Lookup(str1,Arr)) { if (strType == "nominal" ) { CNominalDlg dlg; dlg.SeeData(Prop); dlg.DoModal(); str.Replace(strConc,"\""+ dlg.Val+"\""); if(!(dlg.Val.IsEmpty())) SetToWM(strCpt,strProp,dlg.Val,m_WM,pCptList); } if (strType == "Multi-Value" ) { } if (strType == "date" ) { CDateDlg dlg; dlg.SeeData(Prop); dlg.DoModal(); str.Replace(strConc,dlg.Val); if(!(dlg.Val.IsEmpty())) SetToWM(strCpt,strProp,dlg.Val,m_WM,pCptList); } if (strType == "real" ||strType == "integer") { CNumberDlg dlg; dlg.SeeData(Prop); dlg.DoModal(); str.Replace(strConc,dlg.Val); if(!(dlg.Val.IsEmpty())) SetToWM(strCpt,strProp,dlg.Val,m_WM,pCptList); } }else { Value = (CValue*)Arr->m_CValueList.GetAt(0); if (strType == "integer" || strType == "real"|| strType == "date") { str.Replace(strConc,Value->V); } else { CString str1 = "\"" +Value->V +"\""; str.Replace(strConc,str1); } } x=str.Find("$"); if (x==0)break; } */ return str; } BOOL CRule::strCptPropIn(CString str, CString str2) { int x=str.Find("$"); CString strConc; while(x>=0) { int pos=x+1; CString strCpt; CString strProp; while(str[pos] !='.') { strCpt = strCpt + str[pos++]; } pos++; while(pos < str.GetLength() && str[pos] != '=' && str[pos] !='>' && str[pos] !='<' && str[pos] !='+' &&str[pos] !='-' && str[pos] !='%' && str[pos] !='&' && str[pos] !='|' &&str[pos] !='*' &&str[pos] !='/' && str[pos] !=')' && str[pos] !='!' ) { strProp = strProp + str[pos++]; } strConc = "$"+strCpt + "."+ strProp; if(str2.Find(strConc)>=0)return TRUE; str.Replace(strConc,""); x=str.Find("$"); if (x==0)break; } return FALSE; } BOOL CRule::CEval(CString str) { int * ptr = new int; BOOL RETURN; Tcl_Interp *interp = new Tcl_Interp; interp = Tcl_CreateInterp(); Tcl_ExprBoolean(interp,(char*)(LPCTSTR)str,ptr); if(*ptr) RETURN = TRUE; else RETURN = FALSE; Tcl_DeleteInterp(interp); delete ptr; return RETURN; } double CRule::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; } IMPLEMENT_DYNCREATE(MainDoc, CDocument) MainDoc::MainDoc() { Cluster = new CCluster; Rule = new CRule; Cpt = new CConcept; Prop = new CProperty; } BOOL MainDoc::OnNewDocument() { if (!CDocument::OnNewDocument()) return FALSE; MaxConceptID = 0; MaxRuleID = 0; return TRUE; } MainDoc::~MainDoc() { { POSITION pos = m_WM->GetStartPosition(); while( pos != NULL ) { CValueList * Arr; CString string; m_WM->GetNextAssoc( pos, string, Arr ); int i = 0; while (i < Arr->m_CValueList.GetSize() ) delete Arr->m_CValueList.GetAt( i++ ); Arr->m_CValueList.RemoveAll(); delete Arr; } m_WM->RemoveAll(); delete m_WM; } CString str; POSITION pos = Cluster->RuleList.GetStartPosition(); while (pos !=NULL) { CRule * Rule1; Cluster->RuleList.GetNextAssoc(pos,str,Rule1); delete Rule1; } Cluster->RuleList.RemoveAll(); pos = m_CptList.GetStartPosition(); while (pos !=NULL) { CConcept * Cpt1; m_CptList.GetNextAssoc(pos,str,Cpt1); CProperty * Prop1; POSITION pos1 = Cpt1->m_PropList.GetStartPosition(); while (pos1 !=NULL) { Cpt1->m_PropList.GetNextAssoc(pos1,str,Prop1); delete Prop1; } Cpt1->m_PropList.RemoveAll(); delete Cpt1; } m_CptList.RemoveAll(); pos = m_InferenceList.GetStartPosition(); while (pos !=NULL) { CInference * Inference; m_InferenceList.GetNextAssoc(pos,str,Inference); Inference->StaticRoleList.RemoveAll(); delete Inference; } m_InferenceList.RemoveAll(); } BEGIN_MESSAGE_MAP(MainDoc, CDocument) //{{AFX_MSG_MAP(MainDoc) // NOTE - the ClassWizard will add and remove mapping macros here. //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // MainDoc diagnostics #ifdef _DEBUG void MainDoc::AssertValid() const { CDocument::AssertValid(); } void MainDoc::Dump(CDumpContext& dc) const { CDocument::Dump(dc); } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // MainDoc serialization void MainDoc::Serialize(CArchive& ar) { if (ar.IsStoring()) { ar << MaxConceptID; ar << Language; } else { ar >> MaxConceptID; ar >> Language; } m_InferenceList.Serialize(ar); m_CptList.Serialize(ar); m_ClusterList.Serialize(ar); } void CRule::DecomposeCptProp(CString str, CString &ssCpt, CString &ssProp) { ssCpt = str.SpanExcluding("-"); ssCpt.TrimLeft();ssCpt.TrimRight(); str.MakeReverse(); ssProp = str.SpanExcluding("-"); ssProp.MakeReverse(); ssProp.TrimLeft();ssProp.TrimRight(); } void CRule::RemoveCptPropValFromWM(CString strCpt,CString strProp, CString strVal,CWMStructure * m_WM,CCptList*m_CptList) { CConcept* Cpt; CProperty* Prop; if(!m_CptList->Lookup(strCpt,Cpt))return; if(!Cpt->m_PropList.Lookup(strProp, Prop))return; CValue *Value ; CValueList * Arr; if(!m_WM->Lookup(strCpt+"-"+strProp,Arr)) return; // if(!Arr)return; if (Prop->Type == "Multi-Value") { for(int i = 0;im_CValueList.GetSize();i++) { Value =(CValue*) Arr->m_CValueList.GetAt(i); if(Value->V == strVal) { Arr->m_CValueList.RemoveAt(i); delete Value; break; } } if (Arr->m_CValueList.GetSize() == 0) { m_WM->RemoveKey(strCpt+"-"+strProp); delete Arr; } } else { Value =(CValue*) Arr->m_CValueList.GetAt(0); Arr->m_CValueList.RemoveAt(0); m_WM->RemoveKey(strCpt+"-"+strProp); delete Arr; delete Value; } } /*********************************/ int CDBHandle:: GetTableIndex(CString TableName) { for(int i=0;iPutActiveConnection(tt); } catch(_com_error &e) { _bstr_t bstrSource(e.Source()); _bstr_t bstrDescription(e.Description()); CString ss; ss.Format("\n\tSource : %s \n\tdescription : %s \n ", (LPCSTR)bstrSource,(LPCSTR)bstrDescription); AfxMessageBox(ss); } catch(...) { AfxMessageBox("Error occured in include files...."); } } void CDBHandle:: GetTables(CStringArray& TablesArray) { if(m_pCatalog==NULL){ AfxMessageBox("You Should Open DB Catalog First"); return; } TablesName.RemoveAll(); FieldsName.RemoveAll(); FieldsType.RemoveAll(); _variant_t Index; Index.vt = VT_I2; m_pTable = m_pCatalog->GetTables(); for (int i = 0; i < (int)m_pTable->GetCount(); i++){ Index.iVal=i; CString Type = (LPSTR)m_pTable->GetItem(Index)->GetType(); if(Type == "TABLE"){ CString xx; xx.Format("%s",(LPSTR)m_pTable->GetItem(Index)->GetName()); TablesName.Add(xx); } else TablesName.Add("NotTable"); } for(i=0;iGetItem(Index)->GetColumns()->GetCount(); for (int i = 0; i < n; i++){ INN.iVal=i; CString FName = (LPSTR)m_pTable->GetItem(Index)->GetColumns()->GetItem(INN)->GetName(); int FType = m_pTable->GetItem(Index)->GetColumns()->GetItem(INN)->GetType(); FieldsName.Add(FName); FieldsType.Add(FType); } for(i=0;iGetItem(Index)->GetKeys()->GetCount(); for(int k=0;kGetItem(Index)->GetKeys()->GetItem(INN_Key)->GetType()==adKeyPrimary){ int CCount = m_pTable->GetItem(Index)->GetKeys()->GetItem(INN_Key)->GetColumns()->GetCount(); for(int gm = 0; gm < CCount; gm++){ Index_c.iVal=gm; CString FName = (LPSTR)m_pTable->GetItem(Index)->GetKeys()->GetItem(INN_Key)->GetColumns()->GetItem(Index_c)->GetName(); KeysArray.Add(FName); } } } } /************************************************/ void PrintProviderError(ADODB::_ConnectionPtr pConnection) { ADODB::ErrorPtr pErr = NULL; long nCount = 0; long i = 0; if( (pConnection->Errors->Count) > 0) { nCount = pConnection->Errors->Count; for(i = 0; i < nCount; i++){ pErr = pConnection->Errors->GetItem(i); CString str; str.Format("\n\t Error number: %x\t%s", pErr->Number, (LPCSTR)pErr->Description); AfxMessageBox(str); } } } void CDBHandle:: OpenDBTable(CString DBName,CString Table ) { HRESULT hr = S_OK; CString DBSource; DBSource ="Provider=Microsoft.Jet.OLEDB.4.0; data source=" + DBName + ";"; _bstr_t strCnn(DBSource); try { if(pConnection==NULL){ TESTHR(pConnection.CreateInstance(__uuidof(ADODB::Connection))); pConnection->Open(strCnn,"","",ADODB::adConnectUnspecified); } if(pRsTable==NULL){ _variant_t tt; tt.SetString(Table); TESTHR(pRsTable.CreateInstance(__uuidof(ADODB::Recordset))); pRsTable->Open(tt,_variant_t((IDispatch *) pConnection, true),ADODB::adOpenKeyset,ADODB::adLockOptimistic,ADODB::adCmdTable); CString strQ="select * from " + Table + ";"; _bstr_t strQry(strQ); ADODB::_RecordsetPtr temp= pConnection->Execute(strQry,NULL,ADODB::adCmdText); // ADODB::_RecordsetPtr temp= pConnection->Execute(strQry,NULL,ADODB::adCmdUnspecified); if( temp->EndOfFile != 0){ if(pRsTable!=NULL) pRsTable->Close(); if(pConnection!=NULL) pConnection->Close(); // AfxMessageBox("There is no records under the following key " + strKey + " In Table " + Table); return ; } temp->MoveFirst(); long limit = 0; limit = temp->Fields->Count; ADODB::FieldPtr pFld = NULL; _variant_t vIndex; for (short iIndex = 0; iIndex < limit; iIndex++){ vIndex = iIndex; pFld = temp->Fields->GetItem(&vIndex); CString str; str.Format("Field %d : Name = '%s', ", iIndex, (LPCSTR)pFld->GetName()); AfxMessageBox(str); _variant_t FldVal = pFld->GetValue(); CString Tx; //CTime d; COleDateTime d; switch(FldVal.vt){ case (VT_BOOL): if(FldVal.boolVal) { AfxMessageBox("Value = 'True'\n"); } else { AfxMessageBox("Value = 'False'\n"); } break; case (VT_BSTR): Tx.Format("Value = '%s'\n",(LPCSTR)(_bstr_t)FldVal.bstrVal); AfxMessageBox(Tx); break; case (VT_DATE): d= FldVal; Tx.Format("Value = #%d/%d/%d#",d.GetMonth(),d.GetDay(),d.GetYear()); AfxMessageBox(Tx); break; case (VT_I4): Tx.Format("Value = %d \n",FldVal.iVal); AfxMessageBox(Tx); break; case (VT_EMPTY): Tx.Format("Value = %s\n",(LPCSTR)FldVal.lVal); AfxMessageBox(Tx); break; default: break; } } } if(pRsTable!=NULL) pRsTable->Close(); if(pConnection!=NULL) pConnection->Close(); } catch(_com_error &e) { _variant_t vtConnect = pRsTable->GetActiveConnection(); CString StrError; switch(vtConnect.vt) { case VT_BSTR: StrError.Format("Error:\nCode = %08lx\nMessage = %s\nSource = %s\nDescription = %s\n",e.Error(), e.ErrorMessage(), (LPCSTR) e.Source(), (LPCSTR) e.Description()); AfxMessageBox(StrError); break; case VT_DISPATCH: PrintProviderError(vtConnect); break; default: AfxMessageBox("Errors occured."); break; } } } CString CDBHandle:: GetDBValue(CString DBName,CString Table, CString Field, CStringArray& KeysName, CStringArray& KeysValue, CStringArray& KeysType ) { HRESULT hr = S_OK; CString DBSource,Tx; #ifdef _DEBUG DBName = "E:\\AGamal\\"+ DBName; #endif DBSource ="Provider=Microsoft.Jet.OLEDB.4.0; data source=" + DBName + ";"; _bstr_t strCnn(DBSource); try { //Open a connection if(pConnection==NULL){ TESTHR(pConnection.CreateInstance(__uuidof(ADODB::Connection))); pConnection->Open(strCnn,"","",ADODB::adConnectUnspecified); } if(pRsTable==NULL){ _variant_t tt; tt.SetString(Table); TESTHR(pRsTable.CreateInstance(__uuidof(ADODB::Recordset))); pRsTable->Open(tt,_variant_t((IDispatch *) pConnection, true),ADODB::adOpenKeyset,ADODB::adLockOptimistic,ADODB::adCmdTable); } /*************************************************/ CString strKey; for(int i=0;i0) strQ+=" WHERE " + strKey + " ; "; _bstr_t strQry(strQ); ADODB::_RecordsetPtr temp= pConnection->Execute(strQry,NULL,ADODB::adCmdText); // ADODB::_RecordsetPtr temp= pConnection->Execute(strQry,NULL,ADODB::adCmdUnspecified); if( temp->EndOfFile != 0){ if(pRsTable!=NULL) pRsTable->Close(); if(pConnection!=NULL) pConnection->Close(); AfxMessageBox("There is no records under the following key " + strKey + " In Table " + Table); return ""; } temp->MoveFirst(); long limit = 0; limit = temp->Fields->Count; ADODB::FieldPtr pFld = NULL; _variant_t vIndex; for (short iIndex = 0; iIndex < limit; iIndex++){ vIndex = iIndex; pFld = temp->Fields->GetItem(&vIndex); CString str; str.Format("Field %d : Name = '%s', ", iIndex, (LPCSTR)pFld->GetName()); _variant_t FldVal = pFld->GetValue(); COleDateTime d; switch(FldVal.vt){ case (VT_BOOL): if(FldVal.boolVal) { Tx.Format("%d",1); } else { Tx.Format("%d",0); } break; case (VT_BSTR): Tx.Format("%s",(LPCSTR)(_bstr_t)FldVal.bstrVal); break; case (VT_DATE): d= FldVal; Tx.Format("#%d/%d/%d#",d.GetDay(),d.GetMonth(),d.GetYear()); break; case (VT_I4): Tx.Format("%d",FldVal.iVal); break; case (VT_I8): Tx.Format("%d",FldVal.lVal); break; case (VT_R4): Tx.Format("%.2f",FldVal.fltVal); break; case (VT_R8): Tx.Format("%.2f",FldVal.dblVal); break; case (VT_EMPTY): Tx.Format("%s",(LPCSTR)FldVal.lVal); break; default: break; } } if(pRsTable!=NULL) pRsTable->Close(); if(pConnection!=NULL) pConnection->Close(); return Tx; } catch(_com_error &e) { _variant_t vtConnect = pRsTable->GetActiveConnection(); CString StrError; switch(vtConnect.vt) { case VT_BSTR: StrError.Format("Error:\nCode = %08lx\nMessage = %s\nSource = %s\nDescription = %s\n",e.Error(), e.ErrorMessage(), (LPCSTR) e.Source(), (LPCSTR) e.Description()); AfxMessageBox(StrError); break; case VT_DISPATCH: PrintProviderError(vtConnect); break; default: AfxMessageBox("Errors occured."); break; } } }