View Javadoc

1   package nl.tudelft.goal.ut2004.translators;
2   
3   import java.util.LinkedList;
4   
5   import nl.tudelft.goal.ut2004.messages.FireMode;
6   import cz.cuni.amis.pogamut.ut2004.agent.module.sensor.WeaponPref;
7   import cz.cuni.amis.pogamut.ut2004.communication.messages.ItemType;
8   import cz.cuni.amis.pogamut.ut2004.communication.messages.ItemType.Category;
9   import cz.cuni.amis.pogamut.ut2004.communication.messages.ItemType.Group;
10  import eis.eis2java.exception.TranslationException;
11  import eis.eis2java.translation.Parameter2Java;
12  import eis.eis2java.translation.Translator;
13  import eis.iilang.Function;
14  import eis.iilang.Parameter;
15  
16  /**
17   * 
18   * 
19   * Example: weapon(shock_rifle,primary)<br>
20   * Example: weapon(bio_rifle,primary)]<br>
21   * Example weapon(rocket_launcher,secondary)<br>
22   * 
23   * @author mpkorstanje
24   *
25   */
26  public class WeaponPrefTranslator implements Parameter2Java<WeaponPref> {
27  
28  	private static final int WEAPON_PARAMETERS = 2;
29  	private static final String WEAPON_KEYWORD = "weapon";
30  
31  	@Override
32  	public WeaponPref translate(Parameter parameter) throws TranslationException {
33  
34  		if (!(parameter instanceof Function)) {
35  			String message = String.format("Expected a function but got %s.", parameter);
36  			throw new TranslationException(message);
37  		}
38  		
39  		Function function = (Function)parameter;
40  	
41  		if(!function.getName().equals(WEAPON_KEYWORD)){
42  			String message = String.format("Expected a function named "+WEAPON_KEYWORD+" but got %s.", parameter);
43  			throw new TranslationException(message);
44  		}
45  		
46  		LinkedList<Parameter> parameters = function.getParameters();
47  		if(parameters.size() != WEAPON_PARAMETERS){
48  			String message = String.format("Expected a function named "+WEAPON_KEYWORD+" with exactly "+WEAPON_PARAMETERS+" arguments but got %s.", parameter);
49  			throw new TranslationException(message);
50  		}
51  		
52  		Group group = Translator.getInstance().translate2Java(parameters.getFirst(), Group.class);
53  
54  		ItemType itemType = getWeapon(group);
55  		
56  		FireMode fireMode = Translator.getInstance().translate2Java(parameters.getLast(), FireMode.class);
57  
58  		if(fireMode == FireMode.NONE){
59  			String message = String.format("Fire mode should be either primary or secondary but got %s.", parameter);
60  			throw new TranslationException(message);
61  		}
62  		
63  		return new WeaponPref(itemType, fireMode.isPrimary());
64  		
65  	}
66  
67  	private ItemType getWeapon(Group group) throws TranslationException {
68  		for(ItemType item : group.getTypes()){
69  			if(item.getCategory() == Category.WEAPON){
70  				return item;
71  			}
72  		}
73  		
74  		String message = String.format("%s was not a ItemType in the weapon category.", group);
75  		throw new TranslationException(message);
76  	}
77  
78  	@Override
79  	public Class<WeaponPref> translatesTo() {
80  		return WeaponPref.class;
81  	}
82  
83  }