10
10
use MLL \Utils \Tecan \BasicCommands \BreakCommand ;
11
11
use MLL \Utils \Tecan \BasicCommands \Command ;
12
12
use MLL \Utils \Tecan \BasicCommands \Comment ;
13
+ use MLL \Utils \Tecan \BasicCommands \SetDiTiType ;
13
14
use MLL \Utils \Tecan \BasicCommands \UsesTipMask ;
14
15
use MLL \Utils \Tecan \TipMask \TipMask ;
15
16
@@ -20,19 +21,28 @@ class TecanProtocol
20
21
21
22
public const GEMINI_WORKLIST_FILENAME_SUFFIX = '.gwl ' ;
22
23
24
+ private TipMask $ tipMask ;
25
+
26
+ private string $ protocolName ;
27
+
23
28
/** @var Collection<int, Command> */
24
29
private Collection $ commands ;
25
30
26
- private TipMask $ tipMask ;
31
+ public ? int $ defaultDiTiTypeIndex ;
27
32
28
- private string $ protocolName ;
33
+ public ? int $ currentDiTiTypeIndex ;
29
34
30
- public function __construct (TipMask $ tipMask , ?string $ protocolName = null , ?string $ userName = null )
31
- {
32
- $ this ->protocolName = $ protocolName ?? Str::uuid ()->toString ();
35
+ public function __construct (
36
+ TipMask $ tipMask ,
37
+ ?string $ protocolName = null ,
38
+ ?string $ userName = null ,
39
+ ?int $ defaultDiTiTypeIndex = null
40
+ ) {
33
41
$ this ->tipMask = $ tipMask ;
34
-
35
- $ this ->commands = $ this ->initHeader ($ userName , $ protocolName );
42
+ $ this ->protocolName = $ protocolName ?? Str::uuid ()->toString ();
43
+ $ this ->commands = $ this ->buildHeader ($ userName , $ protocolName );
44
+ $ this ->defaultDiTiTypeIndex = $ defaultDiTiTypeIndex ;
45
+ $ this ->currentDiTiTypeIndex = $ defaultDiTiTypeIndex ;
36
46
}
37
47
38
48
public function addCommand (Command $ command ): void
@@ -43,7 +53,8 @@ public function addCommand(Command $command): void
43
53
/** @param Command&UsesTipMask $command */
44
54
public function addCommandCurrentTip (Command $ command ): void
45
55
{
46
- $ command ->setTipMask ($ this ->tipMask ->currentTip ?? TipMask::firstTip ());
56
+ $ tip = $ this ->tipMask ->currentTip ?? TipMask::firstTip ();
57
+ $ this ->setTipMask ($ command , $ tip );
47
58
48
59
$ this ->commands ->add ($ command );
49
60
}
@@ -55,11 +66,37 @@ public function addCommandForNextTip(Command $command): void
55
66
$ this ->commands ->add (new BreakCommand ());
56
67
}
57
68
58
- $ command ->setTipMask ($ this ->tipMask ->nextTip ());
69
+ $ this ->setTipMask ($ command , $ this ->tipMask ->nextTip ());
59
70
60
71
$ this ->commands ->add ($ command );
61
72
}
62
73
74
+ private function shouldUseDifferentTipTypeIndex (): bool
75
+ {
76
+ return $ this ->defaultDiTiTypeIndex !== null
77
+ && $ this ->defaultDiTiTypeIndex !== $ this ->currentDiTiTypeIndex ;
78
+ }
79
+
80
+ private function setTipMask (UsesTipMask $ command , int $ tip ): void
81
+ {
82
+ $ command ->setTipMask ($ tip );
83
+
84
+ if (! $ this ->shouldUseDifferentTipTypeIndex ()) {
85
+ return ;
86
+ }
87
+
88
+ if ($ this ->currentDiTiTypeIndex === null ) {
89
+ return ;
90
+ }
91
+
92
+ if ($ this ->commands ->isEmpty ()
93
+ || $ this ->commandsAreOnlyComments ()
94
+ || $ this ->commands ->last () instanceof BreakCommand
95
+ ) {
96
+ $ this ->commands ->add (new SetDiTiType ($ this ->currentDiTiTypeIndex ));
97
+ }
98
+ }
99
+
63
100
public function buildProtocol (): string
64
101
{
65
102
return $ this ->commands
@@ -73,8 +110,24 @@ public function fileName(): string
73
110
return $ this ->protocolName . self ::GEMINI_WORKLIST_FILENAME_SUFFIX ;
74
111
}
75
112
113
+ public function setCurrentDiTiTypeIndex (int $ currentDiTiTypeIndex ): void
114
+ {
115
+ if (! $ this ->commandsAreOnlyComments ()
116
+ && ! $ this ->commands ->last () instanceof BreakCommand
117
+ ) {
118
+ throw new TecanException ('Cannot change the DiTi type index if the last command is not a break command. ' );
119
+ }
120
+
121
+ $ this ->currentDiTiTypeIndex = $ currentDiTiTypeIndex ;
122
+ }
123
+
124
+ private function commandsAreOnlyComments (): bool
125
+ {
126
+ return $ this ->commands ->every (fn (Command $ command ): bool => $ command instanceof Comment);
127
+ }
128
+
76
129
/** @return Collection<int, Command> */
77
- private function initHeader (?string $ userName , ?string $ protocolName ): Collection
130
+ private function buildHeader (?string $ userName , ?string $ protocolName ): Collection
78
131
{
79
132
$ package = Meta::PACKAGE_NAME ;
80
133
$ version = InstalledVersions::getPrettyVersion ($ package );
@@ -90,6 +143,7 @@ private function initHeader(?string $userName, ?string $protocolName): Collectio
90
143
if ($ userName !== null ) {
91
144
$ commentCommands ->add (new Comment ("User: {$ userName }" ));
92
145
}
146
+
93
147
if ($ protocolName !== null ) {
94
148
$ commentCommands ->add (new Comment ("Protocol name: {$ protocolName }" ));
95
149
}
0 commit comments