Can I extend Memgraph with a custom function? - memgraphdb

I see that Memgraph has a bunch of functions include, but how do I define my own function and use it inside Memgraph? My goal is to inject my custom function that that I can use expression.

Basically you nned to implement a custom query module with any number of functions, as an example you can take a look under MAGE documentation.
You can use so called "Memgraph Magic Functions" for this. Magic functions are user-defined and operate similarly to read and write procedures. However, they are meant for different purposes and should not alter the graph in any way (creating, deleting, or updating objects).
Below is an example of how to create and run a function, demonstrating a basic scenario of fetching arguments and returning values as a list:
#mgp.function
def func_example(context: mgp.FuncCtx,
argument: mgp.Any,
opt_argument: mgp.Nullable[mgp.Any] = None):
return_arguments = [argument]
if opt_argument is not None:
return_arguments.append(opt_argument)
# Note that we do not need to specify the result Record as long as it is a
# Memgraph defined value type.
return return_arguments

Related

dynamic parameters on datasets in Kedro

I would like to call an API to enrich an existing dataset.
The existing dataset is a CSVDataSet configured in the catalog.
Now I would like to create a Node, that enriches the CSVDataSet with data from the API, that I have to call for every row in the CSV file. Then save the data into a database (SQLTableDataSet). My approach is to create an APIDataSet entry in the catalog and provide it as an input for the node, next to the CSVDataSet.
The issue here is, the APIDataSet is static (in general the DataSets seem to be very static). I need to call the load function at runtime within the Node for every entry in the csv file.
I didn't find a way to do this. Is it just a bad approach? Do I have to call the API within the Node instead of creating a APIDataSet?
So typically, we don't like our nodes having knowledge of IO configuration. The belief is that functionally pure python functions are easier to test, maintain and build.
Typically the way we would keep this distinction would be for you to subclass our APIDataSet / CSVDataSet or both and then add your custom logic to do it all there.
I have done this in my GDALRasterDataSet implementation. The idea is that if you need to enrich a dataset on the go, you can overload the load() method in a custom dataset and pass additional parameters there.
You can see an implementation here and an example of usage here.
The only extra thing you need to do is to re-write the load() method to accept kwargs (line 143) and write your own _load method that enriches your dataset. Everything else is boilerplate.

Can we dynamically create a function in go?

I'm trying to create a service where a user needs to operate over a data and can manipulate it in numerous ways, so I'm not aware of the manipulations at the time of compiling of my program. One way to achieve this is to give the user a function with data as param. Which landed me in the following direction.
Dynamically create a function
Dynamically linking a function after compiling it separately.
I'm open to suggestions. If you have other ways to achieve the end goal.
If you don't like this as an answer I can move to the comment section, but it's rather long that's why I put here in the answer section.
Dynamically Dispatched Method: The only way to have dynamically dispatched methods is through an interface. Methods on a struct or any other concrete type are always resolved statically.
Closure: Go supports anonymous functions, which can form closures. Anonymous functions are useful when you want to define a function inline without having to name it.
Dyncamically call method on Interface:
Please let me know if that helps you to understand the concept in golang.

What is the best way to pass variables among tests from data entry till validation?

I have some test cases for a web application in Robot Framework. In some cases I define a unit and then validate this action by checking database and GUI. The variables which I use in define, should be available in validation in order to check details; keep in mind they are randomly generated in the test case. I have three approaches in mind to pass variables from define to validation:
Make variables global and use their global names further; it makes the scenarios ambiguous since the reader can't detect where did this variable come from without checking inner steps.
Pass variables to both define and validation keywords; scenarios look weird when vast number of parameters are required.
Save variables in a dictionary and pass it to both define and validation keywords.
Which one is the best? Are there any other ways to do the process? Are there any other pros and cons which I've forgotten?
3rd option is better to storing variables in dictionary.
There is similar way out also
Consider following is keyword
My Keyword
[Argument] #{data}
// get respective values from keys and use further for validation
${value1}= Get Template Value From List ${Key1} #{data}
${value2}= Get Template Value From List ${Key2} #{data}
Call above keyword as follows
*** Test Cases ***
Test data
My Keyword
... key1=value1
... key2=value2
So above code will increase readability as you know what kind of data your test case is using
As you are passing data at your test case level, you don't need to go anywhere else to find data.
I finally use a combination of #2 & #3. I used #3 in the top level of test cases and #2 for some inner keywords which don't need all the data and a subclass of that is sufficient for them.

Trouble with complex routing rule

I have a lookup table called BlockCustomer. I also have an FTP Adapter that picks up files from multiple customers. I need to be able to determine the customer from the source of the file and do a lookup on the table. If BlockCustomer.Customer1 = 0 then it will send it to it's target, otherwise it will do nothing.
If I could use javascript I would do something like this:
WHEN Lookup(BlockCustomer,HL7.Source.split("/incoming/")[1].split("/")[0]),1) = 0
But obviously I can't. I found $ZSTRIP but I'm not sure if or how it will work. Is this possible or am I going to have to create a custom class?
In Cache we use function $piece if needs to get some parts of string by delimiter. For rule you could use the same function called Piece, with the same arguments. So you conditions should looks like:
Lookup(BlockCustomer,Piece(HL7.Source,"/incoming/",2),1)=0
By the way if you think, that you need some specific functions for you, you can do it by developing it. Just extend the class Ens.Rule.FunctionSet and add a method. And function will appear with the same name. As an example you can see at Ens.Util.FunctionSet class, which contains almost all available functions.

Ruby Style: should initialize take a file with data or just the raw data as parameters

I was curious if anyone had insight on what is the best way for an object to load data from a file in Ruby. Is there a convention? There are two ways I can think of accomplishing this:
Have the initialize method accept a path or file and parse the data within the initialize method, setting the object variables as well.
Have the main "runner" code open the file and parse it, then pass the correct arguments to your constructor.
I am also aware that I could support both methods through an options hash or *args and looking at its size, but I do not have any need to implement both.
I would use the second option combined with providing the path info as an argument to the main code. This makes it more portable and keeps the object de-coupled from the source of the data

Resources