Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I would start with modeling the interesting operations. Ultimately you will connect them to a UI, but that is the steering wheel and gas pedal, not the engine.</p> <p>What you are attempting to build has a lot in common with programming languages: variables, values, types, expressions, evaluation, etc. Many of the metaphors are applicable and might provide some guidance.</p> <p>If you are using .NET 3.5, you have the option of <a href="http://msdn.microsoft.com/en-us/library/bb397951.aspx" rel="nofollow noreferrer">Expression Trees</a>, which allow you to represent and compile code expressions at runtime.</p> <p>For example, to model your first goal:</p> <pre><code>using System.Linq.Expressions; ConstantExpression theNumber2 = Expression.Constant(2); ConstantExpression theNumber3 = Expression.Constant(3); BinaryExpression add2And3 = Expression.Add(theNumber2, theNumber3); </code></pre> <p>To invoke the expression, we need to wrap <code>add2And3</code> with a method. This is done with a lambda expression:</p> <pre><code>Expression&lt;Func&lt;int&gt;&gt; add2And3Lambda = Expression.Lambda&lt;Func&lt;int&gt;&gt;(add2And3); </code></pre> <p><code>Func&lt;int&gt;</code> represents a method which takes no parameters and returns an <code>int</code>. In C#, the code represented by <code>add2And3Lambda</code> would be:</p> <pre><code>() =&gt; 2 + 3 </code></pre> <p>So what we have is an expression tree whose root is a method. Because a method is <em>callable</em>, we can compile the tree into an instance of the underlying delegate type:</p> <pre><code>Func&lt;int&gt; add2And3Func = add2And3Lambda.Compile(); </code></pre> <p>Now we can invoke the code we built:</p> <pre><code>int theNumber5 = add2And3Func(); </code></pre> <p>Every expression available to .NET languages is supported.</p> <p>Imagine every node in your graph has an <code>Expression</code> associated with it. That might give you an idea of the power of expression trees and how they could help you with this task.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload