// KBEditorsDoc.cpp : implementation of the CKBEditorsDoc class // #include "stdafx.h" //#include "NominalDlg.h" //#include "NumberDlg.h" //#include "DateDlg.h" #include #include "General.h" #include "KBEditorsDoc.h" //#include "KBEditorsView.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;iRuleList.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) { CString str5; Cpt1->m_PropList.GetNextAssoc(pos1,str5,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(); double d = (double)m_WM->GetCount(); if(d > 0){ 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(); /**/ 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 = 2; 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); } else { ar >> Version; switch(Version){ case 0: 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; 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; 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){ // if (LogicalCondition(Rule->GetCondition())) AddinTempList(Rule->GetCondition()); } } } BOOL CKBEditorsDoc::LogicalCondition(CString Cond) { CValueList * Arr; BOOL Found = FALSE; int x=Cond.Find("@"); while(x>=0) { int pos=x+1; CString strCpt; while(Cond[pos] !='.') { strCpt = strCpt + Cond[pos++]; } POSITION posWM = m_WM->GetStartPosition(); while (posWM !=NULL) { CString str; m_WM->GetNextAssoc(posWM,str,Arr); if (str.Find(strCpt,-1)>=0 ){ Found = TRUE; break; } } if (!Found) return FALSE; Cond.Replace("@" + strCpt , "0"); x=Cond.Find("@"); Found = FALSE; } return TRUE; } 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 ; if(!m_CptList.Lookup(strCpt,Cpt)){ //AfxMessageBox("The Concept "+ strCpt +" is not exist in Concept List But it is in Rule !!!!"); return; } if(!Cpt->m_PropList.Lookup(strProp,Prop)){ //AfxMessageBox("The Property "+ strProp +" is not exist in Property List of Concept "+ strCpt+" List But it is in Rule !!!!"); return; } 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" || Value->V=="0")?Add = TRUE:Add=FALSE; }else Add = TRUE; if (Add) { // the oreder of rule affect this action // cpt-prop of suroce user must be first if (IsSourceValue("User",strCpt,strProp) ){ //Helly May2002 if(m_WM->Lookup(strCpt+"-"+strProp,Arr)){ CValue* VV = (CValue*)Arr->m_CValueList.GetAt(0); //Add = Add && (Value->V=="none"); if(VV->V!="none"){ str.Replace(strCptPropVal ,"1"); x=str.Find("@"); continue;//helly said it must be continue insted of return } } } if (IsSourceValue("Relation",strCpt,strProp)){ //Helly May2002 to exclude plant growth stage str.Replace(strCptPropVal ,"1"); x=str.Find("@"); continue;//helly said it must be continue insted of return } if (!m_TempCptList.Lookup(strCpt,Cpt1)) { Cpt1 = new CConcept; if(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; if(Cpt->m_PropList.Lookup(strProp,Prop)){ Prop1->NameA = Prop->NameA; Prop1->NameL = Prop->NameL; Cpt1->m_PropList.SetAt(strProp,Prop1); } } //Helly May 2002 We need to get arabic legal from Cpt->Prop->mlegal CString strVArabic; Prop->m_LegalValueList.Lookup(strVal1,strVArabic); Prop1->m_LegalValueList.SetAt(strVal1,strVArabic); }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{ CTable * TableObj; if (!m_TableList.Lookup(SourceName,TableObj)) break; TableObj->Play(m_WM,&m_CptList); } 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); for(int k=0;k< Arr->m_CValueList.GetSize();k++){ Value = (CValue *)Arr->m_CValueList.GetAt(k); 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() { if(m_WM==NULL)return; double d = (double)m_WM->GetCount(); if(d <=0) return; 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; } } BOOL CKBEditorsDoc::IsSourceValue(CString ssSource, CString ssCpt, CString ssProp) { CConcept * Cpt; CProperty * Prop; if(!m_CptList.Lookup(ssCpt,Cpt)) return FALSE; if(!Cpt->m_PropList.Lookup(ssProp,Prop))return FALSE; 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; } BOOL CKBEditorsDoc::MyOpenDocument(LPCTSTR lpszPathName) { if (IsModified()) TRACE0("Warning: OnOpenDocument replaces an unsaved document.\n"); CFileException fe; CFile* pFile = GetFile(lpszPathName, CFile::modeRead|CFile::shareDenyWrite, &fe); AfxMessageBox("here 1"); if (pFile == NULL) { ReportSaveLoadException(lpszPathName, &fe, FALSE, AFX_IDP_FAILED_TO_OPEN_DOC); return FALSE; } DeleteContents(); SetModifiedFlag(); // dirty during de-serialize CArchive loadArchive(pFile, CArchive::load | CArchive::bNoFlushOnDelete); loadArchive.m_pDocument = this; loadArchive.m_bForceFlat = FALSE; TRY { CWaitCursor wait; if (pFile->GetLength() != 0) Serialize(loadArchive); // load me loadArchive.Close(); ReleaseFile(pFile, FALSE); } CATCH_ALL(e) { ReleaseFile(pFile, TRUE); DeleteContents(); // remove failed contents TRY { ReportSaveLoadException(lpszPathName, e, FALSE, AFX_IDP_FAILED_TO_OPEN_DOC); } END_TRY // DELETE_EXCEPTION(e); return FALSE; } END_CATCH_ALL SetModifiedFlag(FALSE); // start off with unmodified return TRUE; } void CKBEditorsDoc::InitTempList() { POSITION pos = m_TempCptList.GetStartPosition(); while (pos !=NULL) { CConcept * Cpt1; CString str; m_TempCptList.GetNextAssoc(pos,str,Cpt1); CProperty * Prop1; POSITION pos1 = Cpt1->m_PropList.GetStartPosition(); while (pos1 !=NULL) { Cpt1->m_PropList.GetNextAssoc(pos1,str,Prop1); Prop1->m_LegalValueList.RemoveAll(); delete Prop1; } Cpt1->m_PropList.RemoveAll(); delete Cpt1; } m_TempCptList.RemoveAll(); } 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(); }