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.terms;
17  
18  
19  import java.util.*;
20  
21  import org.deri.wsmo4j.logicalexpression.util.*;
22  
23  import org.omwg.logicalexpression.terms.*;
24  import org.wsmo.common.*;
25  
26  
27  /**
28   * @author DERI Innsbruck, reto.krummenacher@deri.org, holger.lausen@deri.org
29   * @version $Revision: 1946 $ $Date: 2007-04-02 15:13:28 +0300 (Mon, 02 Apr 2007) $
30   *
31   * @see org.omwg.logicalexpression.terms.ConstructedTerm
32   */
33  public class ConstructedTermImpl
34          implements ConstructedTerm {
35  
36      private IRI functionSymbol;
37  
38      private List <Term> args = new Vector <Term> ();
39  
40      /**
41       * @param functionSymbol the name of the term, in form of an IRI
42       * @param terms List of Terms
43       * @throws IllegalArgumentException in case the functionSymbol is null or the arguments of
44       * the list aren't all of Type Term
45       * @see org.wsmo.factory.LogicalExpressionFactory#createConstructedTerm(IRI, List)
46       */
47      public ConstructedTermImpl(IRI functionSymbol, List <Term> terms)
48              throws IllegalArgumentException {
49          if (functionSymbol == null) {
50              throw new IllegalArgumentException("functionSymbol may not be null!");
51          }
52          if (!SetUtil.allOfType(terms, Term.class)) {
53              throw new IllegalArgumentException("Only " + Term.class +" are allowed as Arguments!");
54          }
55          this.functionSymbol = functionSymbol;
56          if (terms != null) {
57              this.args = Collections.unmodifiableList(terms);
58          }
59  
60      }
61  
62      /**
63       * @return the name of the term, in wsml only possible in form of an IRI
64       * @see org.omwg.logicalexpression.terms.ConstructedTerm#getFunctionSymbol()
65       */
66      public IRI getFunctionSymbol() {
67          return functionSymbol;
68      }
69  
70      /**
71       * @return the arity of the term, i.e. the number of parameters
72       * @see org.omwg.logicalexpression.terms.ConstructedTerm#getArity()
73       */
74      public int getArity() {
75          return args.size();
76      }
77  
78      /**
79       * @param i the position of the parameter desired, maximal getArity()-1
80       * @return a Term, the chosen parameter
81       * @see org.omwg.logicalexpression.terms.ConstructedTerm#getParameter(int)
82       * @throws IndexOutOfBoundsException if the index is out of range (arity)
83       */
84      public Term getParameter(int i)
85              throws IndexOutOfBoundsException {
86          return args.get(i);
87      }
88  
89      /**
90       * @see org.omwg.logicalexpression.terms.Term#accept(org.omwg.logicalexpression.terms.Visitor)
91       */
92      public void accept(Visitor v) {
93          v.visitConstructedTerm(this);
94      }
95  
96      /**
97       * @return The String representation of the constructed term
98       * @see java.lang.Object#toString()
99       */
100     public String toString() {
101         String ret = functionSymbol.toString() + "(";
102         Iterator i = args.iterator();
103         while (i.hasNext()) {
104             ret += (Term)i.next();
105             if (i.hasNext()) {
106                 ret += ",";
107             }
108         }
109         ret += ")";
110         return ret;
111     }
112 
113     /**
114      * <p>
115      * The <code>equals</code> method implements an equivalence relation
116      * on non-null object references. Two constructed terms are equal if their arity, their
117      * functionSymbol, and all of the arguments from the list of terms are equal.
118      * </p>
119      * <p>
120      * It is generally necessary to override the <code>hashCode</code> method whenever this method
121      * is overridden.
122      * </p>
123      * @param o Object of Type ConstructedTerm
124      * @return <code>true</code> if this object is the same as the obj
125      *          argument; <code>false</code> otherwise.
126      * @see java.lang.Object#equals(java.lang.Object)
127      * @see java.lang.Object#hashCode()
128      */
129     public boolean equals(Object o) {
130         if (o instanceof ConstructedTerm) {
131             ConstructedTerm c = (ConstructedTerm)o;
132             //no need to check further if functionsymbols or
133             //arity don't match
134             if (!c.getFunctionSymbol().equals(functionSymbol)
135                 || c.getArity() != this.getArity()) {
136                 return false;
137             }
138             //check all arguments
139             Iterator i = args.iterator();
140             int n = 0;
141             while (i.hasNext()) {
142                 Term t1 = (Term)i.next();
143                 Term t2 = c.getParameter(n++);
144                 if (!t1.equals(t2)) {
145                     return false;
146                 }
147             }
148             return true;
149         }
150         return false;
151     }
152 
153     /**
154      * <p>
155      * If two objects are equal according to the <code>equals(Object)</code> method, then calling
156      * the <code>hashCode</code> method on each of the two objects must produce the same integer
157      * result. However, it is not required that if two objects are unequal according to
158      * the <code>equals(Object)</code> method, then calling the <code>hashCode</code> method on each of the two
159      * objects must produce distinct integer results.
160      * </p>
161      * <p>
162      * This method should be overriden, when the <code>equals(Object)</code> method is overriden.
163      * </p>
164      * @return A hash code value for this Object.
165      * @see java.lang.Object#hashCode()
166      * @see java.lang.Object#equals(Object)
167      * @see com.ontotext.wsmo4j.common.IRIImpl#hashCode()
168      */
169     public int hashCode() {
170         //can probably be more optimized
171         return functionSymbol.hashCode();
172     }
173 
174     /**
175      * @see org.omwg.logicalexpression.terms.ConstructedTerm#listParameters()
176      */
177     public List listParameters() {
178         return args;
179     }
180 }