6
$\begingroup$

I've been writing a conversion 'script' between two robot description formats and thought I would try out Mathematica as I thought it sounded pretty awesome. Although by this point I should have done it manually!

I'm sure there's a much better way to do the entire mapping process. I had trouble trying to apply the mappings from the elements between the joints and the links. I had tried using variations on Rule @@ ConnectionsOrder, with various operators /., //, @@, @@@ /@

  1. Is there a more idiomatic implementation of the below?

Since Mathematica is symbolically based, I thought something may exists where you can define a format, and then specifying another and some rules to convert and having everything work nicely. Does such a thing exist? - I have been text wrangling to basically manually convert the files.

jointsOrder = {"HAA", "HFE", "KFE", "WHEEL"};
linksOrder = {"BASE", "HIP", "THIGH", "shank_fixed", "WHEEL_L"};
connectionsOrder = Riffle[linksOrder, jointsOrder];
connections = 
 BlockMap[Apply[Rule], Flatten[Subsequences[connectionsOrder, {2}]], 
  2]
$\endgroup$
1
  • $\begingroup$ Thread[LinksOrder[[1 ;; Length[JointsOrder]]] -> JointsOrder] $\endgroup$
    – Rupesh
    Commented Mar 18, 2020 at 22:56

2 Answers 2

5
$\begingroup$

BlockMap can do more of the work than you are allowing it to:

BlockMap[Apply[Rule], connectionsOrder, 2, 1]
{"BASE" -> "HAA", "HAA" -> "HIP", "HIP" -> "HFE", "HFE" -> "THIGH", 
 "THIGH" -> "KFE", "KFE" -> "shank_fixed", "shank_fixed" -> "WHEEL", 
 "WHEEL" -> "WHEEL_L"}
$\endgroup$
2
  • $\begingroup$ For the second part of my question do you know of / point me to a tutorial/manpage of a general method for converting between structured formats? $\endgroup$
    – ixil
    Commented Mar 19, 2020 at 15:47
  • $\begingroup$ @ixil There is Manipulating Lists and Expressions. Browsing top questions in list-manipulation and expression-manipulation would be useful too. $\endgroup$
    – Mr.Wizard
    Commented Mar 19, 2020 at 17:19
6
$\begingroup$
Partition[connectionsOrder, 2, 1, {1, -1}, {}, Rule]
{"BASE" -> "HAA", "HAA" -> "HIP", "HIP" -> "HFE", "HFE" -> "THIGH",  
 "THIGH" -> "KFE", "KFE" -> "shank_fixed", "shank_fixed" -> "WHEEL",    
"WHEEL" -> "WHEEL_L"}

or

Rule @@@ Partition[connectionsOrder, 2, 1]
{"BASE" -> "HAA", "HAA" -> "HIP", "HIP" -> "HFE", "HFE" -> "THIGH",  
 "THIGH" -> "KFE", "KFE" -> "shank_fixed", "shank_fixed" -> "WHEEL",    
"WHEEL" -> "WHEEL_L"}

Also

EdgeRules @ PathGraph @ connectionsOrder
{"BASE" -> "HAA", "HAA" -> "HIP", "HIP" -> "HFE", "HFE" -> "THIGH",   
 "THIGH" -> "KFE", "KFE" -> "shank_fixed", "shank_fixed" -> "WHEEL",   
 "WHEEL" -> "WHEEL_L"}
$\endgroup$
10
  • 1
    $\begingroup$ I notice you are omitting the quote markdown in the output formatting. Now that the block style is not the "ugly yellow" I think you should consider using it, as the syntax highlighting is useful IMHO. $\endgroup$
    – Mr.Wizard
    Commented Mar 18, 2020 at 23:54
  • $\begingroup$ Thank you @Mr.Wizard; very good suggestion. $\endgroup$
    – kglr
    Commented Mar 19, 2020 at 0:04
  • $\begingroup$ I see you figured out I meant "code markdown" -- it's the quote block that is no longer yellow. Anyway the form you have now seems like a good one and an alternative to the one I use. Do you prefer its appearance? $\endgroup$
    – Mr.Wizard
    Commented Mar 19, 2020 at 0:12
  • $\begingroup$ @Mr.Wizard, yours look better. Is there a way to do the line wrapping (I had to break the code into lines manually)? $\endgroup$
    – kglr
    Commented Mar 19, 2020 at 0:24
  • $\begingroup$ Do you mean basic line wrapping, or breaks at specific points? This usually handles the former, but I do tend to manually edit breaks when it looks better. $\endgroup$
    – Mr.Wizard
    Commented Mar 19, 2020 at 4:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.