/**
  * Short description of method createConditionExpressionFromString
  *
  * @access public
  * @author Joel Bout, <*****@*****.**>
  * @param  string string
  * @return core_kernel_rules_Expression
  */
 public function createConditionExpressionFromString($string)
 {
     $returnValue = null;
     //test: "IF    (11+B_Q01a*3)>=2 AND (B_Q01c=2 OR B_Q01c=7)    	THEN ^variable := 2*(B_Q01a+7)-^variable";
     $question = "if " . $string;
     // str_replace taken from the MsReader class
     $question = str_replace("�", "'", $question);
     // utf8...
     $question = str_replace("�", "'", $question);
     // utf8...
     $question = str_replace("�", "\"", $question);
     $question = str_replace("�", "\"", $question);
     try {
         $analyser = new Analyser();
         common_Logger::i('analysing expression \'' . $question . '\'');
         $tokens = $analyser->analyse($question);
     } catch (Exception $e) {
         throw new common_Exception("CapiXML error: {$e->getMessage()}");
     }
     $returnValue = $this->createConditionExpressionFromXML($tokens->getXml());
     return $returnValue;
 }
 function reduce(&$tokens)
 {
     $level = 1;
     $tokenCloseParenthesis = new TokenCloseParenthesis();
     $begin = 1 + $tokens->pos($this);
     // first token after the parenthesis
     // echo "REDUCE $tokens @$begin\n";
     /*
      * echo "call\n"; print_r($tokens);
      */
     for ($cursor = $begin; $cursor < $tokens->count(); $cursor++) {
         $token = $tokens->get($cursor);
         // echo "@ '$token'\n";
         // echo "@ ".get_class($token)." - '".get_class($this)."'\n";
         if ($token->typeOf($this)) {
             $level++;
             // echo "i+\n";
         } else {
             if ($token->typeOf($tokenCloseParenthesis)) {
                 $level--;
                 // echo "i-\n";
             }
         }
         if (0 == $level) {
             $begin--;
             // $begin is at the position of the opening parenthesis
             $end = $cursor;
             // $end is at the position of the closing parenthesis
             // echo "'$begin' '$end'\n";
             // $toto = $tokens->subSet($begin, $end);
             /*
              * print_r($toto); exit();
              */
             $analyser = new Analyser();
             $subReduce = $analyser->analyse($tokens->subSet($begin + 1, $end - 1));
             // exit();
             $tokens->delSet($begin, $end, $subReduce);
             return;
         }
     }
     // TODO: tell which was the opening parenthesis
     throw new Exception("Closing parenthesis not found");
 }
 /**
  * Short description of method analyseExpression
  *
  * @access public
  * @author Joel Bout, <*****@*****.**>
  * @param  string expressionInput
  * @param  boolean isCondition
  * @return DomDocument
  */
 public function analyseExpression($expressionInput, $isCondition = false)
 {
     $returnValue = null;
     //place the following bloc in a helper
     if (!empty($expressionInput)) {
         $question = $expressionInput;
     } else {
         $question = "";
     }
     //question test:
     //$question = "IF    (11+B_Q01a*3)>=2 AND (B_Q01c=2 OR B_Q01c=7)    	THEN ^variable := 2*(B_Q01a+7)-^variable";
     //analyse the expressionInput string and convert to an XML document:
     if (get_magic_quotes_gpc()) {
         $question = stripslashes($question);
         // Magic quotes are deprecated
     }
     //TODO: check if the variables exists and are associated to the process definition
     $returnValue = null;
     if (!empty($question)) {
         // something to parse
         // str_replace taken from the MsReader class
         $question = str_replace("�", "'", $question);
         // utf8...
         $question = str_replace("�", "'", $question);
         // utf8...
         $question = str_replace("�", "\"", $question);
         $question = str_replace("�", "\"", $question);
         if ($isCondition) {
             $question = "if " . $question;
         }
         try {
             $analyser = new Analyser();
             common_Logger::i('analysing expression \'' . $question . '\'');
             $tokens = $analyser->analyse($question);
             // $xml = htmlspecialchars($tokens->getXmlString(true));
             // $xml = $tokens->getXmlString(true);
             $returnValue = $tokens->getXml();
         } catch (Exception $e) {
             throw new common_Exception("CapiXML error: {$e->getMessage()}");
         }
     }
     return $returnValue;
 }