Traverse trough the graph with a list of labels in Neo4j - graph

I have this graph:
CREATE (a:Class {brickClass: 'Building',id:"building1"})
CREATE (b:Class {brickClass: 'Air Handler Unit', id:"ahu1"})
CREATE (c:Class {brickClass: 'Cooling System', id:"cools1"})
CREATE (d:Class {brickClass: 'Heat Exchanger', id:"heatex1"})
CREATE (e:Class {brickClass: 'Heat Exchanger', id:"heatex2"})
CREATE (f:Class {brickClass: 'Heat Exchanger', id:"heatex3"})
CREATE (g:Class {brickClass: 'Pump', id:"pump1"})
CREATE (h:Class {brickClass: 'Pump', id:"pump2"})
CREATE (i:Class {brickClass: 'Pump', id:"pump3"})
CREATE (a)-[r:has_equipment]->(b)
CREATE (a)-[s:has_equipment]->(c)
CREATE (b)-[t:has_equipment]->(d)
CREATE (b)-[u:has_equipment]->(e)
CREATE (c)-[w:has_equipment]->(f)
CREATE (d)-[v:has_equipment]->(g)
CREATE (e)-[x:has_equipment]->(h)
CREATE (f)-[y:has_equipment]->(i)
I would like to return all the pumps that is under a heat exchanger, which again is under a air handler unit, that is under a building. So the input is this path :
["Building","Air Handler Unit","Heat Exchanger","Pump"]
I have tried to code this and the result is this code:
WITH ["Building","Air Handler Unit","Heat Exchanger","Pump"] AS labels
UNWIND range(0, size(labels)-1) AS i
WITH labels[i] AS label1, labels[i+1] AS label2, i, labels
match (n:Class{brickClass:label1})-[*..]->(node2:Class{brickClass:label2})
WHERE i + 1 = size(labels) - 1
RETURN node2.id
This code does not return what I want. This code return all the pumps. But I would like it to return only pump1 and pump2. How can I change the code to do that? The labels list could have varying length and labels, so I can not do any hardcoding.

There is no such built-in capability in Neo4j. However with a bit of imagination using APOC you can achieve your result.
What we will do is we will generate what the Cypher query would look like if you could write it by hand, so based on your examples with your 4 items in the parameters the query would look like this :
MATCH path=(:`Class` {brickClass: "Building"})-[:has_equipment]->(:`Class` {brickClass: "Air Handler Unit"})-[:has_equipment]->(:`Class` {brickClass: "Heat Exchanger"})-[:has_equipment]->(:`Class` {brickClass: "Pump"}) RETURN path
To generate the query based on input elements, we will use some string functions in APOC, mainly apoc.text.join
WITH ["Building","Air Handler Unit","Heat Exchanger","Pump"] AS parts
// convert any item in list above into (:Class {brickClass: <value>})
WITH [x IN parts | apoc.text.format('(:`Class` {brickClass: "%s"})', [x])] AS parts
// start the query with `MATCH path=`,
// add the query parts from above joined by the relationship type
// and end with `RETURN path`
WITH 'MATCH path=' + apoc.text.join(parts, '-[:has_equipment]->') + ' RETURN path' AS query
RETURN query
This will return the following query string
MATCH path=(:`Class` {brickClass: "Building"})-[:has_equipment]->(:`Class` {brickClass: "Air Handler Unit"})-[:has_equipment]->(:`Class` {brickClass: "Heat Exchanger"})-[:has_equipment]->(:`Class` {brickClass: "Pump"}) RETURN path
Now we can use another APOC function, apoc.cypher.run that allows us to execute a generated query and it returns a single value called .. value
WITH ["Building","Air Handler Unit","Heat Exchanger","Pump"] AS parts
WITH [x IN parts | apoc.text.format('(:`Class` {brickClass: "%s"})', [x])] AS parts
WITH 'MATCH path=' + apoc.text.join(parts, '-[:has_equipment]->') + ' RETURN path' AS query
CALL apoc.cypher.run(query, {})
YIELD value
RETURN value
And the result is what you expect
Now let's try the same for your example with 2 elements only
WITH ["Heat Exchanger", "Pump"] AS parts
WITH [x IN parts | apoc.text.format('(:`Class` {brickClass: "%s"})', [x])] AS parts
WITH 'MATCH path=' + apoc.text.join(parts, '-[:has_equipment]->') + ' RETURN path' AS query
CALL apoc.cypher.run(query, {})
YIELD value
RETURN value
And the result is there as well, so very dynamic

Related

How to write Key Value Pairs in Map in a Loop without overwriting them?

the following is my problem:
I do a select on my database and want to write the values of every record i get in a map. When i do this, i only have the values of one record in my map, because the put() function overwrites the entries everytime the loop starts again. Since I have to transfer the Key:Value pairs via JSON into my Javascript and write something into a field for every Key:Value pair, an ArrayList is not an option.
I've already tried to convert a ArrayList, which contains the Map, to a Map or to a String and then to Map, but i failed.
EDIT:
HereĀ“s my Code
def valueArray = new ArrayList();
def recordValues = [:]
while (rs.next())
{
fremdlKorr = rs.getFloatValue(1)
leistungKorr = rs.getFloatValue(2)
materialKorr = rs.getFloatValue(3)
strid = rs.getStringValue(4)
recordValues.put("strid", strid);
recordValues.put("material", materialKorr);
recordValues.put("fremdl", fremdlKorr);
recordValues.put("leistung", leistungKorr);
valueArray.add(korrekturWerte);
}
The ArrayList was just a test, i dont want to have an ArrayList, i need a Map.
The code as written, will give you a list of maps, but the maps will all contain the values of the last row. The reaons is the def recordValues = [:] that is outside of the while loop. So you basically add always the same map to the list and the map values get overwritten each loop.
While moving the code, would fix the problem, I'd use Sql instead. That all boils down to:
def valueArray = sql.rows("select strid, material, fremdl, leistung from ...")

Filter object by a value of child array

Say I want to select all research_report-published results containing values for algorithm "Autoencoder" or "Boosted Regression trees" or both
For matching all filters I have idea like this:
Add a new field algorithms-cancatinated (with sorted and concatinated strings of all values in algorithms array ) e.g storing Autoencoder,Boosted Regression,Long Short-Term Memory for first object and Autoencoder,Boosted Regression for 2nds one, then on that field do exact match like this
constructor(private afDb: AngularFireDatabase) {}
this.afDb.list(`research_reports-published/`).orderby(algorithms-cancatinated).StartAt(`Autoencoder,Boosted Regression`).EndAt(`Autoencoder,Boosted Regression`);
But I can not do "Autoencoder" or "Boosted Regression trees" here
I am using angularfire2.

Multiple commits to neo4j from R

I have collected some tweets using the twitteR package and thereafter exported them to a neo4j database using Nicole White's various tutorials. I extract the tweets to a dataframe called kdf and thereafter use functions from stringr for basic cleaning up as demonstrated by Nicole. I am then sending this to neo4j from R. The essential part of my code is:
library(RNeo4j)
graph = startGraph("http://localhost:7474/db/data/", username="xxxx", password="xxxx")
clear(graph)
addConstraint(graph, "Tweet", "id")
addConstraint(graph, "User", "username")
addConstraint(graph, "Hashtag", "hashtag")
addConstraint(graph, "Tags", "ent_tag")
query = "
CREATE (tweet:Tweet {id: {tweetID}})
SET tweet.text = {text}
CREATE (user:User {name: {Username}})
CREATE (user)-[:TWEETED]->(tweet)
FOREACH(reply_to_sn IN CASE {reply_to_sn} WHEN NULL then [] else [{reply_to_sn}] END |
MERGE (replytouser:User {username:{reply_to_sn}})
CREATE (tweet)-[:IN_REPLY_TO]->(replytouser)
)
FOREACH(retweet_sn IN CASE {retweet_sn} WHEN NULL THEN [] ELSE [{retweet_sn}] END |
MERGE(retweet_user:User {username: {retweet_sn}})
CREATE (tweet)-[:RETWEET_OF]->(retweet_user)
)
FOREACH(hastag_nodes IN CASE {hashtag_nodes} WHEN NULL then [] else [{hashtag_nodes}] END |
MERGE (h:Hashtag {hashtag :{hashtag_nodes}})
CREATE (tweet)-[:HASHTAG]->(h)
)
FOREACH(mentioned_users IN CASE {mentioned_users} WHEN NULL then [] else [{mentioned_users}] END |
MERGE (m:User {username :{mentioned_users}})
CREATE (tweet)-[:MENTIONED]->(m)
)
"
tx = newTransaction(graph)
for(i in 1:nrow(kdf)){
row = kdf[i, ]
appendCypher(tx, query,
tweetID=row$id,
text=row$text,
Username=row$screenName,
reply_to_sn=row$replyToSN,
retweet_sn=getRetweetSN(row$text),
hashtag_nodes=getHashtags(row$text),
mentioned_users=getMentions(row$text))
}
commit(tx)
What I have done thereafter is extracted named entities for all the text using Watson's Alchemy API. This is stored in a dataframe called ent_tbl. This contains three variables, tweetid, etext and etype. Now I am trying to export this data too to the same neo4j databse and join on the id of the tweets. This is the other part of the code:
query="
MATCH(t:ent_tag {id : $twid, type :$etype, text :$etext})
MATCH(tw:tweet {tweetID : $twid })
CREATE (tw)-[:HAS_ENT]->(t)
"
tx=newTransaction(graph)
for (i in 1:nrow(ent_tbl)){
row = ent_tbl[i,]
appendCypher(tx, query,
twid=row2$tweetid,
etype=row2$etype,
etext=row2$etext)
}
commit(tx)
While I do not get any errors on committing this, summary(graph) does not show me the relationship between the tags (t) and the tweets (tw) that I expected to see.
> summary(graph)
This To That
1 User TWEETED Tweet
2 Tweet RETWEET_OF User
3 Tweet HASHTAG Hashtag
4 Tweet MENTIONED User
5 Tweet IN_REPLY_TO User
Why would this happen?
This is my db.schema in neo4j:
That is because the MATCH does not find any tag or tweet so it breaks. If you want to add data to existing nodes, you should match them by ID and then set their properties. And you got to be consistent with labels and upper/lower cases. I think this is what you are looking for.
query="
MATCH(t:Tags {ent_tag : $twid})
MATCH(tw:Tweet {tweetID : $twid })
SET t.type=$etype, t.text=$etext
CREATE (tw)-[:HAS_ENT]->(t)
"
tx=newTransaction(graph)
for (i in 1:nrow(ent_tbl)){
row = ent_tbl[i,]
appendCypher(tx, query,
twid=row2$tweetid,
etype=row2$etype,
etext=row2$etext)
}
commit(tx)

How to access and mutate node property value by the property name string in Cypher?

My goal is to access and mutate a property of a node in a cypher query where the name of the property to be accessed and mutated is an unknown string value.
For example, consider a command:
Find all nodes containing a two properties such that the name of the first property is lower-case and the name of the latter is the upper-case representation of the former. Then, propagate the value of the property with the lower-case string name to the value of the property with the upper-case name.
The particular case is easy:
MATCH ( node )
WHERE has(node.age) AND has(node.AGE) AND node.age <> node.AGE
SET node.AGE = node.age
RETURN node;
But I can't seem to find a way to implement the general case in a single request.
Specifically, I am unable to:
Access the property of the node with a string and a value
Mutate the property of the node with a string and a value
For the sake of clarity, I'll include my attempt to handle the general case. Where I failed to modify the property of the node I was able to generate the cypher for a command that would accomplish my end goal if it were executed in a subsequent transaction.
MERGE ( justToMakeSureOneExists { age: 14, AGE : 140 } ) WITH justToMakeSureOneExists
MATCH (node)
WHERE ANY ( kx IN keys(node) WHERE kx = LOWER(kx) AND ANY ( ky in keys(node) WHERE ky = UPPER(kx) ) )
REMOVE node.name_conflicts // make sure results are current
FOREACH(kx in keys(node) |
SET node.name_conflicts
= COALESCE(node.name_conflicts,[])
+ CASE kx
WHEN lower(kx)
THEN []
+ CASE WHEN any ( ky in keys(node) WHERE ky = upper(kx) )
THEN ['match (node) where id(node) = ' + id(node)+ ' and node.' + upper(kx) + ' <> node.' + kx + ' set node.' + upper(kx) + ' = node.' + kx + ' return node;']
ELSE [] END
ELSE []
END )
RETURN node,keys(node)
Afterthought: It seems like the ability to mutate a node property by property name would be a pretty common requirement, but the lack of obvious support for the feature leads me to believe that the feature was omitted deliberately? If this feature is indeed unsupported is there any documentation to explain why and if there is some conflict between the approach and the recommended way of doing things in Neo/Cypher?
There is some discussion going on regarding improved support for dynamic property access in Cypher. I'm pretty confident that we will see support for this in the future, but I cannot comment on a target release nor on a date.
As a workaround I'd recommend implementing that into a unmanaged extension.
It appears that the desired language feature was added to Cypher in Neo4j 2.3.0 under the name "dynamic property". The Cypher docs from version 2.3.0-up declare the following syntax group as a valid cypher expression:
A dynamic property: n["prop"], rel[n.city + n.zip], map[coll[0]].
This feature is documented for 2.3.0 but is absent from the previous version (2.2.9).
Thank you Neo4j Team!

Plone and Dexterity: default values for "relation" field

In one of my Plone sites, I have a few dexterity models that I use to generate letters. The models are: "Model" (the base content of the letter), "Contact" (that contains the contact information, such as name, address etc) and "Merge" (which is a Model object rendered, in which we substitute some parts of the model with the recipients information).
The schema of the "Merge" object is the following:
class IMergeSchema(form.Schema):
"""
"""
title = schema.TextLine(
title=_p(u"Title"),
)
form.widget(text='plone.app.z3cform.wysiwyg.WysiwygFieldWidget')
text = schema.Text(
title=_p(u"Text"),
required=False,
)
form.widget(recipients=MultiContentTreeFieldWidget)
recipients = schema.List(
title=_('label_recipients',
default='Recipients'),
value_type=schema.Choice(
title=_('label_recipients',
default='Recipients'),
# Note that when you change the source, a plone.reload is
# not enough, as the source gets initialized on startup.
source=UUIDSourceBinder(portal_type='Contact')),
)
form.widget(model=ContentTreeFieldWidget)
form.mode(model='display')
model = schema.Choice(
title=_('label_model',
default='Model'),
source=UUIDSourceBinder(portal_type='Model'),
)
When creating a new "Merge" object, I want to have the "recipients" fields be preset with all contacts available in the folder where the new object is created.
I followed Martin Aspelli's guide to add a default value for a field: http://plone.org/products/dexterity/documentation/manual/developer-manual/reference/default-value-validator-adaptors
It works fine for text input fields, but I can't have it working for the "recipients" field. The method to generate the default values is the following (with some debug info with ugly print, but they'll be removed later ;) ):
#form.default_value(field=IMergeSchema['recipients'])
def all_recipients(data):
contacts = [x for x in data.context.contentValues()
if IContact.providedBy(x)]
paths = [u'/'.join(c.getPhysicalPath()) for c in contacts]
uids = [IUUID(c, None) for c in contacts]
print 'Contacts: %s' % contacts
print 'Paths: %s' % paths
print 'UIDs: %s' % uids
return paths
I tried to return the objects directly, their relative path (in the add view, when accessing "self.widgets['recipients'].value", I get this type of data) their UIDs but none of the solution as any effect.
I also tried to return tuples instead of lists or even generators, but still no effect at all.
The method is called for sure, as I see traces in the instance log.
I think you need to get the "int_id" of the related content. That's how dexterity relation fields store relation info::
from zope.component import getUtility
from zope.intid.interfaces import IIntIds
#form.default_value(field=IMergeSchema['recipients'])
def all_recipients(data):
contacts = [x for x in data.context.contentValues()
if IContact.providedBy(x)]
intids = getUtility(IIntIds)
# The following gets the int_id of the object and turns it into
# RelationValue
values = [RelationValue(intids.getId(c)) for c in contacts]
print 'Contacts: %s' % contacts
print 'Values: %s' % values
return values

Resources