ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 {
16 
22  protected $condition;
23 
30  protected $expressions;
31 
38  protected $operators;
39 
45  protected $index;
46 
52  protected $spaces;
53 
57  public function __construct()
58  {
59  include_once 'Modules/TestQuestionPool/classes/questions/LogicalAnswerCompare/Factory/ilAssLacExpressionManufacturer.php';
60  include_once 'Modules/TestQuestionPool/classes/questions/LogicalAnswerCompare/Factory/ilAssLacOperationManufacturer.php';
61  include_once "Modules/TestQuestionPool/classes/questions/LogicalAnswerCompare/ilAssLacCompositeBuilder.php";
62  }
63 
72  public function parse($condition)
73  {
74  $this->condition = $condition;
75  $this->checkBrackets();
76  $this->fetchExpressions();
77  $this->fetchOperators();
78  $this->cannonicalizeCondition();
79  $nodes = $this->createNodeArray();
80  $compositeBuilder = new ilAssLacCompositeBuilder();
81  return $compositeBuilder->create($nodes);
82  }
83 
91  protected function fetchExpressions()
92  {
94  $this->expressions = $manufacturer->match($this->condition);
95  }
96 
104  protected function fetchOperators()
105  {
107  $this->operators = $manufacturer->match($this->condition);
108  }
109 
117  protected function cannonicalizeCondition()
118  {
120  $this->condition = preg_replace($manufacturer->getPattern(), 'n', $this->condition);
122  $this->condition = preg_replace($manufacturer->getPattern(), 'o', $this->condition);
123  $this->condition = preg_replace("/no/", "n o", $this->condition);
124  $this->condition = preg_replace("/on/", "o n", $this->condition);
125 
126  for ($i = 0; $i < strlen($this->condition); $i++) {
127  if ($this->condition[$i] == "!" && !$this->isNegationSurroundedByBrackets($i)) {
129  }
130  }
131  }
132 
133  protected function checkBrackets()
134  {
135  $num_brackets_open = substr_count($this->condition, "(");
136  $num_brackets_close = substr_count($this->condition, ")");
137 
138  if ($num_brackets_open > $num_brackets_close) {
139  throw new ilAssLacMissingBracket(")");
140  }
141  if ($num_brackets_open < $num_brackets_close) {
142  throw new ilAssLacMissingBracket("(");
143  }
144  }
145 
168  protected function createNodeArray()
169  {
170  $expected = array("n", "(", "!");
171  $group = array();
172  $negation = false;
173 
174  while ($this->index < strlen($this->condition)) {
175  $a = $this->condition[$this->index];
176  if (trim($this->condition[$this->index]) != "" && in_array($this->condition[$this->index], $expected)) {
177  if ($this->condition[$this->index] == ')') {
178  return $group;
179  } elseif ($this->condition[$this->index] == 'n') {
180  $group[] = array('type' => 'expression', 'value' => array_shift($this->expressions));
181  $expected = array("o", ")");
182  } elseif ($this->condition[$this->index] == 'o') {
183  $group[] = array('type' => 'operator', 'value' => array_shift($this->operators));
184  $expected = array("n", "(", "!");
185  } elseif ($this->condition[$this->index] == '(') {
186  $this->index++;
187  $elements = $this->createNodeArray();
188  $group[] = array('type' => "group", "negated" => $negation, 'nodes' => $elements);
189  $negation = false;
190  $expected = array("o",")");
191  } elseif ($this->condition[$this->index] == "!") {
192  $negation = true;
193  }
194  } elseif (trim($this->condition[$this->index]) != "") {
195  throw new ilAssLacConditionParserException($this->index - $this->spaces + 1);
196  } else {
197  $this->spaces++;
198  }
199 
200  $this->index++;
201  }
202  return array('type' => 'group', "negated" => $negation, 'nodes' => $group);
203  }
204 
208  public function getExpressions()
209  {
210  return $this->expressions;
211  }
212 
216  protected function surroundNegationExpression($index)
217  {
218  $start = strpos($this->condition, "n", $index + 1);
219  $end = false;
220 
221  if ($start !== false) {
222  $end = strpos($this->condition, "n", $start + 1);
223  }
224 
225  if ($start !== false && $end !== false) {
226  $this->condition = substr_replace($this->condition, "(n o n)", $start, $end - $start + 1);
227  }
228  }
229 
236  {
237  $next_bracket = strpos($this->condition, "(", $index + 1);
238  $next_expression = strpos($this->condition, "n", $index + 1);
239 
240  return $next_bracket !== false & $next_bracket < $next_expression;
241  }
242 }
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...
$start
Definition: bench.php:8
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...
static _getInstance()
Get an Instance of OperationManufacturer.
$i
Definition: disco.tpl.php:19
parse($condition)
Parses the delivered condition and creates a composite tree Structure.
__construct()
Construct requirements.