-5

After some google search, I feel still in the mud.

I am wondering whether what is the best approach for writing wrappers mapping two similar APIs that offer similar semantics but perhaps in different models, syntax or styles.

For example: map Cairo (a graphics library) into Java2D (another graphics library, with a fairly similar semantics model).

PS: Some brainstorming about what it would be nice to have in an integrated tool supporting such a project to map library A into library B.

  • Some formal semantics engine (e.g. K-framework) to define domain semantics (A) and image semantics (B)
  • Some rules-based, configurable parser to perform static code analysis on the domain side to recognize use patters in code calling A and use them to suggest mapped patterns in terms of B
  • A transformation engine (replaces in source code calls to A by equivalent snippets using B, based perhaps on the formal semantics mentioned above

So my question is whether there is some framework, or at least some state-of-the-art approach that would provide with most of this stuff into an API-transformation framework. I have some APIs to port and I would need two and a half lifetimes if I had to do it by hand...

EDIT: after some more research and hints received by others, I'm starting to lose hope on a proven and well-known (within the niche) approach, not even mentioning dedicated tools. On the other hand, I have stumbled upon a bunch of less specific tactiques that might (or might not) work:

  • XML for the semantics definition (there are several formats out there specifically intended to define syntax; probably others can be find to define a little logic constraitns language)
  • XSLT for the transformation (to be implemented from zero); if one could hook in a formal semantics framework for transformation validation, such as the K-Framework, it would be coolest
  • A domain analysis tool to be implemented not from zero, but on the basis of some code analyis API (e.g. the one used by FindBugs or any other such tool)

EDIT2: I wonder why this question is being downvoted with no feedback whatsoever. I wonder how many of those downvoters actually have understood what the problem exposed is really about.

  • 2
    Sounds interesting. But what is your question? – JacquesB Sep 22 '17 at 10:19
  • Point taken. Edited with a more explicit question – AmazingWouldBeGreatBut Sep 22 '17 at 10:21
  • 1
    software recommendations are off-topic per [help/on-topic] – gnat Sep 22 '17 at 10:30
  • `whether there is some framework, `is somewhat asking for libs, tools, etc. Or at least it looks like. I guess you are looking for a sort of pattern or design to automate the abstraction from api A to api B. That would be great and terrible from my payroll point of view :-). But the question is still interesting. – Laiv Sep 22 '17 at 10:44
  • Formal methods and model-driven development are such a small niche that asking for approaches is pretty close to asking for tools, if you know what I mean. The guy having designed the tool will most likely be the guy actually having tried *any* approach. I know this is an exaggeration by me, but it's the kind of reasoning that made me realize that asking this at softwarerecs is pointless. Now, to the matter: I am discovering that acually web-dev people have some stuff going on around RESTful API's refactoring. I will have a look and post here the results. I know zero about web. – AmazingWouldBeGreatBut Sep 22 '17 at 10:49
  • Nah, I haven't digged deep, but my first impression is that RESTful APIs transformation tools obey all of them a very specific semantics. It would be hard or impossible to stretch them enough to accomodate generic APIs - that's what it looks to me at a first glance. – AmazingWouldBeGreatBut Sep 22 '17 at 11:44
  • Adapter Pattern? – Robert Harvey Sep 22 '17 at 14:52
  • @RobertHarvey - probably an adapter pattern will arise somewhere, yes. – AmazingWouldBeGreatBut Sep 22 '17 at 21:00

1 Answers1

0

The problem is similar to decompilation and re-compilation. I would approach it as follows:

  • Decompilation stage: using a parser, create an abstract syntax tree of the calls made to create and modify objects.
  • Recompilation stage: using transformations and techniques similar to optimizing compilers (recognition of common patterns), generate output using the new API.

I would not use anything like xlst as you are struggling with the wrong tool for the job. Use something like ANTLR.

Unless you have a truly enormous code base, you may find that creating this code generation tool takes more time than converting the code.

Frank Hileman
  • 3,922
  • 16
  • 18
  • Thanks for your input. You are right, XSLT is the wrong tool for the job. Today I've seen that I have already great libraries that provide the functionality of parsing and code transformation primitives; for example, Eclipse's JDT for Java. Since Java is my target platform, I think I will go with it. – AmazingWouldBeGreatBut Sep 22 '17 at 20:54
  • One more word: it may be obvious, but it's worth noting that this differs from decompilation/compilation in that it's a non-loss transformation. I mean that the amount of information in the domain and the target is roughly the same, contrarily to what happens when you compile (there are losses and that's obviously why decompilers are so limited). Thanks to this, with good semantics models (that's a hard job though), the transformation may need little or no guessing by the tool. – AmazingWouldBeGreatBut Sep 22 '17 at 20:58
  • And you are right that it's important to keep this tool simple to make it worth the effort. Adding formal semantics would be cool but it might just be too much. Perhaps a simple, per-transformation rule-based configuration to make the tool determine what to transform into what would be enough for the job. – AmazingWouldBeGreatBut Sep 22 '17 at 21:04
  • @AmazingWouldBeGreatBut Right, the formal semantic specification would be similar to the transformation code. If the API's are similar, there won't be much transformations happening. – Frank Hileman Sep 22 '17 at 21:49
  • I have accepted your answer because I have been unable to find any better alternative and I got no other better feedback. Btw, I am not so experienced here, so perhaps you as an experienced user can tell me why there seem some trolling in the background downvoting this question? – AmazingWouldBeGreatBut Sep 23 '17 at 11:20
  • I can't tell you why things are downvoted. I downvote questions that make assumptions that I think harm the industry as a whole, but that is rare. Or very poorly worded questions. But in general, all the questions seem to be often downvoted. So don't take it personally. – Frank Hileman Sep 24 '17 at 17:11
  • Actually it may be some kind of gaming of the system, the downvoting. These types of quantitative rankings always degrade into stupidity. – Frank Hileman Sep 24 '17 at 17:12