20 #include "ktraderparsetree.h"
24 bool ParseTreeOR::eval( ParseContext *_context )
const
26 ParseContext c1( _context );
27 ParseContext c2( _context );
35 if ( !m_pLeft->eval( &c1 ) )
38 if ( c1.type != ParseContext::T_BOOL )
42 _context->type = ParseContext::T_BOOL;
46 if ( !m_pRight->eval( &c2 ) )
49 if ( c2.type != ParseContext::T_BOOL )
52 _context->b = ( c1.b || c2.b );
53 _context->type = ParseContext::T_BOOL;
58 bool ParseTreeAND::eval( ParseContext *_context )
const
60 _context->type = ParseContext::T_BOOL;
62 ParseContext c1( _context );
63 ParseContext c2( _context );
64 if ( !m_pLeft->eval( &c1 ) )
66 if ( c1.type != ParseContext::T_BOOL )
74 if ( !m_pRight->eval( &c2 ) )
76 if ( c2.type != ParseContext::T_BOOL )
79 _context->b = ( c1.b && c2.b );
84 bool ParseTreeCALC::eval( ParseContext *_context )
const
86 ParseContext c1( _context );
87 ParseContext c2( _context );
88 if ( !m_pLeft->eval( &c1 ) )
90 if ( !m_pRight->eval( &c2 ) )
94 if ( c1.type != ParseContext::T_NUM && c1.type != ParseContext::T_DOUBLE && c1.type != ParseContext::T_BOOL )
97 if ( c2.type != ParseContext::T_NUM && c2.type != ParseContext::T_DOUBLE && c2.type != ParseContext::T_BOOL )
100 if ( c1.type == ParseContext::T_BOOL && c2.type == ParseContext::T_BOOL )
106 if ( c1.type == ParseContext::T_NUM && c2.type == ParseContext::T_DOUBLE )
108 c1.type = ParseContext::T_DOUBLE;
111 else if ( c1.type == ParseContext::T_DOUBLE && c2.type == ParseContext::T_NUM )
113 c2.type = ParseContext::T_DOUBLE;
117 else if ( c1.type == ParseContext::T_BOOL && c2.type == ParseContext::T_NUM )
119 c1.type = ParseContext::T_NUM;
126 else if ( c1.type == ParseContext::T_BOOL && c2.type == ParseContext::T_DOUBLE )
128 c1.type = ParseContext::T_DOUBLE;
135 else if ( c1.type == ParseContext::T_NUM && c2.type == ParseContext::T_BOOL )
137 c2.type = ParseContext::T_NUM;
144 else if ( c1.type == ParseContext::T_DOUBLE && c2.type == ParseContext::T_BOOL )
146 c2.type = ParseContext::T_DOUBLE;
153 _context->type = c1.type;
161 if ( c1.type == ParseContext::T_DOUBLE )
163 _context->f = ( c1.f + c2.f );
166 if ( c1.type == ParseContext::T_NUM )
168 _context->i = ( c1.i + c2.i );
173 if ( c1.type == ParseContext::T_DOUBLE )
175 _context->f = ( c1.f - c2.f );
178 if ( c1.type == ParseContext::T_NUM )
180 _context->i = ( c1.i - c2.i );
185 if ( c1.type == ParseContext::T_DOUBLE )
188 _context->f = ( c1.f * c2.f );
191 if ( c1.type == ParseContext::T_NUM )
193 _context->i = ( c1.i * c2.i );
198 if ( c1.type == ParseContext::T_DOUBLE )
200 _context->f = ( c1.f / c2.f );
203 if ( c1.type == ParseContext::T_NUM )
205 _context->i = ( c1.i / c2.i );
214 bool ParseTreeCMP::eval( ParseContext *_context )
const
217 ParseContext c1( _context );
218 ParseContext c2( _context );
219 if ( !m_pLeft->eval( &c1 ) )
222 if ( !m_pRight->eval( &c2 ) )
228 if ( c1.type == ParseContext::T_NUM && c2.type == ParseContext::T_DOUBLE )
230 c1.type = ParseContext::T_DOUBLE;
233 else if ( c1.type == ParseContext::T_DOUBLE && c2.type == ParseContext::T_NUM )
235 c2.type = ParseContext::T_DOUBLE;
242 _context->type = ParseContext::T_BOOL;
247 if ( c1.type != c2.type )
252 if ( c1.type == ParseContext::T_STRING )
254 _context->b = ( c1.str == c2.str );
257 if ( c1.type == ParseContext::T_BOOL )
259 _context->b = ( c1.b == c2.b );
262 if ( c1.type == ParseContext::T_DOUBLE )
264 _context->b = ( c1.f == c2.f );
267 if ( c1.type == ParseContext::T_NUM )
269 _context->b = ( c1.i == c2.i );
274 if ( c1.type != c2.type )
279 if ( c1.type == ParseContext::T_STRING )
281 _context->b = ( c1.str != c2.str );
284 if ( c1.type == ParseContext::T_BOOL )
286 _context->b = ( c1.b != c2.b );
289 if ( c1.type == ParseContext::T_DOUBLE )
291 _context->b = ( c1.f != c2.f );
294 if ( c1.type == ParseContext::T_NUM )
296 _context->b = ( c1.i != c2.i );
301 if ( c1.type != c2.type )
306 if ( c1.type == ParseContext::T_DOUBLE )
308 _context->b = ( c1.f >= c2.f );
311 if ( c1.type == ParseContext::T_NUM )
313 _context->b = ( c1.i >= c2.i );
320 if ( c1.type != c2.type )
325 if ( c1.type == ParseContext::T_DOUBLE )
327 _context->b = ( c1.f <= c2.f );
330 if ( c1.type == ParseContext::T_NUM )
332 _context->b = ( c1.i <= c2.i );
339 if ( c1.type != c2.type )
344 if ( c1.type == ParseContext::T_DOUBLE )
346 _context->b = ( c1.f < c2.f );
349 if ( c1.type == ParseContext::T_NUM )
351 _context->b = ( c1.i < c2.i );
358 if ( c1.type != c2.type )
363 if ( c1.type == ParseContext::T_DOUBLE )
365 _context->b = ( c1.f > c2.f );
368 if ( c1.type == ParseContext::T_NUM )
370 _context->b = ( c1.i > c2.i );
381 bool ParseTreeNOT::eval( ParseContext *_context )
const
383 ParseContext c1( _context );
384 if ( !m_pLeft->eval( &c1 ) )
386 if ( c1.type != ParseContext::T_BOOL )
390 _context->type = ParseContext::T_BOOL;
395 bool ParseTreeEXIST::eval( ParseContext *_context )
const
397 _context->type = ParseContext::T_BOOL;
399 TQVariant prop = _context->service->property( m_id );
400 _context->b = prop.isValid();
405 bool ParseTreeMATCH::eval( ParseContext *_context )
const
407 _context->type = ParseContext::T_BOOL;
409 ParseContext c1( _context );
410 ParseContext c2( _context );
411 if ( !m_pLeft->eval( &c1 ) )
413 if ( !m_pRight->eval( &c2 ) )
415 if ( c1.type != ParseContext::T_STRING || c2.type != ParseContext::T_STRING )
418 _context->b = ( c2.str.find( c1.str ) != -1 );
423 bool ParseTreeIN::eval( ParseContext *_context )
const
425 _context->type = ParseContext::T_BOOL;
427 ParseContext c1( _context );
428 ParseContext c2( _context );
429 if ( !m_pLeft->eval( &c1 ) )
431 if ( !m_pRight->eval( &c2 ) )
434 if ( (c1.type == ParseContext::T_NUM) &&
435 (c2.type == ParseContext::T_SEQ) &&
436 ((*(c2.seq.begin())).type() == TQVariant::Int)) {
438 TQValueList<TQVariant>::ConstIterator it = c2.seq.begin();
439 TQValueList<TQVariant>::ConstIterator end = c2.seq.end();
441 for (; it != end; it++)
442 if ((*it).type() == TQVariant::Int &&
443 (*it).toInt() == c1.i) {
450 if ( c1.type == ParseContext::T_DOUBLE &&
451 c2.type == ParseContext::T_SEQ &&
452 (*(c2.seq.begin())).type() == TQVariant::Double) {
454 TQValueList<TQVariant>::ConstIterator it = c2.seq.begin();
455 TQValueList<TQVariant>::ConstIterator end = c2.seq.end();
457 for (; it != end; it++)
458 if ((*it).type() == TQVariant::Double &&
459 (*it).toDouble() == c1.i) {
466 if ( c1.type == ParseContext::T_STRING && c2.type == ParseContext::T_STR_SEQ )
468 _context->b = ( c2.strSeq.find( c1.str ) != c2.strSeq.end() );
475 bool ParseTreeID::eval( ParseContext *_context )
const
477 TQVariant prop = _context->service->property( m_str );
478 if ( !prop.isValid() )
481 if ( prop.type() == TQVariant::String )
483 _context->str = prop.toString();
484 _context->type = ParseContext::T_STRING;
488 if ( prop.type() == TQVariant::Int )
490 _context->i = prop.toInt();
491 _context->type = ParseContext::T_NUM;
495 if ( prop.type() == TQVariant::Bool )
497 _context->b = prop.toBool();
498 _context->type = ParseContext::T_BOOL;
502 if ( prop.type() == TQVariant::Double )
504 _context->f = prop.toDouble();
505 _context->type = ParseContext::T_DOUBLE;
509 if ( prop.type() == TQVariant::List )
511 _context->seq = prop.toList();
512 _context->type = ParseContext::T_SEQ;
516 if ( prop.type() == TQVariant::StringList )
518 _context->strSeq = prop.toStringList();
519 _context->type = ParseContext::T_STR_SEQ;
527 bool ParseTreeMIN2::eval( ParseContext *_context )
const
529 _context->type = ParseContext::T_DOUBLE;
531 TQVariant prop = _context->service->property( m_strId );
532 if ( !prop.isValid() )
535 if ( !_context->initMaxima( m_strId ) )
538 TQMap<TQString,PreferencesMaxima>::Iterator it = _context->maxima.find( m_strId );
539 if ( it == _context->maxima.end() )
542 if ( prop.type() == TQVariant::Int && it.data().type == PreferencesMaxima::PM_INT )
544 _context->f = (double)( prop.toInt() - it.data().iMin ) /
545 (
double)(it.data().iMax - it.data().iMin ) * (-2.0) + 1.0;
548 else if ( prop.type() == TQVariant::Double && it.data().type == PreferencesMaxima::PM_DOUBLE )
550 _context->f = ( prop.toDouble() - it.data().fMin ) / (it.data().fMax - it.data().fMin )
558 bool ParseTreeMAX2::eval( ParseContext *_context )
const
560 _context->type = ParseContext::T_DOUBLE;
562 TQVariant prop = _context->service->property( m_strId );
563 if ( !prop.isValid() )
567 if ( !_context->initMaxima( m_strId ) )
571 TQMap<TQString,PreferencesMaxima>::Iterator it = _context->maxima.find( m_strId );
572 if ( it == _context->maxima.end() )
575 if ( prop.type() == TQVariant::Int && it.data().type == PreferencesMaxima::PM_INT )
577 _context->f = (double)( prop.toInt() - it.data().iMin ) /
578 (
double)(it.data().iMax - it.data().iMin ) * 2.0 - 1.0;
581 else if ( prop.type() == TQVariant::Double && it.data().type == PreferencesMaxima::PM_DOUBLE )
583 _context->f = ( prop.toDouble() - it.data().fMin ) /
584 (it.data().fMax - it.data().fMin ) * 2.0 - 1.0;
591 int matchConstraint(
const ParseTreeBase *_tree,
const KService::Ptr &_service,
592 const KServiceTypeProfile::OfferList& _list )
598 TQMap<TQString,PreferencesMaxima> maxima;
599 ParseContext c( _service, _list, maxima );
602 if ( !_tree->eval( &c ) )
606 if ( c.type != ParseContext::T_BOOL )
609 return ( c.b ? 1 : 0 );
612 PreferencesReturn matchPreferences(
const ParseTreeBase *_tree,
const KService::Ptr &_service,
613 const KServiceTypeProfile::OfferList& _list )
616 PreferencesReturn ret;
621 TQMap<TQString,PreferencesMaxima> maxima;
622 ParseContext c( _service, _list, maxima );
624 if ( !_tree->eval( &c ) )
628 if ( c.type == ParseContext::T_NUM )
630 ret.type = PreferencesReturn::PRT_DOUBLE;
633 else if ( c.type == ParseContext::T_DOUBLE )
635 ret.type = PreferencesReturn::PRT_DOUBLE;
642 bool ParseContext::initMaxima(
const TQString& _prop )
645 TQVariant prop = service->property( _prop );
646 if ( !prop.isValid() )
650 if ( prop.type() != TQVariant::Int && prop.type() != TQVariant::Double )
654 TQMap<TQString,PreferencesMaxima>::Iterator it = maxima.find( _prop );
655 if ( it != maxima.end() )
656 return ( it.data().type == PreferencesMaxima::PM_DOUBLE ||
657 it.data().type == PreferencesMaxima::PM_INT );
660 PreferencesMaxima extrema;
661 if ( prop.type() == TQVariant::Int )
662 extrema.type = PreferencesMaxima::PM_INVALID_INT;
664 extrema.type = PreferencesMaxima::PM_INVALID_DOUBLE;
667 KServiceTypeProfile::OfferList::ConstIterator oit = offers.begin();
668 for( ; oit != offers.end(); ++oit )
670 TQVariant p = (*oit).service()->property( _prop );
674 if ( extrema.type == PreferencesMaxima::PM_INVALID_INT )
676 extrema.type = PreferencesMaxima::PM_INT;
677 extrema.iMin = p.toInt();
678 extrema.iMax = p.toInt();
681 else if ( extrema.type == PreferencesMaxima::PM_INT )
683 if ( p.toInt() < extrema.iMin )
684 extrema.iMin = p.toInt();
685 if ( p.toInt() > extrema.iMax )
686 extrema.iMax = p.toInt();
689 else if ( extrema.type == PreferencesMaxima::PM_INVALID_DOUBLE )
691 extrema.type = PreferencesMaxima::PM_DOUBLE;
692 extrema.fMin = p.toDouble();
693 extrema.fMax = p.toDouble();
696 else if ( extrema.type == PreferencesMaxima::PM_DOUBLE )
698 if ( p.toDouble() < it.data().fMin )
699 extrema.fMin = p.toDouble();
700 if ( p.toDouble() > it.data().fMax )
701 extrema.fMax = p.toDouble();
707 maxima.insert( _prop, extrema );
710 return ( extrema.type == PreferencesMaxima::PM_DOUBLE ||
711 extrema.type == PreferencesMaxima::PM_INT );
A namespace for TDEIO globals.