Recursive call in neo4j - recursion

I am trying to trace messages in a graph of messages. For example, node A sends message to node B which sends message to node C (and so on), how can I devise a query in Cypher that will keeping calling the next node until a terminal node is reached.
A -> B -> C -> D -> E -> F
start search is A, returns a list containing B,C,D,E,F (in the classic neo4j graph visualisation where these nodes are connected because B sent message to C and so on til F.
The code I have is
MATCH p=(a { address: "A" })-[r]->(b)
RETURN *
This only returns me A and the nodes A sent a message to. How can I modify it to accomplish the recursive call I am seeking.
Note: I have referred to this post and browsed the neo4j manual. However, I still don't get it (either could not find the answer or perhaps I am not 'getting it'). Any help is truly appreciated!

This call:
MATCH p=(a { address: "A" })-[r*]->(b)
RETURN b;
will match as many hops away from A as you want, because of the asterisk on the relationship. The b variable will end up being everything that's downstream of a.
I'm not sure what you mean by "call the next node". This will just return the data behind that node. You don't actually need recursion to do this at all with cypher and neo4j. Rather, you should just ask cypher for what data you want, and it will get it for you. If you were implementing this stuff on a non-graph database, you might use recursion as part of a depth-first or breadth-first search, but it simply isn't necessary with a graph DB. The query language handles all of that for you.

Related

Want two Vertices off the same Edge in Gremlin

I have a graph like this
V('Producer')-E('RESPONSIBLE_PRODUCER)->V('Event')<-E('INSPECTED')-V('Engineer')
V('Event')<-E('ALIGNED_PRODUCER')-V('Producer')
That is, each 'Event' vertex has two incoming edges: one that terminates at an 'Engineer' vertex and another that terminates at a 'Producer' vertex. But the function of the Producer vertices are different depend on the edge label.
I want to get the originating Producer, the Event, Engineer and terminating Producer.
I have this gremlin code:
g.V().hasLabel('Producer').
as('responsible').
has('ProdId', 1234567).
out("RESPONSIBLE_PRODUCER").hasLabel('Event').as('event').
in("INSPECTED").hasLabel('Engineer').as('engineer').
select('responsible', 'event', 'engineer').
by(valueMap('name')).by(valueMap('name')).by(valueMap('name'))
That is, I chose a given Producer and get the Event and Engineer then return some details about each of those vertices.
I also want the Producer aligned to the Event in the same query but am not sure how to do this.
Any help is greatly appreciated.
You are looking for project:
g.V().has('Producer', 'ProdId', '1').as('r').
out("RESPONSIBLE_PRODUCER").hasLabel('Event').
project('responsible', 'event', 'engineer', 'aligned').
by(select('r').values('name')).
by(values('name')).
by(in('INSPECTED').values('name')).
by(in('ALIGNED_PRODUCER').values('name'))
You can see a "live" example of your problem here

Alexa adding extra character to slot value

​I am trying to get user response with option of a b c d or e . I have configured a slot with these possible values and reading the slot in my nodejs. when a user responds with the option a, c,d,e are returned ok in intent.slots.Answer.value (although option "a" is returned as lower case in slot value and C,D and E returns in upper case) but the bigger issue is option "b" is returned with an extra dot (.) appended like this "b." While I can parse it out, I hate to patch it before I know what is causing it. I have done console.log of slot values before any manipulation and it is as per above description. has anyone experienced this?
The slot values you provide are getting used when the language model get's build. But there is no guarantee, that those values will be recognized by alexa and forwarded to your intent. It could by anything alexa understands. I guess alexa is considering a, c, d and e as words, but in the case of b, she understands it's a single letter (which get's returned as "B."). Why? Welcome to the mysteries of black box Alexa :)
I would clean the value like this:
value.toLowerCase().replace(/\./, '')
Building on unnu's answer... I would just use whole word options like One, Two, Three...

How to save Remote Party ID to CDR in Asterisk?

I'm new to Asterisk so any help will be greatly appreciated.
I'm trying to save remote party ID (CONNECTEDLINE) in CDR logs table in transferred calls. In blind transfers there's no problem because I'm getting Remote Party ID as Caller ID in src field.
In attended transfers I'm loosing the original caller ID.
This is how transfer goes:
A calls B (A talking with B)
B holds A, and calls C (B talking with C)
B transfers A to C (A talking with C)
Now, in CDRs table I'm getting two records. One for the first call (A<->B), and another for the two second calls (B<->C and A<->C). The point is in second CDR I have lost any reference to A.
I want to save Remote Party ID (A) in second CDR.
I've already added a custom field to CDR table (connectedID).
I'm reading about editing some configuration files, adding this kind of sentences:
"exten => s,1,set(CDR(connectedID)=${CONNECTEDLINE})"
However, I'm quite lost. I don't know which file I need to edit, even in what part of file I should put these lines of code.
Could somebody point me in the right direction?
You can use Func_SHARED,save cid in shared variables and do lookup by bridged channel name
However that all require debugging and your own effort.
http://www.voip-info.org/wiki/view/Asterisk+func+shared
Other option is collect events and remmember all transfers
Reading call events via AMI (thanks to #arheops) I manage how to save Remote Party ID in CDR.
In hangup event we can see Remote Party ID in ConnectedLineNum field.
For a transferred call like this one:
401 calls 208
208 calls 308 (401 on hold)
208 transfers original call to 308 (401 talking with 308)
this is a hangup event sample for the final segment of the call.
Event Hangup
Privilege: call,all
Channel: SIP/308-00000665
Uniqueid: 1421757614.1658
CallerIDNum: 208
CallerIDName: Juan Ruiz
ConnectedLineNum: 401
ConnectedLineName: Test1
Cause: 16
Cause-txt: Normal Clearing
Original caller is stored in ConnectedLineNum variable.
So I add this line to the hangup section:
exten => s,1,Set(CDR(connectedid)=${CONNECTEDLINE(number)})
I'm using Elastix 2.4.0, so I've added this line at the beggining of the [macro-hangupcall] macro in extensions_override_elastix.conf file.
In other Asterisk based distros it should be in another file.

Prolog: Recursion and splitting lists

I'm trying to write a game in which the program reads through a database, grabs attributes and asks the player if the Movie that the player is thinking of will have that attribute. The user will answer either yes or no and if yes, the game will ask about the next attribute until either the user has said yes to every attribute, at that point the program will output the name of the movie. Or if the user says no, the game will then move along to the next movie in the database and start asking about those attributes.
The game ends when either it finds a movie with all the attributes or there are no more movies in the database in which the program could ask about.
Here is the database file.
object_properties(crocodile_dundee,[comedy,australian,paul_hogan_movie]).
object_properties(flipper,[action_movie,australian, paul_hogan_movie,
-comedy]).
object_properties(jackass,[comedy,-australian]).
object_properties(the_godfather,[drama,crime,-character_batman]).
object_properties(the_dark_knight,[drama,crime,character_batman]).
object_properties(the_living_planet, [documentary,director_attenborough]).
object_properties(the_code, [documentary,nerds_love_it]).
object_properties(the_russian_revolution, [documentary,about_history]).
Here is my game file.
go():-
guess_object(0,[]).
guess_object(Obj,[A|As]) :-
object_categories(Obj,A).
guess_object(1).
guess_object(2).
guess_object(3).
guess_object(4).
guess_object(5). %how do i do this specifically for the current Obj attributes?
guess(X) :- guess_object(_, X),
writeln('Does your object have the attribute '(X)'?'),
read(Input), Input=('yes') -> assert(object(X)) ; retractall(X), guess(X).
writeln('I guess that the film is: 'guess_object(Obj,_).
The main questions I have:
How do I split the object attributes in to a list like I'm trying to do on guess_object(1) through to guess_object(5) depending on whether the object has 2 attributes or 4 attributes, etc.
How do I make my guess(A) predicate recursive so once the user says no it will then go to the next object in the database file?
I'm not too great at prolog but any advice or pointers would be greatly appreciated, even if it's just pointing out a stupid mistake I may have made. I will also answer any questions anyone has to the best of my ability.

signalR message structure flags clarification?

I've read here about the structure of signalR's response message :
for example
For PersistentConnection
{"C":"B,2CE|K,C|L,2|M,0|I,0|J,0","M":["foo"]}
Where
Persistent Response:
C - cursor
M - Messages
T - Timeout (only if true) value is 1
D - Disconnect (only if true) value is 1
R - All Groups (Client groups should be reset to match this list exactly)
G - Groups added
g - Groups removed
Question #1
What's wrong with sending only the message part ? why do i need all the "C" information ? The client only needs the message. A message number #N is not dependent with message number #N-1 (AFAIK) -- so I dont see the reason for this "C" section. ( and I assume Im wrong by missing something here).
Question #2
Even so , how can I understand what the tokens means ? I didn't see in the manual the "K,L,I,J,2CE" tokens.
Where / How can I understand what they are saying ? What if I don't want the server to send that info but only the message ?
Open Source has an often over looked feature. You can simply download the source and take a look around. By simply searching in the source for the string "R" I was able to find some of the information you are looking for.
Answer #2:
These shorthand property names directly map to the JsonSerialization of objects in SignalR.
HubResponse
S - State
R - Result
I - Id
E - Error
T - StackTrace
PersistantResponse
L - LongPollDelay
D - Disconnect
T - TimedOut
G - GroupsToken
Some of the others are not found in the current code base, and since the issue your referring to is 7 months old I would guess they have been refactored out.
Answer #1:
The metadata is important to how SignalR operates. The double edged sword of frameworks is that we offload the domain or what it solves to the framework and its creators, and we implicitly agree to let them be the domain expert. Sometimes that makes it a bit of a black box to use, if you want to see what each of these properties are actually used for download the source and follow the code. If for some performance reason you feel the need to trim out some of the code around what you determine to be extraneous fork the code and give it a shot.

Resources