Skip to content

Commit cf53ae7

Browse files
committed
Generating code for ros action files
1 parent bd6818d commit cf53ae7

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed

message_generation/src/main/java/org/ros/internal/message/GenerateInterfaces.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.ros.exception.RosMessageRuntimeException;
2424
import org.ros.internal.message.definition.MessageDefinitionProviderChain;
2525
import org.ros.internal.message.definition.MessageDefinitionTupleParser;
26+
import org.ros.internal.message.action.ActionDefinitionFileProvider;
2627
import org.ros.internal.message.service.ServiceDefinitionFileProvider;
2728
import org.ros.internal.message.topic.TopicDefinitionFileProvider;
2829
import org.ros.message.MessageDeclaration;
@@ -42,6 +43,7 @@ public class GenerateInterfaces {
4243

4344
private final TopicDefinitionFileProvider topicDefinitionFileProvider;
4445
private final ServiceDefinitionFileProvider serviceDefinitionFileProvider;
46+
private final ActionDefinitionFileProvider actionDefinitionFileProvider;
4547
private final MessageDefinitionProviderChain messageDefinitionProviderChain;
4648
private final MessageFactory messageFactory;
4749
static private final String ROS_PACKAGE_PATH = "ROS_PACKAGE_PATH";
@@ -52,6 +54,8 @@ public GenerateInterfaces() {
5254
messageDefinitionProviderChain.addMessageDefinitionProvider(topicDefinitionFileProvider);
5355
serviceDefinitionFileProvider = new ServiceDefinitionFileProvider();
5456
messageDefinitionProviderChain.addMessageDefinitionProvider(serviceDefinitionFileProvider);
57+
actionDefinitionFileProvider = new ActionDefinitionFileProvider();
58+
messageDefinitionProviderChain.addMessageDefinitionProvider(actionDefinitionFileProvider);
5559
messageFactory = new DefaultMessageFactory(messageDefinitionProviderChain);
5660
}
5761

@@ -110,15 +114,58 @@ private void writeServiceInterfaces(File outputDirectory, Collection<String> pac
110114
MessageDeclaration.of(serviceType.getType(), definition);
111115
writeInterface(serviceDeclaration, outputDirectory, false);
112116
List<String> requestAndResponse = MessageDefinitionTupleParser.parse(definition, 2);
117+
113118
MessageDeclaration requestDeclaration =
114119
MessageDeclaration.of(serviceType.getType() + "Request", requestAndResponse.get(0));
115120
MessageDeclaration responseDeclaration =
116121
MessageDeclaration.of(serviceType.getType() + "Response", requestAndResponse.get(1));
122+
117123
writeInterface(requestDeclaration, outputDirectory, true);
118124
writeInterface(responseDeclaration, outputDirectory, true);
119125
}
120126
}
121127

128+
/**
129+
* @param packages
130+
* a list of packages containing the topic types to generate
131+
* interfaces for
132+
* @param outputDirectory
133+
* the directory to write the generated interfaces to
134+
* @throws IOException
135+
*/
136+
private void writeActionInterfaces(File outputDirectory, Collection<String> packages)
137+
throws IOException {
138+
Collection<MessageIdentifier> actionTypes = Sets.newHashSet();
139+
if (packages.size() == 0) {
140+
packages = actionDefinitionFileProvider.getPackages();
141+
}
142+
for (String pkg : packages) {
143+
Collection<MessageIdentifier> messageIdentifiers =
144+
actionDefinitionFileProvider.getMessageIdentifiersByPackage(pkg);
145+
if (messageIdentifiers != null) {
146+
actionTypes.addAll(messageIdentifiers);
147+
}
148+
}
149+
for (MessageIdentifier actionType : actionTypes) {
150+
String definition = messageDefinitionProviderChain.get(actionType.getType());
151+
MessageDeclaration actionDeclaration =
152+
MessageDeclaration.of(actionType.getType(), definition);
153+
writeInterface(actionDeclaration, outputDirectory, false);
154+
List<String> goalResultAndFeedback = MessageDefinitionTupleParser.parse(definition, 3);
155+
156+
MessageDeclaration goalDeclaration =
157+
MessageDeclaration.of(actionType.getType() + "Goal", goalResultAndFeedback.get(0));
158+
MessageDeclaration resultDeclaration =
159+
MessageDeclaration.of(actionType.getType() + "Response", goalResultAndFeedback.get(1));
160+
MessageDeclaration feedbackDeclaration =
161+
MessageDeclaration.of(actionType.getType() + "Feedback", goalResultAndFeedback.get(2));
162+
163+
writeInterface(goalDeclaration, outputDirectory, true);
164+
writeInterface(resultDeclaration, outputDirectory, true);
165+
writeInterface(feedbackDeclaration, outputDirectory, true);
166+
}
167+
}
168+
122169
private void writeInterface(MessageDeclaration messageDeclaration, File outputDirectory,
123170
boolean addConstantsAndMethods) {
124171
MessageInterfaceBuilder builder = new MessageInterfaceBuilder();
@@ -142,12 +189,15 @@ public void generate(File outputDirectory, Collection<String> packages,
142189
for (File directory : packagePath) {
143190
topicDefinitionFileProvider.addDirectory(directory);
144191
serviceDefinitionFileProvider.addDirectory(directory);
192+
actionDefinitionFileProvider.addDirectory(directory);
145193
}
146194
topicDefinitionFileProvider.update();
147195
serviceDefinitionFileProvider.update();
196+
actionDefinitionFileProvider.update();
148197
try {
149198
writeTopicInterfaces(outputDirectory, packages);
150199
writeServiceInterfaces(outputDirectory, packages);
200+
writeActionInterfaces(outputDirectory, packages);
151201
} catch (IOException e) {
152202
throw new RosMessageRuntimeException(e);
153203
}
@@ -158,6 +208,7 @@ public static void main(String[] args) {
158208
if (arguments.size() == 0) {
159209
arguments.add(".");
160210
}
211+
161212
String rosPackagePath = System.getenv(ROS_PACKAGE_PATH);
162213
// Overwrite with a supplied package path if specified (--package-path=)
163214
for (ListIterator<String> iter = arguments.listIterator(); iter.hasNext(); ) {
@@ -168,13 +219,15 @@ public static void main(String[] args) {
168219
break;
169220
}
170221
}
222+
171223
Collection<File> packagePath = Lists.newArrayList();
172224
for (String path : rosPackagePath.split(File.pathSeparator)) {
173225
File packageDirectory = new File(path);
174226
if (packageDirectory.exists()) {
175227
packagePath.add(packageDirectory);
176228
}
177229
}
230+
178231
GenerateInterfaces generateInterfaces = new GenerateInterfaces();
179232
File outputDirectory = new File(arguments.remove(0));
180233
generateInterfaces.generate(outputDirectory, arguments, packagePath);
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (C) 2011 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package org.ros.internal.message.action;
18+
19+
import org.ros.internal.message.definition.MessageDefinitionFileProvider;
20+
21+
import org.apache.commons.io.filefilter.FileFilterUtils;
22+
import org.apache.commons.io.filefilter.IOFileFilter;
23+
import org.ros.internal.message.StringFileProvider;
24+
25+
import java.io.File;
26+
import java.io.FileFilter;
27+
28+
/**
29+
* @author arne.peters@tum.de (Arne Peters)
30+
*/
31+
public class ActionDefinitionFileProvider extends MessageDefinitionFileProvider {
32+
33+
private static final String PARENT = "action";
34+
private static final String SUFFIX = "action";
35+
36+
private static StringFileProvider newStringFileProvider() {
37+
IOFileFilter extensionFilter = FileFilterUtils.suffixFileFilter(SUFFIX);
38+
IOFileFilter parentBaseNameFilter = FileFilterUtils.asFileFilter(new FileFilter() {
39+
@Override
40+
public boolean accept(File file) {
41+
return getParentBaseName(file.getAbsolutePath()).equals(PARENT);
42+
}
43+
});
44+
IOFileFilter fileFilter = FileFilterUtils.andFileFilter(extensionFilter, parentBaseNameFilter);
45+
return new StringFileProvider(fileFilter);
46+
}
47+
48+
public ActionDefinitionFileProvider() {
49+
super(newStringFileProvider());
50+
}
51+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright (C) 2012 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
/**
18+
* Provides internal classes for representing action messages.
19+
* <p>
20+
* These classes should _not_ be used directly outside of the org.ros package.
21+
*/
22+
package org.ros.internal.message.action;

0 commit comments

Comments
 (0)