0
|
1 |
"{ Package: 'stx:goodies/xmlsuite/relaxng' }"
|
|
2 |
|
|
3 |
"{ NameSpace: RNG }"
|
|
4 |
|
|
5 |
Object subclass:#Automaton
|
|
6 |
instanceVariableNames:'transitions terminalPatterns initialTransitions canBeEmpty'
|
|
7 |
classVariableNames:''
|
|
8 |
poolDictionaries:''
|
|
9 |
category:'Relax NG-Patterns'
|
|
10 |
!
|
|
11 |
|
|
12 |
|
|
13 |
!Automaton class methodsFor:'instance creation'!
|
|
14 |
|
|
15 |
new
|
|
16 |
^ self basicNew initialize.
|
|
17 |
|
|
18 |
"Created: / 14-05-2005 / 18:58:36 / janfrog"
|
|
19 |
! !
|
|
20 |
|
|
21 |
!Automaton methodsFor:'initialization'!
|
|
22 |
|
|
23 |
initialize
|
|
24 |
transitions := self transitionCollectionClass new.
|
|
25 |
initialTransitions := self transitionCollectionClass new.
|
|
26 |
terminalPatterns := Set new.
|
|
27 |
|
|
28 |
"Created: / 14-05-2005 / 18:56:31 / janfrog"
|
|
29 |
!
|
|
30 |
|
|
31 |
initializeFor:aPattern
|
|
32 |
|
|
33 |
| node |
|
|
34 |
node := aPattern node.
|
|
35 |
canBeEmpty := node canBeEmpty.
|
|
36 |
|
|
37 |
node contentModelInitialPatterns do:[:pattern|
|
|
38 |
initialTransitions at:pattern name put:pattern
|
|
39 |
].
|
|
40 |
|
|
41 |
terminalPatterns :=
|
|
42 |
node contentModelFinalPatterns asSet.
|
|
43 |
|
|
44 |
node generateTransitionsIn:self.
|
|
45 |
node children do:[:child|
|
|
46 |
self
|
|
47 |
makeTransitionsFrom:child patternsThatCanBeLast
|
|
48 |
to:child patternsThatCanBeNext
|
|
49 |
]
|
|
50 |
|
|
51 |
"Created: / 14-05-2005 / 18:56:31 / janfrog"
|
|
52 |
! !
|
|
53 |
|
|
54 |
!Automaton methodsFor:'printing'!
|
|
55 |
|
|
56 |
printOn:aStream
|
|
57 |
|
|
58 |
self
|
|
59 |
printTransitions:initialTransitions
|
|
60 |
stateName:'#initialState'
|
|
61 |
final:(initialTransitions isEmpty)
|
|
62 |
on:aStream.
|
|
63 |
|
|
64 |
|
|
65 |
transitions keysAndValuesDo:[:key :value|
|
|
66 |
aStream cr.
|
|
67 |
self
|
|
68 |
printTransitions:value
|
|
69 |
stateName:key nameForPrint
|
|
70 |
final:(terminalPatterns includes:key)
|
|
71 |
on:aStream.
|
|
72 |
|
|
73 |
]
|
|
74 |
|
|
75 |
"Created: / 14-05-2005 / 18:56:31 / janfrog"
|
|
76 |
!
|
|
77 |
|
|
78 |
printTransitions:aDictionary stateName:aString final:aBoolean on:aStream
|
|
79 |
|
|
80 |
aStream nextPut:$[ ; nextPutAll:aString ; nextPut:$].
|
|
81 |
aBoolean ifTrue:[aStream nextPutAll:' (final)'].
|
|
82 |
aStream cr.
|
|
83 |
aDictionary values do:[:pattern|
|
|
84 |
aStream nextPutAll:' -> [',pattern nameForPrint,']'; cr
|
|
85 |
].
|
|
86 |
|
|
87 |
"Created: / 14-05-2005 / 18:56:31 / janfrog"
|
|
88 |
! !
|
|
89 |
|
|
90 |
!Automaton methodsFor:'private'!
|
|
91 |
|
|
92 |
addTarget:target toTransitions:dict
|
|
93 |
^ dict at:target name put:target
|
|
94 |
|
|
95 |
"Created: / 14-05-2005 / 19:01:43 / janfrog"
|
|
96 |
"Modified: / 14-05-2005 / 20:11:07 / janfrog"
|
|
97 |
!
|
|
98 |
|
|
99 |
transitionCollectionClass
|
|
100 |
^ Dictionary
|
|
101 |
|
|
102 |
"Created: / 14-05-2005 / 19:00:05 / janfrog"
|
|
103 |
"Modified: / 14-05-2005 / 20:11:07 / janfrog"
|
|
104 |
! !
|
|
105 |
|
|
106 |
!Automaton methodsFor:'queries'!
|
|
107 |
|
|
108 |
isTerminalPattern:aPatternOrNil
|
|
109 |
|
|
110 |
"nil is special value for initial state"
|
|
111 |
|
|
112 |
^aPatternOrNil ifNil:[
|
|
113 |
canBeEmpty or:[initialTransitions isEmpty].
|
|
114 |
] ifNotNil:[
|
|
115 |
terminalPatterns includes:aPatternOrNil
|
|
116 |
]
|
|
117 |
|
|
118 |
"Created: / 14-05-2005 / 18:56:31 / janfrog"
|
|
119 |
! !
|
|
120 |
|
|
121 |
!Automaton methodsFor:'states'!
|
|
122 |
|
|
123 |
makeTransitionsFrom:sourcePatterns to:targetPatterns
|
|
124 |
sourcePatterns asCollection do:[:source |
|
|
125 |
|dict|
|
|
126 |
"/source isPatternNode ifTrue:[self halt].
|
|
127 |
dict := transitions at:source ifAbsentPut:[ self transitionCollectionClass new ].
|
|
128 |
targetPatterns asCollection do:[:target |
|
|
129 |
self addTarget:target toTransitions:dict
|
|
130 |
]
|
|
131 |
]
|
|
132 |
|
|
133 |
"Created: / 14-05-2005 / 18:56:31 / janfrog"
|
|
134 |
!
|
|
135 |
|
|
136 |
nextStateFrom:aPatternOrNil forTerminal:aString
|
|
137 |
|
|
138 |
"if aPatternOrNil is nil, initialTransitions is used"
|
|
139 |
"returns next pattern or nil if none found"
|
|
140 |
|
|
141 |
aPatternOrNil ifNil:[
|
|
142 |
^initialTransitions
|
|
143 |
at:aString ifAbsent:[nil]
|
|
144 |
].
|
|
145 |
|
|
146 |
^(transitions at:aPatternOrNil ifAbsent:[^nil])
|
|
147 |
at:aString
|
|
148 |
ifAbsent:[nil]
|
|
149 |
|
|
150 |
"Created: / 14-05-2005 / 20:11:02 / janfrog"
|
|
151 |
! !
|
|
152 |
|
|
153 |
!Automaton methodsFor:'utilities'!
|
|
154 |
|
|
155 |
expectedPatternsFromState:aPatternOrNil
|
|
156 |
|
|
157 |
^aPatternOrNil ifNil:[
|
|
158 |
initialTransitions values
|
|
159 |
] ifNotNil:[
|
|
160 |
(transitions at:aPatternOrNil) values
|
|
161 |
]
|
|
162 |
|
|
163 |
"Created: / 14-06-2005 / 12:13:40 / masca"
|
|
164 |
!
|
|
165 |
|
|
166 |
expectedTokensFromState:aPatternOrNil
|
|
167 |
|
|
168 |
^aPatternOrNil ifNil:[
|
|
169 |
initialTransitions keys
|
|
170 |
] ifNotNil:[
|
|
171 |
(transitions at:aPatternOrNil) keys
|
|
172 |
]
|
|
173 |
|
|
174 |
"Created: / 14-05-2005 / 18:56:31 / janfrog"
|
|
175 |
!
|
|
176 |
|
|
177 |
expectedTokensStringFromState:aPatternOrNil
|
|
178 |
|
|
179 |
| s |
|
|
180 |
s := (String new:20) writeStream.
|
|
181 |
(self expectedTokensFromState:aPatternOrNil)
|
|
182 |
do:[:token|s nextPutAll:token]
|
|
183 |
separatedBy:[s nextPutAll:', '].
|
|
184 |
(self isTerminalPattern:aPatternOrNil) ifTrue:[
|
|
185 |
s nextPutAll:' or nothing'
|
|
186 |
].
|
|
187 |
^s contents
|
|
188 |
|
|
189 |
"Created: / 14-05-2005 / 18:56:31 / janfrog"
|
|
190 |
! !
|
|
191 |
|
|
192 |
!Automaton class methodsFor:'documentation'!
|
|
193 |
|
|
194 |
version
|
|
195 |
^ '$Header: /opt/data/cvs/stx/goodies/xmlsuite/relaxng/RNG__Automaton.st,v 1.1.1.1 2005-11-01 22:07:11 vranyj1 Exp $'
|
|
196 |
! !
|