连续关系方向上的密码约束

时间:2019-05-08 22:11:17

标签: neo4j cypher path-finding

简短版本:我需要获取一个路径,该路径可以包含不同方向的不同关系。但是,我在路径上有一个约束,如果它包含特定类型的连续关系,则两个关系都必须在同一方向上。

长版: 我正在使用下面的查询来获取两个节点之间的路径:

plays_df

查询正确地向我返回了两个节点之间的最短路径。但是,我需要进一步约束此查询,以便它仅返回特定类型的连续关系在同一方向上的路径。

例如,假设关系plays_df["AwayOn"] = [[] for i in range(len(plays_df.index))] plays_df["HomeOn"] = [[] for i in range(len(plays_df.index))] previous_game_link = "" for index, row in plays_df.iterrows(): current_away = [] current_home = [] game_link = row["GameLink"] if game_link != previous_game_link: #if first play of new game, get starters player_indices = player_stat_df.index[player_stat_df["GameLink"] == game_link].tolist() players_in_game = player_stat_df.ix[player_indices] starter_indices = players_in_game.index[players_in_game["GS"] == 1].tolist() starters = players_in_game.ix[starter_indices] away_starter_indices = starters.index[starters["Location"] == "A"].tolist() away_starters = starters.ix[away_starter_indices] home_starter_indices = starters.index[starters["Location"] == "H"].tolist() home_starters = starters.ix[home_starter_indices] current_away = away_starters["Player"].tolist() current_home = home_starters["Player"].tolist() else: #middle of game current_away = away_last_play current_home = home_last_play if row["Action"] == "goestothebench": #player subbing out if row["H/A"] == "A" and row["Name"] in current_away: current_away.remove(row["Name"]) elif row["Name"] in current_home: current_home.remove(row["Name"]) elif row["Action"] == "entersthegame": #player subbing in if row["H/A"] == "A" and row["Name"] not in current_away: current_away.append(row["Name"]) elif row["Name"] not in current_home: current_home.append(row["Name"]) print current_away print current_home plays_df.at[index, "AwayOn"] = current_away plays_df.at[index, "HomeOn"] = current_home away_last_play = current_away home_last_play = current_home previous_game_link = game_link 的方向相同,则不应返回MATCH p=shortestPath((n:Class { code: '1' })-[r*]-(m:Class { code: '4'})) WHERE NONE(x IN NODES(p) WHERE 'Ontology' in labels(x)) return p,而可以返回-a->(1)-a->(2)<-a-(3)-b->(4)

以上示例只是我的真实数据的简化。在我的实际用例中,我需要找到具有IRI的节点之间的最短路径 (1)-a->(6)-a->(3)-b->(7)<-c-(5)<-d-(6)-e->(4)和另一个具有IRI (3)-b->(7)<-c-(4)的节点。下面的查询是一个特定的查询,它对我需要的路径进行编码,并返回一个路径,即存在的路径。我需要使用最短路径使其更通用。

http://elite.polito.it/ontologies/dogont.owl#Actuator

密码可以吗?

1 个答案:

答案 0 :(得分:0)

如果您要捕获任一方向上一致的路径(但必须调用shortestPath()两次),则此查询应该起作用:

MATCH (n:Class {code: '1'}), (m:Class {iri: '4'})
OPTIONAL MATCH p1=shortestPath((n)-[*]->(m))
WHERE NONE(x IN NODES(p1) WHERE 'Ontology' in labels(x))
OPTIONAL MATCH p2=shortestPath((n)<-[*]-(m))
WHERE NONE(y IN NODES(p2) WHERE 'Ontology' in labels(y))
RETURN p1, p2
如果分别没有一致的向右或向左路径,则

p1和/或p2将分别为null

但是,如果您知道想要特定的方向(例如,向右),则应该可以:

MATCH p=shortestPath((:Class {code: '1'})-[*]->(:Class {iri: '4'}))
WHERE NONE(x IN NODES(p) WHERE 'Ontology' in labels(x))
RETURN p