forked from Xamla/ROS.NET
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ActionFile.cs
310 lines (264 loc) · 13.6 KB
/
ActionFile.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using YAMLParser;
namespace FauxMessages
{
public class ActionFile
{
public string Name { get; private set; }
public MsgFile GoalMessage { get; private set; }
public MsgFile ResultMessage { get; private set; }
public MsgFile FeedbackMessage { get; private set; }
public MsgFile GoalActionMessage { get; private set; }
public MsgFile ResultActionMessage { get; private set; }
public MsgFile FeedbackActionMessage { get; private set; }
private string fileNamespace = "Messages";
private List<SingleType> stuff = new List<SingleType>();
private string className;
private MsgFileLocation MsgFileLocation;
private List<string> linesOfActionFile = new List<string>();
public ActionFile(MsgFileLocation filename)
{
// Read in action file
string[] lines = File.ReadAllLines(filename.Path);
InitializeActionFile(filename, lines);
}
public ActionFile(MsgFileLocation filename, string[] lines)
{
InitializeActionFile(filename, lines);
}
public void ParseAndResolveTypes()
{
GoalMessage.ParseAndResolveTypes();
GoalActionMessage.ParseAndResolveTypes();
ResultMessage.ParseAndResolveTypes();
ResultActionMessage.ParseAndResolveTypes();
FeedbackMessage.ParseAndResolveTypes();
FeedbackActionMessage.ParseAndResolveTypes();
}
public void Write(string outdir)
{
string[] chunks = Name.Split('.');
for (int i = 0; i < chunks.Length - 1; i++)
outdir = Path.Combine(outdir, chunks[i]);
if (!Directory.Exists(outdir))
Directory.CreateDirectory(outdir);
string contents = GenerateActionMessages();
if (contents != null)
File.WriteAllText(Path.Combine(outdir, MsgFileLocation.basename + "ActionMessages.cs"),
contents.Replace("FauxMessages", "Messages")
);
}
/// <summary>
/// Loads the template for a single message and replaces all the $placeholders with appropriate content
/// </summary>
public string GenerateMessageFromTemplate(string template, MsgFile message, MsgFile childMessage)
{
var properties = message.GenerateProperties();
template = template.Replace("$CLASS_NAME", message.classname);
template = template.Replace("$$PROPERTIES", properties);
template = template.Replace("$ISMETA", message.meta.ToString().ToLower());
template = template.Replace("$MSGTYPE", fileNamespace.Replace("Messages.", "") + "/" + message.classname);
template = template.Replace("$MESSAGEDEFINITION", "@\"" + message.Definition + "\"");
template = template.Replace("$HASHEADER", message.HasHeader.ToString().ToLower());
template = template.Replace("$NULLCONSTBODY", "");
template = template.Replace("$EXTRACONSTRUCTOR", "");
template = template.Replace("$MD5SUM", MD5.Sum(message));
// Set the base class of the message
var actionClasses = new List<string> { "InnerActionMessage",
"GoalActionMessage<$ACTION_GENERIC>",
"ResultActionMessage<$ACTION_GENERIC>",
"FeedbackActionMessage<$ACTION_GENERIC>"
};
var actionClass = actionClasses[(int)message.ActionMessageType];
actionClass = actionClass.Replace("$ACTION_GENERIC", childMessage != null ? childMessage.Name : "");
template = template.Replace("$ACTION_CLASS", actionClass);
string deserializationCode = "";
string serializationCode = "";
string randomizationCode = "";
string equalizationCode = "";
for (int i = 0; i < message.Stuff.Count; i++)
{
deserializationCode += message.GenerateDeserializationCode(message.Stuff[i], 1);
serializationCode += message.GenerateSerializationCode(message.Stuff[i], 1);
randomizationCode += message.GenerateRandomizationCode(message.Stuff[i], 1);
equalizationCode += message.GenerateEqualityCode(message.Stuff[i], 1);
}
template = template.Replace("$SERIALIZATIONCODE", serializationCode);
template = template.Replace("$DESERIALIZATIONCODE", deserializationCode);
template = template.Replace("$RANDOMIZATIONCODE", randomizationCode);
template = template.Replace("$EQUALITYCODE", equalizationCode);
return template;
}
/// <summary>
/// Loads the template for the action message class, which holds all six action messages and inserts the code for each
/// message class.
/// </summary>
/// <returns></returns>
public string GenerateActionMessages()
{
var template = Templates.ActionMessagesPlaceHolder;
template = template.Replace("$NAMESPACE", GoalMessage.Package);
var messages = new List<MessageTemplateInfo>
{
new MessageTemplateInfo(GoalActionMessage, GoalMessage, "$ACTION_GOAL_MESSAGE", "$GOAL_MESSAGE"),
new MessageTemplateInfo(ResultActionMessage, ResultMessage, "$ACTION_RESULT_MESSAGE", "$RESULT_MESSAGE"),
new MessageTemplateInfo(FeedbackActionMessage, FeedbackMessage, "$ACTION_FEEDBACK_MESSAGE", "$FEEDBACK_MESSAGE")
};
foreach (var messagePair in messages)
{
var generatedCode = GenerateMessageFromTemplate(Templates.InnerMessageTemplate, messagePair.InnerMessage, null);
template = template.Replace(messagePair.InnerMessagePlaceHolder, generatedCode);
/*generatedCode = GenerateMessageFromTemplate(Templates.ActionMessageTemplate, messagePair.OuterMessage,
messagePair.InnerMessage
);
template = template.Replace(messagePair.OuterMessagePlaceHoder, generatedCode);*/
}
return template;
}
/// <summary>
/// Wrapper to create a MsgsFile
/// </summary>
private MsgFile CreateMessageFile(MsgFileLocation messageLocation, List<string> parameters, string suffix)
{
var result = new MsgFile(new MsgFileLocation(
messageLocation.Path, messageLocation.searchroot),
parameters,
suffix
);
return result;
}
private void InitializeActionFile(MsgFileLocation filename, string[] lines)
{
MsgFileLocation = filename;
Name = filename.package + "." + filename.basename;
className = filename.basename;
fileNamespace += "." + filename.package;
var parsedAction = ParseActionFile(lines);
// Goal Messages
GoalMessage = CreateMessageFile(filename, parsedAction.GoalParameters, "Goal");
GoalActionMessage = CreateMessageFile(filename, parsedAction.GoalActionParameters, "ActionGoal");
GoalActionMessage.ActionMessageType = ActionMessageType.Goal;
// Result Messages
ResultMessage = CreateMessageFile(filename, parsedAction.ResultParameters, "Result");
ResultActionMessage = CreateMessageFile(filename, parsedAction.ResultActionParameters, "ActionResult");
ResultActionMessage.ActionMessageType = ActionMessageType.Result;
// Feedback Messages
FeedbackMessage = CreateMessageFile(filename, parsedAction.FeedbackParameters, "Feedback");
FeedbackActionMessage = CreateMessageFile(filename, parsedAction.FeedbackActionParameters, "ActionFeedback");
FeedbackActionMessage.ActionMessageType = ActionMessageType.Feedback;
}
/// <summary>
/// Extracts and generates the parameters for the six messages that are needed to use the actionlib, i.e. Goal,
/// ActionGoal, Result, ActionResult, Feedback, ActionFeedback. The Action parameters are the ones that are generated.
/// </summary>
/// <param name="lines">The content of the .action file</param>
/// <returns>A ValueTuple with the parameters in a different field</returns>
private (List<string> GoalParameters, List<string> ResultParameters, List<string> FeedbackParameters,
List<string> GoalActionParameters, List<string> ResultActionParameters, List<string> FeedbackActionParameters)
ParseActionFile (string[] lines)
{
var goalParameters = new List<string>();
var resultParameters = new List<string>();
var feedbackParameters = new List<string>();
var goalActionParameters = new List<string>();
var resultActionParameters = new List<string>();
var feedbackActionParameters = new List<string>();
linesOfActionFile = new List<string>();
int foundDelimeters = 0;
// Search through for the "---" separator between request and response
for (int lineNumber = 0; lineNumber < lines.Length; lineNumber++)
{
lines[lineNumber] = lines[lineNumber].Replace("\"", "\\\"");
if (lines[lineNumber].Contains('#'))
{
lines[lineNumber] = lines[lineNumber].Substring(0, lines[lineNumber].IndexOf('#'));
}
lines[lineNumber] = lines[lineNumber].Trim();
if (lines[lineNumber].Length == 0)
{
continue;
}
linesOfActionFile.Add(lines[lineNumber]);
if (lines[lineNumber].Contains("---"))
{
foundDelimeters += 1;
}
if (goalActionParameters.Count == 0)
{
// Check always if the goal action header has been set, because in the case of an empty goal, the delimeter
// would jump over the this header
goalActionParameters.Add("Header header");
goalActionParameters.Add("actionlib_msgs/GoalID goal_id");
goalActionParameters.Add($"{Name.Replace(".", "/")}Goal goal");
}
if (foundDelimeters == 0)
{
goalParameters.Add(lines[lineNumber]);
}
else if (foundDelimeters == 1)
{
if (resultActionParameters.Count == 0)
{
resultActionParameters.Add("Header header");
resultActionParameters.Add("actionlib_msgs/GoalStatus status");
resultActionParameters.Add($"{Name.Replace(".", "/")}Result result");
} else
{
resultParameters.Add(lines[lineNumber]);
}
}
else if (foundDelimeters == 2)
{
if (feedbackActionParameters.Count == 0)
{
feedbackActionParameters.Add("Header header");
feedbackActionParameters.Add("actionlib_msgs/GoalStatus status");
feedbackActionParameters.Add($"{Name.Replace(".", "/")}Feedback feedback");
} else
{
feedbackParameters.Add(lines[lineNumber]);
}
} else
{
throw new InvalidOperationException($"Action file has an unexpected amount of --- delimeters.");
}
}
return (goalParameters, resultParameters, feedbackParameters, goalActionParameters, resultActionParameters,
feedbackActionParameters);
}
private readonly List<string> actionInterfaceImplementations = new List<string>
{
// No Action Message
"",
// Goal Action Message
"public Messages.std_msgs.Header Header { get { return header; } set { header = value; } }" + "\n" +
"public Messages.actionlib_msgs.GoalID GoalId { get { return goal_id; } set { goal_id = value; } }",
// Result Action Message
"public Messages.std_msgs.Header Header { get { return header; } set { header = value; } }" + "\n" +
"public Messages.actionlib_msgs.GoalStatus GoalStatus { get { return status; } set { status = value; } }",
// Feedback Action Message
"public Messages.std_msgs.Header Header { get { return header; } set { header = value; } }" + "\n" +
"public Messages.actionlib_msgs.GoalStatus GoalStatus { get { return status; } set { status = value; } }",
};
}
class MessageTemplateInfo
{
public MsgFile OuterMessage { get; }
public MsgFile InnerMessage { get; }
public string OuterMessagePlaceHoder { get; }
public string InnerMessagePlaceHolder { get; }
public MessageTemplateInfo(MsgFile OuterMessage, MsgFile InnerMessage, string OuterMessagePlaceHoder,
string InnerMessagePlaceHolder)
{
this.OuterMessage = OuterMessage;
this.InnerMessage = InnerMessage;
this.OuterMessagePlaceHoder = OuterMessagePlaceHoder;
this.InnerMessagePlaceHolder = InnerMessagePlaceHolder;
}
}
}