View Javadoc

1   /*
2    wsmo4j - a WSMO API and Reference Implementation
3    Copyright (c) 2005, University of Innsbruck, Austria
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.logicalexpression.util;
17  
18  
19  import java.util.*;
20  
21  import org.omwg.logicalexpression.*;
22  
23  
24  /**
25   * Class to check if a specified operator of a logical expression is contained in an
26   * array of operators
27   */
28  public class VisitorContainsCheck
29          implements Visitor {
30  
31      private Class[] operators;
32  
33      private boolean containsOperator;
34  
35      /**
36       * @param operators array of classes that will be checked for
37       * @see org.deri.wsmo4j.logicalexpression.util.ExpressionContainmentUtil#contains(LogicalExpression, Class)
38       */
39      protected VisitorContainsCheck(Class[] operators) {
40          containsOperator = false;
41          this.operators = operators;
42      }
43  
44      /**
45       * Checks if a specified operator is contained in the array of operators
46       * @param operator operator that will be checked for
47       * @return <code>true</code> if the specified operator is contained
48       */
49      private boolean contained(Class operator) {
50          for (int i = 0; i < operators.length; i++) {
51              List classes = Arrays.asList(operator.getInterfaces());
52              if (classes.contains(operators[i])) {
53                  return true;
54              }
55          }
56          return false;
57      }
58  
59      /**
60       * Empty method, as an Atom can't contain an operator
61       * @see org.omwg.logicalexpression.Visitor#visitAtom(org.omwg.logicalexpression.Atom)
62       */
63      public void visitAtom(Atom expr) {
64          //atom can't contain operator
65      }
66  
67      /**
68       * Checks if the operator of the Unary expression is contained in the array of operators
69       * @see org.omwg.logicalexpression.Visitor#visitNegationAsFailure(NegationAsFailure)
70       */
71      public void visitNegationAsFailure(NegationAsFailure expr) {
72          if (contained(expr.getClass())) {
73              containsOperator = true;
74              return;
75          }
76          expr.getOperand().accept(this);
77      }
78  
79      /**
80       * Checks if the operator of the Unary expression is contained in the array of operators
81       * @see org.omwg.logicalexpression.Visitor#visitNegation(Negation)
82       */
83      public void visitNegation(Negation expr) {
84          if (contained(expr.getClass())) {
85              containsOperator = true;
86              return;
87          }
88          expr.getOperand().accept(this);
89      }
90  
91      /**
92       * Checks if the operator of the Unary expression is contained in the array of operators
93       * @see org.omwg.logicalexpression.Visitor#visitConstraint(Constraint)
94       */
95      public void visitConstraint(Constraint expr) {
96          if (contained(expr.getClass())) {
97              containsOperator = true;
98              return;
99          }
100         expr.getOperand().accept(this);
101     }
102 
103     /**
104      * Checks if the operator of the Binary expression is contained in the array of operators
105      * @see org.omwg.logicalexpression.Visitor#visitConjunction(Conjunction)
106      */
107     public void visitConjunction(Conjunction expr) {
108         if (contained(expr.getClass())) {
109             containsOperator = true;
110             return;
111         }
112         expr.getLeftOperand().accept(this);
113         expr.getRightOperand().accept(this);
114     }
115 
116     /**
117      * Checks if the operator of the Binary expression is contained in the array of operators
118      * @see org.omwg.logicalexpression.Visitor#visitEquivalence(Equivalence)
119      */
120     public void visitEquivalence(Equivalence expr) {
121         if (contained(expr.getClass())) {
122             containsOperator = true;
123             return;
124         }
125         expr.getLeftOperand().accept(this);
126         expr.getRightOperand().accept(this);
127     }
128 
129     /**
130      * Checks if the operator of the Binary expression is contained in the array of operators
131      * @see org.omwg.logicalexpression.Visitor#visitInverseImplication(InverseImplication)
132      */
133     public void visitInverseImplication(InverseImplication expr) {
134         if (contained(expr.getClass())) {
135             containsOperator = true;
136             return;
137         }
138         expr.getLeftOperand().accept(this);
139         expr.getRightOperand().accept(this);
140     }
141 
142     /**
143      * Checks if the operator of the Binary expression is contained in the array of operators
144      * @see org.omwg.logicalexpression.Visitor#visitImplication(Implication)
145      */
146     public void visitImplication(Implication expr) {
147         if (contained(expr.getClass())) {
148             containsOperator = true;
149             return;
150         }
151         expr.getLeftOperand().accept(this);
152         expr.getRightOperand().accept(this);
153     }
154 
155     /**
156      * Checks if the operator of the Binary expression is contained in the array of operators
157      * @see org.omwg.logicalexpression.Visitor#visitLogicProgrammingRule(LogicProgrammingRule)
158      */
159     public void visitLogicProgrammingRule(LogicProgrammingRule expr) {
160         if (contained(expr.getClass())) {
161             containsOperator = true;
162             return;
163         }
164         expr.getLeftOperand().accept(this);
165         expr.getRightOperand().accept(this);
166     }
167 
168     /**
169      * Checks if the operator of the Binary expression is contained in the array of operators
170      * @see org.omwg.logicalexpression.Visitor#visitDisjunction(Disjunction)
171      */
172     public void visitDisjunction(Disjunction expr) {
173         if (contained(expr.getClass())) {
174             containsOperator = true;
175             return;
176         }
177         expr.getLeftOperand().accept(this);
178         expr.getRightOperand().accept(this);
179     }
180 
181     /**
182      * Checks if the operator of the Quantified expression is contained in the
183      * array of operators
184      * @see org.omwg.logicalexpression.Visitor#visitUniversalQuantification(UniversalQuantification)
185      */
186     public void visitUniversalQuantification(UniversalQuantification expr) {
187         if (contained(expr.getClass())) {
188             containsOperator = true;
189             return;
190         }
191         expr.getOperand().accept(this);
192     }
193 
194     /**
195      * Checks if the operator of the Quantified expression is contained in the
196      * array of operators
197      * @see org.omwg.logicalexpression.Visitor#visitExistentialQuantification(ExistentialQuantification)
198      */
199     public void visitExistentialQuantification(ExistentialQuantification expr) {
200         if (contained(expr.getClass())) {
201             containsOperator = true;
202             return;
203         }
204         expr.getOperand().accept(this);
205     }
206 
207     /**
208      * Checks if the previous logexp the visitor was applied to,
209      * contained the compound expression with the operator passed in constructor.
210      * So it checks if this logexp contained a specified operator.
211      * @return true if it is contained
212      */
213     protected boolean isContained() {
214         return containsOperator;
215     }
216 
217     public void visitAttributeContraintMolecule(AttributeConstraintMolecule expr) {
218         //atom can't contain operator
219     }
220 
221     public void visitAttributeInferenceMolecule(AttributeInferenceMolecule expr) {
222         //atom can't contain operator
223     }
224 
225     public void visitAttributeValueMolecule(AttributeValueMolecule expr) {
226         //atom can't contain operator
227     }
228 
229     public void visitCompoundMolecule(CompoundMolecule expr) {
230         //atom can't contain operator
231     }
232 
233     public void visitMemberShipMolecule(MembershipMolecule expr) {
234         //atom can't contain operator
235     }
236 
237     public void visitSubConceptMolecule(SubConceptMolecule expr) {
238         //atom can't contain operator
239     }
240 }