6

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]
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
ixil
  • 63
  • 4

2 Answers2

6
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"}
kglr
  • 394,356
  • 18
  • 477
  • 896
  • 1
    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. – Mr.Wizard Mar 18 '20 at 23:54
  • Thank you @Mr.Wizard; very good suggestion. – kglr Mar 19 '20 at 00:04
  • 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? – Mr.Wizard Mar 19 '20 at 00:12
  • @Mr.Wizard, yours look better. Is there a way to do the line wrapping (I had to break the code into lines manually)? – kglr Mar 19 '20 at 00:24
  • 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. – Mr.Wizard Mar 19 '20 at 04:34
  • I meant to get from code in block quote (at the end of my post) to the form just above it . – kglr Mar 19 '20 at 05:04
  • I just paste, select the output, then click the { } and " editor buttons in that order. If that's not what you mean, sorry, I must be too tired to understand tonight. Try tomorrow. :-) – Mr.Wizard Mar 19 '20 at 05:33
  • thank you... that's what i meant. – kglr Mar 19 '20 at 05:41
  • I accepted @Mr.Wizard's as it's the most succint. But thanks for pointing out these other functions. I feel there are so many ways to 'skin a cat' in mathematica - that's half the problem! – ixil Mar 19 '20 at 15:36
  • @ixil, that would be my choice too :) – kglr Mar 19 '20 at 16:11
5

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"}
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371