A practical example that uses a rule's parent context to define and evaluate an expression.

ID: 998

				
					using uCalcSoftware;

var uc = new uCalc();
// The transformer 't' belongs to the main 'uc' instance.
var t = new uCalc.Transformer(uc);

// Define a variable in the transformer's parent context.
t.uCalc.DefineVariable("VarX = 123");

// This rule is created within 't' and will need to access VarX.
var myRule = t.FromTo("x", "{@Eval: VarX}");

// Get the rule's parent uCalc instance...
var parent_uc = myRule.uCalc;

// ...and use it to evaluate an expression to prove we have the right context.
Console.WriteLine($"Value of VarX in parent context: {parent_uc.Eval("VarX")}");

// Now, run the transformation. The {@Eval} in the rule
// correctly finds 'VarX' in its parent uCalc context.
Console.WriteLine(t.Transform("The value is: x"));
				
			
Value of VarX in parent context: 123
The value is: 123
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   // The transformer 't' belongs to the main 'uc' instance.
   uCalc::Transformer t(uc);

   // Define a variable in the transformer's parent context.
   t.uCalc().DefineVariable("VarX = 123");

   // This rule is created within 't' and will need to access VarX.
   auto myRule = t.FromTo("x", "{@Eval: VarX}");

   // Get the rule's parent uCalc instance...
   auto parent_uc = myRule.uCalc();

   // ...and use it to evaluate an expression to prove we have the right context.
   cout << "Value of VarX in parent context: " << parent_uc.Eval("VarX") << endl;

   // Now, run the transformation. The {@Eval} in the rule
   // correctly finds 'VarX' in its parent uCalc context.
   cout << t.Transform("The value is: x") << endl;
}
				
			
Value of VarX in parent context: 123
The value is: 123
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      '// The transformer 't' belongs to the main 'uc' instance.
      Dim t As New uCalc.Transformer(uc)
      
      '// Define a variable in the transformer's parent context.
      t.uCalc.DefineVariable("VarX = 123")
      
      '// This rule is created within 't' and will need to access VarX.
      Dim myRule = t.FromTo("x", "{@Eval: VarX}")
      
      '// Get the rule's parent uCalc instance...
      Dim parent_uc = myRule.uCalc
      
      '// ...and use it to evaluate an expression to prove we have the right context.
      Console.WriteLine($"Value of VarX in parent context: {parent_uc.Eval("VarX")}")
      
      '// Now, run the transformation. The {@Eval} in the rule
      '// correctly finds 'VarX' in its parent uCalc context.
      Console.WriteLine(t.Transform("The value is: x"))
   End Sub
End Module
				
			
Value of VarX in parent context: 123
The value is: 123