tags:

views:

80

answers:

3

A project I'm working on involves 3 distinct systems/platforms. C#, Java, and XSLT. I have some simple algorithms (just a bunch of conditionals), expressed in pseudo-code as something like:

 if inputParameter1 is equal to 1
    return "one"
 else if inputParameter2 is equal to 5
    return "five" concatenated with inputParameter1
 else
    return "not found"

simple stuff like that.

I'm trying to figure out a mechanism that will:

  1. Let me write the algorithm once
  2. Be able to execute the algorithm in the native language of each system (C#, Java, and XSL)
  3. Have each system (C#, Java, and XSL) always use the latest version of the algorithm when the algorithm is updated.

So to elaborate on my example, the C# representation would be:

    public string TheMethod(int inputParameter1, int inputParameter2)
    {
       if (inputParameter1 == 1)
       {
          return "one";
       }
       else if (inputParameter2 == 5)
       {
          return string.Concat("five", inputParameter1.ToString());
       }
       else
       {
          return "not found";
       }
    }

and the XSLT representation would be:

<xsl:template name="TheMethod">
  <xsl:param name="inputParameter1" />
  <xsl:param name="inputParameter2" />
  <xsl:choose>
    <xsl:when test="$inputParameter1 = 1">
      <xsl:text>one</xsl:text>
    </xsl:when>
    <xsl:otherwise>
      <xsl:choose>
        <xsl:when test="$inputParameter2 = 5">
          <xsl:text>five</xsl:text>
          <xsl:value-of select="$inputParameter1" />
        </xsl:when>
        <xsl:otherwise>
          <xsl:text>Not Found</xsl:text>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

hopefully you get the idea.

How would I express an algorithm in a generic way and be able to automatically convert it to C#, Java, or XSL?

Thanks!

-Mike

+4  A: 

Well, the "answer" to this is a DSL, or just some common markup that you then render (amusingly, you could do this with XSLT).

But generally, IMHO, implementing this isn't worth the trouble, depending on how complicated your algorithm is and how many of them you'll be writing.

Noon Silk
+1 for mentioning you could implement it in XSLT.
Matt H
Though not quite the solution I hoped for, this is the best answer so far
Mike
A: 

This does not produce a source code, but it make possible to use the logic in various languages with very little coding:

Write a simple xml like this:

...
<case>
   <InputParam1>1</InputParam>
   <InputParam2>NULL</InputParam>
   <answer>one</answer>
</case>
...

Then parse it, and store it in a dictionary/map/whatever-the-language-or-framework-has-for-hashtables. So, store the inputparams as the key (perhaps, as a sturct), and you get the answer very quickly. For the cases when the parameters itselves are used in the return value (like in your example in the "concat part"), I'd use some special syntax for the data like

...
<answer>five$inputparam1$</answer>
...

It is worth mentioning that the more special cases you have, like this concatenation, the less useful this solution can be.

Tamás Szelei
+1  A: 

Looking at this problem a little more generally, your aim is to have only one authoritative version of the algorithm (i.e. the "Don't Repeat Yourself" principle).

Instead of trying to automatically translate/export to different programming languages, a simpler solution would be to choose one language (probably XSL) to implement the algorithm. Then in C# and Java just use some XSL tools to execute the algorithm directly, passing in whatever parameters you like. I haven't done this before, but I assume it is possible with the right third-party tools (whereas I doubt you could do it the other way around, executing Java or C# from within XSL, which is why XSL is the best choice for the "base" language).

Todd Owen
Yes, DRY is exactly the point. And yes, C# can run XSL (which is what I'm doing right now) but it's much, much slower than a native C# algorithm.
Mike
How are you running it from C#? Is it pre-compiled? If not, check out these links to XSLT compilers for .NET (http://msdn.microsoft.com/en-us/library/bb399405.aspx) and Java (http://xml.apache.org/xalan-j/xsltc_usage.html).
Todd Owen
Something precomplied (like a C# 3.0 expression) would be ideal, but I'm open to whatever works.
Mike