summaryrefslogtreecommitdiff
path: root/ooxml/source/framework/SchemaParser/src/org/apache/openoffice/ooxml/schema/automaton/HopcroftMinimizer.java
blob: 9177ccc707ae1f5d67bb540abda2317b05481bb7 (plain)
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
/**************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*
*************************************************************/

package org.apache.openoffice.ooxml.schema.automaton;

import java.io.PrintStream;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Vector;

import org.apache.openoffice.ooxml.schema.model.attribute.Attribute;
import org.apache.openoffice.ooxml.schema.model.base.Location;
import org.apache.openoffice.ooxml.schema.model.base.QualifiedName;

/** Minimize an DFA with respect to its number of states.
 *  This is most important for the use of the 'all' element in the OOXML
 *  specification which leads to a lot of additional states and transitions.
 */
public class HopcroftMinimizer
{
    /** Create a DFA that is equivalent to a given DFA but has the minimal
     *  number of states.
     */
    public static FiniteAutomaton MinimizeDFA (
        final StateContainer aNewStateContainer,
        final StateContext aOriginalStates,
        final Vector<Attribute> aAttributes,
        final Location aLocation,
        final PrintStream aLog)
    {
        if (aLog != null)
        {
            aLog.printf("minimizing %d states and %d transitions\n",
                aOriginalStates.GetStateCount(),
                aOriginalStates.GetTransitionCount());
            DisplayStates(aOriginalStates, aLog);
        }

        TreeSet<StateSet> aT = new TreeSet<>();
        TreeSet<StateSet> aP = new TreeSet<>();
        Map<State,StateSet> aTMap = new HashMap<>();
        Map<State,StateSet> aPMap = new HashMap<>();
        InitializeMap(aT, aTMap, aOriginalStates.GetStates());

        // Split partitions until there is nothing else to do.
        while ( ! AreSetsOfStateSetsEqual(aP, aT))
        {
            if (aLog != null)
                aLog.printf("T has %d members\n", aT.size());

            aP = aT;
            aPMap = aTMap;
            aT = new TreeSet<>();
            aTMap = new HashMap<>();

            for (final StateSet aSet : aP)
            {
                final Iterable<StateSet> aParts = Split(aSet, aP, aPMap);
                if (aParts == null)
                {
                    // No split necessary.
                    assert( ! aSet.IsEmpty());
                    aT.add(aSet);
                    for (final State aState : aSet.GetStates())
                        aTMap.put(aState, aSet);
                }
                else
                {
                    for (final StateSet aPart : aParts)
                    {
                        assert( ! aPart.IsEmpty());
                        aT.add(aPart);

                        for (final State aState : aPart.GetStates())
                            aTMap.put(aState, aPart);
                    }
                }
            }
        }

        // Create new states.
        final StateContext aMinimizedStates = CreateNewStates(
            aP,
            aPMap,
            aNewStateContainer,
            aOriginalStates);

        if (aLog != null)
        {
            aLog.printf("to %d states and %d transitions\n",
                aMinimizedStates.GetStateCount(),
                aMinimizedStates.GetTransitionCount());
            DisplayStates(aMinimizedStates, aLog);
            for (final StateSet aSet : aT)
                aLog.printf("    %s\n", aSet.toString());
        }

        // Create and return the new minimized automaton.
        return new FiniteAutomaton(
            aMinimizedStates,
            aAttributes,
            aLocation);
    }




    /** We start with two sets.  One contains all start states (in our case
    *   just one), the other contains all other states.
    */
    private static void InitializeMap (
        final Set<StateSet> aSet,
        final Map<State,StateSet> aMap,
        final Iterable<State> aStates)
    {
        final StateSet aAcceptingStates = new StateSet();
        final StateSet aNonAcceptingStates = new StateSet();
        for (final State aState : aStates)
        {
            if (aState.IsAccepting())
            {
                aAcceptingStates.AddState(aState);
                aMap.put(aState, aAcceptingStates);
            }
            else
            {
                aNonAcceptingStates.AddState(aState);
                aMap.put(aState, aNonAcceptingStates);
            }
        }
        if (aAcceptingStates.IsEmpty())
            throw new RuntimeException("there should be at least one accepting state");
        aSet.add(aAcceptingStates);
        if ( ! aNonAcceptingStates.IsEmpty())
            aSet.add(aNonAcceptingStates);
    }




    private static Iterable<StateSet> Split (
        final StateSet aSet,
        final Set<StateSet> aT,
        final Map<State,StateSet> aTMap)
    {
        if (aSet.GetStateCount() == 1)
            return null;

        final Set<QualifiedName> aElements = CollectElementNames(aSet);
        for (final QualifiedName aElementName : aElements)
        {
            final Collection<StateSet> aPartitions = Split(aSet, aT, aTMap, aElementName);
            if (aPartitions == null)
                continue;
            if (aPartitions.size() > 1)
                return aPartitions;
        }
        return null;
    }




    /** Create a partition of the given set of states according to their
     *  transitions.
     *  All states whose transitions point to the same state set go in the same
     *  partition.
     */
    private static Collection<StateSet> Split (
        final StateSet aSet,
        final Set<StateSet> aT,
        final Map<State,StateSet> aTMap,
        final QualifiedName aElementName)
    {
        // Set up a forward map that does two steps:
        // from s via transition regarding aElementName to s'
        // from s' to a state set under aTMap(s).
        final Map<State,StateSet> aForwardMap = new HashMap<>();
        for (final State aState : aSet.GetStates())
        {
            final Transition aTransition = GetTransition(aState, aElementName);
            if (aTransition == null)
                aForwardMap.put(aState, null);
            else
                aForwardMap.put(aState, aTMap.get(aTransition.GetEndState()));
        }

        // Create the partion of aSet according to aForwardMap.  All states that map
        // to the same element go into the same state set.
        if (aForwardMap.size() == 1)
        {
            // No split necessary.
            return null;
        }
        else
        {
            // Set up a reverse map that maps that maps the values in aForwardMap to
            // new state sets whose contents are the keys in aForwardMap.
            final Map<StateSet,StateSet> aReverseMap = new HashMap<>();
            for (final Entry<State,StateSet> aEntry : aForwardMap.entrySet())
            {
                StateSet aPartitionSet = aReverseMap.get(aEntry.getValue());
                if (aPartitionSet == null)
                {
                    aPartitionSet = new StateSet();
                    aReverseMap.put(aEntry.getValue(), aPartitionSet);
                }
                aPartitionSet.AddState(aEntry.getKey());
            }
            return aReverseMap.values();
        }
    }




    private static Transition GetTransition (
        final State aState,
        final QualifiedName aElementName)
    {
        Transition aTransition = null;
        for (final Transition aCandidate : aState.GetTransitions())
            if (aCandidate.GetElementName().compareTo(aElementName) == 0)
            {
                assert(aTransition==null);
                aTransition = aCandidate;
                // break;
            }
        return aTransition;
    }




    private static Set<QualifiedName> CollectElementNames (final StateSet aSet)
    {
        final Set<QualifiedName> aNames = new TreeSet<>();
        for (final State aState : aSet.GetStates())
            for (final Transition aTransition : aState.GetTransitions())
                aNames.add(aTransition.GetElementName());

        return aNames;
    }




    private static boolean AreSetsOfStateSetsEqual (
        final TreeSet<StateSet> aSetOfSetsA,
        final TreeSet<StateSet> aSetOfSetsB)
    {
        if (aSetOfSetsA.size() != aSetOfSetsB.size())
            return false;
        else
        {
            final Iterator<StateSet> aSetIteratorA = aSetOfSetsA.iterator();
            final Iterator<StateSet> aSetIteratorB = aSetOfSetsB.iterator();
            while (aSetIteratorA.hasNext() && aSetIteratorB.hasNext())
            {
                if (aSetIteratorA.next().compareTo(aSetIteratorB.next()) != 0)
                    return false;
            }
            return true;
        }
    }




    private static StateContext CreateNewStates (
        final TreeSet<StateSet> aP,
        final Map<State,StateSet> aPMap,
        final StateContainer aNewStateContainer,
        final StateContext aOriginalStates)
    {
        final StateContext aMinimizedStates = new StateContext(
            aNewStateContainer,
            aOriginalStates.GetStartState().GetFullname());

        // Create the new states.
        final Map<State,State> aOldStateToNewStateMap = new TreeMap<>();
        for (final StateSet aSet : aP)
        {
            State aNewState = null;
            for (final State aOldState : aSet.GetStates())
            {
                if (aNewState == null)
                    aNewState = aOldState.Clone(aMinimizedStates);
                aOldStateToNewStateMap.put(aOldState, aNewState);
            }
        }

        // Create the new transitions.
        for (final StateSet aSet : aP)
        {
            final State aOldStartState = aSet.GetStates().iterator().next();
            final State aNewStartState = aOldStateToNewStateMap.get(aOldStartState);

            for (final Transition aTransition : aOldStartState.GetTransitions())
            {
                final State aOldEndState = aTransition.GetEndState();
                final State aNewEndState = aOldStateToNewStateMap.get(aOldEndState);

                // Check if the transition already exists.
                if (HasTransition(aNewStartState, aTransition.GetElementName()))
                    continue;

                aNewStartState.AddTransition(
                    new Transition(
                        aNewStartState,
                        aNewEndState,
                        aTransition.GetElementName(),
                        aTransition.GetElementTypeName()));
            }
        }

        // Transfer skip data and accepting flags.
        for (final State aOldState : aOriginalStates.GetStates())
        {
            final State aNewState = aOldStateToNewStateMap.get(aOldState);
            aNewState.CopyFrom(aOldState);
        }
        return aMinimizedStates;
    }




    private static boolean HasTransition (
        final State aState,
        final QualifiedName aElementName)
    {
        for (final Transition aTransition : aState.GetTransitions())
            if (aTransition.GetElementName().compareTo(aElementName) == 0)
                return true;
        return false;
    }




    private static void DisplayStates (
        final StateContext aStates,
        final PrintStream aLog)
    {
        for (final State aState : aStates.GetStates())
        {
            aLog.printf("  %s %s\n", aState.GetFullname(),
                aState.IsAccepting() ? "is accepting" : "");
            for (final Transition aTransition : aState.GetTransitions())
                aLog.printf("    -> %s via %s\n",
                    aTransition.GetEndState().GetFullname(),
                    aTransition.GetElementName().GetStateName());
        }
    }
}