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.omwg.logicalexpression.terms.*;
31  import org.omwg.ontology.*;
32  import org.wsmo.common.*;
33  import org.wsmo.common.exception.*;
34  import org.wsmo.wsml.compiler.node.*;
35  
36  import com.ontotext.wsmo4j.parser.*;
37  
38  public class NFPAnalysis extends ASTAnalysis {
39  
40      private Stack termsStack;
41      private Stack identifierStack;
42      private Stack entityStack;
43  
44      public NFPAnalysis(ASTAnalysisContainer container) {
45          if (container == null) {
46              throw new IllegalArgumentException();
47          }
48          termsStack = container.getStack(Term[].class);
49          identifierStack = container.getStack(Identifier.class);
50          entityStack = container.getStack(Entity.class);
51  
52          // register the handled nodes
53          container.registerNodeHandler(ANfp.class, this);
54          container.registerNodeHandler(PNfp.class, this);
55      }
56  
57      private Object rootIdentifier;
58      private Object rootTerm;
59  
60      public void inANfp(ANfp node) {
61          // keep a handle to the stack's last element
62          if (!termsStack.isEmpty())
63              rootTerm = termsStack.peek();
64          else
65              rootTerm = null;
66          if (!identifierStack.isEmpty())
67              rootIdentifier = identifierStack.peek();
68          else
69              rootIdentifier = null;
70      }
71  
72      public void outANfp(ANfp node) {
73          if (entityStack.isEmpty()) {
74              return; // NFP node outside of known Entity set
75          }
76          Entity entity = (Entity) entityStack.peek();
77  
78          try {
79              Term[] terms = null;
80              while (!identifierStack.isEmpty() &&
81                      identifierStack.peek() != rootIdentifier) {
82  
83                  if (termsStack.isEmpty()) {
84                      throw new RuntimeException("The NFP term queue is shorter then iri queue!");
85                  }
86  
87                  IRI iri = (IRI) identifierStack.pop();
88                  terms = (Term[]) termsStack.pop();
89  
90                  for (int i = 0; i < terms.length; i++) {
91                      if (terms[i] instanceof DataValue)
92                          entity.addNFPValue(iri, (DataValue) terms[i]);
93                      else // Identifier
94                          entity.addNFPValue(iri, (Identifier) terms[i]);
95                  }
96              }
97  
98              // check that the last stack element did not change
99              if (!termsStack.isEmpty() &&
100                     termsStack.peek() != rootTerm) {
101                 throw new RuntimeException("The NFP term queue is longer then iri queue!");
102             }
103         }
104         catch (InvalidModelException ime) {
105             throw new WrappedInvalidModelException(ime);
106         }
107     }
108 }
109 
110 /*
111  * $Log$
112  * Revision 1.2  2006/02/10 14:37:25  vassil_momtchev
113  * parser addapted to the grammar changes; unused class variables removed;
114  *
115  * Revision 1.1  2005/11/28 13:55:26  vassil_momtchev
116  * AST analyses
117  *
118 */