@@ -17,16 +17,64 @@ StandardMessageReplyAgent::StandardMessageReplyAgent()
17
17
ScResult StandardMessageReplyAgent::DoProgram (ScActionInitiatedEvent const & event, ScAction & action)
18
18
{
19
19
ScAddr messageNode = action.GetArgument (ScKeynodes::rrel_1);
20
- if (!messageNode.IsValid ())
20
+ if (!messageNode.IsValid ())
21
21
{
22
22
m_logger.Debug (" The action doesn't have a message node" );
23
23
}
24
24
25
25
ScAddr logicRuleNode = generateReplyMessage (messageNode);
26
- ScAddr replyMessageNode = IteratorUtils::getAnyByOutRelation (&m_context, messageNode, MessageKeynodes::nrel_reply);
27
- m_context.GenerateConnector (ScType::ConstPermPosArc, MessageKeynodes::concept_message, replyMessageNode);
26
+ if (!m_context.IsElement (logicRuleNode))
27
+ {
28
+ m_logger.Warning (" The reply message isn't generated because reply construction wasn't found through direct inference agent. Trying to generate default reply message" );
29
+ std::stringstream setElementsTextStream;
30
+ ScAddrVector messageClasses;
31
+ setElementsTextStream << " Извините, я не нашла ответа на Ваш вопрос. Я определила, что данное сообщение является элементом классов:" ;
32
+ ScIterator3Ptr it3 = m_context.CreateIterator3 (
33
+ ScType::NodeConst,
34
+ ScType::EdgeAccessConstPosPerm,
35
+ messageNode);
36
+ while (it3->Next ())
37
+ {
38
+ messageClasses.push_back (it3->Get (0 ));
39
+ }
40
+ ScAddr const & defaultReplyMessage = m_context.GenerateNode (ScType::NodeConst);
41
+ ScAddr const & defaultReplyLink = m_context.GenerateLink (ScType::LinkConst);
42
+ std::string replyText = setElementsTextStream.str ();
43
+ replyText[replyText.length () - 2 ] = ' .' ;
44
+ m_context.SetLinkContent (defaultReplyLink, replyText);
45
+ ScTemplate T;
46
+ T.Triple (
47
+ ScKeynodes::lang_ru,
48
+ ScType::EdgeAccessVarPosPerm,
49
+ defaultReplyLink);
50
+ T.Triple (
51
+ ScType::NodeVar >> " _link" ,
52
+ ScType::EdgeAccessVarPosPerm,
53
+ defaultReplyLink
54
+ );
55
+ T.Quintuple (
56
+ " _link" ,
57
+ ScType::EdgeDCommonVar,
58
+ defaultReplyMessage,
59
+ ScType::EdgeAccessVarPosPerm,
60
+ DialogKeynodes::nrel_sc_text_translation
61
+ );
62
+ T.Quintuple (
63
+ messageNode,
64
+ ScType::EdgeDCommonVar,
65
+ defaultReplyMessage,
66
+ ScType::EdgeAccessVarPosPerm,
67
+ MessageKeynodes::nrel_reply
68
+ );
69
+ ScTemplateResultItem result;
70
+ m_context.GenerateByTemplate (T, result);
71
+ action.SetResult (defaultReplyMessage);
72
+ return action.FinishSuccessfully ();
28
73
29
- if (!replyMessageNode.IsValid ())
74
+ }
75
+ ScAddr replyMessageNode = IteratorUtils::getAnyByOutRelation (&m_context, messageNode, MessageKeynodes::nrel_reply);
76
+ m_context.GenerateConnector (ScType::EdgeAccessConstPosPerm, MessageKeynodes::concept_message, replyMessageNode);
77
+ if (!replyMessageNode.IsValid ())
30
78
{
31
79
m_logger.Error (" The reply message isn't generated" );
32
80
return action.FinishUnsuccessfully ();
@@ -77,11 +125,30 @@ ScAddr StandardMessageReplyAgent::generateReplyMessage(const ScAddr & messageNod
77
125
bool const result = actionDirectInference.InitiateAndWait (DIRECT_INFERENCE_AGENT_WAIT_TIME);
78
126
if (result)
79
127
{
80
- ScAddr answer = IteratorUtils::getAnyByOutRelation (&m_context, actionDirectInference, ScKeynodes::nrel_result);
81
- ScAddr solutionNode = IteratorUtils::getAnyFromSet (&m_context, answer);
82
- ScAddr solutionTreeRoot = IteratorUtils::getAnyByOutRelation (&m_context, solutionNode, ScKeynodes::rrel_1);
83
- if (solutionTreeRoot.IsValid ())
84
- logicRuleNode = IteratorUtils::getAnyByOutRelation (&m_context, solutionTreeRoot, ScKeynodes::rrel_1);
128
+ if (actionDirectInference.IsFinishedSuccessfully ())
129
+ // checking if DicrectInferenceAgent
130
+ // performed successfully
131
+ {
132
+ ScAddr answer = IteratorUtils::getAnyByOutRelation (&m_context, actionDirectInference, ScKeynodes::nrel_result);
133
+ ScAddr solutionNode = IteratorUtils::getAnyFromSet (&m_context, answer);
134
+ ScAddr solutionTreeRoot = IteratorUtils::getAnyByOutRelation (&m_context, solutionNode, ScKeynodes::rrel_1);
135
+ if (solutionTreeRoot.IsValid ())
136
+ {
137
+ ScAddr argNode = IteratorUtils::getAnyFromSet (&m_context, solutionTreeRoot);
138
+ ScAddrVector Arguments;
139
+ ScIterator3Ptr ArgIt = m_context.CreateIterator3 (argNode, ScType::EdgeAccessConstPosPerm, ScType::NodeVar);
140
+ while (ArgIt ->Next ())
141
+ {
142
+ Arguments.push_back (ArgIt->Get (2 ));
143
+ }
144
+ logicRuleNode = IteratorUtils::getAnyByOutRelation (&m_context, solutionTreeRoot, ScKeynodes::rrel_1);
145
+ }
146
+ }
147
+ else
148
+ {
149
+ return logicRuleNode;
150
+ }
151
+
85
152
}
86
153
return logicRuleNode;
87
154
}
0 commit comments