1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 package org.apache.openoffice.ooxml.schema.automaton; 23 24 import java.io.File; 25 import java.io.FileOutputStream; 26 import java.io.PrintStream; 27 import java.util.HashMap; 28 import java.util.Map; 29 import java.util.Map.Entry; 30 import java.util.Vector; 31 32 import org.apache.openoffice.ooxml.schema.model.base.QualifiedName; 33 34 /** As there is one FA for each complex type and one for the top level elements, 35 * this container represents the whole set of schemas. 36 */ 37 public class FiniteAutomatonContainer 38 { FiniteAutomatonContainer(final StateContainer aStateContainer)39 FiniteAutomatonContainer (final StateContainer aStateContainer) 40 { 41 maComplexTypeNameToAutomatonMap = new HashMap<>(); 42 } 43 44 45 46 AddAutomaton( final QualifiedName aElementName, final FiniteAutomaton aAutomaton)47 public void AddAutomaton ( 48 final QualifiedName aElementName, 49 final FiniteAutomaton aAutomaton) 50 { 51 maComplexTypeNameToAutomatonMap.put(aElementName, aAutomaton); 52 } 53 54 55 56 GetAutomatons()57 public Iterable<FiniteAutomaton> GetAutomatons() 58 { 59 return maComplexTypeNameToAutomatonMap.values(); 60 } 61 62 63 64 GetAutomatonCount()65 public int GetAutomatonCount () 66 { 67 return maComplexTypeNameToAutomatonMap.size(); 68 } 69 70 71 72 GetStates()73 public Iterable<State> GetStates() 74 { 75 final Vector<State> aStates = new Vector<>(); 76 for (final FiniteAutomaton aAutomaton : maComplexTypeNameToAutomatonMap.values()) 77 for (final State aState : aAutomaton.GetStates()) 78 aStates.add(aState); 79 return aStates; 80 } 81 82 83 84 GetStateCount()85 public int GetStateCount() 86 { 87 int nStateCount = 0; 88 for (final FiniteAutomaton aAutomaton : maComplexTypeNameToAutomatonMap.values()) 89 nStateCount += aAutomaton.GetStateCount(); 90 return nStateCount; 91 } 92 93 94 95 GetTransitionCount()96 public int GetTransitionCount () 97 { 98 int nTransitionCount = 0; 99 for (final FiniteAutomaton aAutomaton : maComplexTypeNameToAutomatonMap.values()) 100 nTransitionCount += aAutomaton.GetTransitionCount(); 101 return nTransitionCount; 102 } 103 104 105 106 CreateDFAs()107 public FiniteAutomatonContainer CreateDFAs () 108 { 109 final StateContainer aDFAStateContainer = new StateContainer(); 110 final FiniteAutomatonContainer aDFAs = new FiniteAutomatonContainer(aDFAStateContainer); 111 for (final Entry<QualifiedName, FiniteAutomaton> aEntry : maComplexTypeNameToAutomatonMap.entrySet()) 112 { 113 aDFAs.AddAutomaton( 114 aEntry.getKey(), 115 aEntry.getValue().CreateDFA( 116 aDFAStateContainer, 117 aEntry.getKey())); 118 } 119 return aDFAs; 120 } 121 122 123 124 MinimizeDFAs()125 public FiniteAutomatonContainer MinimizeDFAs () 126 { 127 PrintStream aLog = null; 128 try 129 { 130 aLog = new PrintStream(new FileOutputStream(new File("/tmp/minimization.log"))); 131 } 132 catch(Exception e) 133 { 134 e.printStackTrace(); 135 return null; 136 } 137 138 final StateContainer aNewStateContainer = new StateContainer(); 139 final FiniteAutomatonContainer aDFAs = new FiniteAutomatonContainer(aNewStateContainer); 140 for (final Entry<QualifiedName, FiniteAutomaton> aEntry : maComplexTypeNameToAutomatonMap.entrySet()) 141 { 142 aDFAs.AddAutomaton( 143 aEntry.getKey(), 144 HopcroftMinimizer.MinimizeDFA( 145 aNewStateContainer, 146 aEntry.getValue().GetStateContext(), 147 aEntry.getValue().GetAttributes(), 148 aEntry.getValue().GetLocation(), 149 aLog)); 150 } 151 return aDFAs; 152 } 153 154 155 156 GetTopLevelAutomaton()157 public FiniteAutomaton GetTopLevelAutomaton () 158 { 159 return maComplexTypeNameToAutomatonMap.get(null); 160 } 161 162 163 164 165 private final Map<QualifiedName, FiniteAutomaton> maComplexTypeNameToAutomatonMap; 166 } 167