View Javadoc

1   /*
2    wsmo4j - a WSMO API and Reference Implementation
3    Copyright (c) 2004, DERI Innsbruck
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;
17  
18  
19  import java.util.*;
20  
21  import org.deri.wsmo4j.logicalexpression.util.SetUtil;
22  import org.omwg.logicalexpression.Atom;
23  import org.omwg.logicalexpression.Visitor;
24  import org.omwg.logicalexpression.terms.Term;
25  import org.omwg.ontology.DataValue;
26  import org.wsmo.common.Identifier;
27  
28  
29  /**
30   * This class is an atom expression with a n-ary domain, where n is
31   * the arity of the predicate represented
32   * @author DERI Innsbruck, reto.krummenacher@deri.org
33   * @version $Revision: 1946 $ $Date: 2007-04-02 15:13:28 +0300 (Mon, 02 Apr 2007) $
34   */
35  public class AtomImpl extends LogicalExpressionImpl
36          implements Atom {
37  
38      protected List <Term> args = new LinkedList <Term>();
39  
40      protected Identifier id;
41  
42      /**
43       * @param id identifier of Atom
44       * @param args list of parameters for that atom
45       * @throws IllegalArgumentException in case the parameter id is a Value or the arguments of
46       * the list args aren't all of Type Term
47       * @see org.wsmo.factory.LogicalExpressionFactory#createAtom(Identifier, List)
48       */
49      public AtomImpl(Identifier id, List <Term> args)
50              throws IllegalArgumentException {
51          if (id instanceof DataValue) {
52              throw new IllegalArgumentException("No Values allowed as Identifier for Atoms");
53          }
54          this.id = id;
55          setParameters(args);
56      }
57  
58      /**
59       * @return the identifier of the atom, i.e. the name of the predicate
60       * @see org.omwg.logicalexpression.Atom#getIdentifier()
61       */
62      public Identifier getIdentifier() {
63          return id;
64      }
65  
66      /**
67       * @param i the position of the parameter desired, maximal value getArity-1
68       * @return the parameter at the position provided as parameter
69       * @throws IllegalArgumentException in case i<0 or i exceeds the arity of the atom
70       * @see org.omwg.logicalexpression.Atom#getParameter(int)
71       */
72      public Term getParameter(int i)
73              throws IllegalArgumentException {
74          if (i < 0 || i > args.size()) {
75              throw new IllegalArgumentException("Parameter " + i + " exceeds arity of atom");
76          }
77          return args.get(i);
78      }
79  
80      /**
81       * @return the arity of the atom, i.e. the number of parameters
82       * @see org.omwg.logicalexpression.Atom#getArity()
83       */
84      public int getArity() {
85          if (args == null) {
86              return 0;
87          }
88          return args.size();
89      }
90  
91      /**
92       * @see org.omwg.logicalexpression.LogicalExpression#accept(org.omwg.logicalexpression.Visitor)
93       */
94      public void accept(Visitor v) {
95          v.visitAtom(this);
96      }
97  
98      /**
99       * <p>
100      * The <code>equals</code> method implements an equivalence relation
101      * on non-null object references. Atoms are equal if their arity, their
102      * identifier and all their parameters are equal.
103      * </p>
104      * <p>
105      * It is generally necessary to override the <code>hashCode</code> method whenever this method
106      * is overridden.
107      * </p>
108      * @param o the reference object with which to compare.
109      * @return <code>true</code> if this object is the same as the obj
110      *          argument; <code>false</code> otherwise.
111      * @see java.lang.Object#equals(java.lang.Object)
112      * @see java.lang.Object#hashCode()
113      */
114     public boolean equals(Object o) {
115         if (o instanceof Atom) {
116             Atom a = (Atom)o;
117             if (a.getArity() != this.getArity()
118                 || !a.getIdentifier().equals(this.getIdentifier())) {
119                 return false;
120             }
121             for (int i = 0; i < a.getArity(); i++) {
122                 if (!this.getParameter(i).equals(a.getParameter(i))) {
123                     return false;
124                 }
125             }
126             return true;
127         }
128         return false;
129     }
130 
131     /**
132      * <p>
133      * If two objects are equal according to the <code>equals(Object)</code> method, then calling
134      * the <code>hashCode</code> method on each of the two objects must produce the same integer
135      * result. However, it is not required that if two objects are unequal according to
136      * the <code>equals(Object)</code> method, then calling the <code>hashCode</code> method on each of the two
137      * objects must produce distinct integer results.
138      * </p>
139      * <p>
140      * This method should be overriden, when the <code>equals(Object)</code> method is overriden.
141      * </p>
142      * @return A hash code value for this Object.
143      * @see java.lang.Object#hashCode()
144      * @see java.lang.Object#equals(Object)
145      */
146     public int hashCode() {
147         if (args == null) {
148             return id.hashCode();
149         }
150         return id.hashCode() + args.size();
151     }
152 
153     public List <Term> listParameters() {
154         return Collections.unmodifiableList(args);
155     }
156 
157     /* (non-Javadoc)
158      * @see org.omwg.logicalexpression.Atom#setParameters(java.util.List)
159      */
160     public void setParameters(List <Term> parameter) throws IllegalArgumentException {
161         if (!SetUtil.allOfType(parameter, Term.class)) {
162             throw new IllegalArgumentException("Only terms allowed as Argument");
163         }
164         if (parameter != null && parameter.size() != 0) {
165             this.args = Collections.unmodifiableList(parameter);
166         }        
167     }
168 }
169 
170 /*
171  * $Log$
172  * Revision 1.13  2007/04/02 12:13:20  morcen
173  * Generics support added to wsmo-api, wsmo4j and wsmo-test
174  *
175  * Revision 1.12  2006/11/17 15:07:59  holgerlausen
176  * added an option to the parser to "remember" the original format of logical expressions, added the representation of the original String to LEImpl, and added to the serializer to check for the orginal formating to not not make complex expressions unreadable when serialized.
177  *
178  */