-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathOutputLanguageVB2.vb
More file actions
executable file
·1707 lines (1486 loc) · 94 KB
/
OutputLanguageVB2.vb
File metadata and controls
executable file
·1707 lines (1486 loc) · 94 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'____________________________________________________________________
'
' © Copyright 2009, brennan-marquez, LLC All rights reserved.
'____________________________________________________________________
'
' $RCSfile: OutputLanguageVB2.vb,v $
'____________________________________________________________________
Imports system.Text.RegularExpressions
Imports System.Windows.Forms.Control
Imports System.Xml
Imports System.IO
Public Class OutputLanguageVB2
Implements IOutputLanguage
Private Enum EA_TYPE ' these are EA types (see the EA help topic "Type" which have been inferred by trial and error
FINAL_STATE = 4
EXIT_STATE = 14
INITIAL_STATE = 3
ENTRY_STATE = 13
TERMINATE_STATE = 12
SYNCH_STATE = 6
End Enum
Private Const CONSTANTS_COLUMN = 70
Private Const COLUMN_WIDTH As Integer = 55
Private _oInterfaces As New Collection
Private _lblOutputFilename As Label
Public Shared Function GetStateTypeName(ByVal oState As EA.Element) As String
Dim sName As String = "Normal"
Static oStateTypeToName As Collection
If oStateTypeToName Is Nothing Then
oStateTypeToName = New Collection
With oStateTypeToName
.Add("Final", CStr(EA_TYPE.FINAL_STATE))
.Add("Exit", CStr(EA_TYPE.EXIT_STATE))
.Add("Initial", CStr(EA_TYPE.INITIAL_STATE))
.Add("Entry", CStr(EA_TYPE.ENTRY_STATE))
.Add("Terminate", CStr(EA_TYPE.TERMINATE_STATE))
.Add("Synch", CStr(EA_TYPE.SYNCH_STATE))
End With
End If
If oStateTypeToName.Contains(oState.Subtype.ToString) Then
sName = oStateTypeToName(oState.Subtype.ToString)
End If
Return sName
End Function
Private Sub createOutputFile(ByVal sOutputFilename As String, ByRef oTextbox As RichTextBox)
If oTextbox.Text.Length > 0 Then
OutputFile.ClearFilesCreated()
Dim oOutputFile As OutputFile = New OutputFile(sOutputFilename, True)
With oOutputFile
.Add("' ________________________________________________________________________________")
.Add("' ")
.Add("' THIS IS AN AUTOMATICALLY GENERATED FILE. DO NOT EDIT IT DIRECTLY")
.Add("' ________________________________________________________________________________")
.Add("' ")
.Add("' File: " & sOutputFilename)
.Add("' ")
.Add("' Created by: " & Application.ProductName & " (EA Model Compiler v" & VERSION & ")")
.Add("' ")
.Add("' Generated: " & Now.ToLongDateString & ", " & Now.ToLongTimeString)
.Add("' ")
.Add("' ________________________________________________________________________________")
.Add("' ")
.Add("' Copyright © 2009, brennan-marquez, LLC All rights reserved.")
.Add("' ________________________________________________________________________________")
.Add("")
.Add("")
.Add("Imports Microsoft.VisualBasic")
.Add("Imports ArchitecturalSupport")
.Add("Imports ArchitecturalSupport.XUnit.Framework")
.Add("Imports Toolbox.Toolbox")
.Add("Imports TestingSupport")
.Add("Imports TestingSupport.Configuration")
.Add("Imports RTSSTests")
.Add("Imports RTSSTests.Utils")
.Add("")
.Add(oTextbox.Text)
End With
oOutputFile.Close()
End If
End Sub
Public Sub CreateDomains(ByVal _oRepository As EA.Repository) Implements IOutputLanguage.CreateDomains
Try
gStatusBox = New frmStatusBox
gStatusBox.VersionStamp = "EA Model Compiler (v" & VERSION & ")"
Dim oRootPackage As EA.Package = _oRepository.Models.GetAt(0).Packages.GetAt(0)
If oRootPackage.Packages.Count > 0 Then
For Each oPackage As EA.Package In oRootPackage.Packages
Dim oTextbox As New RichTextBox
Dim sOutputFilename As String = Path.Combine(Path.GetDirectoryName(_oRepository.ConnectionString), oPackage.Name & ".vb")
gStatusBox.Filename = oPackage.Name
Dim oDomain As Domain = New Domain(oPackage, _oRepository, oTextbox) ' constructor does the work
createOutputFile(sOutputFilename, oTextbox)
Next
gStatusBox.FadeAway()
End If
Catch ex As Exception
Dim oErrorHandler As New sjmErrorHandler(ex)
End Try
End Sub
Protected Class Domain
Private _oTestFixtureElement As EA.Element
Private _TestElements As Collection
Private _oSourceOutput As RichTextBox
Private _oRepository As EA.Repository
Private _sPackageId As String
Private _oProject As EA.Project
Private _oPackage As EA.Package
Private _ClassById As Collection
Private _Triggers As Collection
Private _States As Collection
Private _Notes As Collection
Private _Boundarys As Collection
Private _StateMachines As Collection
Private _IsRealized As Boolean
Private _Name As String
Private _ElementById As Collection
Public ReadOnly Property Name() As String
Get
Return _Name
End Get
End Property
Public ReadOnly Property TestFixtureElement() As EA.Element
Get
Return _oTestFixtureElement
End Get
End Property
Public ReadOnly Property TestElements() As Collection
Get
Return _TestElements
End Get
End Property
Public ReadOnly Property Notes() As Collection
Get
Return _Notes
End Get
End Property
Public ReadOnly Property Boundarys() As Collection
Get
Return _Boundarys
End Get
End Property
Public ReadOnly Property StateMachines() As Collection
Get
Return _StateMachines
End Get
End Property
Public ReadOnly Property ElementById() As Collection
Get
Return _ElementById
End Get
End Property
Public ReadOnly Property States() As Collection
Get
Return _States
End Get
End Property
Public ReadOnly Property EAClass(ByVal iID As Integer) As EA.Element
Get
Dim oClass As EA.Element = Nothing
If _ClassById.Contains(iID.ToString) Then
oClass = _ClassById.Item(iID.ToString)
Else
MsgBox("Unknown class id: " & iID, MsgBoxStyle.Critical)
End If
Return oClass
End Get
End Property
Public ReadOnly Property IsRealized() As Boolean
Get
Return _IsRealized
End Get
End Property
Public Sub New(ByRef oPackage As EA.Package, ByRef oRepository As EA.Repository, ByRef oSourceOutput As RichTextBox)
Try
giNextStateID = oPackage.Name.GetHashCode
giNextEventID = giNextStateID
_oSourceOutput = oSourceOutput
_oRepository = oRepository
_oPackage = oPackage
_sPackageId = oPackage.PackageID
_Name = CanonicalName(oPackage.Name)
_oTestFixtureElement = Nothing
_Boundarys = New Collection
_Notes = New Collection
_TestElements = New Collection
_ClassById = New Collection
_Triggers = New Collection
_States = New Collection
_ElementById = New Collection
_StateMachines = New Collection
_IsRealized = PackageIncludesStereotype(_oPackage, "realized")
If Not _IsRealized Then
With _oSourceOutput
catalogElements()
generateSource()
addStateConstants()
addEventConstants()
.AppendText("End Namespace" & vbCrLf)
addInterfaceClasses()
End With
End If
Catch ex As Exception
Dim oErrorHandler As New sjmErrorHandler(ex)
End Try
End Sub
Private Sub addStateConstants()
Dim oState As EA.Element
Dim iNextStateId As Integer
Dim oUniqueBucket As Collection
Application.DoEvents()
With _oSourceOutput
.AppendText(vbCrLf)
iNextStateId = giNextStateID ' set to our first state id
' .AppendText("Public Module " & _Name & "_States" & vbCrLf)
.AppendText("Friend Module " & _Name & "_States" & vbCrLf)
oUniqueBucket = New Collection
For Each oState In _States
If IsUnique(oState.Name, oUniqueBucket) Then
' .AppendText((" Public Const STATE_" & CanonicalName(CanonicalName(oState.Name)) & " As Long = ").PadRight(CONSTANTS_COLUMN) & iNextStateId & vbCrLf)
.AppendText((" Friend Const STATE_" & CanonicalName(CanonicalName(oState.Name)) & " As Long = ").PadRight(CONSTANTS_COLUMN) & iNextStateId & vbCrLf)
iNextStateId += 1 ' inc the local ID number as well (these will track through this loop)
End If
Next
.AppendText(vbCrLf)
iNextStateId = giNextStateID ' reset back to our first state id (to get IDs aligned)
' .AppendText(" Public sub RegisterStateNames()" & vbCrLf)
.AppendText(" Friend sub RegisterStateNames()" & vbCrLf)
.AppendText(" Static bRegistrationComplete As Boolean = False" & vbCrLf)
.AppendText(vbCrLf)
.AppendText(" If Not bRegistrationComplete Then" & vbCrLf)
.AppendText(" bRegistrationComplete = True" & vbCrLf)
oUniqueBucket = New Collection
For Each oState In _States
If IsUnique(oState.Name, oUniqueBucket) Then
.AppendText((" RegisterStateNameString( """ & CanonicalName(oState.Name) & """, """ & iNextStateId & """) ") & vbCrLf)
iNextStateId += 1
End If
Next
.AppendText(" End If" & vbCrLf)
.AppendText(" End Sub" & vbCrLf)
.AppendText("End Module" & vbCrLf)
.AppendText(vbCrLf)
giNextStateID = iNextStateId ' update the global ID holder
End With
End Sub
Private Sub addEventConstants()
Dim sEvent As String
Dim iNextEventID As Integer
Dim oEvents As New Collection
Dim oState As EA.Element
Dim oConnector As EA.Connector
Dim sTokens() As String
Application.DoEvents()
With _oSourceOutput
iNextEventID = giNextEventID ' set ID starting point
For Each oState In _States
Debug.WriteLine("State: " & oState.Name)
' Debug.Assert(Not oState.Name.Contains("Begin_Initialization"))
For Each oConnector In oState.Connectors
Debug.WriteLine(" Connector: " & oConnector.Name)
Application.DoEvents()
sEvent = oConnector.Name
If sEvent.Length > 0 Then
sTokens = Split(sEvent, ":") ' discard any "ev1:" type prefixes
sEvent = sTokens(0)
sTokens = Split(sEvent, "(") ' discard any parameters supplied
sEvent = CanonicalName(sTokens(0))
If Not oEvents.Contains(sEvent) Then
oEvents.Add(sEvent, sEvent)
End If
End If
Next
Next
.AppendText(vbCrLf)
iNextEventID = giNextEventID ' reset back to starting point
' .AppendText("Public Module " & _Name & "_Events" & vbCrLf)
.AppendText("Friend Module " & _Name & "_Events" & vbCrLf)
For Each sEvent In oEvents
' .AppendText((" Public Const EVENT_" & sEvent & " As Long = ").PadRight(CONSTANTS_COLUMN) & iNextEventID & vbCrLf)
.AppendText((" Friend Const EVENT_" & sEvent & " As Long = ").PadRight(CONSTANTS_COLUMN) & iNextEventID & vbCrLf)
iNextEventID += 1
Next
.AppendText(vbCrLf)
iNextEventID = giNextEventID ' reset back to starting point
' .AppendText(" Public sub RegisterEventNames()" & vbCrLf)
.AppendText(" Friend sub RegisterEventNames()" & vbCrLf)
.AppendText(" Static bRegistrationComplete As Boolean = False" & vbCrLf)
.AppendText(vbCrLf)
.AppendText(" If Not bRegistrationComplete Then" & vbCrLf)
.AppendText(" bRegistrationComplete = True" & vbCrLf)
For Each sEvent In oEvents
.AppendText((" RegisterEventNameString( """ & sEvent & """, """ & iNextEventID & """) ") & vbCrLf)
iNextEventID += 1
Next
.AppendText(" End If" & vbCrLf)
.AppendText(" End Sub" & vbCrLf)
.AppendText("End Module" & vbCrLf)
.AppendText(vbCrLf)
giNextEventID = iNextEventID ' bump the global ID holder to account for the IDs we used
End With
End Sub
Private Sub catalogElements()
Dim oElement As EA.Element
Dim oErrorHandler As New sjmErrorHandler
Try
Application.DoEvents()
For Each oElement In _oPackage.Elements
Application.DoEvents()
oElement.Name = CanonicalName(oElement.Name) ' establish safe names right off the bat (rather than sprinkling everywehre)
_ElementById.Add(oElement, oElement.ElementID) ' just as a debugging convenience, to look up any element from its id only
If oElement.Name.Length = 0 Then
oElement.Name = "NoName_" & oElement.ElementID
End If
Select Case oElement.MetaType
Case "StateMachine"
_StateMachines.Add(oElement, oElement.ElementID)
Case "FinalState"
_States.Add(oElement, oElement.ElementID)
Case "Pseudostate"
_States.Add(oElement, oElement.ElementID)
Case "Trigger"
oErrorHandler.SupplementalInformation = "_Triggers: " & oElement.Name ' in case an exception is thrown
If Not _Triggers.Contains(oElement.Name) Then ' event names may be reused between state machines
_Triggers.Add(oElement, oElement.Name)
End If
Case "Class"
oErrorHandler.SupplementalInformation = "_ClassById: " & oElement.Name ' in case an exception is thrown
_ClassById.Add(oElement, oElement.ElementID)
sortByStereoType(oElement)
Case "State"
recordStateElement(oElement)
Case "Text"
' do nothing with this type, but don't throw a warning that it is unknown
Case Else
Debug.WriteLine(oElement.Name & " is an unhandled metatype " & oElement.MetaType)
End Select
Next
Catch ex As Exception
oErrorHandler.Announce(ex)
End Try
End Sub
Private Sub sortByStereoType(ByVal oElement As EA.Element)
If oElement.Stereotype.ToUpper.Contains("TESTFIXTURE") Then
If _oTestFixtureElement Is Nothing Then
_oTestFixtureElement = oElement
Else
Throw New ApplicationException("Only a single test fixture is allowed per domain, but both '" & _oTestFixtureElement.Name & "' and '" & oElement.Name & "' were found")
End If
Else
If ElementIncludesStereotype(oElement, "test") Then
_TestElements.Add(oElement, oElement.Name)
End If
End If
End Sub
Private Sub recordStateElement(ByVal oElement As EA.Element)
Dim oErrorHandler As New sjmErrorHandler
oErrorHandler.SupplementalInformation = "_States (State): " & oElement.Name ' in case an exception is thrown
'If _StatesNames.Contains(oElement.Name) Then
' Throw New ApplicationException("State name '" & oElement.Name & "' is not unique within the model")
'End If
'_StatesNames.Add(oElement, oElement.Name)
_States.Add(oElement, oElement.ElementID)
End Sub
Private Sub fileFooter(ByVal oPackage As EA.Package)
With _oSourceOutput
.AppendText(vbCrLf)
.AppendText("" & vbCrLf)
.AppendText(vbCrLf)
End With
End Sub
Private Sub addInterfaceClasses()
Dim oClassElement As EA.Element
Dim oEAClass As InterfaceClass
Static iClassCounter As Integer = 0
Try
With _oSourceOutput
For Each oClassElement In _ClassById
Application.DoEvents()
If _sPackageId = oClassElement.PackageID Then
If ElementIncludesStereotype(oClassElement, "interface") Then
oEAClass = New InterfaceClass(oClassElement, Me, _oSourceOutput)
End If
End If
iClassCounter += 1
Next
End With
Catch ex As Exception
Dim oErrorHandler As New sjmErrorHandler(ex)
End Try
End Sub
Private Sub generateSource()
Dim oClassElement As EA.Element
Dim oEAClass As EAClass
Static iClassCounter As Integer = 0
Try
Application.DoEvents()
With _oSourceOutput
.AppendText(vbCrLf)
.AppendText(vbCrLf)
.AppendText("Namespace " & _Name & vbCrLf)
.AppendText("")
If _oPackage.Notes.Length > 0 Then
.AppendText(" ' " & _oPackage.Notes)
End If
.AppendText(vbCrLf)
gStatusBox.ProgressValueMaximum = _ClassById.Count
For Each oClassElement In _ClassById
gStatusBox.ProgressValue = iClassCounter
Application.DoEvents()
If _sPackageId = oClassElement.PackageID Then
If Not ElementIncludesStereotype(oClassElement, "omit") Then
oEAClass = New EAClass(oClassElement, Me, _oSourceOutput)
Else
Debug.WriteLine("Omitting class (by stereotype 'omit'): " & oClassElement.Name)
End If
End If
iClassCounter += 1
Next
fileFooter(_oPackage)
End With
Catch ex As Exception
Dim oErrorHandler As New sjmErrorHandler(ex)
End Try
End Sub
End Class
Private Class EAClass
Private _oSourceOutput As RichTextBox
Private _oDomain As Domain
Private _bFinalStateReported As Boolean = False
Private _oEvents As New Collection
Private _sInitialState As String = ""
Private _bActiveClass As Boolean = False
Private _sParentClassName As String = ""
Private _bIsSupertype As Boolean
Private _bIsSubtype As Boolean
Const COMMENT_START_COLUMN = 40
Public Sub New(ByVal oClassElement As EA.Element, ByRef oDomain As Domain, ByVal oSourceOutput As RichTextBox)
Dim bIsTestFixture As Boolean = False
Dim bIsTest As Boolean = False
Dim sTokens() As String
Try
_oDomain = oDomain
_oSourceOutput = oSourceOutput
If _oDomain.TestFixtureElement IsNot Nothing Then
bIsTestFixture = (_oDomain.TestFixtureElement.Name = oClassElement.Name)
End If
bIsTest = _oDomain.TestElements.Contains(oClassElement.Name)
oClassElement.Name = CanonicalName(oClassElement.Name) ' be sure the class name is in a legal form
sTokens = Split(oClassElement.GetRelationSet(EA.EnumRelationSetType.rsGeneralizeEnd), ",")
_bIsSupertype = (sTokens(0).Length > 0)
sTokens = Split(oClassElement.GetRelationSet(EA.EnumRelationSetType.rsGeneralizeStart), ",")
If (sTokens(0).Length > 0) Then
_sParentClassName = CanonicalName(_oDomain.EAClass(sTokens(0)).Name)
End If
_bIsSubtype = (_sParentClassName.Length > 0) ' this class has a parent class name iff it is a subtype
With _oSourceOutput
.AppendText(vbCrLf)
.AppendText("' _____________________________________________________________________________________" & vbCrLf)
.AppendText("' _ " & vbCrLf)
.AppendText("' _" & (" `" & oClassElement.Name).PadLeft(84) & vbCrLf)
.AppendText("' _____________________________________________________________________________________" & vbCrLf)
.AppendText(vbCrLf)
If _bIsSupertype Then
.AppendText("Friend MustInherit Class " & oClassElement.Name & vbCrLf)
Else
If bIsTestFixture Then
.AppendText("<NUnit.Framework.TestFixture()> _" & vbCrLf)
End If
.AppendText("Friend Class " & oClassElement.Name & vbCrLf)
End If
If _bIsSubtype Then ' if this class is a subtype
.AppendText(" Inherits " & _sParentClassName & vbCrLf)
Else
.AppendText(" Inherits XClass" & vbCrLf)
End If
If bIsTestFixture Then
If _oDomain.TestElements.Count > 0 Then
.AppendText(vbCrLf)
.AppendText(vbCrLf)
.AppendText(" ' NUnit test entry points")
.AppendText(vbCrLf)
For Each oTestElement As EA.Element In _oDomain.TestElements
oTestElement.Name = CanonicalName(oTestElement.Name)
.AppendText(vbCrLf)
.AppendText(" <NUnit.Framework.Test()> _" & vbCrLf)
addCustomAttributes(oTestElement)
' .AppendText(" Public Sub " & oTestElement.Name & " ()" & vbCrLf)
.AppendText(" Friend Sub " & oTestElement.Name & " ()" & vbCrLf)
.AppendText(" Assert.TestBegin()" & vbCrLf)
.AppendText(" XEvent.Send(EVENT_Go, New " & oTestElement.Name & "(""o" & oTestElement.Name & """), ""start " & oTestElement.Name & " running"")" & vbCrLf)
.AppendText(" Assert.TestEnd()" & vbCrLf)
.AppendText(" End Sub" & vbCrLf)
Next
End If
End If
.AppendText(vbCrLf)
.AppendText(" Private Shared _oInstances As New Collection " & vbCrLf)
.AppendText(vbCrLf)
If _bIsSubtype Then
.AppendText(" Protected Overloads Sub deleteSelf()" & vbCrLf)
Else
.AppendText(" Protected Sub deleteSelf()" & vbCrLf)
End If
.AppendText(" On Error Resume Next ' don't worry if the instance has already been removed" & vbCrLf)
.AppendText(" _oInstances.Remove(_sInstanceName)" & vbCrLf)
.AppendText(" End Sub" & vbCrLf)
.AppendText(vbCrLf)
addEvents(oClassElement)
addSynchronousCalls(oClassElement)
addStateMachine(oClassElement)
addAttributes(oClassElement)
.AppendText("End Class " & vbCrLf)
End With
Catch ex As Exception
Dim oErrorHandler As New sjmErrorHandler(ex)
End Try
End Sub
Private Sub addCustomAttributes(ByVal oTestElement As EA.Element)
With _oSourceOutput
If ElementIncludesStereotype(oTestElement, "manual") Then
.AppendText(" <NUnit.Framework.Category(""Manual"")> _" & vbCrLf)
Else
.AppendText(" <NUnit.Framework.Category(""Automated"")> _" & vbCrLf)
End If
If ElementIncludesStereotype(oTestElement, "development") Then
.AppendText(" <NUnit.Framework.Category(""UnderDevelopment"")> _" & vbCrLf)
End If
End With
End Sub
Private Sub addEvents(ByVal oClass As EA.Element)
Dim oMethod As EA.Method
Dim oParameter As EA.Parameter
Dim sLeadingComma As String = ""
With _oSourceOutput
For Each oMethod In oClass.Methods
Application.DoEvents()
If oMethod.Abstract Then ' borrowing the 'abstract' designator to indicate server->client events
Application.DoEvents()
sLeadingComma = ""
' .AppendText(" Public Event " & oMethod.Name & "(")
.AppendText(" Friend Event " & oMethod.Name & "(")
For Each oParameter In oMethod.Parameters
oParameter.Name = CanonicalName(oParameter.Name)
oParameter.Type = canonicalType(oParameter.Type)
.AppendText(sLeadingComma & oParameter.Name & " As " & canonicalType(oParameter.Type))
sLeadingComma = ", "
Next
.AppendText(")")
If oMethod.Notes.Length > 0 Then
.AppendText(" ' " & oMethod.Notes)
End If
.AppendText(vbCrLf)
sLeadingComma = ""
' .AppendText(" Public Sub RaiseEvent_" & oMethod.Name & "(")
.AppendText(" Friend Sub RaiseEvent_" & oMethod.Name & "(")
For Each oParameter In oMethod.Parameters
.AppendText(sLeadingComma & oParameter.Name & " As " & oParameter.Type)
sLeadingComma = ", "
Next
.AppendText(")" & vbCrLf)
sLeadingComma = ""
.AppendText(" RaiseEvent " & oMethod.Name & "(")
For Each oParameter In oMethod.Parameters
.AppendText(sLeadingComma & oParameter.Name)
sLeadingComma = ", "
Next
.AppendText(")" & vbCrLf)
.AppendText(" End Sub" & vbCrLf)
.AppendText(vbCrLf)
End If
Next
.AppendText(vbCrLf)
End With
End Sub
Private Sub addSynchronousCalls(ByVal oClass As EA.Element)
Dim oMethod As EA.Method
Dim oParameter As EA.Parameter
Dim sLeadingComma As String = ""
Dim oOptionalParameters As Collection
Dim oRequiredParameters As Collection
Dim sBehavior As String
Dim sStaticString As String
With _oSourceOutput
For Each oMethod In oClass.Methods
Application.DoEvents()
If oMethod.IsStatic Then
sStaticString = "Shared "
Else
sStaticString = " "
End If
If oMethod.Behavior.Length > 0 Then
sBehavior = StripRichTextFormat(oMethod.Behavior)
Else
Dim sMessage As String = """Method not yet implemented: " & _oDomain.Name & "." & oClass.Name & "." & oMethod.Name & """"
sBehavior = " NUnit.Framework.Assert.Ignore(" & sMessage & ")" & vbCrLf & _
" Console.Writeline(" & sMessage & ")"
End If
oMethod.Name = CanonicalName(oMethod.Name)
If Not oMethod.Abstract Then ' borrowing the 'abstract' designator to indicate server->client events
sLeadingComma = ""
oMethod.ReturnType = canonicalType(oMethod.ReturnType)
If MethodIncludesStereotype(oMethod, "setup") Then
.AppendText(" <NUnit.Framework.Setup()> _" & vbCrLf)
If oMethod.ReturnType.Length > 0 Then ' if there is a return type (other than 'void')
Throw New ApplicationException("An NUnit setup method cannot return a value: " & oMethod.Name)
Else
' .AppendText(" Public " & sStaticString & "Sub " & oMethod.Name & "(")
.AppendText(" Friend " & sStaticString & "Sub " & oMethod.Name & "(")
End If
Else
If oMethod.ReturnType.Length > 0 Then ' if there is a return type (other than 'void')
' .AppendText(" Public" & sStaticString & "Function " & oMethod.Name & "(")
.AppendText(" Friend" & sStaticString & "Function " & oMethod.Name & "(")
Else
' .AppendText(" Public " & sStaticString & "Sub " & oMethod.Name & "(")
.AppendText(" Friend " & sStaticString & "Sub " & oMethod.Name & "(")
End If
End If
oOptionalParameters = New Collection
oRequiredParameters = New Collection
For Each oParameter In oMethod.Parameters
Application.DoEvents()
oParameter.Name = CanonicalName(oParameter.Name)
oParameter.Type = canonicalType(oParameter.Type)
If oParameter.Default.Length > 0 Then ' parameters with default values are assumed to be optional
oParameter.Default = Regex.Replace(oParameter.Default.ToString.Trim, "null", "Nothing", RegexOptions.IgnoreCase)
oOptionalParameters.Add(oParameter)
Else
oRequiredParameters.Add(oParameter)
End If
Next
For Each oParameter In oRequiredParameters
.AppendText(sLeadingComma & oParameter.Name & " As " & oParameter.Type)
sLeadingComma = ", "
Next
For Each oParameter In oOptionalParameters
.AppendText(sLeadingComma & "Optional " & oParameter.Name & " As " & oParameter.Type & " = " & oParameter.Default)
sLeadingComma = ", "
Next
If oMethod.ReturnType.Length > 0 Then ' if there is a return type
.AppendText(") As " & oMethod.ReturnType & vbCrLf)
.AppendText(sBehavior & vbCrLf)
.AppendText(" End Function" & vbCrLf & vbCrLf)
Else
.AppendText(")" & vbCrLf)
.AppendText(sBehavior & vbCrLf)
.AppendText(" End Sub" & vbCrLf & vbCrLf)
End If
End If
Next
End With
End Sub
Private Function getClassStateMachineID(ByVal oClass As EA.Element)
Dim iStateMachineID As Integer = oClass.ElementID
For Each oStateMachineElement As EA.Element In _oDomain.StateMachines
Application.DoEvents()
If oClass.ElementID = oStateMachineElement.ParentID Then
iStateMachineID = oStateMachineElement.ElementID
Exit For
End If
Next
Return iStateMachineID
End Function
Private Sub addStateMachine(ByVal oClass As EA.Element)
Dim oState As EA.Element
Dim oConnector As EA.Connector
Dim sArgumentList As String = ""
Dim bStateMachineHeaderAdded As Boolean = False
Dim oSupplierState As EA.Element = Nothing
Dim oPopulateTransitions As New Collection
Dim sTransitionLine As String
Dim sDomainName As String = _oDomain.Name
Dim Pigtails As New Collection
Dim sPigtail As String
Dim iClassStateMachineID As Integer
Dim oStateNameUniqueBucket As New Collection
Try
iClassStateMachineID = getClassStateMachineID(oClass)
If _oDomain.States.Count > 0 Then
For Each oState In _oDomain.States
If oState.ParentID = iClassStateMachineID Then
_bActiveClass = True
Exit For
End If
Next
If _bActiveClass Then ' if this class has a state machine
With _oSourceOutput
' .AppendText(" Public Overrides Sub DispatchEvent(ByRef oEvent As XEvent, ByVal bIsSelfDirectedEvent As Boolean)" & vbCrLf)
.AppendText(" Friend Overrides Sub DispatchEvent(ByRef oEvent As XEvent, ByVal bIsSelfDirectedEvent As Boolean)" & vbCrLf)
.AppendText(" Dim currentStateAtEntry As Integer = _currentState" & vbCrLf)
.AppendText(" Dim sSelfIcon As String = """"" & vbCrLf)
.AppendText(" Dim bQuiet As Boolean = False" & vbCrLf)
.AppendText(vbCrLf)
.AppendText(" If bIsSelfDirectedEvent Then" & vbCrLf)
.AppendText(" sSelfIcon = ""[""" & vbCrLf)
.AppendText(" End If" & vbCrLf)
.AppendText(vbCrLf)
addNormalPigtailTransition(Pigtails, iClassStateMachineID)
If Pigtails.Count > 0 Then
.AppendText(" Select Case (oEvent.EventID) ' pigtail transitions (from any state to the target state)" & vbCrLf)
For Each sPigtail In Pigtails
Application.DoEvents()
.AppendText(sPigtail)
Next
.AppendText(" Case Else" & vbCrLf)
End If
.AppendText(" Select Case (_currentState)" & vbCrLf)
For Each oState In _oDomain.States
Application.DoEvents()
If oState.ParentID = iClassStateMachineID Then
oPopulateTransitions = New Collection
For Each oConnector In oState.Connectors
Application.DoEvents()
addNormalTransition(oConnector, oState, sArgumentList, oPopulateTransitions, oClass)
Next
If oPopulateTransitions.Count > 0 Then
.AppendText(" Case STATE_" & CanonicalName(oState.Name) & vbCrLf)
.AppendText(" Select Case (oEvent.EventID)" & vbCrLf)
For Each sTransitionLine In oPopulateTransitions
.AppendText(sTransitionLine)
Next
.AppendText(" Case Else" & vbCrLf)
.AppendText(" CannotHappenErrorHandler(""" & CanonicalName(oClass.Name) & """, Me, _currentState, oEvent)" & vbCrLf)
.AppendText(" End Select " & vbCrLf)
.AppendText(vbCrLf)
End If
End If
Next
.AppendText(" Case Else" & vbCrLf)
.AppendText(" TerminalStateErrorHandler(""" & oClass.Name & """, Me, _currentState, oEvent)" & vbCrLf)
.AppendText(" End Select" & vbCrLf)
If Pigtails.Count > 0 Then
.AppendText(" End Select" & vbCrLf)
End If
.AppendText(" AnnounceStateTransition(bQuiet, ""ST "" & Me._sInstanceName & "" "" & ArchitecturalGlobals.StateNameString(currentStateAtEntry) & "" --["" & sSelfIcon & EventNameString(oEvent.EventID) & ""]-> "" & ArchitecturalGlobals.StateNameString(_currentState))" & vbCrLf)
.AppendText(" End Sub" & vbCrLf)
.AppendText(vbCrLf)
For Each oState In _oDomain.States
Application.DoEvents()
If oState.ParentID = iClassStateMachineID Then
If oState.Subtype = EA_TYPE.INITIAL_STATE Then ' if this state is the "meatball" initial state
If oState.Connectors.Count = 1 Then
Dim oOriginalState As EA.Element = _oDomain.States(CType(oState.Connectors.GetAt(0), EA.Connector).SupplierID.ToString)
_sInitialState = CanonicalName(oOriginalState.Name)
Else
MsgBox("An initial state in the state model for class '" & oClass.Name & "' has no transition out", MsgBoxStyle.Critical)
End If
End If
If IsUnique(oState.Name, oStateNameUniqueBucket) Then
addState(oState, oClass)
End If
End If
Next
.AppendText(vbCrLf)
End With
End If
End If
Catch ex As Exception
Dim oErrorHandler As New sjmErrorHandler(ex)
End Try
End Sub
Private Sub addNormalTransition(ByVal oConnector As EA.Connector, _
ByVal oState As EA.Element, _
ByRef sArgumentList As String, _
ByVal oPopulateTransitions As Collection, _
ByVal oClass As EA.Element)
Dim oClientState As EA.Element
Dim sEvent As String
Dim sTokens() As String
Dim oSupplierState As EA.Element = Nothing
Dim sToStateName As String
Dim sTransitionLine As String
Dim oErrorHandler As New sjmErrorHandler
Dim oEVENT_ErrorHandler As New sjmErrorHandler
Try
oErrorHandler.SupplementalInformation = "Class: " & oClass.Name & ", State: " & oState.Name & ", Connector: " & oConnector.Name
If _oDomain.States.Contains(oConnector.ClientID.ToString) Then ' if "to" state is a normal state
oSupplierState = _oDomain.States(oConnector.ClientID.ToString)
If oSupplierState Is oState Then
If _oDomain.States.Contains(oConnector.SupplierID.ToString) Then
oClientState = _oDomain.States(oConnector.SupplierID.ToString)
sEvent = oConnector.Name
sTokens = Split(sEvent, ":")
If sTokens.Length > 1 Then ' peel off any "ev1:" style prefix
sEvent = sTokens(1)
End If
If sEvent.Length > 0 Then
If sEvent.IndexOf(")") > 0 Then
sTokens = Split(sEvent.Substring(0, sEvent.IndexOf(")")), "(") ' peel off any parameter payload
If sTokens.Length > 1 Then
sEvent = sTokens(0)
sArgumentList = sTokens(1)
End If
End If
sToStateName = CanonicalName(oState.Name)
Try
oEVENT_ErrorHandler.SupplementalInformation = "EVENT_" & sEvent
_oEvents.Add(sEvent)
If oClientState.Subtype = EA_TYPE.SYNCH_STATE Then ' if this state is an ignore marker
sTransitionLine = " Case EVENT_" & sEvent & " ' ignored" & vbCrLf & _
" ' do nothing" & vbCrLf
Else
sTransitionLine = " Case EVENT_" & sEvent & vbCrLf & _
" bQuiet = action_" & CanonicalName(oClientState.Name) & "(oEvent)" & vbCrLf
End If
If Not oPopulateTransitions.Contains(sEvent) Then
oErrorHandler.SupplementalInformation = "POP_" & sEvent
oPopulateTransitions.Add(sTransitionLine, sEvent)
End If
Catch ex As Exception
oEVENT_ErrorHandler.Announce(ex)
End Try
End If
End If
End If
End If
Catch ex As Exception
oErrorHandler.Announce(ex)
End Try
End Sub
Private Sub addNormalPigtailTransition(ByRef Pigtails As Collection, ByVal iClassStateMachineID As Integer)
Dim oSupplierState As EA.Element = Nothing
Dim oState As EA.Element
Dim oConnector As EA.Connector
Dim sEvent As String
Dim sTokens() As String
Try
For Each oState In _oDomain.States
Application.DoEvents()
If oState.ParentID = iClassStateMachineID Then
If oState.Subtype = EA_TYPE.ENTRY_STATE Then ' if this state is a pigtail-originating state
For Each oConnector In oState.Connectors
sEvent = oConnector.Name
sTokens = Split(sEvent, ":")
If sTokens.Length > 1 Then ' peel off any "ev1:" style prefix
sEvent = sTokens(1)
End If
If sEvent.Length > 0 Then
If sEvent.IndexOf(")") > 0 Then
sTokens = Split(sEvent.Substring(0, sEvent.IndexOf(")")), "(") ' peel off any parameter payload
If sTokens.Length > 1 Then
sEvent = sTokens(0)
End If
End If
With Pigtails
oSupplierState = _oDomain.States(oConnector.SupplierID.ToString)
.Add(" Case EVENT_" & sEvent & vbCrLf)
.Add(" action_" & CanonicalName(oSupplierState.Name) & "(oEvent)" & vbCrLf)
End With
End If
Next
End If
End If
Next
Catch ex As Exception
Dim oErrorHandler As New sjmErrorHandler(ex)
End Try
End Sub
Private Sub addState(ByVal oState As EA.Element, ByVal oClass As EA.Element)
Dim sLines() As String
Dim sLine As String
Dim bQuiet As Boolean = False
Dim bOmit As Boolean = False
Try
bQuiet = ElementIncludesStereotype(oState, "quiet")
bOmit = ElementIncludesStereotype(oState, "omit")
If (Not bOmit) And (oState.Name.Length > 0) Then
With _oSourceOutput
If oState.Subtype <> EA_TYPE.SYNCH_STATE Then ' if this state is NOT an "ignore event" state
sLines = Split(oState.Notes, vbCrLf)
.AppendText(" '________________________________________________________ " & CanonicalName(oClass.Name) & ": " & CanonicalName(oState.Name) & " (v" & oState.Version & ")" & vbCrLf & vbCrLf)