View Javadoc

1   /*
2    wsmo4j - a WSMO API and Reference Implementation
3   
4    Copyright (c) 2005, University of Innsbruck, Austria
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  package org.deri.wsmo4j.logicalexpression;
19  
20  import java.util.*;
21  
22  import org.deri.wsmo4j.io.serializer.wsml.*;
23  import org.deri.wsmo4j.logicalexpression.util.*;
24  import org.omwg.logicalexpression.*;
25  import org.omwg.logicalexpression.Visitor;
26  import org.omwg.logicalexpression.terms.*;
27  import org.wsmo.common.*;
28  
29  /**
30   * Represents a Compount Molecule
31   *
32   * <pre>
33   * Created on Sep 19, 2005
34   * Committed by $Author$
35   * $Source$,
36   * </pre>
37   *
38   * @author Holger Lausen (holger.lausen@deri.org)
39   *
40   * @version $Revision$ $Date$
41   */
42  public class CompoundMoleculeImpl implements CompoundMolecule {
43      
44      List <Molecule> molecules;
45          
46      public CompoundMoleculeImpl(List <Molecule> molecules){
47          setOperands(new ArrayList <LogicalExpression> (molecules));
48      }
49  
50      public List <AttributeConstraintMolecule> listAttributeConstraintMolecules() {
51          return filter(AttributeConstraintMolecule.class, null);
52      }
53  
54      public List <AttributeInferenceMolecule> listAttributeInferenceMolecules() {
55          return filter(AttributeInferenceMolecule.class, null);
56      }
57  
58      public List <AttributeValueMolecule> listAttributeValueMolecules() {
59          return filter(AttributeValueMolecule.class, null);
60      }
61  
62      public List <AttributeConstraintMolecule> listAttributeConstraintMolecules(Term t) {
63          return filter(AttributeConstraintMolecule.class, t);
64      }
65  
66      public List <AttributeInferenceMolecule> listAttributeInferenceMolecules(Term t) {
67          return filter(AttributeInferenceMolecule.class, t);
68      }
69  
70      public List <AttributeValueMolecule> listAttributeValueMolecules(Term t) {
71          return filter(AttributeValueMolecule.class, t);
72      }
73  
74      public List <MembershipMolecule> listMemberShipMolecules() {
75          return filter(MembershipMolecule.class, null);
76      }
77  
78      public List <SubConceptMolecule> listSubConceptMolecules() {
79          return filter(SubConceptMolecule.class, null);
80      }
81  
82      public List <LogicalExpression> listOperands() {
83          return new ArrayList <LogicalExpression> (Collections.unmodifiableList(molecules));
84      }
85      
86      private List filter(Class clazz, Term attributeName){
87          List ret = new Vector();
88          Iterator i = molecules.iterator();
89          while (i.hasNext()){
90              Object o = i.next();
91              if (clazz.isInstance(o) && //classfilter
92                  (attributeName == null || //if attributeName given check as well
93                          attributeName.equals(((AttributeMolecule)o).getAttribute()))){
94                  ret.add(o);
95              }
96          }
97          return Collections.unmodifiableList(ret);
98      }
99  
100     public void accept(Visitor v) {
101         v.visitCompoundMolecule(this);
102     }
103 
104     /**
105      * 
106      */
107     public CompoundMoleculeImpl() {
108         super();
109         // TODO Auto-generated constructor stub
110     }
111     
112     
113     public boolean equals(Object o){
114         if (!(o instanceof CompoundMolecule)){
115             return false;
116         }
117         if (o==this){
118             return true;
119         }
120         CompoundMolecule cm = (CompoundMolecule)o;
121         if (cm.listOperands().size()!=molecules.size()){
122             return false;
123         }
124         Iterator i = cm.listOperands().iterator();
125         while (i.hasNext()){
126             Object molecule = i.next();
127             if (!molecules.contains(molecule)){
128                 return false;
129             }
130         }
131         return true;
132     }
133     
134     public String toString(){
135         return new LogExprSerializerWSML(null).serialize(this);
136     }
137 
138     /* (non-Javadoc)
139      * @see org.omwg.logicalexpression.CompoundExpression#setOperands(java.util.List)
140      */
141     public void setOperands(List <LogicalExpression> operands) {
142         if (operands.size() < 2){
143             throw new IllegalArgumentException("Compound Molecules have to consist of at least 2 Molecules");
144         }
145         if (!SetUtil.allOfType(operands, Molecule.class)){
146             throw new IllegalArgumentException("Compound Molecule can only consist of Molecules");
147         }
148         Iterator i = operands.iterator();
149         Term t = ((Molecule)i.next()).getLeftParameter();
150         while (i.hasNext()){
151             Term tNext = ((Molecule)i.next()).getLeftParameter();
152             if (!tNext.equals(t)){
153                 throw new IllegalArgumentException("Only Molecules with the same ID can be grouped to a Compound Molecule");
154             }
155         }
156         
157         List <Molecule> temp = new ArrayList <Molecule> (); 
158         for(LogicalExpression le : operands){
159             if (le instanceof Molecule){
160                 temp.add((Molecule) le);
161             }
162         }
163         
164         this.molecules = temp;
165         if (this.listMemberShipMolecules().size() !=0 &&
166                 this.listSubConceptMolecules().size() != 0){
167             throw new IllegalArgumentException("Compound Molecules can have either memberOf or subConceptOf Satement not both!");
168         }
169     }
170     
171     /**
172      * Returns String representation of a logical expression.
173      * @return the string representation of the Logical expression
174      * @param nsHolder namespace container used to abbreviate IRIs to 
175      * sQNames.
176      */
177     public String toString(TopEntity nsHolder) {
178         return new LogExprSerializerWSML(nsHolder).serialize(this);
179     }
180     
181     public int hashCode() {
182         return CompoundMoleculeImpl.class.hashCode()+molecules.size();
183     }
184 }
185 
186 
187 /*
188  *$Log$
189  *Revision 1.7  2007/04/02 12:13:20  morcen
190  *Generics support added to wsmo-api, wsmo4j and wsmo-test
191  *
192  *Revision 1.6  2006/05/04 15:16:45  holgerlausen
193  *fixed bug 1481907 -
194  *for logicalexpression hashCode() and equals where not correctly implemented
195  *
196  *Revision 1.5  2005/11/28 15:46:02  holgerlausen
197  *added support for using a TopEntity with namespace information to shorten string representation of logical expressions (RFE 1363559)
198  *
199  *Revision 1.4  2005/09/21 08:15:38  holgerlausen
200  *fixing java doc, removing asString()
201  *
202  *Revision 1.3  2005/09/21 06:31:55  holgerlausen
203  *allowing to set arguments rfe  1290049
204  *
205  *Revision 1.2  2005/09/20 19:41:01  holgerlausen
206  *removed superflouis interfaces for IO in logical expression (since intgeration not necessary)
207  *
208  *Revision 1.1  2005/09/20 13:21:31  holgerlausen
209  *refactored logical expression API to have simple molecules and compound molecules (RFE 1290043)
210  *
211  */