View Javadoc

1   /*
2    * Zemucan: A Syntax Assistant for DB2
3    * Copyright (C) 2009, 2010 Andres Gomez Casanova
4    *
5    * This file is part of Zemucan.
6    *
7    * Zemucan is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as published by
9    * the Free Software Foundation; either version 3 of the License, or
10   * (at your option) any later version.
11   *
12   * Zemucan is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public License
18   * along with this library; if not, see <http://www.gnu.org/licenses/>.
19   *
20   * Contact:
21   * a n g o c a  at  y a h o o  dot  c o m
22   * Cra. 45 No 61 - 31, Bogota, Colombia.
23   *
24   * Author:   $LastChangedBy: angoca $:
25   * Date:     $LastChangedDate: 2011-03-06 10:15:32 -0500 (dom, 06 mar 2011) $:
26   * Revision: $LastChangedRevision: 1913 $:
27   * URL:      $HeadURL: https://zemucan.svn.sourceforge.net/svnroot/zemucan/branches/zemucan_v1/source-code/grammarReaderApi/src/test/java/name/angoca/zemucan/grammarReader/api/ImplementationInMemoryGrammarReader.java $:
28   */
29  package name.angoca.zemucan.grammarReader.api;
30  
31  import name.angoca.zemucan.AbstractZemucanException;
32  import name.angoca.zemucan.core.graph.model.Graph;
33  import name.angoca.zemucan.tools.Constants;
34  
35  /**
36   * This class permits to load grammars in memory without using a file. The
37   * complete definition of the grammar is here. It's just for tests.
38   * <p>
39   * <b>Control Version</b>
40   * <p>
41   * <ul>
42   * <li>1.0.0 Class creation.</li>
43   * <li>1.9.0 Return graph.</li>
44   * <li>1.10.0 Protected attributes -> private.</li>
45   * </ul>
46   *
47   * @author Andres Gomez Casanova <a
48   *         href="mailto:a n g o c a at y a h o o dot c o m">(AngocA)</a>
49   * @version 1.10.0 2010-06-18
50   */
51  public final class ImplementationInMemoryGrammarReader extends
52          AbstractGrammarReader {
53  
54      /**
55       * Grammar delimiters.
56       */
57      private static final String DELIMITERS = " ,()";
58  
59      /**
60       * Graph.
61       */
62      private final Graph graph;
63  
64      /**
65       * Default constructor.
66       *
67       * @param graphToFill
68       *            Graph where the grammar will be
69       * @param grammarName
70       *            Nothing
71       * @throws Exception
72       *             If there is a problem.
73       */
74      public ImplementationInMemoryGrammarReader(final Graph/* ! */graphToFill,
75              final String/* ! */grammarName) throws Exception {
76          super();
77  
78          assert graphToFill != null;
79          assert grammarName != null;
80  
81          this.graph = graphToFill;
82  
83          if (grammarName.equals("sa_conf-test-grammar1.xml")) { //$NON-NLS-1$
84              // Delimiters
85              super.setDelimiters(ImplementationInMemoryGrammarReader.DELIMITERS);
86  
87              this.testGraph1(graphToFill);
88  
89              super.setStartingNode(graphToFill.getStartingNode());
90          } else if (grammarName.equals("sa_conf-test-grammar2.xml")) { //$NON-NLS-1$
91              // Delimiters
92              super.setDelimiters(ImplementationInMemoryGrammarReader.DELIMITERS);
93  
94              this.testGraph2(graphToFill);
95  
96              super.setStartingNode(graphToFill.getStartingNode());
97          } else if (grammarName.equals("sa_conf-test-grammarIdentical1.xml") //$NON-NLS-1$
98                  || grammarName.equals("sa_conf-test-grammarIdentical2.xml")) { //$NON-NLS-1$
99              // Delimiters
100             super.setDelimiters(ImplementationInMemoryGrammarReader.DELIMITERS);
101 
102             this.testGraph3(graphToFill);
103 
104             super.setStartingNode(graphToFill.getStartingNode());
105         } else {
106             throw new Exception("Grammar not identified"); //$NON-NLS-1$
107         }
108     }
109 
110     @Override
111     public Graph/* ! */generateGraph() throws AbstractZemucanException {
112         super.setGrammarProcessed(true);
113 
114         assert this.graph != null;
115 
116         return this.graph;
117     }
118 
119     private void testGraph1(final Graph/* ! */graphToFill)
120             throws AbstractZemucanException {
121         assert graphToFill != null;
122 
123         // Graph
124         final String create = "create"; //$NON-NLS-1$
125         final String table = "table"; //$NON-NLS-1$
126 
127         graphToFill.addNode(Constants.STARTING_NODE, Constants.STARTING_NODE,
128                 true);
129         graphToFill.addNode(create, create, true);
130         graphToFill.addNode(table, table, true);
131         graphToFill.addNode(Constants.ENDING_NODE, Constants.ENDING_NODE, true);
132         graphToFill.firstPhaseFinished();
133         graphToFill.addRelation(Constants.STARTING_NODE, create);
134         graphToFill.addRelation(create, table);
135         graphToFill.addRelation(table, Constants.ENDING_NODE);
136         graphToFill.secondPhaseFinished();
137     }
138 
139     private void testGraph2(final Graph/* ! */graphToFill)
140             throws AbstractZemucanException {
141         assert graphToFill != null;
142 
143         // Graph
144         final String create = "create"; //$NON-NLS-1$
145         final String tablespace = "tablespace"; //$NON-NLS-1$
146 
147         graphToFill.addNode(Constants.STARTING_NODE, Constants.STARTING_NODE,
148                 true);
149         graphToFill.addNode(create, create, true);
150         graphToFill.addNode(tablespace, tablespace, true);
151         graphToFill.addNode(Constants.ENDING_NODE, Constants.ENDING_NODE, true);
152         graphToFill.firstPhaseFinished();
153         graphToFill.addRelation(Constants.STARTING_NODE, create);
154         graphToFill.addRelation(create, tablespace);
155         graphToFill.addRelation(tablespace, Constants.ENDING_NODE);
156         graphToFill.secondPhaseFinished();
157     }
158 
159     private void testGraph3(final Graph/* ! */graphToFill)
160             throws AbstractZemucanException {
161         assert graphToFill != null;
162 
163         // Graph
164         final String create = "create"; //$NON-NLS-1$
165 
166         graphToFill.addNode(Constants.STARTING_NODE, Constants.STARTING_NODE,
167                 true);
168         graphToFill.addNode(create, create, true);
169         graphToFill.addNode(Constants.ENDING_NODE, Constants.ENDING_NODE, true);
170         graphToFill.firstPhaseFinished();
171         graphToFill.addRelation(Constants.STARTING_NODE, create);
172         graphToFill.addRelation(create, Constants.ENDING_NODE);
173         graphToFill.secondPhaseFinished();
174     }
175 }