Skip to content
Snippets Groups Projects
Commit ad6afb55 authored by marvin's avatar marvin
Browse files

Automatically generate sensorconfigs from gazebo.xacro files

parent 00e97573
No related merge requests found
......@@ -29,38 +29,15 @@ public class SensorConfigSetup extends XMLHelper{
}
/**
* Check whether the given directory exists and if not, create it.
* Takes a file name and alters it so a FeatureIDE model will accept it.
* FeatureIDE says it only takes the following regex as valid feature names: ^[a-zA-Z]+\w*$
* This function will replace all \W characters (no word character) with '_'.
*
* @param path Path to directory as String
* @param name Name to be changes as String.
* @return Corrected name
*/
private static void createDirectory(String path) {
File directory = new File(path);
if(!directory.exists()) {
boolean created = directory.mkdir();
if(!created) {
System.err.println("Error: Directory \'"+directory.getPath()+"\' does not exist and could not be created.");
System.exit(-1);
}
}
}
/**
* Recursively delete a directory and its contents.
*
* @param path Path to directory as String
*/
private static void deleteDirectory(String path) {
File directory = new File(path);
File[] content = directory.listFiles();
if(content != null) {
for(File file : content) {
deleteDirectory(file.getPath());
}
}
if(!directory.delete()) {
System.err.println("Error: Could not delete file/ directory: \'"+directory.getPath()+"\'.");
}
private static String fixName(String name) {
return name.replaceAll("\\W", "_");
}
/**
......@@ -79,12 +56,17 @@ public class SensorConfigSetup extends XMLHelper{
//Get modelsetup.config values.
String in_scPath = getConfigVal(ModelSetupConfig, "input/sensorconfig", "path");///TODO
String in_gxPath = getConfigVal(ModelSetupConfig, "input/gazebo_xacro", "path");
String out_mPath = getConfigVal(ModelSetupConfig, "output/model", "path");
//Declare Document.
//Document xy =
String scAutoPath = in_scPath+"autogenerated/";
String scOtherPath = in_scPath+"other/";
//Declare Documents and their corresponding file names.
List<Document> gazeboModelList = new ArrayList<Document>();
List<String> gazeboModelNamesList = new ArrayList<String>();
List<Document> sensorConfigList = new ArrayList<Document>();
List<String> sensorConfigNamesList = new ArrayList<String>();
//Get sensorconfig- and gazebomodel file names.
//Get gazebomodel file names.
//in_gxPath may contain other files that will be filtered out!
List<String> gxDirectoryFilesList = getFileNames(in_gxPath);
List<String> gxList = new ArrayList<String>();
......@@ -96,32 +78,76 @@ public class SensorConfigSetup extends XMLHelper{
//Check whether input was empty.
if(gxList.isEmpty()) {
System.err.println("Error: No gazebo model \'"+GX_ENDING+"\' files found in \'"+in_gxPath+"\'. Could not autogenerate sensorconfig files");
System.exit(-1);
System.out.println("Warning: No gazebo model \'"+GX_ENDING+"\' files found in \'"+in_gxPath+"\'. Could not autogenerate sensorconfig files");
}
//Get all gazebomodel files as List<Document>.
for(String gx : gxList) {
gazeboModelList.add(readXml(in_gxPath+gx));
gazeboModelNamesList.add(gx.substring(0, gx.length() - GX_ENDING.length()));
}
//###############################################
//######### Check directories ###################
String scAuto = in_scPath+"autogenerated/";
String scOther = in_scPath+"other/";
createDirectory(in_scPath);
if((new File(scAuto)).exists()) {
deleteDirectory(scAuto);
checkDirectory(in_scPath);
if((new File(scAutoPath)).exists()) {
deleteDirectory(scAutoPath);
}
createDirectory(scAuto);
createDirectory(scOther);
checkDirectory(scAutoPath);
checkDirectory(scOtherPath);
//###############################################
//######### Generate sensorconfigs ##############
for(int i = 0; i < gazeboModelList.size(); i++) {
Document gazeboModel = gazeboModelList.get(i);
String gazeboModelName = gazeboModelNamesList.get(i);
Namespace NS = gazeboModel.getRootElement().getNamespace("xacro");
//Replace all $(arg ...) variables with their concrete values.
List<Element> argList = xQueryElements(gazeboModel, "//xacro:arg", NS);
for(Element arg : argList) {
String variable = "$(arg "+arg.getAttributeValue("name")+")";
String concreteValue = arg.getAttributeValue("default");
List<Element> occurrenceList = xQueryElements(gazeboModel, "//*[text()='"+variable+"']");
for(Element occurrence : occurrenceList) {
occurrence.setText(concreteValue);
}
}
//Get all existing sensors in gazebomodel.
List<Element> sensorList = xQueryElements(gazeboModel, "//sensor");
//Create a new sensorconfig for each of them.
for(Element sensor : sensorList) {
Element entry = sensor.getParentElement();
if(entry.getName().equals("gazebo")) {
entry.detach();
Element sensorConfigRoot = new Element("configuration");
Document sensorConfig = new Document(sensorConfigRoot);
sensorConfig.getRootElement().addContent(entry);
sensorConfigList.add(sensorConfig);
String sensorConfigName = fixName(sensor.getAttributeValue("name"))+"___"+fixName(gazeboModelName);
sensorConfigNamesList.add(sensorConfigName);
}
else {
System.out.println("Found a <sensor> element that does not have a <gazebo> element as immediate parent. It was ignored, as this input is unexpected.");
}
}
}
//###############################################
//######### Output result #######################
//saveAsXml(model, out_mPath+out_mName);
for(int i = 0; i < sensorConfigList.size(); i++) {
Document sensorConfig = sensorConfigList.get(i);
String sensorConfigName = sensorConfigNamesList.get(i);
saveAsXml(sensorConfig, scAutoPath+sensorConfigName+SC_ENDING);
}
//###############################################
}
catch (JDOMException | IOException e) {
......
......@@ -83,6 +83,80 @@ public class XMLHelper {
return directoryNames;
}
/**
* Check whether the given directory exists and if not, create it.
*
* @param path Path to directory as String
* @return false if directory denoted by path does not exist and could not be created; true otherwise
*/
public static boolean checkDirectory(String path) {
File directory = new File(path);
if(!directory.exists()) {
boolean created = directory.mkdir();
if(!created) {
System.err.println("Error: Directory \'"+directory.getPath()+"\' does not exist and could not be created.");
return false;
}
}
return true;
}
/**
* Recursively delete a directory and its contents.
*
* @param path Path to directory as String
*/
public static void deleteDirectory(String path) {
File element = new File(path);
File[] content = element.listFiles();
if(content != null) {
for(File file : content) {
deleteDirectory(file.getPath());
}
}
if(!element.delete()) {
System.err.println("Error: Could not delete file/ directory: \'"+element.getPath()+"\'.");
}
}
/**
* Finds a file by recursive search in the directory given by path.
*
* @param name Name of the file including file ending as String
* @param path Path to directory to be searched as String
* @return path to all file matches as List<String> (e.g. .../sensorconfig/other/imu.xml)
*/
public static List<String> findFile(String name, String path) {
List<String> accumulator = new ArrayList<String>();
return findFile(name, path, accumulator);
}
/**
* Executes file search for findFile(String name, String path).
*
* @param name Name of the file including file ending as String
* @param path Path to directory to be searched as String
* @param result Accumulator for all file matches as List<String>
* @return path to all file matches as list<String> (e.g. .../sensorconfig/other/imu.xml)
*/
private static List<String> findFile(String name, String path, List<String> result) {
File element = new File(path);
File[] content = element.listFiles();
if(content != null) {
for(File file : content) {
List<String> subResult = findFile(name, path+"/"+file.getName(), result);
result.addAll(subResult);
}
}
if(element.getName().equals(name)) {
result.add(element.getName());
}
return result;
}
/**
* Executes given XPath expression for elements with JDOM Document as source.
*
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment