ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
ilAssLacConditionParser.php
Go to the documentation of this file.
1 <?php
2 
3 require_once "Modules/TestQuestionPool/classes/questions/LogicalAnswerCompare/Exception/ilAssLacMissingBracket.php";
4 require_once "Modules/TestQuestionPool/classes/questions/LogicalAnswerCompare/Exception/ilAssLacConditionParserException.php";
5 
15 
21  protected $condition;
22 
29  protected $expressions;
30 
37  protected $operators;
38 
44  protected $index;
45 
51  protected $spaces;
52 
56  public function __construct(){
57  include_once 'Modules/TestQuestionPool/classes/questions/LogicalAnswerCompare/Factory/ilAssLacExpressionManufacturer.php';
58  include_once 'Modules/TestQuestionPool/classes/questions/LogicalAnswerCompare/Factory/ilAssLacOperationManufacturer.php';
59  include_once "Modules/TestQuestionPool/classes/questions/LogicalAnswerCompare/ilAssLacCompositeBuilder.php";
60  }
61 
70  public function parse($condition){
71  $this->condition = $condition;
72  $this->checkBrackets();
73  $this->fetchExpressions();
74  $this->fetchOperators();
75  $this->cannonicalizeCondition();
76  $nodes = $this->createNodeArray();
77  $compositeBuilder = new ilAssLacCompositeBuilder();
78  return $compositeBuilder->create($nodes);
79  }
80 
88  protected function fetchExpressions(){
90  $this->expressions = $manufacturer->match($this->condition);
91  }
92 
100  protected function fetchOperators(){
102  $this->operators = $manufacturer->match($this->condition);
103  }
104 
112  protected function cannonicalizeCondition(){
114  $this->condition = preg_replace($manufacturer->getPattern(), 'n', $this->condition);
116  $this->condition = preg_replace($manufacturer->getPattern(), 'o', $this->condition);
117  $this->condition = preg_replace("/no/", "n o", $this->condition);
118  $this->condition = preg_replace("/on/", "o n", $this->condition);
119 
120  for($i = 0; $i < strlen($this->condition); $i++)
121  {
122  if($this->condition[$i] == "!" && !$this->isNegationSurroundedByBrackets($i))
123  {
124  $this->surroundNegationExpression($i);
125  }
126  }
127  }
128 
129  protected function checkBrackets()
130  {
131  $num_brackets_open = substr_count($this->condition, "(");
132  $num_brackets_close = substr_count($this->condition, ")");
133 
134  if($num_brackets_open > $num_brackets_close)
135  {
136  throw new ilAssLacMissingBracket(")");
137  }
138  if($num_brackets_open < $num_brackets_close)
139  {
140  throw new ilAssLacMissingBracket("(");
141  }
142  }
143 
166  protected function createNodeArray()
167  {
168  $expected = array("n", "(", "!");
169  $group = array();
170  $negation = false;
171 
172  while($this->index < strlen($this->condition))
173  {
174  $a = $this->condition[$this->index];
175  if(trim($this->condition[$this->index]) != "" && in_array($this->condition[$this->index], $expected))
176  {
177  if ($this->condition[$this->index] == ')')
178  {
179  return $group;
180  }
181  else if ($this->condition[$this->index] == 'n')
182  {
183  $group[] = array('type' => 'expression', 'value' => array_shift($this->expressions));
184  $expected = array("o", ")");
185  }
186  else if ($this->condition[$this->index] == 'o')
187  {
188  $group[] = array('type' => 'operator', 'value' => array_shift($this->operators));
189  $expected = array("n", "(", "!");
190  }
191  else if ($this->condition[$this->index] == '(')
192  {
193  $this->index++;
194  $elements = $this->createNodeArray();
195  $group[] = array('type' => "group", "negated" => $negation, 'nodes' => $elements);
196  $negation = false;
197  $expected = array("o",")");
198  }
199  else if($this->condition[$this->index] == "!")
200  {
201  $negation = true;
202  }
203  }
204  elseif(trim($this->condition[$this->index]) != "")
205  {
206  throw new ilAssLacConditionParserException($this->index-$this->spaces+1);
207  }
208  else
209  {
210  $this->spaces++;
211  }
212 
213  $this->index++;
214  }
215  return array('type' => 'group', "negated" => $negation, 'nodes' => $group);
216  }
217 
221  public function getExpressions()
222  {
223  return $this->expressions;
224  }
225 
229  protected function surroundNegationExpression($index)
230  {
231  $start = strpos($this->condition, "n", $index + 1);
232  $end = false;
233 
234  if($start !== false)
235  {
236  $end = strpos($this->condition, "n", $start + 1);
237  }
238 
239  if($start !== false && $end !== false)
240  {
241  $this->condition = substr_replace($this->condition, "(n o n)", $start, $end - $start + 1);
242  }
243  }
244 
251  {
252  $next_bracket = strpos($this->condition, "(", $index+1);
253  $next_expression = strpos($this->condition, "n", $index+1);
254 
255  return $next_bracket !== false & $next_bracket < $next_expression;
256  }
257 }
cannonicalizeCondition()
Cannonicalize the condition into a more general form.
createNodeArray()
Creates an array representing all Nodes in a condition based on the fetched expressions and operators...
static _getInstance()
Get an Instance of ExpressionManufacturer.
fetchOperators()
Matches all operators in the current condition and assign these to the class attribute ConditionParse...
fetchExpressions()
Matches all expressions in the current condition and assign these to the class attribute ConditionPar...
Create styles array
The data for the language used.
static _getInstance()
Get an Instance of OperationManufacturer.
parse($condition)
Parses the delivered condition and creates a composite tree Structure.
__construct()
Construct requirements.