1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* NextFlow (http://github.com/nextflow) |
4
|
|
|
* |
5
|
|
|
* @link http://github.com/nextflow/nextflow-php for the canonical source repository |
6
|
|
|
* @copyright Copyright (c) 2014-2016 NextFlow (http://github.com/nextflow) |
7
|
|
|
* @license https://raw.github.com/nextflow/nextflow-php/master/LICENSE MIT |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace NextFlow\Math\Action; |
11
|
|
|
|
12
|
|
|
use NextFlow\Core\Action\AbstractAction as BaseAbstractAction; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The base final class for simple math actions. |
16
|
|
|
*/ |
17
|
|
View Code Duplication |
abstract class AbstractAction extends BaseAbstractAction |
18
|
|
|
{ |
19
|
|
|
/** The output action socket. */ |
20
|
|
|
const SOCKET_OUTPUT = 'out'; |
21
|
|
|
|
22
|
|
|
/** The left variable socket. */ |
23
|
|
|
const SOCKET_LFT = 'lft'; |
24
|
|
|
|
25
|
|
|
/** The right variable socket. */ |
26
|
|
|
const SOCKET_RGT = 'rgt'; |
27
|
|
|
|
28
|
|
|
/** The result variable socket. */ |
29
|
|
|
const SOCKET_RESULT = 'result'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Initializes a new instance of this class. |
33
|
|
|
*/ |
34
|
|
|
public function __construct() |
35
|
|
|
{ |
36
|
|
|
parent::__construct(); |
37
|
|
|
|
38
|
|
|
$this->createSocket(self::SOCKET_OUTPUT); |
39
|
|
|
$this->createSocket(self::SOCKET_LFT); |
40
|
|
|
$this->createSocket(self::SOCKET_RGT); |
41
|
|
|
$this->createSocket(self::SOCKET_RESULT); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Executes the node's logic. |
46
|
|
|
*/ |
47
|
|
|
public function execute() |
48
|
|
|
{ |
49
|
|
|
$lft = $this->getSocket(self::SOCKET_LFT)->getNode(0); |
50
|
|
|
if ($lft === null || $lft->getValue() === null) { |
51
|
|
|
throw new \InvalidArgumentException('No left variable provided.'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$rgt = $this->getSocket(self::SOCKET_RGT)->getNode(0); |
55
|
|
|
if ($rgt === null || $rgt->getValue() === null) { |
56
|
|
|
throw new \InvalidArgumentException('No right variable provided.'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$result = $this->getSocket(self::SOCKET_RESULT)->getNode(0); |
60
|
|
|
if ($result === null) { |
61
|
|
|
throw new \InvalidArgumentException('No result variable provided.'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$calculatedValue = $this->calculateValue($lft->getValue(), $rgt->getValue()); |
65
|
|
|
|
66
|
|
|
$result->setValue($calculatedValue); |
67
|
|
|
|
68
|
|
|
$this->activate(self::SOCKET_OUTPUT); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* The method that calculates the value. |
73
|
|
|
* |
74
|
|
|
* @param double|float|int $lft The left value. |
75
|
|
|
* @param double|float|int $rgt The right value. |
76
|
|
|
* @return double|float|int Returns the calculated value. |
77
|
|
|
*/ |
78
|
|
|
abstract protected function calculateValue($lft, $rgt); |
79
|
|
|
} |
80
|
|
|
|