View Javadoc

1   /**
2    * BaseUnrealEnvironment, an implementation of the environment interface standard that 
3    * facilitates the connection between GOAL and the UT2004 engine. 
4    * 
5    * Copyright (C) 2012 BaseUnrealEnvironment authors.
6    * 
7    * This program is free software: you can redistribute it and/or modify it under
8    * the terms of the GNU General Public License as published by the Free Software
9    * Foundation, either version 3 of the License, or (at your option) any later
10   * version.
11   * 
12   * This program is distributed in the hope that it will be useful, but WITHOUT
13   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14   * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15   * details.
16   * 
17   * You should have received a copy of the GNU General Public License along with
18   * this program. If not, see <http://www.gnu.org/licenses/>.
19   */
20  package nl.tudelft.goal.unreal.translators;
21  
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import nl.tudelft.goal.EIS2Java.exception.TranslationException;
26  import nl.tudelft.goal.EIS2Java.translation.Java2Parameter;
27  import nl.tudelft.goal.EIS2Java.translation.Parameter2Java;
28  import nl.tudelft.goal.EIS2Java.translation.Translator;
29  import cz.cuni.amis.pogamut.base.communication.worldview.object.WorldObjectId;
30  import cz.cuni.amis.pogamut.unreal.communication.messages.UnrealId;
31  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.NavPoint;
32  import eis.iilang.Identifier;
33  import eis.iilang.Parameter;
34  import eis.iilang.ParameterList;
35  
36  public class NavPointTranslator implements Parameter2Java<NavPoint>, Java2Parameter<NavPoint> {
37  
38  	/**
39  	 * To translate from Parameter2Java we are given an UnrealId. However we can
40  	 * not access the agents memory during translation. To work around this we
41  	 * store everything we have send to any agent.
42  	 */
43  	protected Map<WorldObjectId, NavPoint> navPoints = new HashMap<WorldObjectId, NavPoint>();
44  
45  
46  	@Override
47  	public Parameter[] translate(NavPoint o) throws TranslationException {
48  		navPoints.put(o.getId(), o);
49  
50  		ParameterList neightbours = new ParameterList();
51  		for (UnrealId id : o.getOutgoingEdges().keySet()) {
52  			neightbours.add(new Identifier(id.getStringId()));
53  		}
54  
55  		// navpoint(<UnrealID>, <Position>, [<Neigs-UnrealID>])
56  		return new Parameter[] { new Identifier(o.getId().getStringId()),
57  				Translator.getInstance().translate2Parameter(o.getLocation())[0], neightbours };
58  	}
59  
60  	@Override
61  	public NavPoint translate(Parameter p) throws TranslationException {
62  
63  		UnrealId id = Translator.getInstance().translate2Java(p, UnrealId.class);
64  
65  		NavPoint navPoint = navPoints.get(id);
66  
67  		if (navPoint == null) {
68  			String message = String
69  					.format("The identifier must be the unrealID of a navpoint that has been translated once, received: %s.",
70  							p);
71  			throw new TranslationException(message);
72  		}
73  
74  		return navPoint;
75  	}
76  
77  	@Override
78  	public Class<NavPoint> translatesFrom() {
79  		return NavPoint.class;
80  	}
81  
82  	@Override
83  	public Class<NavPoint> translatesTo() {
84  		return NavPoint.class;
85  	}
86  
87  }