V4 Design: RecordCommon Support

From EPICSWIKI
Revision as of 12:57, 22 September 2005 by MartyKraimer (talk | contribs)

EPICS V4: RecordCommonSupport - September 22 2005


Overview

THIS WIKI NEEDS LOTS OF WORK

This document describes the implementation of the record support for RecordCommon, i.e. the set of fields associated with every record type.

For V4 dbCommon.dbd and RecordCommon.dbd replace the V3 dbCommon.dbd. dbCommon.dbd define widely used definitions and RecordCommon.dbd defines the fields that are common to all record types.


This document describes the following:

  • The code automatically generated from dbCommon.dbd and RecordCommon.dbd
  • A proposed implementation of RecordCommonSupport.

This document requires that the reader be familiar with:

  1. V3 iocCore
  2. V4 Design: Record Processing
  3. V4 Design: dbdInterfaces
  4. V4 Design: Runtime interfaces
  5. V4 Design: RecordCommon

Unless the reader is familiar with these most of this document will be very difficult to understand.

Java is used as the example code for two reasons:

  1. For V4 iocCore it is intended that both C++ and Java implementations will be provided. Since V3 is only implemented in C and C++ the Java implemention of V4 is described first to make sure that the interfaces defined in dbdInterfaces will work for Java.
  2. Java provides facilities that make it easy to handle strings, arrays, and structures.

An implementaion of RecordCommonSupport is presented for two reasons:

  1. It demonstrates how to implement code that uses or implements the interface described in "V4 Design: dbdInterfaces" and "V4 Design: Runtime interfaces".
  2. It helps refine the interfaces described in them

dbCommon.dbd

See the description of dbCommon.dbd in "V4 Design: RecordCommon"

Link support must register support each of the link definitions.

From dbCommon.dbd the following class implementations are generated:

  • For the link structures:
    • MonitorLink and MonitorLinkData, and MonitorLinkSupport
    • InputLink, InputLinkData, and InputLinkSupport
    • OutputLink, OutputLinkData, and OutputLinkSupport
    • ProcessLink, ProcessLinkData, and ProcessLinkSupport
  • For TimeStamp
    • TimeStamp, TimeStampData, and TimeStampSupport
  • For Scan
    • Scan, ScanData, and ScanSupport

See "V4 Design: Runtime interfaces" for an example of how these classes are defined.


RecordCommon.dbd

See the description of RecordCommon.dbd in "V4 Design: RecordCommon"

RecordCommon.dbd must be part of every record.

Implementations of Disable, DisableData, and DisableDataSupport are automatically generated.

An implementations of RecordCommon is also generated. See "V4 Design: Runtime interfaces" for an example of the generated classes.


RecordCommonSupport.java

Overview

This section describes the java implementation of RecordCommon functionality. This code handles processing of the following fields of RecordCommon:

disable
Decides if the record is disabled.
alarmStatus and alarmSeverity
The only way to change alarmStatus and alarmSeverity is via interface Common, which is implemented by RecordCommonSupport.
processLink
This is an array of process links, which replace the V3 FLNK

The following subsection contains the complete code for RecordCommonSupport.java. The remaining subsections discuss each part of the code.

IocCommon.java

This defines

    interface Common {
        bool setSeverity(AlarmSeverity severity, string status);
    }


RecordCommonSupport.java

    enum Mode {modeNone,modeInput,modeOutput};
    
    public class RecordCommonSupport implements RecordSupport,Callback,Common
    {
        private RecordInstance instance;
        private Mode           mode;
        private Input          input;
        private Output         output;
        private Alarm          alarm;
        private bool           requestProcess;
    
        // Constructor
        public RecordCommonSupport(RecordInstance instance) {
            this.instance = instance;
        }
    
        // Beginning of RecordSupport

public destroy() {} // destroy not needed for Java

        public destroy() {} // destroy not needed for Java
        public initialize(int pass) {
            if(pass==0) return;
            input.init(instance);
            output.init(instance);
            alarm.init(instance);
        }
        public processState process(processState state) {
            switch(state) {
            case processCancel:

//MUCH MORE NEEDS TO BE DONE // MUST CANCAL ANY OUTSTANDING I/O mode = modeNone; return processDone;

            case processStart: {
                mode = modeInput;
                requestProcess = false;
                LinkWaitResult result = input.disableSupport.getWait(
                    input.disableInput,0.0,this);
                if(result==linkNoop || result==linkDone) return processDone;
                requestProcess = true;
            }
            // fall through to processInputActive
            case processInputActive: {
                if(requestProcess) return processInputActive;
                if(input.disable.disableValue == disable.disableInput)
                        return processDone;
                return processActive;
            }
            case processDone: {
                mode = modeOutput;
                output.nextLink = 0;
                output.numberLinksActive = 0;
                    
            }
            // fall through to processOutputActive
            case processOutputActive:
                LinkWaitResult result = linkWaitParallel;
                requestProcess = false;
                while(output.nextLink< output.link.length) {
                    if(result==linkWaitSequential) break;
                    result = output.link[nextLink].support.processWait(0.0,this);
                    if(result==linkWaitSequential || result==linkWaitParallel)
                        output.numberLinksActive++
                    output.nextLink++;
                }
                if(output.numberLinksActive==0) {
                     mode = modeNone;
                     return processDone;
                }
                requestProcess = true;
                return processOutputActive;
            default: throw java.lang.IllegalStateException;
            }
        }
        public bool special(bool after,Dbf[] field) {
            int nlevels = field.length;
            assert(nlevels>0);
            if(index==alarmStatusIndex || index==alarmSeverityIndex) {
                 if(!alarm.modifyingAlarm) return false;
                 return true;
            }
            if(!after) return;
            switch(index) {
                case disableIndex: {
                    if(nlevels==1) {
                        input.init(instance);
                    } else {
                        input.special(instance,Dbf[1]);
                    }
                    break;
                }
                case processLinkIndex : output.init(instance); break;
                default: printf("Why is special being called?\n");
            }
        }
        // Start of Callback methods
        public void done() {
            lock(instance);
            switch(mode) {
            case modeNone:
                 unlock(instance)
                 throw java.lang.IllegalStateException;
            case modeInput:
                if(requestProcess) {
                    requestProcess = false;
                    DbAccess.process(instance);
                }
                break;
            case modeOutput:
                output.numberLinksActive--;
                if(output.numberLinksActive==0 && requestProcess) {
                    requestProcess = false;
                    DbAccess.process(instance); 
                }
                if(output.numberLinksActive==0 && requestProcess)
                break;
            }
            unlock(instance)
        }
        public void failure() { done(); }
        // Start of Common methods
        bool setSeverity(AlarmSeverity severity, string status) {
            Alarm alarm = this.alarm;
            if(severity<= alarm.severity) return false;
            alarm.severity = severity;
            alarm.status = status;
            alarm.modifyingAlarm = true;
            alarm.alarmSeverity.putIndex(severity);
            alarm.alarmStatus.put(status);
            alarm.modifyingAlarm = false;
        }
    }
    
    // Definitions for modeInput
    class Input {
        DisableData disable;
        LinkSupport disableSupport;
        DbfInt16    disableInput;
        DbfInt16    disableValue;
        DbfInt16    alarmSeverity;
    
        void init(RecordInstance instance) {
            RecordAccess recordAccess = instance.getRecordCommonAccess();
            DbfLink link = recordAccess.getField("disable.disableLink");
            disableSupport = link.getSupport();
            DisplayLimitAccess.get(
                recordAccess.getField("disable"),disable);
            recordAccess.getField("disable.disableInput",disableInput);
        }
        void special(RecordInstance instance,Dbf field) {
            int16 index = Dbf.getIndex();
            switch(index) {
                case disableValueIndex:
                    disableValue = ((DbfInt16)Dbf).get(); break;
                case disableInputIndex:
                    disableInput = ((DbfInt16)Dbf).get(); break;
                case disableAlarmSeverityIndex:
                    alarmSeverity = ((dbfMenu)Dbf).getIndex(); break;
                case disableLinkIndex:
                    init(instance); break;
                default:
                    throw java.lang.IllegalStateException;
            }
        }
    }
    // Definitions for modeOutput
    class OutputLink {
        Link        link;
        LinkSupport support;
        void init(RecordInstance instance,String linkFieldName) {
            RecordAccess recordAccess = instance.getRecordCommonAccess();
            DbfLink link = recordAccess.getField(linkFieldName);
            support = link.getSupport();
        }
    };
    
    class Output {
        int          nextLink;
        int          numberLinksActive;
        OutputLink[] link;
        void init(RecordInstance instance) {
            RecordAccess recordAccess = instance.getRecordCommonAccess();
            DbfArray array = recordAccess.getField("processLink");
            int n = array.getNelements();
            link = new OutputLink[n];
            for(i=0; i< n; i++) {
                String field = "processLink[" + "i" + "]";
                link[i].init(instance,field);
            }
        }
    }
    // Definitions for alarm handling
    class Alarm {
        short severity;
        String status;
        DbfMenu alarmSeverity;
        DbfString alarmStatus;
        bool modifyingAlarm;
        void init(RecordInstance instance) {
            RecordAccess recordAccess = instance.getRecordCommonAccess();
            alarmSeverity = recordAccess.getField("alarmSeverity");
            alarmStatus = recordAccess.getField("alarmStatus");
        }
    }

Beginning of class definition

    enum Mode {modeNone,modeInput,modeOutput};
    
    public class RecordCommonSupport implements RecordSupport,Callback, Common
    {
        private RecordInstance instance;
        private Mode           mode;
        private Input          input;
        private Output         output;
        private Alarm          alarm;
        private bool           requestProcess;
    
        // Constructor
        public RecordCommonSupport(RecordInstance instance) {
            this.instance = instance;
        }


RecordCommonSupport implements the following interfaces:

RecordSupport
Record support for the fields in RecordCommon.
Callback
The callbacks called by link support
Common
For now this just has the method setSeverity

It has the following private fields:

  • instance - The record instance to which it is attached. RecordCommonSupport has a single constructor which is passed the RecordInstance to which it is attached.
  • mode - This keeps the state for record processing:
    • modeNone - The record is not being processed.
    • modeInput - The disable link is active but not complete.
    • modeOutput - The processLinks are active but not complete.
  • input - Input is a private class that provides access to the fields of disable.
  • output - Output is a private class that provides access to processLink.
  • alarm - Alarm is a private class that provides access to fields alarmSeverity and alarmStatus.
  • requestProcess - This is used by callback to decide if a request should be made to process the record.

Notes about Input, Output, and Alarm.

  • Each of these is a private class to RecordCommonSupport.
  • Each makes fields available to RecordCommonSupport.
  • Each has "introspection" code that gets the values of the private fields from RecordCommon fields during record initialization of if the an RecordCommon fields is changed via a call to Dbf.put.
  • The implementations of each of these private classes is given after the implementaion of RecordCommonSupport.


RecordSupport Methods

initialize

        public initialize(int pass) {
            if(pass==0) return;
            input.init(instance);
            output.init(instance);
            alarm.init(instance);
        }

During the first pass of initialization, nothing needs to be done. Before initialize is called on pass 1, iocInit has already called link support so that it connects. Initialize just calls its private class init methods. They get the current values of their private fields.

destroy

        public destroy() {} // destroy not needed for Java

Since Java does garbage collection destroy is not needed. Note that iocCore will call all the link support destroy methods.

process

process is called by dbProcess at the beginning of record processing and after the record specific record support completes. During the beginning of record processing process takes care of field disable During the end of record processing process takes care of field processLink.

A few features of process are:

  • implements a state machine that handles the links.
  • interacts with Callback, which is called by link support when links complete.
  • interacts with special, which is called when fields in RecordCommon are modified.
        public processState process(processState state) {
            switch(state) {
            case processCancel: mode = modeNone; return processDone;
            case processStart: {
                mode = modeInput;
                requestProcess = false;
                LinkWaitResult result = input.disableSupport.getWait(
                    input.disableInput,0.0,this);
                if(result==linkNoop || result==linkDone) return processDone;
                requestProcess = true;
            }
            // fall through to processInputActive
            case processInputActive: {
                if(requestProcess) return processInputActive;
                if(input.disable.disableValue == disable.disableInput)
                        return processDone;
                return processActive;
            }
            case processDone: {
                mode = modeOutput;
                output.nextLink = 0;
                output.numberLinksActive = 0;
                    
            }
            // fall through to processOutputActive
            case processOutputActive:
                LinkWaitResult result = linkWaitParallel;
                requestProcess = false;
                while(output.nextLink< output.link.length) {
                    if(result==linkWaitSequential) break;
                    result = output.link[nextLink].support.processWait(0.0,this);
                    if(result==linkWaitSequential || result==linkWaitParallel)
                        output.numberLinksActive++
                    output.nextLink++;
                }
                if(output.numberLinksActive==0) {
                     mode = modeNone;
                     return processDone;
                }
                requestProcess = true;
                return processOutputActive;
            default: throw java.lang.IllegalStateException;
            }
        }

special

special is called when any of the fields in disable, alarmStatus, alarmSeverity, or processLink are changed.

        public bool special(bool after,Dbf[] field) {
            int nlevels = field.length;
            assert(nlevels>0);
            if(index==alarmStatusIndex || index==alarmSeverityIndex) {
                 if(!alarm.modifyingAlarm) return false;
                 return true;
            }
            if(!after) return;
            switch(index) {
                case disableIndex: {
                    if(nlevels==1) {
                        input.init(instance);
                    } else {
                        input.special(instance,Dbf[1]);
                    }
                    break;
                }
                case processLinkIndex : output.init(instance); break;
                default: printf("Why is special being called?\n");
            }
        }
    

Private Class Methods

Callback methods

These are the callback methods that are called by link support.

        // Start of Callback methods
        public void done() {
            lock(instance);
            switch(mode) {
            case modeNone:
                unlock(instance);
                throw java.lang.IllegalStateException;
            case modeInput:
                if(requestProcess) {
                    requestProcess = false;
                    DbAccess.process(instance);
                }
                break;
            case modeOutput:
                output.numberLinksActive--;
                if(output.numberLinksActive==0 && requestProcess) {
                    requestProcess = false;
                    DbAccess.process(instance); 
                }
                return;
            }
            unlock(instance);
        }
        public void failure() { done(); }
    

Common methods

For now the only method is setSeverity, The only way code can change severity and status is to call this method.

        // Start of Common methods
        bool setSeverity(AlarmSeverity severity, string status) {
            Alarm alarm = this.alarm;
            if(severity<= alarm.severity) return false;
            alarm.severity = severity;
            alarm.status = status;
            alarm.modifyingAlarm = true;
            alarm.alarmSeverity.putIndex(severity);
            alarm.alarmStatus.put(status);
            alarm.modifyingAlarm = false;
        }
    }

Input

This is a private class for RecordCommonSupport. It directly accesses the fields in this class. The init and special methods are called to get values for the private fields.

    // Definitions for modeInput
    class Input {
        DisableData disable;
        LinkSupport disableSupport;
        DbfInt16    disableInput;
        DbfInt16    disableValue;
        DbfInt16    alarmSeverity;
    
        void init(RecordInstance instance) {
            RecordAccess recordAccess = instance.getRecordCommonAccess();
            DbfLink link = recordAccess.getField("disable.disableLink");
            disableSupport = link.getSupport();
            DisplayLimitAccess.get(
                recordAccess.getField("disable"),disable);
            recordAccess.getField("disable.disableInput",disableInput);
        }
        void special(RecordInstance instance,Dbf field) {
            int16 index = Dbf.getIndex();
            switch(index) {
                case disableValueIndex:
                    disableValue = ((DbfInt16)Dbf).get(); break;
                case disableInputIndex:
                    disableInput = ((DbfInt16)Dbf).get(); break;
                case disableAlarmSeverityIndex:
                    alarmSeverity = ((dbfMenu)Dbf).getIndex(); break;
                case disableLinkIndex:
                    init(instance); break;
                default:
                    throw java.lang.IllegalStateException;
            }
        }
    }
    

OutputLink and Output

Output is a private class for RecordCommonSupport. It directly accesses the fields in this class. The init method is called to get values for the private fields. OutputLink is used by Output.

    // Definitions for modeOutput
    class OutputLink {
        Link        link;
        LinkSupport support;
        void init(RecordInstance instance,String linkFieldName) {
            RecordAccess recordAccess = instance.getRecordCommonAccess();
            DbfLink link = recordAccess.getField(linkFieldName);
            support = link.getSupport();
        }
    };
    
    class Output {
        int          nextLink;
        int          numberLinksActive;
        OutputLink[] link;
        void init(RecordInstance instance) {
            RecordAccess recordAccess = instance.getRecordCommonAccess();
            DbfArray array = recordAccess.getField("processLink");
            int n = array.getNelements();
            link = new OutputLink[n];
            for(i=0; i< n; i++) {
                String field = "processLink[" + "i" + "]";
                link[i].init(instance,field);
            }
        }
    }
    

Alarm

This is a private class for RecordCommonSupport. It directly accesses the fields in this class. The init method is called to get values for the private fields.

    // Definitions for alarm handling
    class Alarm {
        short severity;
        String status;
        DbfMenu alarmSeverity;
        DbfString alarmStatus;
        bool modifyingAlarm;
        void init(RecordInstance instance) {
            RecordAccess recordAccess = instance.getRecordCommonAccess();
            alarmSeverity = recordAccess.getField("alarmSeverity");
            alarmStatus = recordAccess.getField("alarmStatus");
        }
    }
    

RecordCommonSupportFactory

This is the factory for creating an RecordCommonSupport. It is called by dbLoadRecords.

    public class RecordCommonSupportFactory implements RecordSupportFactory{
        RecordSupport create(RecordInstance instance) {
            return new RecordCommonSupport(instance);
        }
        void destroy(RecordSupport support) {
            RecordCommonSupport RecordCommonSupport = support;
            RecordCommonSupport.destroy();
        }
    }
    public final class RecordCommonSupportFactoryRegister {
        static public createAndRegister() {
            RecordCommonSupportFactory factory = new RecordCommonSupportFactory;
            RegisterSupport.record(factory,"RecordCommon");
        }
    }

Question Who calls RecordCommonSupportFactoryRegister.createAndRegister?