📄 adstmach.pas
字号:
{*********************************************************}
{* ADSTMACH.PAS 4.04 *}
{* Copyright (C) TurboPower Software 1996-2002 *}
{* All rights reserved. *}
{*********************************************************}
{Global defines potentially affecting this unit}
{$I AWDEFINE.INC}
unit AdStMach;
{
Design philosophy: The TApdCustomStateMachine is a container for
TApdCustomStates. TApdCustomStateMachine manages the TApdCustomStates.
The TApdCustomStateMachine contains the reference to the TApdComPort,
the TApdCustomState contains definitions for the data conditions (through
published properties in the Conditions property).
To start the state machine, call the TApdCustomStateMachine.Start method;
the TApdCustomState that is designated the TApdCustomStateMachine.StartState
gets activated. As a TApdCustomState is activated, a TApdDataPacket is
created for each condition, and the appropriate/applicable TApdDataPacket
properties are set. All of the TApdDataPacket's .OnPacket event handlers
point to the TApdCustomStateMachine.PacketEvent method. When that event is generated
the collected data is placed in the TApdCustomState's and TApdStateMachine's
.Data property. The current TApdCustomState is then deactivated (data packets
disabled and freed), and the condition's NextState is activated. This
continues until the Condition does not define a NextState (when we assume that
is a terminal state) or the state defined by the TApdCustomStateMachine's
TerminalState is activated.
}
{!!.04 - Start of Changes }
interface
uses
Windows,
Messages,
SysUtils,
Classes,
Controls,
ExtCtrls,
Graphics,
Forms,
Dialogs,
StdCtrls,
OoMisc,
AdPacket,
AdPort,
AdExcept,
AdStrMap,
TypInfo;
type
TApdConnectAddType = (atAdd, atSub, atNone);
TAdConnectoidClickStyle = (ccsHighlight, ccsHint, ccsDashedLine);
TAdConnectoidClickStyles = set of TAdConnectoidClickStyle;
TApdCustomStateMachine = class;
TApdCustomState = class;
TApdStateCondition = class;
TApdStateConnectoid = class;
TApdOnConnectoidClickEvent = procedure (Sender : TObject;
Connectoid : TApdStateConnectoid) of object;
TApdOnDataSourceGetData = procedure (Sender : TObject; Data : Pointer;
DataSize : Integer) of object;
TApdOnDataSourceGetDataString = procedure (Sender : TObject;
DataString : string) of object;
TApdOnStateGetData = procedure (Sender : TObject; Data : Pointer;
DataSize : Integer) of object;
TApdOnStateGetDataString = procedure (Sender : TObject;
DataString : string) of object;
{
TApdStateCustomDataSource
StateActivate
StateDeactivate
}
TApdStateCustomDataSource = class (TApdBaseComponent)
private
FStateMachine : TApdCustomStateMachine;
FPauseDepth : Integer;
FOnGetData : TApdOnDataSourceGetData;
FOnGetDataString : TApdOnDataSourceGetDataString;
protected
function GetPaused : Boolean;
procedure Notification (AComponent : TComponent;
Operation : TOperation); override;
function SearchStateMachine (
const C : TComponent) : TApdCustomStateMachine;
property StateMachine : TApdCustomStateMachine
read FStateMachine;
property OnGetData : TApdOnDataSourceGetData
read FOnGetData write FOnGetData;
property OnGetDataString : TApdOnDataSourceGetDataString
read FOnGetDataSTring write FOnGetDataString;
public
constructor Create (AOwner : TComponent); override;
{ Pause the state machine }
procedure Output (AString : string); virtual; abstract;
procedure OutputBlock (ABlock : Pointer; ASize : Integer); virtual;
abstract;
procedure Pause; virtual;
procedure Resume; virtual;
procedure StateActivate (State : TApdCustomState);
virtual; abstract;
procedure StateChange (OldState, NewState : TApdCustomState);
virtual; abstract;
procedure StateDeactivate (State : TApdCustomState);
virtual; abstract;
procedure StateMachineActivate (State : TApdCustomState;
Condition : TApdStateCondition;
Index : Integer); virtual; abstract;
procedure StateMachineDeactivate (State : TApdCustomState);
virtual; abstract;
procedure StateMachineStart (AOwner : TApdCustomStateMachine);
virtual;
procedure StateMachineStop; virtual;
property Paused : Boolean read GetPaused;
published
end;
TApdStateComPortSource = class (TApdStateCustomDataSource)
private
PacketList : TList;
FComPort : TApdCustomComPort;
FBuffer : Pointer;
FBufferSize : Integer;
protected
procedure SetComPort (const Value: TApdCustomComPort);
procedure PacketEvent (Sender : TObject;
Data : Pointer;
Size : Integer);
procedure PacketTimeout(Sender: TObject);
procedure Notification (AComponent : TComponent;
Operation : TOperation); override;
procedure TriggerHandler (Msg, wParam : Cardinal; lParam : Longint);
public
constructor Create (AOwner : TComponent); override;
destructor Destroy; override;
procedure Output (AString : string); override;
procedure OutputBlock (ABlock : Pointer; ASize : Integer); override;
procedure Pause; override;
procedure Resume; override;
procedure StateActivate (State : TApdCustomState); override;
procedure StateDeactivate (State : TApdCustomState); override;
procedure StateMachineActivate (State : TApdCustomState;
Condition : TApdStateCondition;
Index : Integer); override;
procedure StateMachineDeactivate (State : TApdCustomState);
override;
procedure StateChange (OldState, NewState : TApdCustomState);
override;
procedure StateMachineStart (AOwner : TApdCustomStateMachine);
override;
procedure StateMachineStop; override;
published
property ComPort : TApdCustomComPort
read FComPort write SetComPort;
property OnGetData;
property OnGetDataString;
end;
{ defines how the connectoid line appears }
TApdStateConnectoid = class (TPersistent)
private
FWidth: Integer;
FCaption: TCaption;
FColor: TColor;
FCondition : TApdStateCondition;
FSelected : Boolean;
FFont : TFont;
procedure SetCaption(const Value: TCaption);
procedure SetColor(const Value: TColor);
procedure SetFont (const Value : TFont);
procedure SetWidth(const Value: Integer);
protected
{ Answerback Property Maintenance }
procedure DefineProperties (Filer : TFiler); override;
function IsCaptionStored: Boolean;
procedure ReadCaption (Reader : TReader);
procedure WriteCaption (Writer : TWriter);
public
constructor Create(AOwner : TApdStateCondition);
destructor Destroy; override;
procedure Changed;
published
property Caption : TCaption read FCaption write SetCaption;
property Color : TColor read FColor write SetColor;
property Width : Integer read FWidth write SetWidth;
property Font : TFont read FFont write SetFont;
end;
{ describes the conditions for failure/success/etc }
TApdStateCondition = class (TCollectionItem)
protected
function GetDisplayName: string; override;
private
FPacketSize : Integer;
FTimeout : Integer;
FErrorCode : Integer;
FNextState : TApdCustomState;
FEndString : string;
FStartString : string;
FConnectoid : TApdStateConnectoid;
FIgnoreCase : Boolean;
FDefaultError : Boolean;
FDefaultNext : Boolean;
FOutputOnActivate : string;
procedure SetNextState (const Value : TApdCustomState);
procedure SetConnectoid (const Value : TApdStateConnectoid);
protected
function GetCaption : TCaption;
function GetColor : TColor;
function GetFont : TFont;
procedure SetCaption (const v : TCaption);
procedure SetColor (const v : TColor);
procedure SetDefaultError (const v : Boolean);
procedure SetDefaultNext (const v : Boolean);
procedure SetFont (const v : TFont);
procedure SetOutputOnActivate (const v : string);
public
constructor Create(Collection : TCollection); override;
destructor Destroy; override;
procedure Changed;
published
property DefaultError : Boolean
read FDefaultError write SetDefaultError;
property DefaultNext : Boolean
read FDefaultNext write SetDefaultNext;
property StartString : string
read FStartString write FStartString;
property EndString : string
read FEndString write FEndString;
property OutputOnActivate : string
read FOutputOnActivate write SetOutputOnActivate;
property PacketSize : Integer
read FPacketSize write FPacketSize;
property Timeout : Integer
read FTimeout write FTimeout;
property NextState : TApdCustomState
read FNextState write SetNextState;
property ErrorCode : Integer
read FErrorCode write FErrorCode;
property IgnoreCase : Boolean
read FIgnoreCase write FIgnoreCase;
property Connectoid : TApdStateConnectoid
read FConnectoid write SetConnectoid;
property Caption : TCaption read GetCaption write SetCaption;
property Color : TColor read GetColor write SetColor;
property Font : TFont read GetFont write SetFont;
end;
{ describes the container for the aforementioned conditions }
TApdStateConditions = class(TCollection)
private
function GetItem(Index: Integer): TApdStateCondition;
procedure SetItem(Index: Integer; const Value: TApdStateCondition);
protected
FState : TApdCustomState;
function GetOwner : TPersistent; override;
public
constructor Create(State : TApdCustomState;
ItemClass: TCollectionItemClass);
procedure Update(Item: TCollectionItem); override;
function Add : TApdStateCondition;
property Items[Index: Integer] : TApdStateCondition
read GetItem write SetItem; default;
{$IFNDEF VERSION5}
procedure Delete(Item : Integer);
{$ENDIF}
end;
{ TApdCustomStateMachine events }
TApdStateMachineStateChangeEvent = procedure(StateMachine : TApdCustomStateMachine;
FromState, ToState : TApdCustomState) of object;
TApdStateMachineFinishEvent = procedure(StateMachine : TApdCustomStateMachine;
ErrorCode : Integer) of object;
{ TApdCustomState events }
TApdStateFinishEvent = procedure(State : TApdCustomState;
Condition : TApdStateCondition; var NextState : TApdCustomState) of object;
TApdStateNotifyEvent = procedure(State : TApdCustomState) of object;
{ the container for the states }
TApdCustomStateMachine = class(TApdBaseScrollingWinControl)
private
FStartState: TApdCustomState;
FTerminalState: TApdCustomState;
FCurrentState : TApdCustomState;
FCanvas : TCanvas;
FDefaultDataSource : TApdStateComPortSource;
{FComPort: TApdCustomComPort;}
FOnStateMachineFinish: TApdStateMachineFinishEvent;
FOnStateChange: TApdStateMachineStateChangeEvent;
FBorderStyle: TBorderStyle;
FData: Pointer;
FDataSize: Integer;
FDataString: string;
FLastErrorCode: Integer;
{PacketList : TList;}
FCaption: TCaption;
FActive: Boolean;
FDataSource : TApdStateCustomDataSource;
FConnectoidClickEvent : TApdOnConnectoidClickEvent;
FMovableStates : Boolean;
FConnectoidClickStyle : TAdConnectoidClickStyles;
function GetComPort : TApdCustomComPort;
function GetDataSource : TApdStateCustomDataSource;
function GetLiveDataSource : TApdStateCustomDataSource;
procedure SetStartState(const Value: TApdCustomState);
procedure SetTerminalState(const Value: TApdCustomState);
procedure SetComPort(const Value: TApdCustomComPort);
procedure SetBorderStyle(const Value: TBorderStyle);
procedure SetMovableStates (const v : Boolean);
procedure WMNCHitTest(var Message: TMessage); message WM_NCHITTEST;
procedure CMCtl3DChanged(var Message: TMessage); message CM_CTL3DCHANGED;
function GetData: Pointer;
function GetDataSize: Integer;
function GetDataString: string;
function GetStateNames: TStringList;
procedure SetCaption(const Value: TCaption);
procedure SetConnectoidClickStyle (const v : TAdConnectoidClickStyles);
protected
{ Protected declarations }
procedure CMDesignHitTest (var Msg : TWMMouse); message CM_DESIGNHITTEST;
procedure ConnectoidAtPoint (AddType : TApdConnectAddType; Point : TPoint);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -