View Javadoc

1    package nl.tudelft.goal.ut2004.translators;
2   
3   import java.util.ArrayList;
4   
5   import nl.tudelft.goal.ut2004.messages.None;
6   import nl.tudelft.goal.ut2004.messages.Percept;
7   import eis.eis2java.exception.TranslationException;
8   import eis.eis2java.translation.Java2Parameter;
9   import eis.eis2java.translation.Translator;
10  import eis.iilang.Parameter;
11  import eis.iilang.ParameterList;
12  
13  /**
14   * Translates percepts of objects to arrays such that they can be reasonably be
15   * expected to match. When objects translate to multiple elements they are
16   * considered to be a {@link ParameterList}. When they translate to an empty
17   * list they're ignored. Single element lists are collapsed.
18   * 
19   * @author mpkorstanje
20   * 
21   */
22  public class PerceptTranslator implements Java2Parameter<Percept> {
23  
24  	@Override
25  	public Parameter[] translate(Percept percept) throws TranslationException {
26  		ArrayList<Object> translated = new ArrayList<Object>(percept.size());
27  		for (Object elements : percept) {
28  
29  			// Null elements are considered "none".
30  			if (elements == null) {
31  				elements = new None();
32  			}
33  
34  			Parameter[] t = Translator.getInstance().translate2Parameter(elements);
35  
36  			// Collapse single and empty arrays.
37  			if (t.length == 0) {
38  				continue;
39  			} else if (t.length == 1) {
40  				translated.add(t[0]);
41  			} else {
42  				translated.add(new ParameterList(t));
43  			}
44  		}
45  		return translated.toArray(new Parameter[translated.size()]);
46  	}
47  
48  	@Override
49  	public Class<? extends Percept> translatesFrom() {
50  		return Percept.class;
51  	}
52  
53  }