public function testConvertPaperExample() { // Example 2. Let W = {abccde, cccad, bfegg, bfehi}. $automaton = new slCountingSingleOccurenceAutomaton(); $automaton->learn(array('a', 'b', 'c', 'c', 'd', 'e')); $automaton->learn(array('c', 'c', 'c', 'a', 'd')); $automaton->learn(array('b', 'f', 'e', 'g', 'g')); $automaton->learn(array('b', 'f', 'e', 'h', 'i')); $converter = new slChareConverter(); $regexp = $converter->convertAutomaton($automaton); $this->assertEquals(new slRegularExpressionSequence(new slRegularExpressionRepeatedAtLeastOnce(new slRegularExpressionChoice(new slRegularExpressionElement('a'), new slRegularExpressionElement('b'), new slRegularExpressionElement('c'))), new slRegularExpressionChoice(new slRegularExpressionElement('d'), new slRegularExpressionElement('f')), new slRegularExpressionOptional(new slRegularExpressionElement('e')), new slRegularExpressionRepeated(new slRegularExpressionElement('g')), new slRegularExpressionOptional(new slRegularExpressionElement('h')), new slRegularExpressionOptional(new slRegularExpressionElement('i'))), $regexp); }
/** * Build regular expression * * Builds the regilar expression from the provided automaton, where each * node associates with one of the equivalency classes, and a topologically * sorted list of these nodes. * * The provided automaton must be an instance of the * slCountingSingleOccurenceAutomaton, so t at it provides information on * how often the token occur in the learned strings. * * @param slCountingSingleOccurenceAutomaton $automaton * @param array $classes * @return slRegularExpression */ protected function buildRegularExpression(slCountingSingleOccurenceAutomaton $automaton, array $classes) { // xsd:all may only occur outmost and makes only sense for equivalence // classes with more then one elment. if (count($classes) === 1) { $class = reset($classes); $term = $eClasses = $this->equivalenceClasses[$class]; $count = $automaton->getOccurenceSum($eClasses); $nodes = $automaton->getNodes(); $generalCount = $automaton->getGeneralOccurences($eClasses); if (count($term) > 1 && $generalCount['max'] === 1 && $count['max'] > 1) { // Inference all $term = new slRegularExpressionAll(array_map(function ($term) use($nodes) { return new slRegularExpressionElement($nodes[$term]); }, $term)); $term->minOccurences = $generalCount['min']; return $term; } } return parent::buildRegularExpression($automaton, $classes); }
/** * Convert automaton to regular expression * * @param slAutomaton $automaton * @return slRegularExpression */ protected function convertRegularExpression($automaton) { // Convert automatons $converter = new slSoreConverter(); if (($expression = $converter->convertAutomaton($automaton)) !== false) { return $expression; } $converter = new slChareConverter(); return $converter->convertAutomaton($automaton); }