1 // Condition.cpp: implementation of the CCondition class.
3 //////////////////////////////////////////////////////////////////////
6 #include "logic_editor.h"
8 #include "nel/logic/logic_condition.h"
14 static char THIS_FILE
[]=__FILE__
;
19 using namespace NLLOGIC
;
21 //////////////////////////////////////////////////////////////////////
22 // CConditionNode implementation
23 //////////////////////////////////////////////////////////////////////
25 CConditionNode::CConditionNode(const CConditionNode
&node
)
27 this->m_dComparand
= node
.m_dComparand
;
28 this->m_pParentNode
= node
.m_pParentNode
;
29 this->m_sConditionName
= node
.m_sConditionName
;
30 this->m_sOperator
= node
.m_sOperator
;
31 this->m_sVariableName
= node
.m_sVariableName
;
32 this->m_type
= node
.m_type
;
34 // copy sub condition node tree
35 CConditionNode
*pNode
, *newNode
;
36 POSITION pos
= node
.m_ctSubTree
.GetHeadPosition();
39 pNode
= m_ctSubTree
.GetNext( pos
);
43 newNode
= new CConditionNode( *pNode
);
44 this->m_ctSubTree
.AddTail( newNode
);
45 newNode
->m_pParentNode
= this;
53 CConditionNode::~CConditionNode()
55 CConditionNode
*pNode
= NULL
;
57 // delete all sub nodes
58 POSITION pos
= m_ctSubTree
.GetHeadPosition();
61 pNode
= m_ctSubTree
.GetNext( pos
);
71 const CString
& CConditionNode::getNodeAsString() const
73 m_sNodeString
.Empty();
76 m_sNodeString
= "NOT";
77 else if (m_type
== TERMINATOR
)
78 m_sNodeString
= "term";
79 else if (m_type
== SUB_CONDITION
)
80 m_sNodeString
= m_sConditionName
;
83 m_sNodeString
.Format(_T("%s %s %g"),LPCTSTR(m_sVariableName
),LPCTSTR(m_sOperator
), m_dComparand
);
91 void CConditionNode::changeConditionName( const CString
&old
, const CString
&newName
)
93 CConditionNode
*pNode
= NULL
;
95 POSITION pos
= m_ctSubTree
.GetHeadPosition();
98 pNode
= m_ctSubTree
.GetNext( pos
);
102 pNode
->changeConditionName(old
, newName
);
107 if ( m_sConditionName
== old
)
108 m_sConditionName
= newName
;
111 void CConditionNode::conditionDeleted( const CString
&name
)
113 CConditionNode
*pNode
= NULL
;
116 POSITION pos
= m_ctSubTree
.GetHeadPosition();
120 pNode
= m_ctSubTree
.GetNext( pos
);
124 if ( pNode
->m_sConditionName
!= name
)
125 pNode
->conditionDeleted(name
);
128 this->m_ctSubTree
.RemoveAt( oldpos
);
140 //////////////////////////////////////////////////////////////////////
141 // CCondition implementation
142 //////////////////////////////////////////////////////////////////////
143 CCondition::CCondition()
148 CCondition::CCondition( const CCondition
&cond
)
150 this->m_sName
= cond
.m_sName
;
152 // copy sub condition node tree
153 CConditionNode
*pNode
, *newNode
;
154 POSITION pos
= cond
.m_ctConditionTree
.GetHeadPosition();
157 pNode
= m_ctConditionTree
.GetNext( pos
);
161 newNode
= new CConditionNode( *pNode
);
162 this->m_ctConditionTree
.AddTail( newNode
);
169 CCondition::~CCondition()
171 CConditionNode
*pNode
= NULL
;
173 // delete all sub nodes
174 POSITION pos
= m_ctConditionTree
.GetHeadPosition();
177 pNode
= m_ctConditionTree
.GetNext( pos
);
189 void CCondition::changeConditionName( CString old
, const CString
&newName
) const
191 CConditionNode
*pNode
= NULL
;
193 POSITION pos
= m_ctConditionTree
.GetHeadPosition();
196 pNode
= m_ctConditionTree
.GetNext( pos
);
200 pNode
->changeConditionName( old
, newName
);
206 void CCondition::conditionDeleted( CString name
)
208 CConditionNode
*pNode
= NULL
;
211 POSITION pos
= m_ctConditionTree
.GetHeadPosition();
215 pNode
= m_ctConditionTree
.GetNext( pos
);
219 if ( pNode
->m_sConditionName
!= name
)
220 pNode
->conditionDeleted(name
);
223 this->m_ctConditionTree
.RemoveAt( oldpos
);
233 //-----------------------------------------------------
234 // cConditionNodeToCLogicConditionNode (Editor --> Service)
236 //-----------------------------------------------------
237 void cConditionNodeToCLogicConditionNode(CConditionNode
* conditionNode
, CLogicConditionNode
* logicConditionNode
)
239 // if this node is a terminator node
240 if( conditionNode
->m_type
== CConditionNode::TERMINATOR
)
242 logicConditionNode
->Type
= CLogicConditionNode::TERMINATOR
;
245 // this node is a logic node
247 logicConditionNode
->Type
= CLogicConditionNode::LOGIC_NODE
;
249 // part 1 : a logic block(not/comparison/subcondition)
250 switch( conditionNode
->m_type
)
252 case CConditionNode::NOT
:
254 logicConditionNode
->LogicBlock
.Type
= CLogicConditionLogicBlock::NOT
;
259 case CConditionNode::COMPARISON
:
261 logicConditionNode
->LogicBlock
.Type
= CLogicConditionLogicBlock::COMPARISON
;
263 logicConditionNode
->LogicBlock
.ComparisonBlock
.VariableName
= NLMISC::tStrToUtf8(conditionNode
->m_sVariableName
);
264 logicConditionNode
->LogicBlock
.ComparisonBlock
.Operator
= NLMISC::tStrToUtf8(conditionNode
->m_sOperator
);
265 logicConditionNode
->LogicBlock
.ComparisonBlock
.Comparand
= (sint64
)conditionNode
->m_dComparand
;
269 case CConditionNode::SUB_CONDITION
:
271 logicConditionNode
->LogicBlock
.Type
= CLogicConditionLogicBlock::SUB_CONDITION
;
273 logicConditionNode
->LogicBlock
.SubCondition
= NLMISC::tStrToUtf8(conditionNode
->m_sConditionName
);
278 // part 2 : a condition sub tree
280 for( pos
= conditionNode
->m_ctSubTree
.GetHeadPosition(); pos
!= NULL
; )
282 CConditionNode
* pConditionNode
= conditionNode
->m_ctSubTree
.GetNext( pos
);
283 CLogicConditionNode
* logicConditionNodeTmp
= new CLogicConditionNode();
284 cConditionNodeToCLogicConditionNode( pConditionNode
, logicConditionNodeTmp
);
285 logicConditionNode
->addNode( logicConditionNodeTmp
);
289 } // cConditionNodeToCLogicConditionNode //
294 //-----------------------------------------------------
295 // cConditionToCLogicCondition (Editor --> Service)
297 //-----------------------------------------------------
298 void cConditionToCLogicCondition( CCondition
& condition
, CLogicCondition
& logicCondition
)
301 logicCondition
.setName(NLMISC::tStrToUtf8(condition
.m_sName
));
305 for( pos
= condition
.m_ctConditionTree
.GetHeadPosition(); pos
!= NULL
; )
308 CConditionNode
* pConditionNode
= condition
.m_ctConditionTree
.GetNext( pos
);
311 CLogicConditionNode
* logicConditionNode
= new CLogicConditionNode();
312 cConditionNodeToCLogicConditionNode( pConditionNode
, logicConditionNode
);
315 logicCondition
.addNode( *logicConditionNode
);
318 } // cConditionToCLogicCondition //
325 //-----------------------------------------------------
326 // cLogicConditionNodeToCConditionNode (Service --> Editor)
328 //-----------------------------------------------------
329 void cLogicConditionNodeToCConditionNode( const CLogicConditionNode
* logicConditionNode
, CConditionNode
* node
)
332 if(logicConditionNode
->Type
== CLogicConditionNode::TERMINATOR
)
334 node
->m_type
= CConditionNode::TERMINATOR
;
336 // logic block with condition sub tree
339 // part 1 : a logic block(not/comparison/subcondition)
340 switch( logicConditionNode
->LogicBlock
.Type
)
342 case CLogicConditionLogicBlock::NOT
:
344 node
->m_type
= CConditionNode::NOT
;
348 case CLogicConditionLogicBlock::COMPARISON
:
350 node
->m_type
= CConditionNode::COMPARISON
;
352 node
->m_sVariableName
= CString(logicConditionNode
->LogicBlock
.ComparisonBlock
.VariableName
.c_str());
353 node
->m_sOperator
= CString(logicConditionNode
->LogicBlock
.ComparisonBlock
.Operator
.c_str());
354 node
->m_dComparand
= (double)logicConditionNode
->LogicBlock
.ComparisonBlock
.Comparand
;
359 case CLogicConditionLogicBlock::SUB_CONDITION
:
361 node
->m_type
= CConditionNode::SUB_CONDITION
;
362 node
->m_sConditionName
= CString(logicConditionNode
->LogicBlock
.SubCondition
.c_str());
368 node
->m_type
= CConditionNode::TERMINATOR
;
373 // part 2 : a condition sub tree
374 vector
<CLogicConditionNode
*>::const_iterator itNode
;
375 for( itNode
= logicConditionNode
->_Nodes
.begin(); itNode
!= logicConditionNode
->_Nodes
.end(); ++itNode
)
377 CConditionNode
* nodeTmp
= new CConditionNode();
378 cLogicConditionNodeToCConditionNode( *itNode
, nodeTmp
);
379 nodeTmp
->m_pParentNode
= node
;
380 node
->m_ctSubTree
.AddTail( nodeTmp
);
385 } // cLogicConditionNodeToCConditionNode //
390 //-----------------------------------------------
391 // cLogicConditionToCCondition
393 //-----------------------------------------------
394 void cLogicConditionToCCondition( const CLogicCondition
& logicCondition
, CCondition
& condition
)
397 condition
.m_sName
= CString( logicCondition
.getName().c_str() );
400 vector
<CLogicConditionNode
>::const_iterator itNode
;
401 for( itNode
= logicCondition
.Nodes
.begin(); itNode
!= logicCondition
.Nodes
.end(); ++itNode
)
404 CConditionNode
* node
= new CConditionNode();
405 cLogicConditionNodeToCConditionNode( &(*itNode
), node
);
408 condition
.m_ctConditionTree
.AddTail( node
);
411 } // cLogicConditionToCCondition //