Using and testing FetchByKey in Daml - daml

I have problems in retrieving contracts in Daml. Here is what I want to do:
I create an asset
I update the asset (new docs, new descriptions …)
Then I want to retrieve the last updated contract (if there are any updates, otherwise the original contract)
Finally I want to sell the last updated contract
I have created the asset and the code to update the asset. However, it's not clear to me how to use and test the fetchByKet function. So when I want to test FetchByKey to retrieve the contract, my code doesn't work.
My understanding is that I need to create an Helper template to use FetchByKey. Please my code below.
template AssetHelper
with
p : Party
where
signatory p
choice FetchAssetByKey : (ContractId Asset, Asset)
with
assetKey : (Party, Text)
controller p
do
fetchByKey #Asset assetKey
I don't know if my process is correct and I don't know how test the script as it gives me an error and it doesn't compile.
I am learning Daml for my PhD but I dont' have a background in coding.

Related

stable-baseline3, gym, train while also step/predict

With stable-baselines3 given an agent, we can call "action = agent.predict(obs)". And then with Gym, this would be "new_obs, reward, done, info = env.step(action)". (more or less, maybe missed an input or an output).
We also have "agent.learn(10_000)" as an example, yet here we're less involved in the process and don't call the environment.
Looking for a way to train the agent while still calling "env.step". If you wander why, just trying to implement self play (agent and a previous version of it) playing with one environment (for example turns play as Chess).
WKR, Oren.
But why do you need it? If you take a look at the implementation of any learn method, you will see it is nothing more than an iteration over time steps calling collect_rollouts and train with some additional logging and setup at the beginning for, e.g., further saving the agent etc. Your env.step is called inside collect_rollouts.
I'd better suggest you to write a callback based on CheckpointCallback, which saves your agent (model) after N training steps and then attach this callback to your learn call. In your environment you could instantiate each N steps a "new previous" version of your model by calling ModelClass.load(file) on the file saved by a callback, so that finally you would be able to select actions of the other player using a self-play in your environment

Yii2 MySQL table names and HTTP404 errors

I am new to the Yii technology, but having worked in IT for many years I hoped I understood generally how software is designed. But the Yii guys have got me good.
I have been trying to understand why I am getting HTTP404 errors.
I have a Win 10 environment using MySQL, with latest versions of MySQL and PHP. I naively assumed that Yii would support the nomenclature standards for objects in the MySQL, especially table names. However it appears I am wrong, so here is a heads up for those interested.
I use all lower case characters for my table names, and where I have a table that is a link between two entities I use a name such as entity1_entity2. I use gii to generate a model from this table and the the CRUD option to generate a basic initial application.
I have the pretty URL active, so the initial URL I use is :
\localhost\Movies\movieactor
Where Movies is the website name defined to IIS (virtual directory) and movieactor is the lowercase name I used for the model and controller objects. The actual name prefix I used was MovieActor for the model and controller objects. I used movieactor for the view sub-directory.
Much to my surprise I was presented with an HTTP404 error message. I spent most of a day trying to understand what I had done wrong.
After a good sleep, I started again the next morning. I decided to try changing the name of the relevant table from movie_actor to movieactor. I then regenerated the model, and CRUD components. This time when I invoked the same URL I was pleasantly surprised to see the web application show me the data.
I have no idea what restrictions the Yii guys place on table names, no documentation that I can find, but this is my experience, and hopefully it may save others some grief.
Have I missed something or is my story correct ?
First of all in Yii there are no requirements for table names. Table or model class names are not directly related to URLs.
When you are using gii to generate model you are prompted to enter the table name. Then the model name is suggested as pascal case version of table name. For example if your table is named movie_actor the suggested model class name will be MovieActor. But you don't have to accept that, you can change it to whatever you want.
Then when you are generating CRUD it asks you to enter model class, search model class and controller class. The model class here is the MovieActor generated earlier. Search model class can be whatever you like, but what is important for URL is the controller class. There is something called controller id based on controller class name. It's a kebab case version of controller class name after removing the Controller suffix. If you name your controller as MovieActorController its controller id will be movie-actor. It's this controller id that you need to use in your url to reach that controller. So, url you have to use for this controller would be \localhost\Movies\movie-actor. You need to use same controller id as a sub-directory for your views too.
When you've decided to change table name to movieactor you've probably also generated the controller as MovieactorController. Because of that its id was movieactor and your url and views subfolder were correct.

Accessing regmap RegFields

I am trying to find a clean way to access the regmap that is used with *RegisterNode for creating documentation and testing files. The TLRegisterNode has methods for generating the json through some Annotations. These are done in the regmap method by adding them to the ElaborationArtefacts object. Other protocols don't seem to have these annotations.
Is there anyway to iterate over the "regmap" Register Fields post elaboration or during?
I cannot just access the regmap as it's not really a val/var since it's a method. I can't quite figure out where this information is being stored. I don't really believe it's actually "storing" any information as much as it is simply creating the hardware to attach the specified logic to the RegisterNode based logic.
The JSON output is actually fine for me as I could just write a post processing script to convert JSON to my required formats, but I'm wondering if I can access this information OR if I could add a custom function call at the end. I cannot extend the case class *RegisterNode, but I'm not sure if it's possible to add custom functions to run at the end of the regmap method.
Here is something I threw together quickly:
//in *RegisterRouter.scala
def customregmap(customFunc: (RegField.Map*) => Unit, mapping: RegField.Map*) = {
regmap(mapping:_*)
customFunc(mapping:_*)
}
def regmap(mapping: RegField.Map*) = {
//normal stuff
}
A user could then create a custom function to run and pass it to the regmap or to the RegisterRouter
def myFunc(mapping: RegField.Map*): Unit = {
println("I'm doing my custom function for regmap!")
}
// ...
node.customregmap(myFunc,
0x0 -> coreControlRegFields,
0x4 -> fdControlRegFields,
0x8 -> fdControl2RegFields,
)
This is just a quick example I have. I believe what would be better, if something like this was possible, would be to have a Seq of functions that could be added to the RegisterNode that are ran at the end of the regmap method, similar to how TLRegisterNode currently works. So a user could add an arbitrary number and you still use the regmap call.
Background (not directly part of question):
I have a unified register script that I have built over the years in which I describe the registers for a particular IP. It works very similar to the RegField/node.regmap, except it obviously doesn't know about diplomacy and the like. It will generate the Verilog, but also a variety of files for DV (basic `defines for simple verilog simulations and more complex uvm_reg_block defines also with the ability to describe multiple of the IPs for a subsystem all the way up to an SoC level). It will also print out C Header files for SW and Sphinx reStructuredText for documentation.
Diplomacy actually solves one of the main issues I've been dealing with so I'm obviously trying to push most of my newer designs to Chisel/Diplo.
I ended up solving this by creating my own RegisterNode which is the same as the rocketchip RegisterNodes except that I use a different Elaboration Artifact to grab the info and store it for later.

Acess variables in DAML template like in an object

Okay am new to DAML and have extensive Java programming experience. Now, I have a question. In Java, 'Class A' has 'Class B', and like this, A can use 'state' of 'Class B'. I want to do something like this in DAML too.
If I think template as a class, 'contract id' is an instance of it, and the 'state' of the template (the things we declare in 'with') should be accessible in another template when passed in as a parameter, but I get compilation errors in the code I wrote.
One way to proceed is to send 'party' as a parameter instead of contract ID, and then try to access the party in contract, but I wanted to check if/ what is wrong with this!
Thanks in advance!
First template
daml 1.2
module RFP where
template RFP
with
requestorCEO: Party
where
signatory requestorCEO
Second template
daml 1.2
module InternalComm where
import RFP
template InternalComm
with
-- RFP is sent in as a parameter to this template.
rfpContractID: ContractId RFP
where
-- Here with this, I'm trying to say that the CEO who would be approving
-- an RFP is the signatory for internal communications too. It is the
-- below line that fails with compilation error.
signatory rfpContractID.requestorCEO
This is the actual error message I get for aforementioned problem. Any thoughts would be greatly appreciated!
No instance for (DA.Internal.Record.HasField
"requestorCEO" (ContractId RFP) a0)
In DAML the RFP template gives you a type RFP on which you can project out the fields (just like Java), and a type ContractId RFP which is more like a pointer to the contract on the ledger. You can "dereference" the ContractId to get the RFP using the function fetch. However, that function runs in an Update, so can't be called from a signatory. I suspect you need to change InternalComm to take a RFP without the ContractId wrapper.
This is how it worked - just remove ContractId from whole of the template.
module InternalComm where
import RFP
template InternalComm
with
-- ContractId to be removed from below line, and compilation error is resolved.
-- rfpContractID: ContractId RFP
rfpContractID: RFP
where
signatory rfpContractID.requestorCEO

CODED UI TESTS: is there a way to add a warning in html report?

In my Coded UI Test project, I need to check if few Labels or Messages are consistent with the context. But those checks are not critical if not consistent and I need to output them only as warnings.
Note that I'm using nested ordered tests to use only one global ordered test with vstest.console.exe and get in one shot the overall test coverage report.
Till now I was creating assertions to check those consistencies, but an assertion failure leads to Test failure, then to ordered test failure and then to playback stop.
I tried to change Playback.PlaybackSettings.ContinueOnError value before and after the assertion: this works as I expect as the assertion is well reported as a warning in the html report file. But whatever, it causes the ordered test to stop and then my global ordered test chaining to fail...
I tried to use TestContext.WriteLine too instead of creating assert, but it seems that this is not output in the html report.
So my question is:
is there any way to create an assertion only as a Warning that will be output in the html report file and that doesn't lead to a test failure?
Thanks a lot for any answer and help on this ;)
So I got my solution with developping my own Warning Engine to integrate Warnings in test report, 'cause I found no existing solution for that with the current Coded UI Test Assertion engine.
I'll try to take some time to post generic parts of the code structure with comments translated in english (we're french so default comments are french for now...), but here are the main step lines :
Create a template based on the UITestActionLog.html original file
report structure of Coded UI Test engine, with only the start
bloc and the javascript functions and CSS declarations in it.
Create an assertion class with a main function to manage insertion
of Warning html bloc in the html report first created from the template.
Then create custom assert functions to call the main function
whereever on runtime, and custom Stopwatch to inject elapsed time in
the report ('cause I could'nt found a way to get back the elapsed
time directly from the Coded UI Test engine).
That's it.
Just a proposition as a way to do it, maybe not the best one but it worked for me. I'll try to take time to put blocl codes to be clearer on it.