// KSRKB.cpp : Implementation of CKSRKB #include "stdafx.h" #include "KSRCom.h" #include "KSRKB.h" #include "General.h" #include "KBEditorsDoc.h" ///////////////////////////////////////////////////////////////////////////// // CKSRKB STDMETHODIMP CKSRKB::TestKB(BSTR *str) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CString xstr="It is work right"; CComBSTR bstr; bstr.Append(xstr.AllocSysString()); *str = bstr.Detach(); return S_OK; } STDMETHODIMP CKSRKB::OpenKBDoc(BSTR str) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CString ss(str); pDoc = new CKBEditorsDoc; BOOL result = pDoc->OnOpenDocument(ss); pDoc->SetDefaultInWM(); return S_OK; } STDMETHODIMP CKSRKB::GetBasicData(BSTR *DB) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CString strCpt; CString strProp; CString strRetCpt,strRetProp,strRetVal; CComBSTR bstr; CConcept * Cpt; CProperty * Prop; BOOL AddCpt ; CRule R; POSITION cptpos = pDoc->m_CptList.GetStartPosition(); while (cptpos !=NULL) { AddCpt = FALSE; strRetVal.Empty(); strRetProp.Empty(); pDoc->m_CptList.GetNextAssoc(cptpos,strCpt,Cpt); POSITION pos = Cpt->m_PropList.GetStartPosition(); while (pos) { strRetVal.Empty(); CString strType; Cpt->m_PropList.GetNextAssoc(pos,strProp,Prop); if (pDoc->IsSourceValue("Database",Prop)) { AddCpt = TRUE; strType = R.GetType(strCpt,strProp,&pDoc->m_CptList); if(strType == "nominal" || strType == "Multi-Value"|| strType == "string"){ POSITION pos1 = Prop->m_LegalValueList.GetStartPosition(); while (pos1){ CString str; CString str1; Prop->m_LegalValueList.GetNextAssoc(pos1,str,str1); strRetVal += str +","; } } strRetVal.Delete(strRetVal.GetLength()-1,1); strRetProp += "." +strProp+"*"+strType+"="+strRetVal; if(strType == "integer" || strType == "real"|| strType == "date") strRetProp.Delete(strRetProp.GetLength()-1,1); } } if(AddCpt){ strRetCpt = strRetCpt+"@"+strCpt+strRetProp; } } bstr.Append(strRetCpt.AllocSysString()); *DB = bstr.Detach(); return S_OK; } STDMETHODIMP CKSRKB::CloseKB() { AFX_MANAGE_STATE(AfxGetStaticModuleState()) pDoc->OnCloseDocument(); return S_OK; } STDMETHODIMP CKSRKB::GetFromWM(BSTR Cpt, BSTR Prop, BSTR *Val) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CString strCpt(Cpt); CString strProp(Prop); CRule * r = new CRule; CComBSTR bstr; //r->GetValue(strCpt,strProp,List,pDoc->m_WM,&pDoc->m_CptList); CString str; CValueList * Arr; CValue *Value; if(pDoc->m_WM->Lookup(strCpt+ "-" + strProp,Arr)) { for ( int i = 0;i < Arr->m_CValueList.GetSize(); i++) { Value=(CValue*)Arr->m_CValueList.GetAt(i); str += Value->V+"#"; } str.MakeReverse(); if (!str.IsEmpty()) str.Delete(0,1); str.MakeReverse(); }else { str = r->GetDefaultValue(strCpt,strProp,&pDoc->m_CptList); } delete r; bstr.Append(str.AllocSysString()); *Val = bstr.Detach(); return S_OK; } STDMETHODIMP CKSRKB::GetInitConcept(BSTR *InitCpt) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CString strCpt; CString strProp; CString strRetCpt,strRetProp,strRetVal; CComBSTR bstr; CConcept * Cpt; CProperty * Prop; BOOL AddCpt ; CRule R; POSITION cptpos = pDoc->m_CptList.GetStartPosition(); while (cptpos !=NULL) { AddCpt = FALSE; strRetVal.Empty(); strRetProp.Empty(); pDoc->m_CptList.GetNextAssoc(cptpos,strCpt,Cpt); POSITION pos = Cpt->m_PropList.GetStartPosition(); while (pos) { strRetVal.Empty(); CString strType; Cpt->m_PropList.GetNextAssoc(pos,strProp,Prop); if (pDoc->IsSourceValue("User",Prop)) { AddCpt = TRUE; strType = R.GetType(strCpt,strProp,&pDoc->m_CptList); if(strType == "nominal" || strType == "Multi-Value"|| strType == "string"){ POSITION pos1 = Prop->m_LegalValueList.GetStartPosition(); while (pos1){ CString str; CString str1; Prop->m_LegalValueList.GetNextAssoc(pos1,str,str1); strRetVal += str +","; } } strRetVal.Delete(strRetVal.GetLength()-1,1); strRetProp += "." +strProp+"*"+strType+"="+strRetVal; if(strType == "integer" || strType == "real"|| strType == "date") strRetProp.Delete(strRetProp.GetLength()-1,1); } } if(AddCpt){ strRetCpt = strRetCpt+"@"+strCpt+strRetProp; } } bstr.Append(strRetCpt.AllocSysString()); *InitCpt = bstr.Detach(); return S_OK; } STDMETHODIMP CKSRKB::PlayInference(BSTR strInf) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CString str(strInf); pDoc->PlayInference(str); return S_OK; } STDMETHODIMP CKSRKB::SetToWM(BSTR Cpt, BSTR Prop, BSTR Val) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CString strCpt(Cpt); CString strProp(Prop); CString strVal(Val); CRule * r = new CRule; r->SetToWM(strCpt,strProp,strVal,pDoc->m_WM,&pDoc->m_CptList); delete r; return S_OK; } STDMETHODIMP CKSRKB::Abduct(BSTR strCpt, BSTR strInf, BSTR strProp, BSTR *List) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CStringList CptList; CComBSTR bstr; pDoc->InitTempList(); CString ssInf(strInf); CString ssCpt(strCpt); CString ssProp(strProp); //split strCpt into CptList for(int i = 0;im_InferenceList.Lookup(ssInf,inf)) return S_OK; POSITION pos1 = inf->StaticRoleList.GetHeadPosition(); while(pos1) { CString SourceName,SourceType; CString strSource = inf->StaticRoleList.GetAt(pos1); pDoc->DecomposeString(strSource,SourceName,SourceType,"("); if(SourceType =="Cluster"){ CString strCluster =SourceName ; CCluster * Cluster; pDoc->m_ClusterList.Lookup( strCluster,Cluster); POSITION pp = CptList.GetHeadPosition(); while(pp){ CString strCpt; strCpt = CptList.GetAt(pp); pDoc->AbductAll(strCluster, strCpt,ssProp); CptList.GetNext(pp); } } inf->StaticRoleList.GetNext(pos1); } CString strRet = ConstructAdditionalCpt(); bstr.Append(strRet.AllocSysString()); *List = bstr.Detach(); return S_OK; } CString CKSRKB::ConstructAdditionalCpt() { CString strCpt; CString strProp; CString strRetCpt,strRetProp,strRetVal; CConcept * Cpt; CProperty * Prop; CRule R; POSITION cptpos = pDoc->m_TempCptList.GetStartPosition(); while (cptpos !=NULL){ //strRetVal.Empty(); strRetProp.Empty(); pDoc->m_TempCptList.GetNextAssoc(cptpos,strCpt,Cpt); POSITION pos = Cpt->m_PropList.GetStartPosition(); while (pos){ strRetVal.Empty(); CString strType; Cpt->m_PropList.GetNextAssoc(pos,strProp,Prop); strType = R.GetType(strCpt,strProp,&pDoc->m_CptList); if(strType == "nominal" || strType == "Multi-Value"|| strType == "string"){ POSITION pos1 = Prop->m_LegalValueList.GetStartPosition(); while (pos1){ CString str; CString str1; Prop->m_LegalValueList.GetNextAssoc(pos1,str,str1); strRetVal += str +","; } } strRetVal.Delete(strRetVal.GetLength()-1,1); strRetProp += "." +strProp+"*"+strType+"="+strRetVal; if(strType == "integer" || strType == "real"|| strType == "date") strRetProp.Delete(strRetProp.GetLength()-1,1); } strRetCpt = strRetCpt+"@"+strCpt+strRetProp; } return strRetCpt; } STDMETHODIMP CKSRKB::ResetWM() { AFX_MANAGE_STATE(AfxGetStaticModuleState()) POSITION pos = pDoc->m_WM->GetStartPosition(); while( pos != NULL ) { CValueList * Arr; CString string,strCpt,strProp; pDoc->m_WM->GetNextAssoc( pos, string, Arr ); CRule * r = new CRule; r->DecomposeCptProp(string,strCpt,strProp); delete r; int i = 0; int size = Arr->m_CValueList.GetSize(); while (i < size) delete Arr->m_CValueList.GetAt( i++ ); Arr->m_CValueList.RemoveAll(); pDoc->m_WM->RemoveKey(string); delete Arr; } pDoc->m_WM->RemoveAll(); // delete pDoc->m_WM; return S_OK; } STDMETHODIMP CKSRKB::GetSuspectedDisorders(BSTR inStr, BSTR *outStr) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CString ss(inStr); CComBSTR bstr; CValue *Value ; CValueList * Arr; POSITION pos = pDoc->m_WM->GetStartPosition(); CString strCpt,strProp,String; CString xx; while(pos) { pDoc->m_WM->GetNextAssoc(pos,String, Arr); CRule * r = new CRule; r->DecomposeCptProp(String,strCpt,strProp); delete r; if(strProp == ss) { Value =(CValue*) Arr->m_CValueList.GetAt(0); CValueList * Arr1; if(!pDoc->m_WM->Lookup(strCpt+"-confirm",Arr1)) if(Value->V == "yes") xx = xx + "@" + strCpt; } } bstr.Append(xx.AllocSysString()); *outStr = bstr.Detach(); return S_OK; } STDMETHODIMP CKSRKB::GetAllWM(BSTR *str) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CString xx; POSITION pos = pDoc->m_WM->GetStartPosition(); while( pos != NULL ) { CValueList * Arr; CString string; pDoc->m_WM->GetNextAssoc( pos, string, Arr ); int i = 0; CValue *Value ; while (i < Arr->m_CValueList.GetSize() ){ Value = (CValue*) Arr->m_CValueList.GetAt( i++ ); xx=xx+ "
" + string + " = " + Value->V; } } CComBSTR bstr; bstr.Append(xx.AllocSysString()); *str = bstr.Detach(); return S_OK; } STDMETHODIMP CKSRKB::GetConfirmedDis(BSTR inStr, BSTR *outStr) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CString ss(inStr); CComBSTR bstr; CValue *Value ; CValueList * Arr; POSITION pos = pDoc->m_WM->GetStartPosition(); CString strCpt,strProp,String; CString xx; while(pos) { pDoc->m_WM->GetNextAssoc(pos,String, Arr); CRule * r = new CRule; r->DecomposeCptProp(String,strCpt,strProp); delete r; if(strProp == ss) { Value =(CValue*) Arr->m_CValueList.GetAt(0); if(Value->V == "likely" || Value->V == "most likely") xx = xx + "@" + strCpt; } } bstr.Append(xx.AllocSysString()); *outStr = bstr.Detach(); return S_OK; } STDMETHODIMP CKSRKB::RemoveCptPropFromWM(BSTR Cpt, BSTR Prop) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CString searchStr = CString(Cpt) + "-" + CString(Prop); POSITION pos = pDoc->m_WM->GetStartPosition(); while( pos != NULL ) { CValueList * Arr; CString string; pDoc->m_WM->GetNextAssoc( pos, string, Arr ); if (string.Find(searchStr,0) < 0) continue; int size = Arr->m_CValueList.GetSize(); int i=0; while (i < size) delete Arr->m_CValueList.GetAt( i++ ); Arr->m_CValueList.RemoveAll(); pDoc->m_WM->RemoveKey(string); delete Arr; } return S_OK; } STDMETHODIMP CKSRKB::IsInWM(BSTR strCpt, BSTR strProp, BSTR strVal,BSTR *Ret) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CString ssCpt(strCpt); CString ssProp(strProp); CString ssVal(strVal); CComBSTR bstr; CValue *Value ; CValueList * Arr; BOOL found = FALSE; if(pDoc->m_WM->Lookup(ssCpt+"-"+ssProp,Arr)){ for(int i = 0;im_CValueList.GetSize();i++){ Value =(CValue*) Arr->m_CValueList.GetAt(i); if(Value->V == ssVal) { found = TRUE; break; } } } CString out; if(found) out="TRUE"; else out="FALSE"; bstr.Append(out.AllocSysString()); *Ret = bstr.Detach(); return S_OK; } STDMETHODIMP CKSRKB::Trim(BSTR inStr, BSTR *outStr) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CString trimStr(inStr); trimStr.TrimLeft(); trimStr.TrimRight(); CComBSTR bstr; bstr.Append(trimStr.AllocSysString()); *outStr = bstr.Detach(); return S_OK; } STDMETHODIMP CKSRKB::GetDisImage(BSTR DisStr, BSTR *ImageStr) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CString DisString(DisStr); CString ImageString; CImageOb *Image; //ImageString.Format("count = %d", pDoc->m_ImageListOb.GetCount()); if (pDoc->m_ImageListOb.Lookup(DisString,Image)){ POSITION pos = Image->m_ImageList.GetHeadPosition(); ImageString = Image->m_ImageList.GetAt(pos); }else ImageString="?"; CComBSTR bstr; bstr.Append(ImageString.AllocSysString()); *ImageStr = bstr.Detach(); return S_OK; } STDMETHODIMP CKSRKB::GetProperName(BSTR inStr, BSTR *outStr) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CString inString(inStr); inString.Replace('_',' '); inString.MakeUpper(); CComBSTR bstr; bstr.Append(inString.AllocSysString()); *outStr = bstr.Detach(); return S_OK; } STDMETHODIMP CKSRKB::GetMostLikelyConfirmedDis(BSTR inStr, BSTR *outStr) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CString ss(inStr); CComBSTR bstr; CValue *Value ; CValueList * Arr; POSITION pos = pDoc->m_WM->GetStartPosition(); CString strCpt,strProp,String; CString xx; while(pos) { pDoc->m_WM->GetNextAssoc(pos,String, Arr); CRule * r = new CRule; r->DecomposeCptProp(String,strCpt,strProp); delete r; if(strProp == ss) { Value =(CValue*) Arr->m_CValueList.GetAt(0); if( Value->V == "most likely") xx = xx + "@" + strCpt; } } bstr.Append(xx.AllocSysString()); *outStr = bstr.Detach(); return S_OK; } STDMETHODIMP CKSRKB::GetLikelyConfirmedDis(BSTR inStr, BSTR *outStr) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CString ss(inStr); CComBSTR bstr; CValue *Value ; CValueList * Arr; POSITION pos = pDoc->m_WM->GetStartPosition(); CString strCpt,strProp,String; CString xx; while(pos) { pDoc->m_WM->GetNextAssoc(pos,String, Arr); CRule * r = new CRule; r->DecomposeCptProp(String,strCpt,strProp); delete r; if(strProp == ss) { Value =(CValue*) Arr->m_CValueList.GetAt(0); if(Value->V == "likely" ) xx = xx + "@" + strCpt; } } bstr.Append(xx.AllocSysString()); *outStr = bstr.Detach(); return S_OK; } STDMETHODIMP CKSRKB::DelCptPropValFromWM(BSTR Cpt, BSTR Prop, BSTR Val) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CString searchStr = CString(Cpt) + "-" + CString(Prop); CString strVal(Val); POSITION pos = pDoc->m_WM->GetStartPosition(); while( pos != NULL ) { CValueList * Arr; CString string; pDoc->m_WM->GetNextAssoc( pos, string, Arr ); if (string.Find(searchStr,0) < 0) continue; int size = Arr->m_CValueList.GetSize(); int i=0; while (i < size){ CValue * Value; Value = (CValue * )Arr->m_CValueList.GetAt(i); if (Value->V == strVal){ Arr->m_CValueList.RemoveAt(i); delete Value; break; } i++; } if (Arr->m_CValueList.GetSize()==0){ Arr->m_CValueList.RemoveAll(); pDoc->m_WM->RemoveKey(string); delete Arr; } } return S_OK; } STDMETHODIMP CKSRKB::ReverseProperName(BSTR inStr, BSTR *outStr) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CString inString(inStr); inString.Replace(' ','_'); inString.MakeLower(); CComBSTR bstr; bstr.Append(inString.AllocSysString()); *outStr = bstr.Detach(); return S_OK; } STDMETHODIMP CKSRKB::GetChildList(BSTR strCpt, BSTR *ChildListXML) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CString Cpt(strCpt); CComBSTR bstr; CString str; CConcept * CptSuper; CString strOut=""; if (pDoc->m_CptList.Lookup(Cpt,CptSuper)) AddChild(strOut,CptSuper); /* POSITION pos = pDoc->m_CptList.GetStartPosition(); while (pos !=NULL) { CConcept * CptObj; pDoc->m_CptList.GetNextAssoc(pos,str,CptObj); if (CptObj->SupperNID == ID) AddChild(strOut,Cpt,CptSuper); //strOut += ""; } */ strOut += ""; bstr.Append(strOut.AllocSysString()); *ChildListXML = bstr.Detach(); return S_OK; } void CKSRKB::AddChild(CString &strOut, CConcept *CptObj) { bool Added = FALSE; POSITION pos = pDoc->m_CptList.GetStartPosition(); CString str; while (pos !=NULL) { CConcept * CptObjChild; pDoc->m_CptList.GetNextAssoc(pos,str,CptObjChild); if (CptObj->nID == CptObjChild->SupperNID){ AddChild(strOut,CptObjChild); Added = TRUE; //strOut += ""; } } if (Added==FALSE) strOut =strOut+ "NameL + "\"/>"; } STDMETHODIMP CKSRKB::InstallArabicCptList() { AFX_MANAGE_STATE(AfxGetStaticModuleState()) POSITION pos = pDoc->m_CptList.GetStartPosition(); while(pos) { CString str; pDoc->m_CptList.GetNextAssoc(pos,str,pDoc->Cpt); pDoc->Cpt->NameL = str; if (!pDoc->Cpt->NameA.IsEmpty()) pDoc->m_CptListA.SetAt(pDoc->Cpt->NameA,pDoc->Cpt); } return S_OK; } STDMETHODIMP CKSRKB::GetLegal(BSTR Cpt, BSTR Prop, BSTR *Legal) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CString strRetLegal,strCpt(Cpt); CString strProp(Prop); CComBSTR bstr; CConcept * CptObj; CProperty* PropObj; if(pDoc->m_CptList.Lookup(strCpt,CptObj)){ if(CptObj->m_PropList.Lookup(strProp,PropObj)){ CString strVal,strVal1; POSITION valPos= PropObj->m_LegalValueList.GetStartPosition(); while(valPos) { PropObj->m_LegalValueList.GetNextAssoc(valPos,strVal,strVal1); strRetLegal+="" +strVal +""; } strRetLegal = "" + strRetLegal + ""; bstr.Append(strRetLegal.AllocSysString()); *Legal = bstr.Detach(); } } return S_OK; } STDMETHODIMP CKSRKB::GetArabicCpt(BSTR inStr, BSTR *outStr) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CString inString(inStr); CComBSTR bstr; CString outString=""; if (pDoc->m_CptList.Lookup(inString,pDoc->Cpt)) outString= pDoc->Cpt->NameA; bstr.Append(outString.AllocSysString()); *outStr = bstr.Detach(); return S_OK; } STDMETHODIMP CKSRKB::GetEnglishCpt(BSTR inStr, BSTR *outStr) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CString inString(inStr); CComBSTR bstr; CString outString=""; if (pDoc->m_CptListA.Lookup(inString,pDoc->Cpt)) outString= pDoc->Cpt->NameL; bstr.Append(outString.AllocSysString()); *outStr = bstr.Detach(); return S_OK; } STDMETHODIMP CKSRKB::GetPropList(BSTR Cpt, BSTR *PropList) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CString CptStr(Cpt); CComBSTR bstr; CString outString=""; if (pDoc->m_CptList.Lookup(CptStr,pDoc->Cpt)){ POSITION pos = pDoc->Cpt->m_PropList.GetStartPosition(); while (pos) { CProperty* Prop; CString strProp; pDoc->Cpt->m_PropList.GetNextAssoc(pos,strProp,Prop); outString += "" + strProp +""; } } outString +=""; bstr.Append(outString.AllocSysString()); *PropList = bstr.Detach(); return S_OK; } STDMETHODIMP CKSRKB::GetArabicProp(BSTR Cpt, BSTR Prop, BSTR *PropA) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CString CptStr(Cpt); CString PropStr(Prop); CComBSTR bstr; CString outString=""; if (pDoc->m_CptList.Lookup(CptStr,pDoc->Cpt)) if (pDoc->Cpt->m_PropList.Lookup(PropStr,pDoc->Prop)){ outString = pDoc->Prop->NameA; } bstr.Append(outString.AllocSysString()); *PropA = bstr.Detach(); return S_OK; } STDMETHODIMP CKSRKB::GetEnglishProp(BSTR Cpt, BSTR Prop, BSTR *PropA) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CString CptStr(Cpt); CString PropStr(Prop); CComBSTR bstr; CString outString=""; if (pDoc->m_CptList.Lookup(CptStr,pDoc->Cpt)){ POSITION pos = pDoc->Cpt->m_PropList.GetStartPosition(); while (pos) { CProperty* PropObj; CString strProp; pDoc->Cpt->m_PropList.GetNextAssoc(pos,strProp,PropObj); if (PropStr ==PropObj->NameA){ outString=PropObj->NameL; break; } } } bstr.Append(outString.AllocSysString()); *PropA = bstr.Detach(); return S_OK; } STDMETHODIMP CKSRKB::PrinWM() { AFX_MANAGE_STATE(AfxGetStaticModuleState()) pDoc->PrintWM(); return S_OK; } STDMETHODIMP CKSRKB::GetCptFromWM(BSTR Prop, BSTR Val, BSTR *CptList) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CString sProp(Prop); sProp.TrimLeft();sProp.TrimRight(); CString sVal(Val); sVal.TrimLeft();sVal.TrimRight(); CComBSTR bstr; CValue *Value ; CValueList * Arr; CString strCpt,strProp,String; //CString xx = ""; CString xx = ""; POSITION pos = pDoc->m_WM->GetStartPosition(); while(pos) { pDoc->m_WM->GetNextAssoc(pos,String, Arr); CRule * r = new CRule; r->DecomposeCptProp(String,strCpt,strProp); strCpt.TrimLeft();strCpt.TrimRight(); strProp.TrimLeft();strProp.TrimRight(); delete r; if(strProp == sProp) { for(int i=0;im_CValueList.GetSize();i++){ Value =(CValue*) Arr->m_CValueList.GetAt(i); if(Value->V == sVal){ CConcept* CptObj; if(pDoc->m_CptList.Lookup(strCpt,CptObj)) //xx = xx + "NameL + "\">"; xx = xx + "NameL + "\" NameA=\"" + CptObj->NameA+"\"/>"; break; } } } } xx += ""; bstr.Append(xx.AllocSysString()); *CptList = bstr.Detach(); return S_OK; } STDMETHODIMP CKSRKB::GetType(BSTR Cpt, BSTR Prop, BSTR *Type) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CString strRetType,strCpt(Cpt); CString strProp(Prop); CComBSTR bstr; CConcept * CptObj; CProperty* PropObj; if(pDoc->m_CptList.Lookup(strCpt,CptObj)){ if(CptObj->m_PropList.Lookup(strProp,PropObj)){ strRetType = PropObj->Type ; bstr.Append(strRetType.AllocSysString()); *Type = bstr.Detach(); } } return S_OK; } STDMETHODIMP CKSRKB::GetChildListLevel1(BSTR strCpt, BSTR *ChildListXML) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CString Cpt(strCpt); CComBSTR bstr; CString str; CConcept * CptSuper; CString strOut=""; if (pDoc->m_CptList.Lookup(Cpt,CptSuper)){ POSITION pos = pDoc->m_CptList.GetStartPosition(); CString str; while (pos !=NULL) { CConcept * CptObjChild; pDoc->m_CptList.GetNextAssoc(pos,str,CptObjChild); if (CptSuper->nID == CptObjChild->SupperNID) strOut =strOut+ "NameA + "\" NameL=\"" + CptObjChild->NameL + "\"/>"; } } strOut += ""; bstr.Append(strOut.AllocSysString()); *ChildListXML = bstr.Detach(); return S_OK; } STDMETHODIMP CKSRKB::GetPromptA(BSTR Cpt, BSTR Prop, BSTR *Prompt) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CString strRetPropmt,strCpt(Cpt); CString strProp(Prop); CComBSTR bstr; CConcept * CptObj; CProperty* PropObj; if(pDoc->m_CptList.Lookup(strCpt,CptObj)){ if(CptObj->m_PropList.Lookup(strProp,PropObj)){ strRetPropmt = PropObj->PromptA ; bstr.Append(strRetPropmt.AllocSysString()); *Prompt = bstr.Detach(); } } return S_OK; } STDMETHODIMP CKSRKB::GetLegalA(BSTR Cpt, BSTR Prop, BSTR *Legal) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CString strRetLegal,strCpt(Cpt); CString strProp(Prop); CComBSTR bstr; CConcept * CptObj; CProperty* PropObj; if(pDoc->m_CptList.Lookup(strCpt,CptObj)){ if(CptObj->m_PropList.Lookup(strProp,PropObj)){ CString strVal,strVal1; POSITION valPos= PropObj->m_LegalValueList.GetStartPosition(); while(valPos) { PropObj->m_LegalValueList.GetNextAssoc(valPos,strVal,strVal1); strRetLegal+="" +strVal1 +""; } strRetLegal = "" + strRetLegal + ""; bstr.Append(strRetLegal.AllocSysString()); *Legal = bstr.Detach(); } } return S_OK; } STDMETHODIMP CKSRKB::GetLegalE(BSTR Cpt, BSTR Prop, BSTR ArVal, BSTR *Legal) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CString strRetLegal,strCpt(Cpt); CString strProp(Prop); CString ssVal(ArVal); CComBSTR bstr; CConcept * CptObj; CProperty* PropObj; if(pDoc->m_CptList.Lookup(strCpt,CptObj)){ if(CptObj->m_PropList.Lookup(strProp,PropObj)){ CString strVal,strVal1; POSITION valPos = PropObj->m_LegalValueList.GetStartPosition(); strRetLegal = ssVal; while(valPos) { PropObj->m_LegalValueList.GetNextAssoc(valPos,strVal,strVal1); if (strVal1 == ssVal){ strRetLegal = strVal; break; } } bstr.Append(strRetLegal.AllocSysString()); *Legal = bstr.Detach(); } } return S_OK; } STDMETHODIMP CKSRKB::GetLegalAr(BSTR Cpt, BSTR Prop, BSTR ArVal, BSTR *Legal) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CString strRetLegal,strCpt(Cpt); CString strProp(Prop); CString ssVal(ArVal); CComBSTR bstr; CConcept * CptObj; CProperty* PropObj; if(pDoc->m_CptList.Lookup(strCpt,CptObj)){ if(CptObj->m_PropList.Lookup(strProp,PropObj)){ CString strVal1; POSITION valPos = PropObj->m_LegalValueList.GetStartPosition(); strRetLegal = ssVal; if(PropObj->m_LegalValueList.Lookup(ssVal,strVal1)) strRetLegal = strVal1; bstr.Append(strRetLegal.AllocSysString()); *Legal = bstr.Detach(); } } return S_OK; } void CKSRKB::SortList(CStringArray& List) { CString Value,Value1; CString Valuex; CRule* r = new CRule; CString D1,Min,Temp; for(int i = 0;iGetValue(Value1,"date",pDoc->m_WM); for(int j = i+1;jGetValue(Value,"date",pDoc->m_WM); if(LessThanDate(D1,Min)){ Min = D1; Temp = List[j]; List[j] = List[i]; List[i] = Temp; } } } delete r; } BOOL CKSRKB::LessThanDate(CString D1,CString Min) { CTime tD1,tMin; int ret; CRule* r = new CRule; tD1 = r->StrToDate(D1,ret); tMin = r->StrToDate(Min,ret); if(tD1 < tMin){ delete r; return TRUE; } else { delete r; return FALSE; } } BOOL CKSRKB::Satisfy3days(CString Obj1, CString Obj2) { CRule* r4 = new CRule; CString D1 = r4->GetValue(Obj1,"date",pDoc->m_WM); CString D2 = r4->GetValue(Obj2,"date",pDoc->m_WM); CTime tD1,tD2; int ret; tD1 = r4->StrToDate(D1,ret); tD2 = r4->StrToDate(D2,ret); CTimeSpan ts = tD1 - tD2; delete r4; if (abs(ts.GetDays()) >=3) return TRUE; return FALSE; } void CKSRKB::Propagate(CString Obj,int dif, int j,CStringArray& List) { CRule* r3 = new CRule; AddTime(Obj,dif); CString d1,d2; d1 = r3->GetValue(List[j],"disorder_name",pDoc->m_WM); for(int i = j+1;iGetValue(List[i],"disorder_name",pDoc->m_WM); if(d1 == d2) AddTime(List[i],dif); } delete r3; } void CKSRKB::AddTime(CString Obj, int dif) { CTime tD1; int ret; CRule* r2 = new CRule; CString D1 = r2->GetValue(Obj,"date",pDoc->m_WM); tD1 = r2->StrToDate(D1,ret); CTimeSpan ts(dif+1,0,0,0) ; tD1 = tD1 + ts; CString Temp; Temp.Format("#%d/%d/%d#",tD1.GetDay(),tD1.GetMonth(),tD1.GetYear()); r2->SetToWM(Obj,"date",Temp,pDoc->m_WM,&pDoc->m_CptList); delete r2; } void CKSRKB::adjust(CString Obj1,CString Obj2,int i,int j,CStringArray& List) { CTimeSpan tsFinalDif,tsDif,ts(3,0,0,0) ; CRule* r1 = new CRule; int ret; CString D1 = r1->GetValue(Obj1,"date",pDoc->m_WM); CString D2 = r1->GetValue(Obj2,"date",pDoc->m_WM); CTime tD1 = r1->StrToDate(D1,ret); CTime tD2 = r1->StrToDate(D2,ret); tsDif = tD1-tD2; tsFinalDif = ts- tsDif; int dif = tsFinalDif.GetDays(); dif = abs(dif); CString d1,d2; d1 = r1->GetValue(List[i],"disorder_name",pDoc->m_WM); d2 = r1->GetValue(List[j],"disorder_name",pDoc->m_WM); if(d1==d2) {delete r1; return;} int p1 =atoi(r1->GetValue(Obj1,"priority",pDoc->m_WM)); int p2 =atoi(r1->GetValue(Obj2,"priority",pDoc->m_WM)); if(p1 < p2) Propagate(List[j],dif,j,List); else Propagate(List[i],dif,i, List); delete r1; } //this order is based on "priority" Property void CKSRKB::Order(CStringArray & List) { CRule* r = new CRule; BOOL satisfied = FALSE; while(!satisfied){ for(int i =0;iGetValue(List[i],"priority",pDoc->m_WM); if(M1 == "0") continue; for(int j=i+1;jGetValue(List[j],"priority",pDoc->m_WM); if(M2 == "0") continue; if(Satisfy3days(List[i],List[j]))break; adjust(List[i],List[j],i,j,List); SortList(List); // PrintList(List); } } //if there is two operations with the same disorder name //and the same date then this case cause an infint loop adjust function will not adjust them satisfied = CheckSatisfy(List); } delete r; } //this order is based on "method" Property void CKSRKB::Order(CStringArray & List, CString Prop) { CRule* r = new CRule; BOOL satisfied = FALSE; while(!satisfied){ for(int i =0;iGetValue(List[i],Prop,pDoc->m_WM); if(! (M1 == "foliar application" || M1 == "foliage nutrition")) continue; for(int j=i+1;jGetValue(List[j],Prop,pDoc->m_WM); if(! (M2 == "foliar application" || M1 == "foliage nutrition")) continue; if(Satisfy3days(List[i],List[j]))break; adjust(List[i],List[j],i,j,List); SortList(List); // PrintList(List); } } //if there is two operations with the same disorder name //and the same date then this case cause an infint loop adjust function will not adjust them satisfied = CheckSatisfy(List, Prop); } delete r; } //Shoud be called as //CheckSatisfy(List) //this checkSatisfy based on "priority" property BOOL CKSRKB::CheckSatisfy(CStringArray & List) { CRule* r = new CRule; for(int i =0;iGetValue(List[i],"priority",pDoc->m_WM); if( M1 == "0") continue; CString M2 = r->GetValue(List[i+1],"priority",pDoc->m_WM); if(M2 == "0") continue; if(!Satisfy3days(List[i],List[i+1])){ delete r; return FALSE; } } delete r; return TRUE; } //this checkSatisfy based on "method" property BOOL CKSRKB::CheckSatisfy(CStringArray & List, CString Prop) { CRule* r = new CRule; for(int i =0;iGetValue(List[i],Prop,pDoc->m_WM); if(! (M1 == "foliar application")) continue; CString M2 = r->GetValue(List[i+1],Prop,pDoc->m_WM); if(! (M2 == "foliar application")) continue; if(!Satisfy3days(List[i],List[i+1])){ delete r; return FALSE; } } delete r; return TRUE; } STDMETHODIMP CKSRKB::OrderOpList(BSTR strCpt, BSTR *ChildList) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CString strCpt1(strCpt); CConcept * CptSuper; CConcept * Cpt; CStringArray List; if (pDoc->m_CptList.Lookup(strCpt1,CptSuper)){ POSITION cptpos = pDoc->m_CptList.GetStartPosition(); while (cptpos !=NULL) { pDoc->m_CptList.GetNextAssoc(cptpos,strCpt1,Cpt); if (CptSuper->nID == Cpt->SupperNID){ CValueList * Arr; CValue *Value; if(pDoc->m_WM->Lookup(strCpt1+ "-disorder_name" ,Arr)){ Value=(CValue*)Arr->m_CValueList.GetAt(0); if (Value->V !="none" && Value->V !="") List.Add(Cpt->NameL); } } } } SortList(List); Order(List); CString strOut; for(int i=0;im_CptList.GetStartPosition(); while(pos) { CString str; pDoc->m_CptList.GetNextAssoc(pos,str,CptObj); if(CptObj->m_PropList.Lookup(strProp,PropObj)) strOut+=""; } strOut+=""; bstr.Append(strOut.AllocSysString()); *List = bstr.Detach(); return S_OK; } void CKSRKB:: ExpandRule(CRule * Rule, CString strWMKeyList,CString strCptReplaced) { CValue *Value; CValueList * Arr; CString Condition, Action; if(pDoc->m_WM->Lookup(strWMKeyList,Arr)){ for(int i = 0;im_CValueList.GetSize();i++){ Value=(CValue*)Arr->m_CValueList.GetAt(i); Condition = Rule->GetCondition(); Action = Rule->GetAction(); Condition.Replace(strCptReplaced,Value->V); Action.Replace(strCptReplaced,Value->V); CRule *r = new CRule; r->SetCondition(Condition); r->SetAction(Action); r->Priority= Rule->Priority; CString ss ; ss.Format("%d",i); r->RuleID = Rule->RuleID + "_EXPANDED_" + ss; //AfxMessageBox(r->RuleID+"\n"+Condition +"\n"+Action); pDoc->Cluster->RuleList.SetAt(r->RuleID,r); } } } STDMETHODIMP CKSRKB::ExpandRules(BSTR ssCluster, BSTR ssWMKEY, BSTR ssCptReplaced) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CString strCluster(ssCluster); CString strWMKeyList(ssWMKEY); CString strCptReplaced(ssCptReplaced); if(!pDoc->m_ClusterList.Lookup(strCluster,pDoc->Cluster)) return S_OK; POSITION pos = pDoc->Cluster->RuleList.GetStartPosition(); CString str; while (pos !=NULL){ pDoc->Cluster->RuleList.GetNextAssoc(pos,str,pDoc->Rule); if((pDoc->Rule->RuleID).Find("_EXPANDED_") >= 0) continue; ExpandRule(pDoc->Rule, strWMKeyList,strCptReplaced); } return S_OK; } STDMETHODIMP CKSRKB::DeleteExpandedRule(BSTR ssCluster) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CString strCluster(ssCluster); pDoc->m_ClusterList.Lookup(strCluster,pDoc->Cluster); POSITION pos = pDoc->Cluster->RuleList.GetStartPosition(); CString str; CRule* Rule; while (pos !=NULL){ pDoc->Cluster->RuleList.GetNextAssoc(pos,str,Rule); if(str.Find("_EXPANDED_") <0) continue; pDoc->Cluster->RuleList.RemoveKey(str); delete Rule; } return S_OK; } STDMETHODIMP CKSRKB::ResetAllPropertyOfCptFromWM(BSTR Cpt) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CString ssCpt(Cpt); POSITION pos = pDoc->m_WM->GetStartPosition(); while( pos != NULL ) { CValueList * Arr; CString string,strCpt,strProp; pDoc->m_WM->GetNextAssoc( pos, string, Arr ); CRule * r = new CRule; r->DecomposeCptProp(string,strCpt,strProp); delete r; if(ssCpt== strCpt){ int i = 0; int size = Arr->m_CValueList.GetSize(); while (i < size) delete Arr->m_CValueList.GetAt( i++ ); Arr->m_CValueList.RemoveAll(); pDoc->m_WM->RemoveKey(string); delete Arr; } } return S_OK; } STDMETHODIMP CKSRKB::DifferenceTwoDate(long D, long M, long Y, long D1, long M1, long Y1, BSTR *Dif) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CTime t(Y,M,D,0,0,0); CTime t1(Y1,M1,D1,0,0,0); CTimeSpan ts; CString ss; if(t==t1) ss.Format("0%d",0); else{ if(t > t1) ts = t - t1; else ts = t1 - t; ss.Format("0%d",ts.GetDays()); } CComBSTR bstr; bstr.Append(ss.AllocSysString()); *Dif = bstr.Detach(); return S_OK; } STDMETHODIMP CKSRKB::OrderObligatoryList(BSTR strProp, BSTR *ChildList) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CString ssProp(strProp); CStringArray List; //We should fill from WM to List CValue *Value; CValueList * Arr; if(pDoc->m_WM->Lookup("obligatory_material-name",Arr)){ for(int i = 0;im_CValueList.GetSize();i++){ Value=(CValue*)Arr->m_CValueList.GetAt(i); List.Add(Value->V); } } SortList(List); Order(List,ssProp); CString strOut; for(int i=0;im_CptList.GetStartPosition(); while (pos !=NULL) { CConcept * CptObj; CString str; pDoc->m_CptList.GetNextAssoc(pos,str,CptObj); strOut += ""; } strOut+=""; CComBSTR bstr; bstr.Append(strOut.AllocSysString()); *AllCpt = bstr.Detach(); return S_OK; } STDMETHODIMP CKSRKB::GetAllProps(BSTR strCpt, BSTR *outStr) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CConcept * CptObj; CProperty * Prop; CString Cpt(strCpt); CComBSTR bstr; CString strOut=""; if(pDoc->m_CptList.Lookup(Cpt,CptObj)){ POSITION pos = CptObj->m_PropList.GetStartPosition(); while (pos) { CString strProp; CptObj->m_PropList.GetNextAssoc(pos,strProp,Prop); strOut += "PromptL+"\"/>"; } } strOut +=""; bstr.Append(strOut.AllocSysString()); *outStr = bstr.Detach(); return S_OK; } STDMETHODIMP CKSRKB::GetPromptL(BSTR Cpt, BSTR Prop, BSTR *Prompt) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CString strRetPropmt,strCpt(Cpt); CString strProp(Prop); CComBSTR bstr; CConcept * CptObj; CProperty* PropObj; if(pDoc->m_CptList.Lookup(strCpt,CptObj)){ if(CptObj->m_PropList.Lookup(strProp,PropObj)){ strRetPropmt = PropObj->PromptL ; bstr.Append(strRetPropmt.AllocSysString()); *Prompt = bstr.Detach(); } } return S_OK; } STDMETHODIMP CKSRKB::AssertCptPropValToWM(BSTR Cpt, BSTR Prop, BSTR Val) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CString strCpt(Cpt); CString strProp(Prop); CString strVal(Val); /* CValue *Value; CValueList * Arr; if(!pDoc->m_WM->Lookup(strCpt+"-"+strProp,Arr)){ Value = new CValue; Value->V = strVal; Arr= new CValueList; Arr->m_CValueList.SetAtGrow(0,Value); pDoc->m_WM->SetAt(strCpt+"-"+strProp,Arr); } else{ Value=(CValue*)Arr->m_CValueList.GetAt(0); Value->V = strVal; } */ pDoc->AssertToWM(strCpt,strProp,strVal); return S_OK; } STDMETHODIMP CKSRKB::SetDBAssoListToWM() { AFX_MANAGE_STATE(AfxGetStaticModuleState()) pDoc->SetDBAssociationToWM(); return S_OK; } STDMETHODIMP CKSRKB::GetInitObs(BSTR *InitObs) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) CString strCpt; CString strProp; CString strRetCpt; CComBSTR bstr; CConcept * Cpt; CProperty * Prop; strRetCpt=""; POSITION cptpos = pDoc->m_CptList.GetStartPosition(); while (cptpos !=NULL){ pDoc->m_CptList.GetNextAssoc(cptpos,strCpt,Cpt); POSITION pos = Cpt->m_PropList.GetStartPosition(); while (pos) { Cpt->m_PropList.GetNextAssoc(pos,strProp,Prop); if (pDoc->IsSourceValue("User",Prop)){ strRetCpt+=""; break; } } } strRetCpt+=""; bstr.Append(strRetCpt.AllocSysString()); *InitObs = bstr.Detach(); return S_OK; }