Hello World Object is Saved Automatically using Jetfire

Note: This example can be found in 'JetfireCoreExampleTests' project, which is part of the release download.

The following unit test demonstrates how to create a simple Jetfire workflow class using c# code. In this example the file system is used as permanent storage. A database can also be used.

The Jetfire script is entered as string (jetfireCode) .
 string jetfireCode =
                @"
            namespace test
            {
               workflow HelloWorld
               {
                  DateTime creationTime = DateTime.Now;
                  public string Hello
                  {
                     get{return ""This workflow was created at:"" + creationTime.ToString();}
                  }
               }
            }
            ";

The C# code parses the Jetfire code to create the Jetfire class that is automatically stored in TjFileStorage. Then the workflow class is instantiated to create a Jetfire workflow object(TjWorkflow workflow ). When the 'workflow' object is created it is also automatically stored. The C# code then shows how to get the Jetfire property object (Hello ) and set the property's value. When the property is set the workflow object is updated and saved to the persistent storage.

The persisted objects and classes can be examined directly in the file system (value of 'JetfireTest.CDRIVE_JETFIRETEST').

It should be noted that Jetfire intrinsic objects, usually on a one to one basis, wrap .net intrinsic objects with the exception of string. The Jetfire string class is represented by the .net object 'TjString' which is a collection of .net strings indexed by language. The default language is English.
Note: This code can be found in 'JetfireCoreExampleTests' project, which is part of the release download.
[TestClass]
    public class HelloWorldTest
    {
        /// <summary>
        /// This is simple 'HelloWorld' workflow class.
        /// Since Jetfire objects (workflows) are automatically persistent, 
        /// once an "HelloWorld" workflow is instantiated, it will remember 
        /// its creation time.  The workflow will continue to exist until it
        /// is explicitly deleted or the server database is erased.
        /// 
        /// Once a 'HelloWorld' workflow is created it is also allowed to exist 
        /// in multiple client nexus's.
        /// </summary>
        [TestMethod]
        public void HelloWorld()
        {
            string jetfireCode =
                @"
            namespace test
            {
               workflow HelloWorld
               {
                  DateTime creationTime = DateTime.Now;
                  public string Hello
                  {
                     get{return ""This workflow was created at:"" + creationTime.ToString();}
                  }
               }
            }
            ";
            LinqStartObjects start = JetfireTest.NewNexusServer(
                JetfireTest.CDRIVE_JETFIRETEST,     // directory name - where the objects are stored
                TjServerStorageType.FileLinq, 
                TjStorageCommand.ClearStorage       // deletes any existing files in the directory
                );
            TjNexus nexus = start.ClientNexus;
            //
            //  Parse source code and create the Jetfire executable code expressions, 
            //  get the workflow class 'HelloWorld',
            //  then instantiate a new workflow object.
            //
            nexus.ParseServer(jetfireCode);
            TjWorkflowClass workflowClass = nexus.FindClass<TjWorkflowClass>("test", "HelloWorld");
            TjWorkflow helloWorld = workflowClass.New<TjWorkflow>();

            //
            // Get the property 'Hello', then check the value
            //
            string creationTime = helloWorld.Get<string>("Hello");
            Assert.IsTrue(creationTime.StartsWith("This workflow was created at:"));
            //
            //  Shut down the client nexus, but first save
            //  the workflow GUID so the workflow can be found in the new nexus.
            //
            TjGuid workflowGuid = helloWorld.JetfireGuid;
            JetfireTest.AssertNoIncidents(nexus);
            start.Transport.Dispose();
            
            //
            //   Create a new client nexus - This will restore old objects.
            //
            LinqStartObjects start2 = JetfireTest.NewNexusServer(
                JetfireTest.CDRIVE_JETFIRETEST, 
                TjServerStorageType.FileLinq, 
                TjStorageCommand.ReUseExisingStorage
                );
            TjNexus nexus2 = start2.ClientNexus;
            //
            // no need to parse and generate code - that has already been done.
            // Just get the workflow.
            //
            TjWorkflow helloWorld2 = nexus2.Cache.GetUnit<TjWorkflow>(workflowGuid);

            string creationTime2 = helloWorld2.Get<string>("Hello");
            //
            //  Check that the workflow creation time is the same.
            //  This is good proof that it is the same workflow.
            //
            Assert.AreEqual(creationTime, creationTime2);
            Assert.AreEqual(workflowGuid, helloWorld2.JetfireGuid);
            
            JetfireTest.AssertNoIncidents(nexus2);
            start2.Transport.Dispose();
        }
    }
}

Also see Hello World blog article

Last edited Jan 5, 2011 at 3:39 PM by JohnHansen, version 24

Comments

No comments yet.