// KBEditorsDoc.cpp : implementation of the CKBEditorsDoc class // #include "stdafx.h" #include "NominalDlg.h" #include "NumberDlg.h" #include "DateDlg.h" #include #include "General.h" #include "kdate.h" #include "KBEditors.h" #include "KBEditorsDoc.h" #include "KBEditorsView.h" #include #include "XMLRPC.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CKBEditorsDoc IMPLEMENT_DYNCREATE(CKBEditorsDoc, CDocument) BEGIN_MESSAGE_MAP(CKBEditorsDoc, CDocument) //{{AFX_MSG_MAP(CKBEditorsDoc) // NOTE - the ClassWizard will add and remove mapping macros here. // DO NOT EDIT what you see in these blocks of generated code! //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CKBEditorsDoc construction/destruction class CKBEditorsView; void CKBEditorsDoc::SetDBValueToWM(CString strCpt, CString strProp, CString strVal) { CValue *Value = new CValue; Value->V = strVal; CValueList * Arr; Arr= new CValueList; Arr->m_CValueList.SetAtGrow(Arr->m_CValueList.GetSize(),Value); m_WM->SetAt(strCpt+"-"+strProp,Arr); } void CKBEditorsDoc::AssertToWM(CString strCpt, CString strProp, CString strVal) { CValue *Value; CValueList * Arr; if(strCpt.IsEmpty()){ AfxMessageBox("Can not Assert Empty Concept to Working Memory"); return; } CString strKey; if(strProp.IsEmpty()) strKey=strCpt; else strKey=strCpt+"-"+strProp; if(!m_WM->Lookup(strKey,Arr)){ Value = new CValue; Value->V = strVal; Arr= new CValueList; Arr->m_CValueList.SetAtGrow(0,Value); m_WM->SetAt(strKey,Arr); } else{ Value=(CValue*)Arr->m_CValueList.GetAt(0); Value->V = strVal; } } BOOL CKBEditorsDoc::SetDBAssociationToWM() { if(DBName.IsEmpty()){ AfxMessageBox("There is no Database Associated with this Knowledge Base"); return FALSE; } if(m_CKBDBList.GetCount()==0){ AfxMessageBox("The Association List is Empty"); return FALSE; } CStringArray KeyVal; for(int i=0;iGetCptPropTblFld(strCpt,strProp,strTbl,strFld); if(m_CTblKeysList.Lookup(strTbl,tblKey)){ tblKey->GetTblKeys(KeyNames,KeyType); } for(int i=0;im_WM = new CWMStructure; DB_Doc->OpenDBDoc("DBDoc.DB"); DB_WM = DB_Doc->m_WM; //Rpc = new CXMLRPC("http://localhost/mfc/rpc.asp"); } CKBEditorsDoc::~CKBEditorsDoc() { InitWM(); 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(); pos = m_WM->GetStartPosition(); while( pos != NULL ) { CValueList * Arr; CString string,strCpt,strProp; m_WM->GetNextAssoc( pos, string, Arr ); int i=0, size = Arr->m_CValueList.GetSize(); while (i < size) delete Arr->m_CValueList.GetAt( i++ ); Arr->m_CValueList.RemoveAll(); m_WM->RemoveKey(string); delete Arr; } m_WM->RemoveAll(); delete m_WM; pos = m_ServiceList.GetStartPosition(); while (pos !=NULL) { CService * Service; m_ServiceList.GetNextAssoc(pos,str,Service); delete Service; } m_ServiceList.RemoveAll(); /**/ if(Version == 2){ CKBDB* ckbdb; pos = m_CKBDBList.GetStartPosition(); while (pos !=NULL ){ m_CKBDBList.GetNextAssoc(pos,str,ckbdb); m_CKBDBList.RemoveKey(str); delete ckbdb; } m_CKBDBList.RemoveAll(); m_CBDKeysList.RemoveAll(); CTblKeys* tblKey; if(m_CTblKeysList.Lookup(str,tblKey)){ tblKey->RemoveAllKeys(); m_CTblKeysList.RemoveKey(str); delete tblKey; } m_CTblKeysList.RemoveAll(); } } BOOL CKBEditorsDoc::OnNewDocument() { if (!CDocument::OnNewDocument()) return FALSE; MaxConceptID = 0; MaxRuleID = 0; // TODO: add reinitialization code here // (SDI documents will reuse this document) return TRUE; } ///////////////////////////////////////////////////////////////////////////// // CKBEditorsDoc serialization void CKBEditorsDoc::Serialize(CArchive& ar) { Version = 3; if (ar.IsStoring()) { ar << Version; ar << MaxConceptID; ar << Language; m_InferenceList.Serialize(ar); m_CptList.Serialize(ar); m_ClusterList.Serialize(ar); m_ImageListOb.Serialize(ar); m_TableList.Serialize(ar); m_ServiceList.Serialize(ar); //nevien ar << DBName; ar << DBPath; m_CKBDBList.Serialize(ar); m_CTblKeysList.Serialize(ar); m_CBDKeysList.Serialize(ar); m_DGNodeList.Serialize(ar); m_FunctionList.Serialize(ar);//ahmed_mohammed } else { ar >> Version; switch(Version){ case 0: ar >> MaxConceptID; ar >> Language; m_InferenceList.Serialize(ar); m_CptList.Serialize(ar); m_ClusterList.Serialize(ar); //m_WM.Serialize(ar); break; case 1: ar >> MaxConceptID; ar >> Language; m_InferenceList.Serialize(ar); m_CptList.Serialize(ar); m_ClusterList.Serialize(ar); m_ImageListOb.Serialize(ar); m_TableList.Serialize(ar); m_ServiceList.Serialize(ar); //nevien break; case 2: ar >> MaxConceptID; ar >> Language; m_InferenceList.Serialize(ar); m_CptList.Serialize(ar); m_ClusterList.Serialize(ar); m_ImageListOb.Serialize(ar); m_TableList.Serialize(ar); m_ServiceList.Serialize(ar); //nevien ar >> DBName; ar >> DBPath; m_CKBDBList.Serialize(ar); m_CTblKeysList.Serialize(ar); m_CBDKeysList.Serialize(ar); break; case 3: ar >> MaxConceptID; ar >> Language; m_InferenceList.Serialize(ar); m_CptList.Serialize(ar); m_ClusterList.Serialize(ar); m_ImageListOb.Serialize(ar); m_TableList.Serialize(ar); m_ServiceList.Serialize(ar); //nevien ar >> DBName; ar >> DBPath; m_CKBDBList.Serialize(ar); m_CTblKeysList.Serialize(ar); m_CBDKeysList.Serialize(ar); m_DGNodeList.Serialize(ar); m_FunctionList.Serialize(ar);//ahmed_mohammed break; default: AfxMessageBox ("Error: Invalid Document Format"); return; } } } ///////////////////////////////////////////////////////////////////////////// // CKBEditorsDoc diagnostics #ifdef _DEBUG void CKBEditorsDoc::AssertValid() const { CDocument::AssertValid(); } void CKBEditorsDoc::Dump(CDumpContext& dc) const { CDocument::Dump(dc); } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CKBEditorsDoc commands void CKBEditorsDoc::OnCloseDocument() { CDocument::OnCloseDocument(); } BOOL CKBEditorsDoc::OnOpenDocument(LPCTSTR lpszPathName) { AppName = lpszPathName; if (!CDocument::OnOpenDocument(lpszPathName)) return FALSE; AfxGetApp()->m_pMainWnd->SetWindowText(lpszPathName); CString Ver,V; AfxGetApp()->m_pMainWnd->GetWindowText(Ver); V.Format("%d",Version); Ver+=" Version No. " + V; AfxGetApp()->m_pMainWnd->SetWindowText(Ver); return TRUE; } void CKBEditorsDoc::RemoveItemFromWM(CString Cpt, CString Prop) { CValue *Value; CValueList * Arr; if(!m_WM->Lookup(Cpt+"-"+Prop, Arr)) return; int size = Arr->m_CValueList.GetSize(); for ( int i = size-1;i>=0;i--) { Value = (CValue*)Arr->m_CValueList.GetAt(i); Arr->m_CValueList.RemoveAt(i); delete Value; } m_WM->RemoveKey(Cpt+"-"+Prop); delete Arr; } void CKBEditorsDoc::AbductAll(CString strCluster, CString strCpt,CString strProp) { CCluster * Cluster; CString inStr = "@"+strCpt+"."+strProp; if (!m_ClusterList.Lookup(strCluster,Cluster)) return; POSITION pos = Cluster->RuleList.GetStartPosition(); while (pos !=NULL) { CString str; Cluster->RuleList.GetNextAssoc(pos,str,Rule); CString Act = Rule->GetAction(); if(Act.Find(inStr) >=0) AddinTempList(Rule->GetCondition()); } } void CKBEditorsDoc::AbductAll(CString strCluster, CString strCpt,CString strProp, CString strVal) { CCluster * Cluster; if (!m_ClusterList.Lookup(strCluster,Cluster)) return; POSITION pos = Cluster->RuleList.GetStartPosition(); while (pos !=NULL) { CString str; Cluster->RuleList.GetNextAssoc(pos,str,Rule); CString Act = Rule->GetAction(); if(Act.Find(strVal) >=0) AddinTempList(Rule->GetCondition()); } } void CKBEditorsDoc::AddinTempList(CString str) { int x=str.Find("@"); CString strConc; CString strConc1; 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++]; } CString strCptPropVal; strCptPropVal = "@"+strCpt + "."+ strProp; strCpt.TrimLeft();strCpt.TrimRight(); strProp.TrimLeft();strProp.TrimRight(); strConc = "@"+strCpt + "."+ strProp; CString strCptProp=strCpt + "-"+ strProp; CValue *Value ; CValueList * Arr ; m_CptList.Lookup(strCpt,Cpt); Cpt->m_PropList.Lookup(strProp,Prop); CString strType = Prop->Type; while(pos < str.GetLength() && str[pos] ==' ' ) strCptPropVal = strCptPropVal + str[pos++]; if (Prop->Type == "Multi-Value" || Prop->Type == "nominal") { int pos1 = pos; 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]; CConcept * Cpt1; CProperty * Prop1; BOOL Add; if(m_WM->Lookup(strCptProp,Arr)) { Value = (CValue*)Arr->m_CValueList.GetAt(0); //Add = Add && (Value->V=="none"); (Value->V=="none" )?Add = TRUE:Add=FALSE; }else Add = TRUE; if (Add) { if (!m_TempCptList.Lookup(strCpt,Cpt1)) { Cpt1 = new CConcept; m_CptList.Lookup(strCpt,Cpt); Cpt1->NameL = Cpt->NameL; Cpt1->NameA = Cpt->NameA; m_TempCptList.SetAt(strCpt,Cpt1); } if (!Cpt1->m_PropList.Lookup(strProp,Prop1)) { Prop1= new CProperty; Cpt->m_PropList.Lookup(strProp,Prop); Prop1->NameA = Prop->NameA; Prop1->NameL = Prop->NameL; Cpt1->m_PropList.SetAt(strProp,Prop1); } Prop1->m_LegalValueList.SetAt(strVal1,strVal1); }else { if(strType == "Multi-Value") { BOOL found = FALSE; for ( int i = 0;i< Arr->m_CValueList.GetSize();i++) { Value = (CValue*)Arr->m_CValueList.GetAt(i); if(Value->V==strVal1) { found = TRUE; break; } } if (!found) { CString ss = strVal1; // if (!Cpt1->m_PropList.Lookup(strProp,Prop1)) // Prop1= new CProperty; Prop1 = new CProperty;//Helly Prop1->m_LegalValueList.SetAt(strVal1,ss); } } } str.Replace(strCptPropVal ,"1"); } else str.Replace(strConc ,"1"); x=str.Find("@"); if (x==0)break; } } void CKBEditorsDoc::PlayInference(CString strInf) { CInference * inf; if (!m_InferenceList.Lookup(strInf,inf)) return; POSITION pos1 = inf->StaticRoleList.GetHeadPosition(); while(pos1) { CString SourceName,SourceType; CString strSource = inf->StaticRoleList.GetAt(pos1); DecomposeString( strSource,SourceName,SourceType,"("); if(SourceType == "Cluster"){ CCluster * Cluster; if (!m_ClusterList.Lookup(SourceName,Cluster)) break; Cluster->Run(m_WM,&m_CptList); } else{ } inf->StaticRoleList.GetNext(pos1); } } void CKBEditorsDoc::PrintWM() { /* CFileDialog cfdlg(FALSE,"*.html","*.html",OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"HTML Files (*.html)"); cfdlg.DoModal(); CString str = cfdlg.GetPathName(); */ CStdioFile OutFile(WorkingMemoryFile,CFile::modeCreate | CFile::modeWrite ); // Ontology CString str = "All Working Memory

Working Memory

"; OutFile.WriteString(str); CValueList * Arr; CValue *Value; POSITION pos = m_WM->GetStartPosition(); while (pos !=NULL) { m_WM->GetNextAssoc(pos,str,Arr); Value = (CValue *)Arr->m_CValueList.GetAt(0); OutFile.WriteString("
"+str+" = "+Value->V); } OutFile.WriteString("

Defualt Values

"); POSITION pos11 = m_CptList.GetStartPosition(); while (pos11 !=NULL) { CConcept * Cpt1; m_CptList.GetNextAssoc(pos11,str,Cpt1); CProperty * Prop1; POSITION pos1 = Cpt1->m_PropList.GetStartPosition(); while (pos1 !=NULL) { CString str1; Cpt1->m_PropList.GetNextAssoc(pos1,str1,Prop1); if (!Prop1->Default.IsEmpty()) OutFile.WriteString("
"+str+"-"+str1+" = " +Prop1->Default); } } // Print Temp List OutFile.WriteString("

Temp List

"); pos11 = m_TempCptList.GetStartPosition(); while (pos11 !=NULL) { CConcept * Cpt1; m_TempCptList.GetNextAssoc(pos11,str,Cpt1); CProperty * Prop1; OutFile.WriteString("

Concept name"+str+"

" ); POSITION pos1 = Cpt1->m_PropList.GetStartPosition(); while (pos1 !=NULL) { CString str1; Cpt1->m_PropList.GetNextAssoc(pos1,str1,Prop1); OutFile.WriteString("
"+str1 ); POSITION pos111 = Prop1->m_LegalValueList.GetStartPosition(); OutFile.WriteString("

Legal List

"); while (pos111 !=NULL) { Prop1->m_LegalValueList.GetNextAssoc(pos111,str,str1); OutFile.WriteString(str1+ "--" ); } } } OutFile.Close(); } void CKBEditorsDoc::RemoveCptPropValFromWM(CConcept *Cpt, CString strVal) { } void CKBEditorsDoc::InitWM() { POSITION pos = m_WM->GetStartPosition(); while( pos != NULL ) { CValueList * Arr; CString string,strCpt,strProp; m_WM->GetNextAssoc( pos, string, Arr ); CRule * r = new CRule; r->DecomposeCptProp(string,strCpt,strProp); delete r; if (IsSourceValue("Database",strCpt,strProp)) continue; int i = 0; int size = Arr->m_CValueList.GetSize(); while (i < size) delete Arr->m_CValueList.GetAt( i++ ); Arr->m_CValueList.RemoveAll(); m_WM->RemoveKey(string); delete Arr; } // m_WM->RemoveAll(); // delete m_WM; } void CKBEditorsDoc::InitDBWM() { POSITION pos = DB_Doc->m_WM->GetStartPosition(); while( pos != NULL ) { CValueList * Arr; CString string,strCpt,strProp; DB_Doc->m_WM->GetNextAssoc( pos, string, Arr ); CRule * r = new CRule; r->DecomposeCptProp(string,strCpt,strProp); delete r; //if (IsSourceValue("Database",strCpt,strProp)) continue; int i = 0; int size = Arr->m_CValueList.GetSize(); while (i < size) delete Arr->m_CValueList.GetAt( i++ ); Arr->m_CValueList.RemoveAll(); DB_Doc->m_WM->RemoveKey(string); delete Arr; } DB_Doc->m_WM->RemoveAll(); // delete DB_Doc->m_WM; } BOOL CKBEditorsDoc::IsSourceValue(CString ssSource, CString ssCpt, CString ssProp) { CConcept * Cpt; CProperty * Prop; m_CptList.Lookup(ssCpt,Cpt); Cpt->m_PropList.Lookup(ssProp,Prop); CString Source; POSITION Spos = Prop->m_SearchOrderList.GetHeadPosition(); while (Spos !=NULL) { Source = Prop->m_SearchOrderList.GetAt(Spos); if (Source == ssSource) return TRUE; Prop->m_SearchOrderList.GetNext(Spos); } return FALSE; } BOOL CKBEditorsDoc::IsSourceValue(CString ssSource, CProperty* Prop) { CString Source; POSITION Spos = Prop->m_SearchOrderList.GetHeadPosition(); while (Spos !=NULL) { Source = Prop->m_SearchOrderList.GetAt(Spos); if (Source == ssSource) return TRUE; Prop->m_SearchOrderList.GetNext(Spos); } return FALSE; } void CKBEditorsDoc::SetDefaultInWM() { POSITION pos = m_CptList.GetStartPosition(); CRule * r = new CRule; while (pos !=NULL) { CConcept * Cpt1; CString str,strCpt,strProp; m_CptList.GetNextAssoc(pos,strCpt,Cpt1); CProperty * Prop1; POSITION pos1 = Cpt1->m_PropList.GetStartPosition(); while (pos1 !=NULL) { Cpt1->m_PropList.GetNextAssoc(pos1,strProp,Prop1); str = Prop1->Default; if (str.IsEmpty()) continue; if (Prop1->Type == "date") str = r->ReplaceNow(Prop1->Default); r->SetToWM(strCpt,strProp,str,m_WM,&m_CptList); } } delete r; } void CKBEditorsDoc::DecomposeString(CString str, CString &str1, CString &str2,const char * delemeter) { str1 = str.SpanExcluding(delemeter); str1.TrimLeft();str1.TrimRight(); str.MakeReverse(); str2 = str.SpanExcluding(delemeter); str2.MakeReverse(); str2.TrimLeft();str2.TrimRight(); str2.Remove(')'); if(str1==str2)str2.Empty(); }