Synchronous_Fifo of Objects in Ada - concurrency

I created record in Ada and later created Synchronous_Fifo out of these records:
type Basic_Record is record
Argument_1: Integer;
Operator: Character;
Argument_2: Integer;
Product: Integer;
end record;
package Tasks_Fifo is new Synchronous_Fifo(Basic_Record);
use Tasks_Fifo;
Buffer : Fifo;
and this is working fine. Later I wanted to do the same Synchronous_Fifo out of objects:
package Adding_Machine is
protected type Add_Machine is
entry Store_Record(Task_Record: Basic_Record);
entry Push_Button;
entry Get_Product(Product: out Integer);
private
My_Record : Basic_Record;
My_Product: Integer;
end Add_Machine;
end Adding_Machine;
use Adding_Machine;
package Object_Fifo is new Synchronous_Fifo(Add_Machine);
use Object_Fifo;
Buffer: Fifo;
and as a result I got several errors:
Multiple markers at this line
- Occurrence of package
- actual for non-limited "Element_Type" cannot be a limited type
- instantiation abandoned
in line, where I am creating Object_Fifo.
How should I create this Fifo? Or maybe there is something wrong with package Adding_Machine?

Below is shown package Synchronous_Fifo:
generic
type Element_Type is private;
package Synchronous_Fifo is
protected type Fifo is
entry Push(Item : Element_Type);
entry Pop(Item : out Element_Type);
private
Value : Element_Type;
Is_New : Boolean := False;
end Fifo;
end Synchronous_Fifo;
package body Synchronous_Fifo is
----------
-- Fifo --
----------
protected body Fifo is
---------
-- Push --
---------
entry Push (Item : Element_Type) when not Is_New is
begin
Value := Item;
Is_New := True;
end Push;
---------
-- Pop --
---------
entry Pop (Item : out Element_Type) when Is_New is
begin
Item := Value;
Is_New := False;
end Pop;
end Fifo;
end Synchronous_Fifo;
I created access to Adding_Machine and later created Fifo out of these accesses.
type Adding_Machine_Access is access Adding_Machine;
package Adding_Machines_Fifo is new Synchronous_Fifo(adding_Machine_Access);
use Tasks_Fifo;
Adding_Machines_Fifo : Fifo;
It should work.

Related

Using Roslyn, how to enumerate members (Namespaces, classes etc) details in Visual Basic Document?

Using Roslyn, the only mechanism for determining members of Visual Basic document appears to be:
var members = SyntaxTree.GetRoot().DescendantNodes().Where(node =>
node is ClassStatementSyntax ||
node is FunctionAggregationSyntax ||
node is IncompleteMemberSyntax ||
node is MethodBaseSyntax ||
node is ModuleStatementSyntax ||
node is NamespaceStatementSyntax ||
node is PropertyStatementSyntax ||
node is SubNewStatementSyntax
);
How do get the member name, StarLineNumber and EndLineNumber of each member?
Exists not only the one way to get it:
1) As you try: I willn't show this way for all of kind member (they count are huge and the logic is the similar), but only a one of them, for example ClassStatementSyntax:
to achive it name just get ClassStatementSyntax.Identifier.ValueText
to get start line you can use Location as one of ways:
var location = Location.Create(SyntaxTree, ClassStatementSyntax.Identifier.Span);
var startLine = location.GetLineSpan().StartLinePosition.Line;
logic for retrieving the end line looks like a logic to receive the start line but it dependents on the corresponding closing statement (some kind of end statement or self)
2) More useful way – use SemanticModel to get a data that you want:
In this way you will need to receive semantic info only for ClassStatementSyntax, ModuleStatementSyntxt and NamespaceStatementSyntax, and all of their members will be received just calling GetMembers():
...
SemanticModel semanticModel = // usually it is received from the corresponding compilation
var typeSyntax = // ClassStatementSyntax, ModuleStatementSyntxt or NamespaceStatementSyntax
string name = null;
int startLine;
int endLine;
var info = semanticModel.GetSymbolInfo(typeSyntax);
if (info.Symbol is INamespaceOrTypeSymbol typeSymbol)
{
name = typeSymbol.Name; // retrieve Name
startLine = semanticModel.SyntaxTree.GetLineSpan(typeSymbol.DeclaringSyntaxReferences[0].Span).StartLinePosition.Line; //retrieve start line
endLine = semanticModel.SyntaxTree.GetLineSpan(typeSymbol.DeclaringSyntaxReferences[0].Span).EndLinePosition.Line; //retrieve end line
foreach (var item in typeSymbol.GetMembers())
{
// do the same logic for retrieving name and lines for all others members without calling GetMembers()
}
}
else if (semanticModel.GetDeclaredSymbol(typeSyntax) is INamespaceOrTypeSymbol typeSymbol2)
{
name = typeSymbol2.Name; // retrieve Name
startLine = semanticModel.SyntaxTree.GetLineSpan(typeSymbol2.DeclaringSyntaxReferences[0].Span).StartLinePosition.Line; //retrieve start line
endLine = semanticModel.SyntaxTree.GetLineSpan(typeSymbol2.DeclaringSyntaxReferences[0].Span).EndLinePosition.Line; //retrieve end line
foreach (var item in typeSymbol2.GetMembers())
{
// do the same logic for retrieving name and lines for all others members without calling GetMembers()
}
}
But attention, when you have a partial declaration your DeclaringSyntaxReferences will have a couple items, so you need to filter SyntaxReference by your current SyntaxTree

Fabric Composer test code not working

I´ve just replaced the Composer default sample ("sampleAsset", "sampleTransaction", etc) by another one I created, for my better understanding. Everything works except for the transaction, which return me the error message:
"**Error: Could not find any functions to execute for transaction org.acme.sample.CompraDoVinho#**2b2d0624-bc..."
Find below the source codes:
Blockquote
Model file:
namespace org.acme.sample
asset Vinho identified by IDvinho {
o String IDvinho
--> Participante owner
o String uva
o String nomeVinho
o Integer preco
}
participant Participante identified by IDparticipante {
o String IDparticipante
o String tipo
o String nomeEmpresa
}
transaction CompraDoVinho identified by IDcompra {
o String IDcompra
--> Vinho asset
o Integer precoVenda
}
Logic:
function onSampleTransaction(CompraDoVinho) {
CompraDoVinho.asset.preco = CompraDoVinho.precoVenda;
return getAssetRegistry('org.acme.sample.Vinho')
.then(function (assetRegistry) {
return assetRegistry.update(CompraDoVinho.asset);
});
}
Permissions:
rule Default {
description: "Allow all participants access to all resources"
participant: "ANY"
operation: ALL
resource: "org.acme.sample"
action: ALLOW
}
Blockquote
Could anybody help me finding where is the bug in my code?
Thanks in advance
The issue is almost certainly because you've renamed the transaction. Composer has 2 mechanisms to route transactions to JS functions:
(Legacy) using an onMyTransactionType naming convention. I.e. the function will be called when an instance of MyTransactionType is submitted.
(Preferred) using the #transaction and #param annotations. See below for an example. The #transaction annotation indicates that the function would like to process transactions and the #param annotation is used to specify the type of the transaction to process.
/**
* Place an order for a vehicle
* #param {org.acme.vehicle.lifecycle.manufacturer.PlaceOrder} placeOrder - the PlaceOrder transaction
* #transaction
*/
function placeOrder(placeOrder) {
console.log('placeOrder');
let factory = getFactory();
let NS = 'org.acme.vehicle.lifecycle.manufacturer';
let order = factory.newResource(NS, 'Order', placeOrder.transactionId);
order.vehicleDetails = placeOrder.vehicleDetails;
order.orderStatus = 'PLACED';
order.manufacturer = placeOrder.manufacturer;
// save the order
return getAssetRegistry(order.getFullyQualifiedType())
.then(function (registry) {
return registry.add(order);
});
}
Absolutely. The annotation is essential for the function to work!
#param must state the class name of the transaction and the param name
#transaction declared underneath, with function to follow in block below
#param {org.acme.mynetwork.Foo} foo - the report to be processed
* #transaction
Please Replace the code in your logic.js file with following code and the error will surely be gone. Mine was the same problem, I just added the required JS doc annotations above the function and the same issue was resolved!
'use strict';
var NS = 'org.acme.sample';
/**
* #param {org.acme.sample} CompraDoVinho
* #transaction
*/
function onSampleTransaction(CompraDoVinho) {
CompraDoVinho.asset.preco = CompraDoVinho.precoVenda;
return getAssetRegistry('org.acme.sample.Vinho')
.then(function (assetRegistry) {
return assetRegistry.update(CompraDoVinho.asset);
});
}
Hope this helps you!

Delphi - Must I free all elements inside TObject before release itself?

Please, about this code:
type
TClient = class(TObject)
public
Host: String;
Queue: TIdThreadSafeStringList;
end;
var
Clients: TThreadList;
procedure TMain.FormCreate(Sender: TObject);
const
Hosts: Array[0..4] of String = (
'HOST1', 'HOST2', 'HOST3', 'HOST4, 'HOST5'
);
var
I: Integer;
List: TList;
Client: TClient;
begin
Clients := TThreadList.Create;
Clients.Duplicates := dupAccept;
for I := Low(Hosts) to High(Hosts) do
begin
Client := TClient.Create;
Client.Host := Hosts[I];
Client.Queue := TIdThreadSafeStringList.Create;
Clients.Add(Client);
Client := nil;
end;
end;
I would like to know if the correct way of releasing it memory is:
procedure TMain.FormDestroy(Sender: TObject);
var
I: Integer;
List: TList;
begin
List := Clients.LockList;
try
for I := 0 to List.Count - 1 do
TClient(List[I]).Free;
finally
Clients.UnlockList;
Clients.Free;
end;
end;
Or maybe like this:
procedure TMain.FormDestroy(Sender: TObject);
var
I: Integer;
List: TList;
begin
List := Clients.LockList;
try
for I := 0 to List.Count - 1 do
begin
TClient(List[I]).Queue.Free;
TClient(List[I]).Free;
end;
finally
Clients.UnlockList;
Clients.Free;
end;
end;
In other words, I would like to know if when I release an object (TClient) all elements (Queue) are released automatically, or must I do it manually.
Thanks!
The queue object needs to be destroyed when the client object is destroyed. However, the correct way to do this is to make the client class take charge of its members.
type
TClient = class
private
FHost: String;
FQueue: TIdThreadSafeStringList;
public
constructor Create(const Host: string);
destructor Destroy; override;
end;
....
constructor TClient.Create(const Host: string);
begin
inherited Create;
FQueue := TIdThreadSafeStringList.Create;
FHost := Host;
end;
destructor TClient.Destroy;
begin
FQueue.Free;
inherited;
end;
If you do it this way then it's not possible to instantiate the class and fail to instantiate its members. Do it your way, and every time you need to instantiate the class then you have to repeat the code to instantiate the members. It's just all too easy to make a mistake that way. What's more it makes the code harder to read and maintain.

Delphi - ValueListEditor not adding new row

I have a procedure which needs to read data from an ini file with the following format:
'Prices', [integer], [data to be read]
The data read consists of two pieces of information split by a '/' symbol. The data is split successfully when I call my procedure below.
I have a TValueListEditor (called ledtPrices) placed on the form and would like to add the values from the ini file to the List Editor. If I call ledtPrices.InsertRow via a button click, the values I enter into to add to the row are added and the list editor is refreshed.
However, when I call the same function from my RefreshPList procedure, the values are not added as new rows (the list editor is blank). I have tested my code with ShowMessage dialogues to ensure each part of the procedure is functioning when it should. My code is as follows:
procedure RefreshPList;
var
l: TValueListEditor;
xFile: TINIFile;
temprow, tl, tp: string;
tempr: TStringList;
i: integer;
begin
i := 0;
l := frmSettings.ledtPrices;
try
tempr := TStringList.Create;
tempr.StrictDelimiter := True;
tempr.Delimiter := '/';
xFile := TIniFIle.Create('C:\MData.ini');
try
temprow := xFile.ReadString('Prices', '0', 'xx');
if temprow = 'xx' then
ShowMessage('no prices saved')
else
begin
repeat
temprow := xFile.ReadString('Prices', IntToStr(i), 'xx');
if temprow <> 'xx' then
begin
tempr.DelimitedText := temprow;
tl := tempr[0];
tp := tempr[1];
l.InsertRow(tl,tp,true);
//ShowMessage(tl);
Inc(i);
end
else
ShowMessage('End of list');
until (temprow = 'xx');
//l.Refresh;
end;
finally
xFile.Free;
end;
LastLine := i;
finally
tempr.Free;
end;
end;
LastLine is a global integer value to be used later. I'm trying to add, remove and edit data within the list editor, without editing the cells directly. The procedure to add new data to the ini file has been written and runs successfully.
UPDATE
I've come to realise that any procedure that I create which tries to edit a components values does not edit the components values. Am I missing something simple here?
For example, I created a memo on the form and created a procedure which adds the contents of an array to the memo.lines. This procedure did not execute when called from a buttonclick. However, if I copy the contents of the procedure directly into the buttonclick and execute it, it works.
The procedures are called from buttonclick commands. The form is created from a mainform. The components all sit within a pagecontrol tabsheet.
A quick test application (XE5, VCL Forms) cannot reproduce the problem.
I start with a new blank application, drop a TValueListEditor and a TButton on the form, and use the Object Inspector to add two key/value combinations:
Key Value
--- -----
A Aaaaaaa
C Ccccccc
In the TButton.OnClick event, I use the following code:
procedure TForm1.Button1Click(Sender: TObject);
var
NewKey, NewValue: string;
begin
NewKey := 'B';
NewValue := 'Bbbbbbb';
ValueListEditor1.InsertRow(NewKey, NewValue, True);
end;
I run the application:
I click Button1, and the code successfully adds the new item at the end (bottom) of the TValueListEditor.
I change the last parameter to InsertRow to False, and it inserts it at the start (top) of the TValueListEditor.
This indicates that either you're not getting the values you expect from your ini file, or the code that inserts the new row isn't executing.
Here's the full code of the test app I created:
Unit1.dfm
object Form4: TForm1
Left = 0
Top = 0
Caption = 'Form4'
ClientHeight = 178
ClientWidth = 447
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object ValueListEditor1: TValueListEditor
Left = 40
Top = 16
Width = 306
Height = 137
Strings.Strings = (
'A=Aaaaaaa'
'C=Ccccccc')
TabOrder = 0
end
object Button1: TButton
Left = 352
Top = 16
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 1
OnClick = Button1Click
end
end
Unit1.pas
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Grids, Vcl.ValEdit;
type
TForm1 = class(TForm)
ValueListEditor1: TValueListEditor;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
NewKey, NewValue: string;
begin
NewKey := 'B';
NewValue := 'Bbbbbbb';
ValueListEditor1.InsertRow(NewKey, NewValue, True);
end;
end.

Releasing Variable used by WebService

How can I release a Variable that is being used by the WebService.
I'm using this form:
HttpPrincipal.WSDLLocation: = FrmPrincipal.edtWS.Text;
HttpPrincipal.Service: = 'CADServicesService';
HttpPrincipal.Port := 'CADServices';
Trinity: = HttpPrincipal as CADServices;
At the moment when I will close the Form appears an error, and discovered that when I declare this part:
Trinity: = HttpPrincipal as CADServices;
I think it is getting stuck in memory.
The error is the following:
"Invalid Pointer"
The error happens when you close the form, does not have any event in the OnClose or OnDestroy form.
Descriptions:
Trinity : CADServices,
HttpPrincipal is a THTTPRIO,
CADServices is my Unit containing all procedures / functions from WebService.
Instead of using the designtime component try to create HTTPRIO at runtime:
function GetCadServices(Addr : String): CadServices;
const
defSvc = 'CADServicesService';
defPrt = 'CADServices';
var
RIO: THTTPRIO;
begin
Result := nil;
RIO := THTTPRIO.Create(nil)
try
Result := (RIO as CadServices);
RIO.WSDLLocation := Addr;
RIO.Service := defSvc;
RIO.Port := defPrt;
finally
if (Result = nil) then
RIO.Free;
end;
end;
Usage:
Trinity := GetCadServices(FrmPrincipal.edtWS.Text);
If you imported the WSDL with the WSDL importer this code is automatically generated for you (look in the CadServices1 unit)