This project is read-only.

DVD Library Example

The Jetfire code is the first box below. At first glance it looks like C#; however it is actually Jetfire code. The Jetfire code only needs to be parsed once. Once parsed it is permanately in the server storage.

In this example a physical movie DVD collection is represented by a Jetfire workflow (class). Each physical DVD becomes an Jetfire object or an instantiation of the DVD workflow. The workflow has 2 commands, 'Borrow' and 'Return', represented by 2 methods. The status of the physical DVD is represented by 2 states, either 'CheckedIn' or 'Borrowed'.

State Method Access Modifier
CheckedIn Borrow public
Return private
Borrowed Borrow private
Return public


When the DVD is in the 'CheckedIn' state it is possible to borrow the DVD, so the 'Borrow' method is public. Borrowing the DVD by executing the 'Borrow' method causes the DVD workflow to enter the 'Borrowed' state (leaving the 'CheckedIn' state). When in the 'Borrowed' state it is not possible to execute the 'Borrow' method, but now the 'Return' method is public.

Note: This code can be found in 'JetfireCoreExampleTests' project, which is part of the release download.
// Code Samples
        # region DVD code class
        string code =
           @"namespace DVDLib 
           { 
              public workflow DVD 
              { 
                 // CheckedIn State
                 // - it looks like a constructor
                 // Instances of this workflow class with be in one of 
                 // the defined states ('Checkedin' or 'Borrowed').
                 CheckedIn()   
                 {
                    // place code here to executed 
                    // when the workflow enters the 'CheckedIn' state.
                 }
                 Borrowed() 
                 {
                    // place code here to be executed when the workflow 
                    // enters the 'Borrowed' state.
                 } 
                 
                 // Construtor - that can be used to set the subject 
                 // when the DVD is instantiated
                 // Parameters are used to pass information into the constructor
                 public DVD(string subject)
                 {
                    this.Subject = subject;
                    if (subject.StartsWith(""Sweeney""))
                       this.favs = true;
                    enterstate CheckedIn();
                 }
                 // Borrow Method  
                 // This method is only public when this workflow 
                 // in the 'Borrowed' state.
                 // When this workflow is not in the 'Borrowed' state the 
                 // 'Borrow' method is private.
                 public void Borrow() : states(CheckedIn)
                 { 
                     enterstate Borrowed();
                 }
                 public void Return() : states(Borrowed)
                 {
                     enterstate CheckedIn();
                 }
                 bool favs = false;
                 public bool MyFavs
                 {
                    get { return this.favs;      }
                    set { this.favs = value;   }
                 }
              }
           }";
        # endregion


To use this code, create a Test project in the Jetfire solution and paste this code in a new C# file - call it DVDLibTest.cs. The example below uses a helper class called JetfireTest.

#region Change State from CheckedIn to Borrowed by calling a method
        /// <summary>
        /// Change State from CheckedIn to Borrowed by calling a method.
        /// </summary>
        [TestMethod]
        public void DVDStatesAndMethods()
        {
            // Create the the File Storage
            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;
            // Add the Jetfire code to the nexus
            TjParser parser = nexus.ParseServer(this.code);
           
            TjWorkflowClass wClass = nexus.FindClass<TjWorkflowClass>("DVDLib", "DVD");
            //
            // Create a workflow - each workflow represents a DVD
            //
            TjParameter[] parameters = TjParameter.ConvertToArray(nexus, "Gone with the Wind" );
            TjWorkflow dvd = wClass.NewWorkflow(parameters);
            Assert.AreEqual("Gone with the Wind", dvd.Get<string>("Subject"));
            //
            // Verify that the state is CheckedIn
            //
            Assert.AreEqual("CheckedIn", dvd.CurrentState.Name);
            //
            // Change the state to Borrowed by executing the "Borrow" method.
            //
            Assert.AreEqual("Public", dvd.DynamicAccessModifier("Borrow").ToString());
            dvd.Execute("Borrow"); // execute the borrow method.
            // Verify that the state changed to Borrowed
            Assert.AreEqual("Borrowed", dvd.CurrentState.Name);
            //
            //  Execute the borrow command again.
            //
            Assert.AreEqual("Private", dvd.DynamicAccessModifier("Borrow").ToString());
            JetfireTest.AssertNoIncidents(nexus);
            try
            {
                // an exception will occur because the 'Borrow' method is now private.
                dvd.Execute("Borrow"); 
                Assert.Fail();
            }
            catch
            {
                TjAuditIncident[] incidentArray = nexus.ToIncidentArray();
                Assert.IsTrue(incidentArray.Length > 0);
            }
            
            start.Transport.Dispose();
        }
        #endregion

Last edited Feb 23, 2009 at 10:03 PM by JohnHansen, version 37

Comments

No comments yet.