Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
Semesterprojekt Modulbasiertes Testen
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Jonas Trappe
Semesterprojekt Modulbasiertes Testen
Commits
ad6afb55
Commit
ad6afb55
authored
5 years ago
by
marvin
Browse files
Options
Downloads
Patches
Plain Diff
Automatically generate sensorconfigs from gazebo.xacro files
parent
00e97573
No related merge requests found
Changes
42
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/var/FeatureModelImplGenerator/src/SensorConfigSetup.java
+72
-46
72 additions, 46 deletions
src/var/FeatureModelImplGenerator/src/SensorConfigSetup.java
src/var/FeatureModelImplGenerator/src/XMLHelper.java
+74
-0
74 additions, 0 deletions
src/var/FeatureModelImplGenerator/src/XMLHelper.java
with
146 additions
and
46 deletions
src/var/FeatureModelImplGenerator/src/SensorConfigSetup.java
+
72
−
46
View file @
ad6afb55
...
...
@@ -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
);
}
c
reate
Directory
(
scAuto
);
c
reate
Directory
(
scOther
);
c
heck
Directory
(
scAuto
Path
);
c
heck
Directory
(
scOther
Path
);
//###############################################
//######### 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
)
{
...
...
This diff is collapsed.
Click to expand it.
src/var/FeatureModelImplGenerator/src/XMLHelper.java
+
74
−
0
View file @
ad6afb55
...
...
@@ -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.
*
...
...
This diff is collapsed.
Click to expand it.
Prev
1
2
3
Next
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment