Skip to content

Commit 1f38c6d

Browse files
committed
Init
0 parents  commit 1f38c6d

20 files changed

+1158
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Math PHP computational engine
2+
=============================
3+
4+
Extremely complex library for advance work with math patterns, tokens and computing.
5+
6+
> Please help improve this documentation by sending a Pull request.
7+
8+
Install by Composer:
9+
10+
```
11+
composer require mathematicator-core/engine
12+
```

composer.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "mathematicator-core/engine",
3+
"description": "Math computational Engine",
4+
"homepage": "https://github.com/mathematicator-core/engine",
5+
"authors": [
6+
{
7+
"name": "Jan Barášek",
8+
"homepage": "http://baraja.cz"
9+
}
10+
],
11+
"require": {
12+
"php": ">=7.1"
13+
},
14+
"autoload": {
15+
"classmap": [
16+
"src/"
17+
]
18+
},
19+
"minimum-stability": "stable"
20+
}

config.neon

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
services:
2+
- Mathematicator\Engine\Engine
3+
- Mathematicator\Engine\QueryNormalizer
4+
- Model\Math\NumberRewriter
5+
6+
orm.annotations:
7+
paths:
8+
Mathematicator: %appDir%/../vendor/mathematicator-core

src/Engine.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
namespace Mathematicator\Engine;
4+
5+
6+
use Mathematicator\Router\Router;
7+
use Mathematicator\SearchController\BaseController;
8+
use Nette\DI\Container;
9+
use Tracy\Debugger;
10+
11+
class Engine
12+
{
13+
14+
/**
15+
* @var Router
16+
*/
17+
private $router;
18+
19+
/**
20+
* @var Container
21+
*/
22+
private $serviceFactory;
23+
24+
/**
25+
* @param Router $router
26+
* @param Container $container
27+
*/
28+
public function __construct(Router $router, Container $container)
29+
{
30+
$this->router = $router;
31+
$this->serviceFactory = $container;
32+
}
33+
34+
/**
35+
* @param string $query
36+
* @return EngineResult
37+
*/
38+
public function compute(string $query): EngineResult
39+
{
40+
if (preg_match('/^(?<left>.+?)\s*vs\.?\s*(?<right>.+?)$/', $query, $versus)) {
41+
$result = new EngineMultiResult($query, null);
42+
43+
$result->addResult($this->compute($versus['left']), 'left');
44+
$result->addResult($this->compute($versus['right']), 'right');
45+
46+
return $result;
47+
}
48+
49+
$callback = $this->router->routeQuery($query);
50+
$callbackResult = $this->callCallback($query, $callback);
51+
52+
$result = new EngineSingleResult(
53+
$query,
54+
preg_replace('/^.+\\\\([^\\\\]+)$/', '$1', $callback),
55+
$callbackResult === null ? null : $callbackResult->getInterpret(),
56+
$callbackResult === null ? null : $callbackResult->getBoxes(),
57+
$callbackResult === null ? [] : $callbackResult->getSources()
58+
);
59+
$result->setTime((int) round(Debugger::timer('search_request') * 1000));
60+
61+
return $result;
62+
}
63+
64+
/**
65+
* @param string $query
66+
* @param string $callback
67+
* @return BaseController|null
68+
*/
69+
private function callCallback(string $query, string $callback): ?BaseController
70+
{
71+
/** @var BaseController|null $return */
72+
$return = $this->serviceFactory->getByType($callback);
73+
74+
if ($return !== null) {
75+
$return->setQuery($query);
76+
$return->resetBoxes();
77+
78+
try {
79+
$return->actionDefault();
80+
} catch (TerminateException $e) {
81+
}
82+
}
83+
84+
return $return;
85+
}
86+
87+
}

src/Entity/EngineMultiResult.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Mathematicator\Engine;
4+
5+
6+
class EngineMultiResult extends EngineResult
7+
{
8+
9+
/**
10+
* @var EngineSingleResult[]
11+
*/
12+
private $results;
13+
14+
/**
15+
* @return EngineSingleResult[]
16+
*/
17+
public function getResults(): array
18+
{
19+
return $this->results;
20+
}
21+
22+
/**
23+
* @param string|null $name
24+
* @return EngineResult
25+
* @throws NoResultsException
26+
*/
27+
public function getResult(string $name = null): EngineResult
28+
{
29+
if (!isset($this->results[$name])) {
30+
throw new NoResultsException('Result "' . $name . '" does not exists.');
31+
}
32+
33+
return $this->results[$name];
34+
}
35+
36+
/**
37+
* @param EngineResult $result
38+
* @param string|null $name
39+
*/
40+
public function addResult(EngineResult $result, ?string $name = null): void
41+
{
42+
if ($name !== null) {
43+
$this->results[$name] = $result;
44+
} else {
45+
$this->results[] = $result;
46+
}
47+
48+
$this->setTime($this->getTime() + $result->getTime());
49+
}
50+
51+
}

src/Entity/EngineResult.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace Mathematicator\Engine;
4+
5+
6+
use Nette\SmartObject;
7+
use Nette\Utils\Strings;
8+
9+
class EngineResult
10+
{
11+
12+
use SmartObject;
13+
14+
/**
15+
* @var string
16+
*/
17+
private $query;
18+
19+
/**
20+
* @var string|null
21+
*/
22+
private $matchedRoute;
23+
24+
/**
25+
* @var int
26+
*/
27+
private $time;
28+
29+
/**
30+
* @param string $query
31+
* @param string|null $matchedRoute
32+
*/
33+
public function __construct(string $query, ?string $matchedRoute)
34+
{
35+
$this->query = $query;
36+
$this->matchedRoute = $matchedRoute;
37+
}
38+
39+
/**
40+
* @return string
41+
*/
42+
public function getQuery(): string
43+
{
44+
return $this->query;
45+
}
46+
47+
/**
48+
* @return int
49+
*/
50+
public function getLength(): int
51+
{
52+
return Strings::length($this->getQuery());
53+
}
54+
55+
/**
56+
* @return string|null
57+
*/
58+
public function getMatchedRoute(): ?string
59+
{
60+
return $this->matchedRoute;
61+
}
62+
63+
/**
64+
* @return int
65+
*/
66+
public function getTime(): int
67+
{
68+
return $this->time ?? 0;
69+
}
70+
71+
/**
72+
* @param int $time
73+
*/
74+
public function setTime(int $time): void
75+
{
76+
$this->time = $time;
77+
}
78+
79+
}

src/Entity/EngineSingleResult.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Mathematicator\Engine;
4+
5+
6+
use Mathematicator\Search\Box;
7+
8+
class EngineSingleResult extends EngineResult
9+
{
10+
11+
/**
12+
* @var Box|null
13+
*/
14+
private $interpret;
15+
16+
/**
17+
* @var Box[]
18+
*/
19+
private $boxes;
20+
21+
/**
22+
* @var Source[]
23+
*/
24+
private $sources = [];
25+
26+
/**
27+
* @param string $query
28+
* @param string $matchedRoute
29+
* @param Box|null $interpret
30+
* @param Box[] $boxes
31+
* @param Source[] $sources
32+
*/
33+
public function __construct(string $query, string $matchedRoute, ?Box $interpret, array $boxes, array $sources = [])
34+
{
35+
parent::__construct($query, $matchedRoute);
36+
$this->interpret = $interpret;
37+
$this->boxes = $boxes;
38+
$this->sources = $sources;
39+
}
40+
41+
/**
42+
* @return Box[]
43+
*/
44+
public function getBoxes(): array
45+
{
46+
return $this->boxes;
47+
}
48+
49+
/**
50+
* @return Box|null
51+
*/
52+
public function getInterpret(): ?Box
53+
{
54+
return $this->interpret;
55+
}
56+
57+
/**
58+
* @return Source[]
59+
*/
60+
public function getSources(): array
61+
{
62+
return $this->sources;
63+
}
64+
65+
}

0 commit comments

Comments
 (0)