Lab VIEW Object Oriented Programming LVOOP Introduction of
Lab. VIEW Object Oriented Programming (LVOOP) Introduction of the HGF Base Class Library (Helmholtz, GSI, FAIR) Holger Brand 11/29/2020 H. Brand@gsi. de 1
Contents • Introduction to LVOOP – – – – What is Lab. VIEW? Why Object Oriented Programming? Why should use LVOOP? Differences to conventional OO languages Some remarks on constructors, destructors and dataflow VI & Object Memory Simple classes, inheritance, examples • LVOOP Design Patterns – – – Focus of HGF Base Class Library Factory & HGF_Base Reference & Object Manager Functional Global Variable & Visitor Events & Thread Pool • Network Communication 11/29/2020 H. Brand@gsi. de 2
What is Lab. VIEW? • Lab. VIEW is a development environment – For development of Measurement- , Automation-, Test- and Control Systems. – It includes an advanced math and analysis library. – Provides interfaces to DAQ, many field busses, network communication, Active. X, . Net, DLL, etc. • Lab. VIEW is a graphical programming language – – Inherent multithreaded with easy to use synchronization methods VI: Virtual Instrument consists of frontpanel and blockdiagram sub. VI: VI to be used as function Express VI: configurable sub. VI • Lab. VIEW Modules and Add-Ons extend the base functionality • Refer to http: //www. ni. com/labview 11/29/2020 H. Brand@gsi. de 3
Lab. VIEW 8. 5 11/29/2020 H. Brand@gsi. de 4
Dataflow Paradigm • Lab. VIEW is a data flow language. • Data flows from data source to sink. • It does not normally have variables. – Wires are not variables. – Front panel controls are not variables. – Even local or global variables are not scoped or allocated the way variables are in other languages. • Variables are part of a model that conflicts with data flow. – Without variables and a scope defining the lifetime of those variables, construction and destruction are meaningless concepts, and therefore they are left out of the language design. Therefore: Lab. VIEW forbid Lab. VIEW classes in the interface of LVbuilt DLLs (shared libraries). 11/29/2020 H. Brand@gsi. de 5
Scope and Live time • • What is the scope of the C++ integer? It exists from the point it is declared until the closing brace. What is the scope of the Lab. VIEW integer? Unknown. – – – • Until the wire ends? Until the VI stops executing? Until probe is closed? Until front panel closes? … Lab. VIEW does not have “space scope”. Lab. VIEW has “temporal scope”. – – A piece of data exists as long as it is needed. If it is copied into front panel control, it stays there, even after execution finishes. Copies on the wires exist until the next execution of that wire. Another copy will be made for any probe on the wire. – In a pure theoretical data flow language, there would be a separate copy on every individual wire, because every wire is an independent computation unit. Of course, that would be inefficient to actually implement, so Lab. VIEW's compiler optimizes the number of copies. But the principle is the same: data lives for a long time, sometimes outliving the program that generated that data. 11/29/2020 H. Brand@gsi. de 6
Why Object Oriented Programming? • Classes – – – Attribute data encapsulation Methods operating on attribute data Access scope (Multiple-) Inheritance Recipe, how to create objects • Advantages – Well defined public interface – Well define responsibilities (class and developer) – Easier to debug – Better scalability – Better maintenance using namespace std; class CRectangle { private: int x, y; public: void set_values (int, int); int area () {return (x*y); } }; void CRectangle: : set_values (int a, int b) { x = a; y = b; } – Each object can be viewed as an independent little machine or actor with a distinct role or responsibility (object by reference). – Leads to better software design and architecture (hopefully!) 11/29/2020 // C++ class example #include <iostream> H. Brand@gsi. de int main () { CRectangle rect; rect. set_values (3, 4); cout << "area: " << rect. area(); return 0; } 7
Differences between C++ and LVOOP C++ Lab. VIEW text-based, functional language graphical, dataflow language Not required ultimate ancestor class constructors no need destructors no need by reference and by value syntax only, no reference no, class must provide mutation code for data versioning, automatic data mutation templates Lab. VIEW 8. 2 and later does not pure virtual functions Lab. VIEW 8. 2 and later does not multiple inheritance Lab. VIEW 8. 2 and later does not OO for LV means strict encapsulation, simple inheritance and dynamic dispatching (virtual methods) Refer to http: //zone. ni. com/devzone/cda/tut/p/id/3574 11/29/2020 H. Brand@gsi. de 8
Some remarks • LVOOP Naming – A Class is • • – Access scope: public, protected and private VIs • – • No overloading! All dynamic VI’s with the same name in one inheritance line must have the same connector pane. No friends in this version Default constructor only – – • Lab. VIEW Object is THE ultimate ancestor class No multiple inheritance Static member VI’s can not be overloaded Dynamic dispatch and overwrite VI’s (C++: virtual functions) • – Class attribute is always private! Inheritance: Parent (ancestor) and child class • • – – Cluster of private data (C: struct) and Member VI’s operating on that data Default class attribute values in case of frontpanel control or blockdiagram constant Current attribute data from caller No destructor LVOOP FAQ: http: //zone. ni. com/devzone/cda/tut/p/id/3573 11/29/2020 H. Brand@gsi. de 9
VI & Object Memory • Lab. VIEW allocates a “dataspace” for all the data needed to exectute a VI. • Therefore it is thread safe. • There is no need for mutex locking. • A LV class attribute is a cluster of clusters. • Lab. VIEW Object has an empty cluster that will be filled with child class attribute clusters. • The VI data space has a pointer to object info which refers to • Object data and • Class information 11/29/2020 H. Brand@gsi. de 10
A Simple Class A simple example VI demonstrates: A LV object is just encapsulated structured data. initialize Default object Clone object. constant. 11/29/2020 H. Brand@gsi. de 11
Contents • Introduction to LVOOP – – – – What is Lab. VIEW? Why Object Oriented Programming? Why should use LVOOP? Differences to conventional OO languages Some remarks on constructors, destructors and dataflow VI & Object Memory Simple classes, inheritance, examples • LVOOP Design Patterns – – – Focus of HGF Base Class Library Factory & HGF_Base Reference & Object Manager Functional Global Variable & Visitor Events & Thread Pool • Network Communication 11/29/2020 H. Brand@gsi. de 12
LVOOP Design Patterns http: //en. wikipedia. org/wiki/Design_pattern_%28 computer_science%29 • In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design. • A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. • Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved. • Algorithms are not thought of as design patterns, since they solve computational problems rather than design problems. • Since Lab. VIEW objects follow the dataflow paradigm, many design pattern must be reinvented with respect to dataflow. 11/29/2020 H. Brand@gsi. de 13
Focus of HGF Base Class Library • How to deal with Lab. VIEW objects as entities? – Factory • Named and initialized objects • Constructor and destructor • How to prevent unintended copying of objects at wire forks? – Reference, Singleton – Object Manager – Functional Global Variable & Visitor • How to deal with Agents? – Separation of passive object data and active threads –> Thread Pool – Event handling – Behaviour -> State Chart • How to overcome some technical restrictions? – – 11/29/2020 Missing class information -> vi. libUtilityLVClass Missing overloading -> Variant Missing Multiple Inheritance -> Aggregation Missing Interfaces -> Delegation H. Brand@gsi. de 14
HGF Class Hierarchy 11/29/2020 Blue: HGF Base Class library Red: Example classes H. Brand@gsi. de 15
Contents • LVOOP Design Patterns – – – Focus of HGF Base Class Library Factory & HGF_Base Reference & Object Manager Functional Global Variable & Visitor Events & Thread Pool • Network Communication 11/29/2020 H. Brand@gsi. de 16
HGF_Factory & HGF_Base How to create initialized, named objects? (Lab. VIEW provides objects with default attribute values only!) • HGF_Factory is a base class for Entities – It creates initialized objects that remember their class and object names and whether it was created and initialized by a Factory. – It defines two dynamic dispatch VIs (virtual methods) • classes – VI that returns a default object of a desired class. – To be overwritten by child factories. • initialize – VI that uses variant attributes to initialize the default object. – To be overwritten by child classes. • HGF_Base is the base class – Overwrites the initialize method. – More common attributes could be added 11/29/2020 H. Brand@gsi. de 17
11/29/2020 H. Brand@gsi. de 18
HGF_Factory Implementation 11/29/2020 H. Brand@gsi. de 19
Contents • LVOOP Design Patterns – – – Focus of HGF Base Class Library HGF_Factory & HGF_Base Reference & Object Manager Functional Global Variable & Visitor Events & Thread Pool • Network Communication 11/29/2020 H. Brand@gsi. de 20
HGF_Reference How to create References to objects? • Reference object should follow dataflow paradigm! (That means such reference objects become cloned at wire forks. ) – Reference? → Idea → Queue! • Single element size queue – Queues can be referenced in LV – Size=1 → Object is in the queue or not → Mutual exclusion 11/29/2020 H. Brand@gsi. de 21
HGF_Reference Example Reference object becomes cloned at wire fork, but not the object itself! save public sub. VI 11/29/2020 H. Brand@gsi. de 22
HGF_Singleton 11/29/2020 H. Brand@gsi. de 23
HGF_Object. Manager FGV with object info 11/29/2020 H. Brand@gsi. de 24
HGF_Object. Manager Example delete new Reference by name 11/29/2020 H. Brand@gsi. de 25
Contents • LVOOP Design Patterns – – – Focus of HGF Base Class Library Factory & HGF_Base Reference & Object Manager Functional Global Variable & Visitor Events & Thread Pool • Network Communication 11/29/2020 H. Brand@gsi. de 26
HGF_FGV and its Visitors • An object oriented Functional Global Variable • No direct access to objects • Only Visitors are accepted 11/29/2020 H. Brand@gsi. de 27
HGF_FGV_Action Following HGF_FGV_Action classes are available: • Object-Array manipulation – – Clear, Size Append, Insert, Replace, Delete Index, Subset, Selection Select. By. Name • Object manipulation HGF_FGV_Visitor • All, Subset, Selection • By. Name 11/29/2020 H. Brand@gsi. de 28
HGF_FGV Example HB_Simple. Visitor FGV_Visitor create objects Same FGV_DB instance FGV_Action 11/29/2020 H. Brand@gsi. de 29
HGF_Visitable & HGF_Visitor • HGF_Visitable – HB_Simple – HGF_Component • HGF_Leaf – HGF_Device. Base • HGF_VISADevice – NInstr. Sim • HGF_Visitor – HGF_Network. Visitor • HGF_Stop – HGF_Device. Visitor • NInstr. Sim_Visitor 11/29/2020 H. Brand@gsi. de 30
HGF_Visitable & HGF_Visitor 11/29/2020 H. Brand@gsi. de 31
Contents (Break) • LVOOP Design Patterns – – – Focus of HGF Base Class Library Factory & HGF_Base Reference & Object Manager Functional Global Variable & Visitor Events & Thread Pool • Network Communication – Towards Agents • Questions? Comments? • Starting Points for your own work – http: //wiki. gsi. de/cgibin/view/NIUser/Lab. VIEWObject. Oriented. Programming – http: //wiki. gsi. de/cgi-bin/view/NIUser/HGFBase. Class. Library 11/29/2020 H. Brand@gsi. de 32
Agents with Dataflow Objects? • Separation of – active generic threads, that perform – tasks, that know when and what to do with – passive objects that provide the functionality. The object knows how operate on its attribute data. – Idea → Thread Pool • Thread Manager → HGF_Thread. Pool • Worker → HGF_Thread. Worker • Tasks → HGF_Thread. Task – Events & Exceptions • Diploma thesis about Lab. VIEW Agents – started at GSI → Frederik Berck. – Example for stationary Agent with behavior → Device Agent 11/29/2020 H. Brand@gsi. de 33
HGF_Thread. Pool 11/29/2020 H. Brand@gsi. de 34
HGF_Events • HGF_Event (red) • Lab. VIEW Object wait(timeout) • send(Lab. VIEW Object) – HGF_Timing • HGF_Internal. Event – HGF_Wait – HGF_Next. Multiple – HGF_Rendezvous • HGF_Triggered. Event – – – • HGF_Occurrence HGF_Notifier HGF_Queue FAIR_Start. Event HGF_Network. Event » HGF_DIMEvent » HGF_SVEvent » … Helper classes – HGF_Variant. Data. Object – HGF_PVData. Converter – HGF_PVBase • HGF_PVPublisher • HGF_PVSubscriber – HGF_Exception 11/29/2020 H. Brand@gsi. de 35
HGF_Thread. Worker: start. Thread 11/29/2020 H. Brand@gsi. de 36
HGF_Task. Once: action. vi 1. 2. 3. – Wait on Visitor from Event Apply Visitor’s action to all Visitables End task and return Demo 11/29/2020 H. Brand@gsi. de 37
HGF_Task. Loop: action. vi 1. Wait on Visitor, maybe timeout 2. Apply Visitor’s action to all Visitables 3. If (error) – Send copy of task as Exception. Event 4. If (not Stop-Visitor) goto 1 5. Else end task and return – Demo 11/29/2020 H. Brand@gsi. de 38
HGF_Task. Event. Listener: action 1. 2. 3. 4. 5. 6. Wait on Visitor, maybe timeout Get addressee from Object. Manager by name Apply Visitor’s action to addressee If (error) publish Exception If (not Stop-Visitor) goto 1 Else end task and return 11/29/2020 H. Brand@gsi. de 39
Contents • LVOOP Design Patterns – – – Focus of HGF Base Class Library Factory & HGF_Base Reference & Object Manager Functional Global Variable & Visitor Events & Thread Pool • Network Communication – Towards Agents 11/29/2020 H. Brand@gsi. de 40
Communication Layer: DIM • • • Distributed Information Management: www. cern. ch/dim originally developed at DELPHI@LEP/CERN around 1991 available for a multitude of platforms and languages light-weight, aiming at high performance, based on TCP/IP today: "backbone" of control systems for LHC experiments concept: named services, peer-to-peer connections command "iii" DIM server A 11/29/2020 DIM client 1 service "i" DIM server B DIM name server (negotiates connections) DIM client 2 service "ii" H. Brand@gsi. de 41
Process. Variable & Data. Converter 11/29/2020 H. Brand@gsi. de 42
HGF_DIM Example & Demo 11/29/2020 H. Brand@gsi. de 43
Contents • LVOOP Design Patterns – – – Focus of HGF Base Class Library Factory & HGF_Base Reference & Object Manager Functional Global Variable & Visitor Events & Thread Pool • Network Communication – Towards Agents • Stationary Device Agent with Behavior 11/29/2020 H. Brand@gsi. de 44
Stationary Device Agent • A Device need to be an entity! → Maybe referenced • A Device should have well defined behavior → State Chart • A Device is maybe active → Thread • Accessible via Network → Network Communication → We have already all necessary base classes defined • • • HGF_Reference HGF_Thread. Pool → HGF_Thread. Worker and HGF_Thread. Task HGF_Event → Triggered. Event → Network. Event HGF_Visitable → HGF_Device. Base HGF_Visitor → HGF_Network. Visitor → HGF_Device. Visitor → Need for • Behavior: HGF_Device. FSM. lvsc (Device Finite State Machine) • Device State Chart Task: HGF_Task. DFSM. lvclass 11/29/2020 H. Brand@gsi. de 45
Device Agent: Behavior • Device is copied to State. Data during initialization. • Device. Visitors are accepted from Inputs with configure and visit triggers and returned via Outputs. ctl • Return device via Outputs during deinitialization 11/29/2020 H. Brand@gsi. de 46
Device Agent: State Actions & Visitor • HGF_Device. Base. lvclass – Specifies dynamic dispatch VI’s for every state • Entry- and Exit-Action and • Additionally a do. Action, which becomes executed on NULL-Trigger – Can be visited by Device. Visitor • HGF_Device. FSM. lvsc – HGF_Device. Base. accept(Device. Visitor) on configure and visit entry action – HGF_Device. FSM_Internals. ctl can be changed within the override VI’s to trigger internal state changes 11/29/2020 H. Brand@gsi. de 47
Worker Device Agent: Task Publish Exception Publish Status of State Chart Wait on DFSM Visitor Trigger State Chart Set Device. Visitor (from previous iteration) Send Device. Visitor to its destination Run State Chart 11/29/2020 H. Brand@gsi. de 48
Device Agent: Main. vi Dispatch Task. DFSM Destroy Device Prepare network events Prepare Task. DFSM Dispatch Network. Event (trigger Device. FSM) Extract data from returning Device. Visitor Monitor DFSM Status 11/29/2020 H. Brand@gsi. de 49
Device Agent: Sequence Diagram This diagram gives an overview how all objects and threads work together to implement the Stationary Device Agent Pattern. Thread color scheme • Main. vi thread • Agent thread (Thread. Worker) • DFSM thread (State Chart) • Command thread • Receiver thread • DFSM Monitor thread • Thin black line: device object data flow • Thick blue line: device visitor data flow 11/29/2020 H. Brand@gsi. de 50
Thank for your attention! • Questions? • Starting Points for your own work – Lab. VIEW examples – http: //wiki. gsi. de/cgibin/view/NIUser/Lab. VIEWObject. Oriented. Programming – http: //wiki. gsi. de/cgi-bin/view/NIUser/HGFBase. Class. Library 11/29/2020 H. Brand@gsi. de 51
- Slides: 51