View Javadoc

1   /*
2    wsmo4j - a WSMO API and Reference Implementation
3   
4    Copyright (c) 2004-2005, OntoText Lab. / SIRMA
5   
6    This library is free software; you can redistribute it and/or modify it under
7    the terms of the GNU Lesser General Public License as published by the Free
8    Software Foundation; either version 2.1 of the License, or (at your option)
9    any later version.
10   This library is distributed in the hope that it will be useful, but WITHOUT
11   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13   details.
14   You should have received a copy of the GNU Lesser General Public License along
15   with this library; if not, write to the Free Software Foundation, Inc.,
16   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17   */
18  
19  /**
20   * <p>Title: WSMO4J</p>
21   * <p>Description: WSMO API and a Reference Implementation</p>
22   * <p>Copyright:  Copyright (c) 2004-2005</p>
23   * <p>Company: OntoText Lab. / SIRMA </p>
24   */
25  
26  package com.ontotext.wsmo4j.parser.wsml;
27  
28  import java.util.*;
29  
30  import org.deri.wsmo4j.io.parser.WrappedParsingException;
31  import org.deri.wsmo4j.logicalexpression.*;
32  import org.omwg.logicalexpression.terms.*;
33  import org.omwg.ontology.*;
34  import org.wsmo.common.*;
35  import org.wsmo.factory.*;
36  import org.wsmo.wsml.ParserException;
37  import org.wsmo.wsml.compiler.node.*;
38  
39  public class ValueAnalysis extends ASTAnalysis {
40  
41      private DataFactory dFactory;
42      private LogicalExpressionFactory lFactory;
43      private ASTAnalysisContainer container;
44      private WsmoFactory factory;
45  
46      private Stack termStack;
47      private Stack lastTermBeforeFunctionSymbol = new Stack();
48      private ConstantTransformer cf = ConstantTransformer.getInstance();
49  
50      public ValueAnalysis(ASTAnalysisContainer container,
51              DataFactory dFactory,
52              LogicalExpressionFactory lFactory,
53              WsmoFactory factory) {
54          if (container == null || dFactory == null || lFactory == null) {
55              throw new IllegalArgumentException();
56          }
57          this.dFactory = dFactory;
58          this.lFactory = lFactory;
59          this.factory = factory;
60          this.container = container;
61          termStack = container.getStack(Term.class);
62  
63          // register the handled nodes
64          container.registerNodeHandler(AVarTerm.class, this);
65          container.registerNodeHandler(AParametrizedFunctionsymbol.class, this);
66          container.registerNodeHandler(ADecimalNumber.class, this);
67          container.registerNodeHandler(AIntegerNumber.class, this);
68          container.registerNodeHandler(AStringValue.class, this);
69          container.registerNodeHandler(ATermlist.class, this);
70          container.registerNodeHandler(ATermTermlist.class, this);
71          container.registerNodeHandler(ATermValue.class, this);
72          container.registerNodeHandler(ATermValuelist.class, this);
73          container.registerNodeHandler(AValuelistValuelist.class, this);
74          container.registerNodeHandler(ANbAnonymousTerm.class, this);
75          
76      }
77  
78      public void inAParametrizedFunctionsymbol(AParametrizedFunctionsymbol node) {
79          if (!termStack.isEmpty()){
80              lastTermBeforeFunctionSymbol.add(termStack.peek());
81          }
82      }
83  
84      // PValueList types
85  
86      Object lastItem;
87  
88      public void inATermValuelist(ATermValuelist node) {
89          if (!termStack.isEmpty())
90              lastItem = termStack.peek();
91      }
92  
93      public void outATermValuelist(ATermValuelist node) {
94          if (termStack.isEmpty()) {
95              throw new RuntimeException("No data value nodes processed!");
96          }
97  
98          Vector terms = new Vector();
99          while (!termStack.isEmpty() &&
100                 termStack.peek() != lastItem) {
101             terms.add(0, termStack.pop());
102         }
103 
104         container.getStack(Term[].class).push(terms.toArray(new Term[]{}));
105     }
106 
107     public void inAValuelistValuelist(AValuelistValuelist node) {
108         inATermValuelist(null); // same implementation
109     }
110 
111     public void outAValuelistValuelist(AValuelistValuelist node) {
112         outATermValuelist(null); // same implementation
113     }
114 
115     // PValue types
116 
117     public void outAParametrizedFunctionsymbol(AParametrizedFunctionsymbol node) {
118         IRI type = (IRI) container.popFromStack(Identifier.class, IRI.class);
119         Vector args = new Vector();
120         Object last = null;
121         if (!lastTermBeforeFunctionSymbol.isEmpty()){
122             last=lastTermBeforeFunctionSymbol.pop();
123         }
124         while (!termStack.isEmpty() &&
125                 termStack.peek()!=last){
126             args.add(0,termStack.pop());
127         }
128 
129         if (cf.isSimpleDataType(type.toString())) {
130             termStack.push(args.get(0));
131         }
132         else if (cf.isDataType(type.toString())) {
133             ComplexDataType complexType = (ComplexDataType) dFactory.createWsmlDataType(type);
134             if (complexType.getIRI().toString().equals(WsmlDataType.WSML_IRI)) {
135                 termStack.push(args.firstElement());
136             }
137             else if (complexType.getIRI().toString().equals(WsmlDataType.WSML_SQNAME) &&
138                     args.size() == 2) {
139                 AAnySqname sqname = new AAnySqname();
140                 sqname.setName(new TName(args.get(1).toString()));
141                 sqname.setPrefix(new APrefix(new TName(args.get(0).toString()), new THash()));
142                 sqname.apply(container.getNodeHandler(PId.class));
143                 termStack.push(container.getStack(Identifier.class).pop());
144             }
145             else {
146                 if (args.firstElement() instanceof SimpleDataValue == false)  {
147                     ParserException pe = new ParserException("Data type has no valid value", null);
148                     pe.setExpectedToken("Value");
149                     pe.setErrorLine(node.getLpar().getLine());
150                     pe.setErrorPos(node.getLpar().getPos()+1);
151                     throw new WrappedParsingException(pe);
152                     }
153                 termStack.push(dFactory.createDataValue(complexType, 
154                         (SimpleDataValue[]) args.toArray(new SimpleDataValue[]{})));
155                 }
156         }
157         else{ //no DATATYPE
158             termStack.add(lFactory.createConstructedTerm(type,args));
159         }
160     }
161 
162     public void inADecimalNumber(ADecimalNumber node) {
163         termStack.push(dFactory.createWsmlDecimal(node.toString().trim()));
164     }
165 
166     public void inAIntegerNumber(AIntegerNumber node) {
167         String intAsString = node.getInteger().toString().trim();
168         // sablecc does change "-5" to "- 5" which will later not be parsed
169         // correctly
170         if (intAsString.length() >= 2 && intAsString.charAt(1) == ' ') {
171             intAsString = "-" + intAsString.substring(2);
172 
173         }
174         termStack.push(dFactory.createWsmlInteger(intAsString));
175     }
176 
177     public void inAStringValue(AStringValue node) {
178         String str = node.getString().toString();
179         //get rid of quotes
180         str = str.substring(1,str.length()-2);
181         //unescape
182         str = str.replaceAll("\\\\\\\\", "\\\\");
183         str = str.replaceAll("\\\\\"","\"");
184 
185         termStack.push(dFactory.createWsmlString(str));
186     }
187 
188     public void inATermValue(ATermValue node) {
189         node.getId().apply(container.getNodeHandler(PId.class));
190         termStack.push((Term) container.getStack(Identifier.class).pop());
191     }
192 
193     public void inAVarTerm(AVarTerm node) {
194         termStack.push(lFactory.createVariable(
195                 node.getVariable().getText()));
196     }
197 
198     public void outATermTermlist(ATermTermlist node) {
199         container.getStack(Term[].class).push(
200                 new Term[]{(Term)termStack.pop()});
201     }
202 
203     public void inATermlist(ATermlist node) {
204         if (!termStack.isEmpty())
205             lastItem = termStack.peek();
206     }
207 
208     public void outATermlist(ATermlist node) {
209         Vector terms = new Vector();
210         while (!termStack.isEmpty() &&
211                 termStack.peek() != lastItem) {
212             terms.add(0, termStack.pop());
213         }
214         container.getStack(Term[].class).push(terms.toArray(new Term[]{}));
215     }
216 
217     public void inANbAnonymousTerm(ANbAnonymousTerm arg0) {
218         String no  = arg0.getNbAnonymous().getText();
219         no = no.substring(2);
220         byte number = (byte)Integer.parseInt(no);
221         termStack.push(lFactory.createAnonymousID(number));
222     }
223 }
224 
225 /*
226  * $Log$
227  * Revision 1.9  2006/11/10 14:46:21  holgerlausen
228  * fixed bug with negative ints
229  *
230  * Revision 1.8  2006/11/08 12:02:48  vassil_momtchev
231  * fixed bug when parsed data value "_boolean(IRI)"
232  *
233  * Revision 1.7  2006/11/03 07:17:32  holgerlausen
234  * fixed a bug in parsing nested function symbols (only arguments of last functionsymbol where added correctly but args of rest has been ignored)
235  *
236  * Revision 1.6  2006/06/21 07:46:29  vassil_momtchev
237  * createVariable(String) method moved from WsmoFactory to LogicalExpressionFactory interface
238  *
239  * Revision 1.5  2006/03/06 14:58:41  vassil_momtchev
240  * unused imports removed
241  *
242  * Revision 1.4  2006/03/06 14:55:45  vassil_momtchev
243  * _iri(_"http://iri...") and _sqname("prefix", "localpart") construction handled also
244  *
245  * Revision 1.3  2005/12/02 14:11:59  holgerlausen
246  * fixed the handling of escpaed character during parsing and serialization
247  *
248  * Revision 1.2  2005/12/02 12:47:59  holgerlausen
249  * numbered anon id processing added
250  *
251  * Revision 1.1  2005/11/28 13:55:26  vassil_momtchev
252  * AST analyses
253  *
254 */