-
Notifications
You must be signed in to change notification settings - Fork 0
/
PTSystemBuilder.php
97 lines (84 loc) · 2.3 KB
/
PTSystemBuilder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
namespace ziguss\petrinet;
use ziguss\petrinet\net\DirectedNet;
use ziguss\petrinet\net\PTSystem;
/**
* Help building place/transition system.
*
* @author ziguss <yudoujia@163.com>
*/
class PTSystemBuilder
{
protected $places;
protected $transitions;
protected $arcs;
protected $capacity;
protected $weight;
protected $marking;
public function __construct()
{
$this->places = [];
$this->transitions = [];
$this->arcs = [];
$this->capacity = new Capacity();
$this->weight = new Weight();
$this->marking = new Marking();
}
/**
* @param int $tokens
* @param int $capacity
*
* @return Place
*/
public function place($tokens = 0, $capacity = PHP_INT_MAX)
{
if ($capacity < $tokens) {
throw new \InvalidArgumentException('The place is not enough capacity.');
}
$place = new Place();
$this->marking->setTokens($place, $tokens);
$this->capacity->setCapacity($place, $capacity);
return $this->places[] = $place;
}
/**
* @return Transition
*/
public function transition()
{
return $this->transitions[] = new Transition();
}
/**
* @param Element $source
* @param Element $target
* @param int $weight
*
* @return $this
*/
public function connect(Element $source, Element $target, $weight = 1)
{
if ($source instanceof Place && $target instanceof Transition) {
$direct = Arc::PLACE_TRANSITION;
} elseif ($source instanceof Transition && $target instanceof Place) {
$direct = Arc::TRANSITION_PLACE;
} else {
throw new \InvalidArgumentException('An arc must connect a place to a transition or vice-versa.');
}
$arc = new Arc($source, $target, $direct);
$source->addOutputArc($arc);
$target->addInputArc($arc);
$this->weight->setWeight($arc, $weight);
return $this->arcs[] = $arc;
}
/**
* @return PTSystem
*/
public function getPTSystem()
{
return new PTSystem(
new DirectedNet($this->places, $this->transitions, $this->arcs),
$this->capacity,
$this->weight,
$this->marking
);
}
}