How to know an apoc procedure output name - stored-procedures

I am using some of the awesome apoc.refactor procedures. I noticed that in the documentation there is no mention of the output variables names that one can use with YIELDS.
I figured out that refactor.mergeNodes outputs node (as the new merged node), but I can't figure out what is the output name of refactor.to or refactor.from. I tried rel and newRel with no success. I am not a java programmer, but inspecting the code (especially RelationshipRefactorResult.java) I thought 'rel' was the one to go.
This is the query I am trying:
MATCH ()-[r]->(), (n)
WHERE id(r) = 16 AND id(n) = 4
CALL apoc.refactor.from(r,n) YIELD rel
RETURN rel
And this is the output message:
Unknown procedure output: `rel` (line 3, column 36 (offset: 96))
"RETURN rel"

Sorry that's a shortcoming of the current docs.
We want to automate / improve that.
You can see the output types if you CALL dbms.procedures()
CALL dbms.procedures() YIELD name, signature
WITH * WHERE name STARTS WITH 'apoc.refactor'
RETURN name, signature
The signature is always name :: TYPE
e.g. in your case:
apoc.refactor.to(relationship :: RELATIONSHIP?, newNode :: NODE?)
:: (input :: INTEGER?, output :: RELATIONSHIP?, error :: STRING?)
Parameters:
Name: relationship, Type: Relationship
Name: newNode, Type: Node
Output Columns:
Name: input, Type: Integer
Name: output, Type Relationship
Name: error, Type: String

Building on the answer of Michael, here is a little tool.
Just a query that builds tidy documentation for these. :)
Maybe it can be adapted to work for others procedures too.
//EXPOSED YIELD PARAMETERS OUTPUT FROM apoc.periodic...
CALL dbms.procedures() YIELD name, signature
WITH * WHERE name STARTS WITH 'apoc.periodic'
RETURN name AS procedure, 'YIELD',
tail(
apoc.text.split(
toString(apoc.text.split(signature, '\\)')[1])
, '[^a-z]+[^a-zA-Z]*[^a-z]')
) AS exposed_parameters

Related

RuntimeError when using pydrake.symbolic.Evaluate

When trying to use evaluate to retrieve the numerical value of my continuous decision variable q, I get the following error:
RuntimeError: The following environment does not have an entry for the variable q(0,0)
Trying to get the value of a single entry in q (akin to the answer given here), results in this error:
TypeError: Evaluate(): incompatible function arguments. The following argument types are supported:
1. (m: numpy.ndarray[object[m, n]], env: Dict[pydrake.symbolic.Variable, float] = {}, generator: pydrake.common._module_py.RandomGenerator = None) -> numpy.ndarray[numpy.float64[m, n]]
2. (m: numpy.ndarray[object[m, n]], env: Dict[pydrake.symbolic.Variable, float]) -> numpy.ndarray[numpy.float64[m, n]]
Invoked with: Variable('q(0,0)', Continuous)
What am I missing?
Evaluate(expression) will only provide output if the Expression is a constant. To get the values of your decision variables, use MathmaticalProgramResult.GetSolution.
There are many great tutorials on using MathematicalProgram here: https://github.com/RobotLocomotion/drake/blob/master/tutorials/README.md

Cypher: How to get all possible variable-length chains and output concatenated string of node properties?

First time using neo4j. This is what my graph looks like.
The central node is of type Job, and the child nodes are of type Word. Each Word node has property word (i.e. Word.word), which is equivalent to the node labels such as "react", "php" etc. in the attached image.
What I am trying to do is for each chain of child nodes, generate a concatenated string of Word.word property values. For example, for the attached graph, I want to return something like:
[ "php", "react js", "javascript", "full stack development", "multithreaded load-balancing reactor engine"]
My current brute force approach looks like this:
match (webdev:Job {name:"Web Developer"}),
(webdev)-[a00:Appearance]->(w1:Word),
(w1)-[a01:Appearance]->(w2:Word),
(w2)-[a02:Appearance]->(w3:Word)
return w1.word + ' ' + w2.word + ' ' + w3.word as name
union
match (webdev:Job {name:"Web Developer"}),
(webdev)-[a00:Appearance]->(w1:Word),
(w1)-[a01:Appearance]->(w2:Word)
where not ((w2)-->())
return w1.word + ' ' + w2.word as name
union
match (webdev:Job {name:"Web Developer"}),
(webdev)-[a00:Appearance]->(w1:Word)
where not ((w1)-->())
return w1.word as name
which produces the output:
["multithreaded load-balancing reactor","full stack development","react js","php","javascript"]
This works for chains of length <= 3, but obviously it fails for length > 3. Notice how the string "multithreaded load-balancing reactor" should be "multithreaded load-balancing reactor engine".
My question is: how to I generalize this for all chains of variable length?
You can use a variable length relationship pattern:
MATCH p = (:Job {name:"Web Developer"})-[:Appearance*]->(leaf)
WHERE NOT (leaf)-[:Appearance]->()
RETURN REDUCE(s = NODES(p)[1].word, w IN NODES(p)[2..] | s + ' ' + w.word) AS name

Why does pattern matching records in Erlang throw error

Hello i am trying to use record syntax in Erlang to both bind a variable to the enclosing record and to one of its fields and i keep getting this error:
d:/Erlang/AeRlang/rec.erl:19: syntax error before: child
d:/Erlang/AeRlang/rec.erl:17: function isMale/1 undefined
d:/Erlang/AeRlang/rec.erl:17: Warning: function maleChildren/1 is
unused
-module(rec).
-export([makeFam/0]).
-record(man,{name,
age,
children=[]}).
-record(child,{
name,
age,
sex
}).
makeFam()->
#man{name="Adrian",
age=33,
children=[#child{name="Daniel",age=33,sex="Male"},
#child{name="Chris" ,sex="Male"},
#child{name="Anne",age=33,sex="Female"}]
}.
fatherAndSons(Man#man{children=Ch})->{Man,[Elem|| Elem<-Ch,isMale(Elem)]}.
isMale(C#child{_,_,Y})->
case Y of
"Male"->true;
_ ->false
end.
What is wrong in my isMale method.I am binding the enclosing structure #child to the variable C and i am also pattern matching on of its fields.What is the problem ?
P.S: Does it have to do with the fact that in isMale method i did not specify the name of the field of which i binded the variable Y?
For both the fatherAndSons/1 and isMale/1 functions, you're using the wrong syntax for matching a record as a function argument. Here are some corrected versions:
fatherAndSons(Man=#man{children=Ch}) -> {Man, [Elem || Elem <- Ch, isMale(Elem)]}.
isMale(#child{sex="Male"})-> true;
isMale(_) -> false.
In the function head for fatherAndSons/1 we're essentially saying "Man is expected to be a #man record whose children field we'll bind to Ch". The = operator is what does the matching, but you were missing that for the Man binding.
For isMale/1 I'm using two clauses, where the first clause matches all #child records whose sex field matches the string "Male"and the second clause matches anything else. If you want to keep the second clause restricted to matching only #child records, you could instead use:
isMale(#child{sex="Male"})-> true;
isMale(#child{}) -> false.
Either way, note that there's no need to bind the record to a variable in the function head for isMale/1 since you don't need the binding in the function body.
There are some wrongs in the way get sex in your child record. You can change your functions fatherAndSons/1 and isMale/1 like below:
fatherAndSons(Man#man{children=Ch})->{Man,[Elem|| Elem<-Ch,isMale(Elem) == true]}.
isMale(C)->
case C#child.sex of
"Male"->true;
_ ->false
end.
Or just remove your isMale/1 and pattern matching in list comprehension:
fatherAndSons(Man#man{children=Ch})->
{Man, [Elem || Elem <-Ch, Elem#child.sex == "Male"]}.

What does (_,[]) mean?

I was given a question which was:
given a number N in the first argument selects only numbers greater than N in the list, so that
greater(2,[2,13,1,4,13]) = [13,4,13]
This was the solution provided:
member(_,[]) -> false;
member(H,[H|_]) -> true;
member(N,[_,T]) -> member(N,T).
I don't understand what "_" means. I understand it has something to do with pattern matching but I don't understand it completely. Could someone please explain this to me
This was the solution provided:
I think you are confused: the name of the solution function isn't even the same as the name of the function in the question. The member/2 function returns true when the first argument is an element of the list provided as the second argument, and it returns false otherwise.
I don't understand what "_" means. I understand it has something to do with pattern matching but I don't understand it completely. Could someone please explain this to me
_ is a variable name, and like any variable it will match anything. Here are some examples of pattern matching:
35> f(). %"Forget" or erase all variable bindings
ok
45> {X, Y} = {10, 20}.
{10,20}
46> X.
10
47> Y.
20
48> {X, Y} = {30, 20}.
** exception error: no match of right hand side value {30,
20}
Now why didn't line 48 match? X was already bound to 10 and Y to 20, so erlang replaces those variables with their values, which gives you:
48> {10, 20} = {30, 20}.
...and those tuples don't match.
Now lets try it with a variable named _:
49> f().
ok
50> {_, Y} = {10, 20}.
{10,20}
51> Y.
20
52> {_, Y} = {30, 20}.
{30,20}
53>
As you can see, the variable _ sort of works like the variable X, but notice that there is no error on line 52, like there was on line 48. That's because the _ variable works a little differently than X:
53> _.
* 1: variable '_' is unbound
In other words, _ is a variable name, so it will initially match anything, but unlike X, the variable _ is never bound/assigned a value, so you can use it over and over again without error to match anything.
The _ variable is also known as a don't care variable because you don't care what that variable matches because it's not important to your code, and you don't need to use its value.
Let's apply those lessons to your solution. This line:
member(N,[_,T]) -> member(N,T).
recursively calls the member function, namely member(N, T). And, the following function clause:
member(_,[]) -> false;
will match the function call member(N, T) whenever T is an empty list--no matter what the value of N is. In other words, once the given number N has not matched any element in the list, i.e. when the list is empty so there are no more elements to check, then the function clause:
member(_,[]) -> false;
will match and return false.
You could rewrite that function clause like this:
member(N, []) -> false;
but erlang will warn you that N is an unused variable in the body of the function, which is a way of saying: "Are you sure you didn't make a mistake in your function definition? You defined a variable named N, but then you didn't use it in the body of the function!" The way you tell erlang that the function definition is indeed correct is to change the variable name N to _ (or _N).
It means a variable you don't care to name. If you are never going to use a variable inside the function you can just use underscore.
% if the list is empty, it has no members
member(_, []) -> false.
% if the element I am searching for is the head of the list, it is a member
member(H,[H|_]) -> true.
% if the elem I am searching for is not the head of the list, and the list
% is not empty, lets recursively go look at the tail of the list to see if
% it is present there
member(H,[_|T]) -> member(H,T).
the above is pseudo code for what is happening. You can also have multiple '_' unnamed variables.
According to Documentation:
The anonymous variable is denoted by underscore (_) and can be used when a variable is required but its value can be ignored.
Example:
[H, _] = [1,2] % H will be 1
Also documentation says that:
Variables starting with underscore (_), for example, _Height, are normal variables, not anonymous. They are however ignored by the compiler in the sense that they do not generate any warnings for unused variables.
Sorry if this is repetitive...
What does (_,[]) mean?
That means (1) two parameters, (2) the first one matches anything and everything, yet I don't care about it (you're telling Erlang to just forget about its value via the underscore) and (3) the second parameter is an empty list.
Given that Erlang binds or matches values with variables (depending on the particular case), here you're basically looking to a match (like a conditional statement) of the second parameter with an empty list. If that match happens, the statement returns false. Otherwise, it tries to match the two parameters of the function call with one of the other two statements below it.

confusion regarding erlang maps, lists and ascii

This code is an excerpt from this book.
count_characters(Str) ->
count_characters(Str, #{}).
count_characters([H|T], #{ H => N }=X) ->
count_characters(T, X#{ H := N+1 });
count_characters([H|T], X) ->
count_characters(T, X#{ H => 1 });
count_characters([], X) ->
X.
So,
1> count_characters("hello").
#{101=>1,104=>1,108=>2,111=>1}
What I understand from this is that, count_characters() takes an argument hello, and place it to the first function, i.e count_characters(Str).
What I don't understand is, how the string characters are converted into ascii value without using $, and got incremented. I am very new to erlang, and would really appreciate if you could help me understand the above. Thank you.
In erlang the string literal "hello" is just a more convenient way of writing the list [104,101,108,108,111]. The string format is syntactic sugar and nothing erlang knows about internally. An ascii string is internally string is internally stored as a list of 32-bit integers.
This also becomes confusing when printing lists where the values happen to be within the ascii range:
io:format("~p~n", [[65,66]]).
will print
"AB"
even if you didn't expect a string as a result.
As said previously, there is no string data type in Erlang, it uses the internal representation of an integer list, so
"hello" == [$h,$e,$l,$l,$o] == [104|[101|[108|[108|[111|[]]]]]]
Which are each a valid representation of an integer list.
To make the count of characters, the function use a new Erlang data type: a map. (available only since R17)
A map is a collection of key/value pairs, in your case the keys will be the characters, and the values the occurrence of each characters.
The function is called with an empty map:count_characters(Str, #{}).
Then it goes recursively through the list, and for each head H, 2 cases are posible:
The character H was already found, then the current map X will match with the pattern #{ H => N } telling us that we already found N times H, so we continue the recursion with the rest of the list and a new map where the value associated to H is now N+1: count_characters(T, X#{ H := N+1 }.
The character H is found for the first time, then we continue the recursion with the rest of the list and a new map where the key/value pair H/1 is added: count_characters(T, X#{ H => 1 }).
When the end of the list is reached, simply return the map: count_characters([], X) -> X.

Resources