View Javadoc

1   /*
2    wsmo4j - a WSMO API and Reference Implementation
3    Copyright (c) 2004, University of Innsbruck, Institute of Computer Science
4    This library is free software; you can redistribute it and/or modify it under
5    the terms of the GNU Lesser General Public License as published by the Free
6    Software Foundation; either version 2.1 of the License, or (at your option)
7    any later version.
8    This library is distributed in the hope that it will be useful, but WITHOUT
9    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11   details.
12   You should have received a copy of the GNU Lesser General Public License along
13   with this library; if not, write to the Free Software Foundation, Inc.,
14   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15   */
16  package org.deri.wsmo4j.io.parser.wsml;
17  
18  import java.util.*;
19  
20  import org.deri.wsmo4j.logicalexpression.LogicalExpressionImpl;
21  import org.omwg.logicalexpression.*;
22  import org.omwg.ontology.*;
23  import org.wsmo.factory.*;
24  import org.wsmo.wsml.compiler.node.*;
25  
26  import com.ontotext.wsmo4j.parser.wsml.*;
27  
28  
29  /**
30   * 
31   * <pre>
32   * Created on 21.11.2005
33   * Committed by $Author$
34   * $Source$,
35   * </pre>
36   *
37   * @author Holger Lausen
38   * @version $Revision$ $Date$
39   */
40  public class CompoundExpressionAnalysis extends ASTAnalysis {
41      private LogicalExpressionFactory lFactory;
42      private ASTAnalysisContainer container;
43      
44      private Stack logexps; // handle to logexp stack
45      
46      /**
47       * Adapter for all Compound Expressions
48       * @param container
49       * @param lFactory
50       */
51      public CompoundExpressionAnalysis(ASTAnalysisContainer container, 
52              LogicalExpressionFactory lFactory) {
53          
54          if (container == null || lFactory == null) {
55              throw new IllegalArgumentException();
56          }
57          this.container = container;
58          this.lFactory = lFactory;
59          
60          // register the handled nodes
61          container.registerNodeHandler(AConjunction.class, this);
62          container.registerNodeHandler(ADisjunction.class, this);
63          container.registerNodeHandler(AConstraintLogExpr.class, this);
64          container.registerNodeHandler(ALpRuleLogExpr.class, this);
65          container.registerNodeHandler(AQuantified.class, this);
66          container.registerNodeHandler(ANegatedSubexpr.class, this);
67          container.registerNodeHandler(AImplicationExpr.class, this);
68  
69          container.registerNodeHandler(AOtherExpressionLogExpr.class, this);
70          
71          //shortcuts
72          logexps = container.getStack(LogicalExpression.class);
73  
74      }
75  
76      private void outALogDefinition(){
77          Stack orgLes = container.getStack(String.class);
78          Object le = logexps.peek();
79          if (orgLes.size()==0 || !(le instanceof LogicalExpressionImpl)){
80              return;
81          }
82          String org = (String)orgLes.remove(0);
83          ((LogicalExpressionImpl)le).setStringRepresentation(org);
84      }
85  
86      public void outAOtherExpressionLogExpr(AOtherExpressionLogExpr node){
87          outALogDefinition();
88      }
89      
90      public void outAConjunction(AConjunction node) {
91          LogicalExpression le2 = (LogicalExpression)logexps.pop();
92          LogicalExpression le1 = (LogicalExpression)logexps.pop();
93          logexps.add(lFactory.createConjunction(le1,le2));
94      }
95  
96      public void outADisjunction(ADisjunction node) {
97          LogicalExpression le2 = (LogicalExpression)logexps.pop();
98          LogicalExpression le1 = (LogicalExpression)logexps.pop();
99          logexps.add(lFactory.createDisjunction(le1,le2));
100     }
101 
102     public void outAConstraintLogExpr(AConstraintLogExpr node) {
103         logexps.add(lFactory.createConstraint(
104                 (LogicalExpression)logexps.pop()));
105         outALogDefinition();
106     }    
107     
108     public void outALpRuleLogExpr(ALpRuleLogExpr node) {
109         LogicalExpression le2 = (LogicalExpression)logexps.pop();
110         LogicalExpression le1 = (LogicalExpression)logexps.pop();
111         logexps.add(lFactory.createLogicProgrammingRule(
112                 le1,le2));
113         outALogDefinition();
114     }
115     
116     public void outAQuantified(AQuantified node) {
117         PQuantifierKey quantifierKey = node.getQuantifierKey();
118         Variable[] vars = (Variable[])container.popFromStack(Variable[].class,Variable[].class);
119         Set <Variable> variables = new HashSet <Variable> ();
120         for (int i=0; i<vars.length; i++){
121             variables.add(vars[i]);
122         }
123         
124         if (quantifierKey instanceof AExistsQuantifierKey) {
125             logexps.add(lFactory.createExistentialQuantification(
126                     variables, 
127                     (LogicalExpression)logexps.pop()));
128         }
129         else {
130             logexps.add(lFactory.createUniversalQuantification(
131                     variables, 
132                     (LogicalExpression)logexps.pop()));
133         }
134     }
135     
136     public void outANegatedSubexpr(ANegatedSubexpr node) {
137         if (node.getTNot().getText().equals("neg")) {
138             logexps.add(lFactory.createNegation(
139                     (LogicalExpression)logexps.pop()));
140         }
141         else {
142             logexps.add(lFactory.createNegationAsFailure(
143                     (LogicalExpression)logexps.pop()));
144         }
145     }
146     
147     public void outAImplicationExpr(AImplicationExpr node) {
148         PImplyOp pOp = node.getImplyOp();
149         LogicalExpression le2 = (LogicalExpression)logexps.pop();
150         LogicalExpression le1 = (LogicalExpression)logexps.pop();
151         if (pOp instanceof AImpliesImplyOp) {
152             logexps.add(lFactory.createImplication(
153                     (LogicalExpression)le1, 
154                     (LogicalExpression)le2));
155         }
156         else if (pOp instanceof AEquivalentImplyOp) {
157             logexps.add(lFactory.createEquivalence(
158                     (LogicalExpression)le1, 
159                     (LogicalExpression)le2));
160         }
161         else {
162             logexps.add(lFactory.createInverseImplication(
163                     (LogicalExpression)le1, 
164                     (LogicalExpression)le2));
165         }
166     }
167 }