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  import java.util.Collections;
19  import java.util.HashMap;
20  import java.util.HashSet;
21  import java.util.Iterator;
22  import java.util.Map;
23  import java.util.Set;
24  
25  import org.deri.wsmo4j.logicalexpression.AbstractVisitor;
26  import org.deri.wsmo4j.logicalexpression.ConstantTransformer;
27  import org.omwg.logicalexpression.Atom;
28  import org.omwg.logicalexpression.AttributeConstraintMolecule;
29  import org.omwg.logicalexpression.AttributeInferenceMolecule;
30  import org.omwg.logicalexpression.AttributeValueMolecule;
31  import org.omwg.logicalexpression.CompoundMolecule;
32  import org.omwg.logicalexpression.MembershipMolecule;
33  import org.omwg.logicalexpression.Molecule;
34  import org.omwg.logicalexpression.SubConceptMolecule;
35  import org.omwg.logicalexpression.terms.Term;
36  import org.omwg.ontology.DataValue;
37  import org.omwg.ontology.Variable;
38  
39  /**
40   * Helper class for retrieving the concepts, instances and 
41   * attributes from logical expressions.
42   * 
43   * @author Nathalie Steinmetz, DERI Innsbruck
44   *
45   */
46  public class ItemCollectHelper extends AbstractVisitor {
47  
48  	private Set <Term> conceptIds = new HashSet <Term>();
49  	
50  	// key is a concept id, value is an attribute id
51  	private Map <Term, Term> attributeIds = new HashMap <Term, Term> ();
52  
53      private Set <Term> instanceIds = new HashSet <Term> ();
54      
55      private Set <Term> relationIds = new HashSet <Term> ();
56      
57      public ItemCollectHelper() {
58          super();
59      }
60  	
61      public void visitAtom(Atom expr) {
62      	Iterator it = expr.listParameters().iterator();
63      	while (it.hasNext()) {
64      		Term t = (Term) it.next();
65      		if (isValid(t)) {
66      			instanceIds.add(t);
67      		}
68      	}
69      	relationIds.add(expr.getIdentifier());
70      }
71      
72  	public void visitCompoundMolecule(CompoundMolecule expr) {
73  		Iterator i = expr.listOperands().iterator();
74          while(i.hasNext()){
75              ((Molecule)i.next()).accept(this);
76          }
77  		
78  	}
79  
80  	public void visitSubConceptMolecule(SubConceptMolecule expr) {
81  		Term left = expr.getLeftParameter();
82  		Term right = expr.getRightParameter();
83  		if (isValid(left)) {
84  			conceptIds.add(left);
85  		}
86  		if (isValid(right)) {
87  			conceptIds.add(right);
88  		}
89  	}
90  
91  	public void visitMemberShipMolecule(MembershipMolecule expr) {
92  		Term left = expr.getLeftParameter();
93  		Term right = expr.getRightParameter();
94  		if (isValid(left)) {
95  			instanceIds.add(left);
96  		}
97  		if (isValid(right)) {
98  			conceptIds.add(right);
99  		}
100 	}
101 
102 	public void visitAttributeValueMolecule(AttributeValueMolecule expr) {
103 		Term left = expr.getLeftParameter();
104 		Term right = expr.getRightParameter();
105 		if (isValid(left)) {
106 			instanceIds.add(left);
107 		}
108 		if (isValid(right)) {
109 			instanceIds.add(right);
110 		}	
111 	}
112 
113 	public void visitAttributeContraintMolecule(AttributeConstraintMolecule expr) {
114 		Term left = expr.getLeftParameter();
115 		Term right = expr.getRightParameter();
116 		Term attribute = expr.getAttribute();
117 		if (isValid(left)) {
118 			conceptIds.add(left);
119 		}			
120 		attributeIds.put(expr.getLeftParameter(), attribute);
121 		if (isValid(right)) {
122 			conceptIds.add(right);
123 		}
124 	}
125 
126 	public void visitAttributeInferenceMolecule(AttributeInferenceMolecule expr) {
127 		Term left = expr.getLeftParameter();
128 		Term right = expr.getRightParameter();
129 		Term attribute = expr.getAttribute();
130 		if (isValid(left)) {
131 			conceptIds.add(left);
132 		}			
133 		attributeIds.put(expr.getLeftParameter(), attribute);
134 		if (isValid(right)) {
135 			conceptIds.add(right);
136 		}
137 	}
138 
139 	/**
140      * @return Set containing the identifier of the logical expression's concepts
141      */
142     public Set <Term> getConceptIds() {
143         return Collections.unmodifiableSet(conceptIds);
144     }
145 
146     /**
147      * @return Set containing the identifier of the logical expression's instances
148      */
149     public Set <Term> getInstanceIds() {
150         return Collections.unmodifiableSet(instanceIds);
151     }
152 
153     /**
154      * @return Set containing the identifier of the logical expression's attributes
155      */
156     public Map <Term, Term> getAttributeIds() {
157     	return Collections.unmodifiableMap(attributeIds);
158     }
159 
160     /**
161      * @return Set containing the identifier of the logical expression's relations
162      */
163     public Set <Term> getRelationIds() {
164     	return Collections.unmodifiableSet(relationIds);
165     }
166     
167     /*
168      * A term must only be a constructed term, an identifier or a numbered anonymous id
169      */
170     private boolean isValid(Term term) {
171     	return (!ConstantTransformer.getInstance().isDataType(term.toString()) 
172     			&& !(term instanceof DataValue)
173     			&& !(term instanceof Variable));
174     }
175     
176 }