9
Dynamic Instantiation of Occurrence Based Architecture Or Creating an array of Occurrences Introduction: <A href="http://zone.ni.com/reference/en-XX/help/371361B- 01/glang/occurrence_functions/" target=_blank>Occurrences </A>are an elegant way of implementing a Boolean event based architecture. Building an array of Occurrence references is not straightforward. Solution: Dynamically instantiate re-entrant sub-VI’s that create the Occurrence references. Discussion: An example of Occurrence Based Architecture (OBA) that recently came to mind after I viewed a copy of one of the <A href="http://www.animusic.com/dvd-info-clips-1.html" target=_blank>Animusic videos </A>. In this video a set of synchronized machines work together to produce music. My buddies turned to me and said, “We could build one of those… Couldn’t we?” OBA.jpg Latter my mind wondered to the question of how would I architect such an application. An OBA came to mind were I could use a single control VI to distribute Occurrences to a set of VI’s that would control the associated instrument and note. The control VI would read the “sheet music” and distribute “triggers” or Occurrences to the VI’s that implement each instruments note.

Dynamic Instantiation of Occurrence Based Architecture Or ... · Dynamic Instantiation of Occurrence Based Architecture . Or . Creating an array of Occurrences . ... a new GO at the

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Dynamic Instantiation of Occurrence Based Architecture Or ... · Dynamic Instantiation of Occurrence Based Architecture . Or . Creating an array of Occurrences . ... a new GO at the

Dynamic Instantiation of Occurrence Based Architecture Or Creating an array of Occurrences Introduction: <A href="http://zone.ni.com/reference/en-XX/help/371361B-01/glang/occurrence_functions/" target=_blank>Occurrences </A>are an elegant way of implementing a Boolean event based architecture. Building an array of Occurrence references is not straightforward. Solution: Dynamically instantiate re-entrant sub-VI’s that create the Occurrence references. Discussion: An example of Occurrence Based Architecture (OBA) that recently came to mind after I viewed a copy of one of the <A href="http://www.animusic.com/dvd-info-clips-1.html" target=_blank>Animusic videos </A>. In this video a set of synchronized machines work together to produce music. My buddies turned to me and said, “We could build one of those… Couldn’t we?”

OBA.jpg Latter my mind wondered to the question of how would I architect such an application. An OBA came to mind were I could use a single control VI to distribute Occurrences to a set of VI’s that would control the associated instrument and note. The control VI would read the “sheet music” and distribute “triggers” or Occurrences to the VI’s that implement each instruments note.

Page 2: Dynamic Instantiation of Occurrence Based Architecture Or ... · Dynamic Instantiation of Occurrence Based Architecture . Or . Creating an array of Occurrences . ... a new GO at the

Occurence_Source.jpg Occurrence Source.jpg illustrates the arbitrary distribution of triggers from a single control loop. After a fixed size array of Occurrences is built, a set of “Instrument-Note-Controllers”(Occurrence_Sink.vit) are created to by the For Loop. In order to keep this example simple, the note “intensity” is illustrated by Boolean color intensity. The while loop fires the Occurrences based on random indexing of the Occurrence Reference.

Page 3: Dynamic Instantiation of Occurrence Based Architecture Or ... · Dynamic Instantiation of Occurrence Based Architecture . Or . Creating an array of Occurrences . ... a new GO at the

Running.jmp When attempting to implement this type of architecture we run into a bit of a complication when you attempt to realize it in such a way as it can be of arbitrary size. The arbitrary size enters into my LVAnimusic exercise when I want to implement a different number of instruments and notes for different songs. The complication with OBA of arbitrary size becomes apparent when we look at the value returned by the Generate Occurrence node (GO).

Page 4: Dynamic Instantiation of Occurrence Based Architecture Or ... · Dynamic Instantiation of Occurrence Based Architecture . Or . Creating an array of Occurrences . ... a new GO at the

Occurence_Source_mod1.jpg The GO will generate AN occurrence. It will not generate a new Occurrence each time it executes. If you drop a GO in a For Loop and index of the output tunnel you will get an array of identical Occurrences.

Page 5: Dynamic Instantiation of Occurrence Based Architecture Or ... · Dynamic Instantiation of Occurrence Based Architecture . Or . Creating an array of Occurrences . ... a new GO at the

Occurence_Source_mod1_FP.jpg Since all of the “Instruments” (Occurence_Sink.vit) will be looking at the same Occurrence we will only be capable of very dissonant music.

Page 6: Dynamic Instantiation of Occurrence Based Architecture Or ... · Dynamic Instantiation of Occurrence Based Architecture . Or . Creating an array of Occurrences . ... a new GO at the

Occurence_Source.jpg As shown in “Occurence_Source.jpg” it is possible to create an array of unique Occurrence using a unique GO for each, but how to get an arbitrary number? By using LabVIEW’s <A href=" http://zone.ni.com/reference/en-XX/help/371361B-01/lvconcepts/capabilities_of_the_vi_server/" target=_blank> VI server </A> and opening multiple copies of a non-re-entrant sub-VI, we can generate an array of unique Occurrences. This is illustrated in “Occurence_Source_mod4.jpg’.

Page 7: Dynamic Instantiation of Occurrence Based Architecture Or ... · Dynamic Instantiation of Occurrence Based Architecture . Or . Creating an array of Occurrences . ... a new GO at the

Occurence_Source_mod4.jpg This approach works because LabVIEW will load a unique “copy” of a VI when the VI is marked as non-re-entrant. Each time the sub-VI loaded, a new copy of the GO is loaded into memory. When we open a new reference to the non-re-entrant sub-VI we are loading a new GO at the same time we create the reference to the VI. When we pass unique VI references to the “Call by Reference Node” we are executing DIFFERENT copies of the GO each time. There is one last point that I would be negligent if I did not mention. It is the “good little assistant” nature of LV can get carried away if we do not take into consideration the lifetime of the resources of a VI. The resources that affect the illustrated OBA are the Occurrence itself. The Occurrence is valid only as long as the VI that holds the GO is active. If we closed the reference to the sub-VI’s used to create the Occurrence references in the For Loop where they are created, the Occurrences would go invalid before they were ever used. To prevent the Occurrences from going invalid, all of the VI references are closed only when the application no longer needs the Occurrences.

Page 8: Dynamic Instantiation of Occurrence Based Architecture Or ... · Dynamic Instantiation of Occurrence Based Architecture . Or . Creating an array of Occurrences . ... a new GO at the

Summary: OBA – can be used to distribute an arbitrary number events GO – will only return a single Occurrence reference regardless of how many times it executes VI Server-Non-Re-Entrant VI – Open will load a new copy of the VI’s code and resources. VI Server Close – will remove all resources allocated when the VI was opened. Closing: A copy of all of the code used in this Nugget can be found in the attached zip as LV 7.1 I thank you for reading, invite your comments and hope that you and LabVIEW will be beautiful music together. If you have a Nugget you would like to present please post <A href=" http://forums.ni.com/ni/board/message?board.id=BreakPoint&message.id=3194&jump=true" target=_blank> here </A> to reserve your week! A list of all Nuggets can be found <A href=" http://forums.ni.com/ni/board/message?board.id=BreakPoint&message.id=1669&jump=true " target=_blank> here </A> . Ben

Page 9: Dynamic Instantiation of Occurrence Based Architecture Or ... · Dynamic Instantiation of Occurrence Based Architecture . Or . Creating an array of Occurrences . ... a new GO at the

Comments on Occurrence Nugget “Very nice, Ben” Yair Neeman, LabVIEW Champion, Isreal “Good job Ben! Interesting problem, elegant solution, lucid explanation. Thank you!” Matthew Williams, Principal Scientist, Mack Information Systems “Great post! I love the tie-in to generating musical notes.” Jarod Slocum. Application Engineer National Instruments “Nice nugget, Ben!” Tomi Maila, LabVIEW Champion, Finland “Great post Ben!” Steve Briggs Proven Active Veteran on NI Forums “This is pretty cool… thanks for the nugget Ben!” Jeff B NI LabVIEW R&D “Congratulations Ben on this Nugget (or Boulder? )! Very interesting subject indeed...” Ray Robichard LabVIEW Champion, Canada “Holy BEEP batman that just BEEP BEEP BEEP! I think I felt my brain explode when this boulder rolled over me. I think I might quit programming. You's talk of such high level subject matter like most normal people talk about the type of coffee they like. Now if you'll excuse me I'm going to drink till I puke. Ben, I have earmarked this "boulder" so when I grow up I can understand it! But I think it's going to be great. Great work! ” Chris Co, Proven Veteran NI-Forums “That was a beauty ben.Nice explanation for event based structure.is really useful” rpr Proven Regular on NI-Forums “serioulsly ben u r great. Apart from the the actual solution your explanation was very helpful.Thanx ben” muks Proven Active Veteran NI-Forums