/** * @return Llk */ public static function getCompiler() { if (!self::$compiler) { static::$compiler = Llk::load(new Read('hoa://Library/Math/Arithmetic.pp')); } return static::$compiler; }
/** * Create a parser for arithmitic expressions * * @return Hoa\Compiler\Llk\Parser */ protected function getParser() { if (!isset(static::$parser)) { static::$parser = Hoa\Compiler\Llk\Llk::load(new Hoa\File\Read('hoa://Library/Math/Arithmetic.pp')); } return static::$parser; }
/** * @param $prefix * @param $rules * @return mixed * @throws \Hoa\Compiler\Exception * @throws \Hoa\Regex\Exception */ public function generate($prefix, $rules) { $grammar = new Read('hoa://Library/Regex/Grammar.pp'); $compiler = Llk::load($grammar); $ast = $compiler->parse($prefix . $rules); $generator = new Isotropic(new Random()); return $generator->visit($ast); }
protected function setUp() { $grammar_path = 'grammar/query.pp'; $project_root = '../../../../..'; $grammar_path = realpath(implode('/', [__DIR__, $project_root, $grammar_path])); $parser = Compiler\Llk\Llk::load(new File\Read($grammar_path)); $structure = $this->getMock(Structure::class); $queryVisitorFactory = function () use($structure) { return new QueryVisitor($structure); }; $thesaurus = $this->getMockBuilder(Thesaurus::class)->disableOriginalConstructor()->getMock(); $this->compiler = new QueryCompiler($parser, $queryVisitorFactory, $thesaurus); }
public function case_visitor_exhaustively() { $this->given($sampler = new Compiler\Llk\Sampler\BoundedExhaustive(Compiler\Llk\Llk::load(new File\Read('hoa://Library/Math/Test/Unit/Arithmetic.pp')), new Regex\Visitor\Isotropic(new LUT\Sampler\Random()), 9), $compiler = Compiler\Llk\Llk::load(new File\Read('hoa://Library/Math/Arithmetic.pp')), $visitor = new CUT())->executeOnFailure(function () use(&$expression) { echo 'Failed expression: ', $expression, '.', "\n"; })->when(function () use(&$sampler, &$compiler, &$visitor) { foreach ($sampler as $expression) { $dump = $expression; try { $x = (double) $visitor->visit($compiler->parse($expression)); } catch (\Exception $e) { continue; } eval('$y = (float) ' . $expression . ';'); if (is_nan($x) || is_nan($y)) { $this->boolean(true); continue; } $this->float($x)->isNearlyEqualTo($y); } }); }
public function case_change_default_context() { $this->given($compiler = Compiler\Llk\Llk::load(new File\Read('hoa://Library/Math/Arithmetic.pp')), $visitor = new CUT(), $context = new \Mock\Hoa\Math\Context(), $variableName = 'a_variable', $variableValue = 42)->when($context->addVariable($variableName, function () use($variableValue) { return $variableValue; }))->then->object($visitor->setContext($context))->isNotIdenticalTo($context)->object($visitor->getContext())->isIdenticalTo($context)->float($visitor->visit($compiler->parse('abs(' . $variableName . ' - PI)')))->isEqualTo(abs($variableValue - M_PI))->mock($context)->call('getFunction')->withArguments('abs')->once->call('getVariable')->withArguments($variableName)->once->call('getConstant')->withArguments('PI')->once; }
public function register(Application $app) { $app['phraseanet.SE'] = function ($app) { return $app['search_engine']; }; $app['phraseanet.SE.logger'] = $app->share(function (Application $app) { return new SearchEngineLogger($app); }); $app['search_engine'] = $app->share(function ($app) { $type = $app['conf']->get(['main', 'search-engine', 'type']); if ($type !== SearchEngineInterface::TYPE_ELASTICSEARCH) { throw new InvalidArgumentException(sprintf('Invalid search engine type "%s".', $type)); } /** @var ElasticsearchOptions $options */ $options = $app['elasticsearch.options']; return new ElasticSearchEngine($app, $app['search_engine.structure'], $app['elasticsearch.client'], $options->getIndexName(), $app['query_context.factory'], $app['elasticsearch.facets_response.factory'], $options); }); $app['search_engine.structure'] = $app->share(function (\Alchemy\Phrasea\Application $app) { $databoxes = $app->getDataboxes(); return GlobalStructure::createFromDataboxes($databoxes); }); $app['elasticsearch.facets_response.factory'] = $app->protect(function (array $response) use($app) { return new FacetsResponse(new Escaper(), $response, $app['search_engine.structure']); }); /* Indexer related services */ $app['elasticsearch.indexer'] = $app->share(function ($app) { return new Indexer($app['elasticsearch.client'], $app['elasticsearch.options'], $app['elasticsearch.indexer.term_indexer'], $app['elasticsearch.indexer.record_indexer'], $app['phraseanet.appbox']); }); $app['elasticsearch.indexer.term_indexer'] = $app->share(function ($app) { return new TermIndexer($app['phraseanet.appbox'], array_keys($app['locales.available'])); }); $app['elasticsearch.indexer.record_indexer'] = $app->share(function ($app) { // TODO Use upcomming monolog factory $logger = new \Monolog\Logger('indexer'); $logger->pushHandler(new \Monolog\Handler\ErrorLogHandler()); return new RecordIndexer($app['search_engine.structure'], $app['elasticsearch.record_helper'], $app['thesaurus'], $app['phraseanet.appbox'], array_keys($app['locales.available']), $logger); }); $app['elasticsearch.record_helper'] = $app->share(function ($app) { return new RecordHelper($app['phraseanet.appbox']); }); $app['dispatcher'] = $app->share($app->extend('dispatcher', function (EventDispatcherInterface $dispatcher, $app) { $subscriber = new IndexerSubscriber(new LazyLocator($app, 'elasticsearch.indexer')); $dispatcher->addSubscriber($subscriber); $listener = array($subscriber, 'flushQueue'); // Add synchronous flush when used in CLI. if (isset($app['console'])) { foreach (array_keys($subscriber->getSubscribedEvents()) as $eventName) { $dispatcher->addListener($eventName, $listener, -10); } return $dispatcher; } $dispatcher->addListener(KernelEvents::TERMINATE, $listener); return $dispatcher; })); /* Low-level elasticsearch services */ $app['elasticsearch.client'] = $app->share(function ($app) { /** @var ElasticsearchOptions $options */ $options = $app['elasticsearch.options']; $clientParams = ['hosts' => [sprintf('%s:%s', $options->getHost(), $options->getPort())]]; // Create file logger for debug if ($app['debug']) { /** @var Logger $logger */ $logger = new $app['monolog.logger.class']('search logger'); $logger->pushHandler(new RotatingFileHandler($app['log.path'] . DIRECTORY_SEPARATOR . 'elasticsearch.log', 2), Logger::INFO); $clientParams['logObject'] = $logger; $clientParams['logging'] = true; } return new Client($clientParams); }); $app['elasticsearch.options'] = $app->share(function ($app) { $options = ElasticsearchOptions::fromArray($app['conf']->get(['main', 'search-engine', 'options'], [])); if (empty($options->getIndexName())) { $options->setIndexName(strtolower(sprintf('phraseanet_%s', str_replace(array('/', '.'), array('', ''), $app['conf']->get(['main', 'key']))))); } return $options; }); /* Querying helper services */ $app['thesaurus'] = $app->share(function ($app) { // TODO Use upcomming monolog factory $logger = new \Monolog\Logger('thesaurus'); $logger->pushHandler(new \Monolog\Handler\ErrorLogHandler()); return new Thesaurus($app['elasticsearch.client'], $app['elasticsearch.options'], $logger); }); $app['query_context.factory'] = $app->share(function ($app) { return new QueryContextFactory($app['search_engine.structure'], array_keys($app['locales.available']), $app['locale']); }); $app['query_parser.grammar_path'] = function ($app) { $configPath = ['registry', 'searchengine', 'query-grammar-path']; $grammarPath = $app['conf']->get($configPath, 'grammar/query.pp'); $projectRoot = '../../../../..'; return realpath(implode('/', [__DIR__, $projectRoot, $grammarPath])); }; $app['query_parser'] = $app->share(function ($app) { $grammarPath = $app['query_parser.grammar_path']; return Compiler\Llk\Llk::load(new File\Read($grammarPath)); }); $app['query_visitor.factory'] = $app->protect(function () use($app) { return new QueryVisitor($app['search_engine.structure']); }); $app['query_compiler'] = $app->share(function ($app) { return new QueryCompiler($app['query_parser'], $app['query_visitor.factory'], $app['thesaurus']); }); }