View Javadoc

1   package cz.cuni.amis.pogamut.usar2004.communication.messages.usarinfomessages;
2   
3   import java.util.logging.Level;
4   import java.util.logging.Logger;
5   import cz.cuni.amis.pogamut.base.communication.worldview.event.*;
6   import cz.cuni.amis.pogamut.base.communication.translator.event.*;
7   import cz.cuni.amis.pogamut.usar2004.communication.messages.*;
8   import cz.cuni.amis.pogamut.usar2004.communication.messages.datatypes.*;
9   import java.util.List;
10  
11  /**
12   *
13   * A Mission Package state message reports the mission package links current
14   * properties. Every mission package state includes a Name segment and several
15   * link segments.
16   *
17   * Corresponding GameBots message is MISSTA.
18   *
19   */
20  public class MissionPackageMessage extends GBEvent implements IWorldEvent, IWorldChangeEvent
21  {
22      /**
23       * Creates new instance of command MissionPackageMessage.
24       *
25       * @param Time Timestamp form the UT since server start in seconds.
26       * @param Name Name of the mission package
27       * @param Link This parameter gives the link number that will be described
28       * by the next two parameters (Value and Torque). Please note that you will
29       * have as many as these parameters as you have links.
30       *
31       * @param Value This parameter has two possible meanings. If the link being
32       * described is a prismatic joint, double gives the distance (in meters)
33       * from the original position of the link. If the link being described is a
34       * revolute joint, it gives a sector(probably).
35       *
36       * @param Torque The Torque parameter gives the current torque of the link
37       * being described.
38       */
39      public MissionPackageMessage(double Time, String Name, int Link, double Value, double Torque)
40      {
41          this.Time = Time;
42          this.Name = Name;
43          this.Links.add(Link, Value, Torque);
44      }
45  
46      public MissionPackageMessage(double Time, String Name, LinkState LS)
47      {
48          this.Time = Time;
49          this.Name = Name;
50          this.Links.add(LS);
51      }
52      /**
53       * Example how the message looks like - used during parser tests.
54       */
55      public static final String PROTOTYPE = "MISSTA {Time 0} {Name text}";//{Value 0} {Torque 0}";
56      /////// Properties BEGIN
57      protected double Time = 0;
58  
59      /**
60       * Timestamp form the UT since server start in seconds.
61       *
62       * @return Returns seconds elapsed from the start of the server.
63       */
64      public double getTime()
65      {
66          return Time;
67      }
68      protected String Name = null;
69  
70      /**
71       * Name of the mission package
72       *
73       * @return; Returns the name of the mission package
74       */
75      public String getName()
76      {
77          return Name;
78      }
79      //LinkStateSet brings data of all links to one structure
80      protected LinkStateSet Links = new LinkStateSet();
81  
82      /**
83       * This parameter gives the link number that will be described by the next
84       * two parameters (Value and Torque). Please note that you will have as many
85       * as these parameters as you have links.
86       *
87       * VALUE parameter has two possible meanings. If the link being described is
88       * a prismatic joint, double gives the distance (in meters) from the
89       * original position of the link. If the link being described is a revolute
90       * joint, it gives a sector(probably).
91       *
92       * The TORQUE parameter gives the current torque of the link being
93       * described. * All of the above are collected into one data structure below
94       * this text.
95       *
96       * @return Returns collection of Link States.
97       */
98      public List<LinkState> getLinkStateSet()
99      {
100         return Links.GetLinkStateList();
101     }
102 
103     /**
104      * Cloning constructor.
105      */
106     public MissionPackageMessage(MissionPackageMessage original)
107     {
108         this.Time = original.Time;
109         this.Name = original.Name;
110         this.Links = original.Links;
111     }
112 
113     /**
114      * Used by Yylex to create empty message then to fill it's protected fields
115      * (Yylex is in the same package).
116      */
117     public MissionPackageMessage()
118     {
119     }
120 
121     @Override
122     public String toString()
123     {
124         StringBuilder buf = new StringBuilder();
125 
126         buf.append(super.toString()).append(" | " + "Time = ").append(String.valueOf(Time)).append(" | " + "Name = ").append(String.valueOf(Name)).append(" | ");
127         if(!Links.isEmpty())
128         {
129 
130             try
131             {
132                 for(LinkState linkState : Links.GetLinkStateList())
133                 {
134                     buf.append("Link = " + String.valueOf(linkState.getLink()) + " | "
135                             + "Value = "
136                             + String.valueOf(linkState.getValue()) + " | "
137                             + "Torque = "
138                             + String.valueOf(linkState.getTorque()) + " | ");
139                 }
140             }
141             catch(Exception ex)
142             {
143                 Logger.getLogger(MissionPackageMessage.class.getName()).log(Level.SEVERE, null, ex);
144             }
145         }
146 
147         return buf.toString();
148 
149     }
150 
151     /**
152      * Gets all properties and values to create a HTML formated string;
153      *
154      * @return Returns all properties in HTML format
155      */
156     public String toHtmlString()
157     {
158         StringBuilder buf = new StringBuilder();
159         buf.append(super.toString()).append("<b>Time</b> : ").append(String.valueOf(Time)).append(" <br/> " + "<b>Name</b> : ").append(String.valueOf(Name)).append(" <br/> ");
160         if(!Links.isEmpty())
161         {
162             try
163             {
164                 for(LinkState linkState : Links.GetLinkStateList())
165                 {
166                     buf.append("<b>Link</b> : "
167                             + String.valueOf(linkState.getLink())
168                             + " <br/> "
169                             + "<b>Value</b> : "
170                             + String.valueOf(linkState.getValue())
171                             + " <br/> "
172                             + "<b>Torque</b> : "
173                             + String.valueOf(linkState.getTorque())
174                             + " <br/> ");
175 
176                 }
177             }
178             catch(Exception ex)
179             {
180                 Logger.getLogger(MissionPackageMessage.class.getName()).log(Level.SEVERE, null, ex);
181             }
182         }
183         return buf.toString();
184 
185     }
186 }