Utilizando o toolkit KGTK em testes na máquina VM029
cd kgtk
conda activate kgtk-env
PATHS=path.tsv
FRIENDS=friends.tsv
Consultas ... arquivo meta_inf.kypher
kgtk query -i $GRAPH --match '()-[]->()'
kgtk query -i $GRAPH --match '(:Person)<-[t2:type]-(p)-[r]->(c)-[t1:type]->(:Country), (c_name)<-[n:name]-(c), (r)-[:date_of_start]->(v)' --where 'c_name = "\"United Kingdom\"" AND v = "^2014-01-01"' --return 'r, p, c, v'
kgtk query -i $GRAPH --match '(:Person)<-[t:type]-(p)-[k:FRIENDS_WITH]->(f),(p)-[n1:name]->(p_name), (f)-[n2:name]->(f_name), (k)-[:date_of_start]->(v) ' --where 'v = "^2014-01-01"' --return 'p_name, f_name, v'
kgtk query -i $GRAPH --match '(:Person)<-[t:type]-(p)-[k:FRIENDS_WITH]->(f), (p)-[n1:name]->(p_name), (f)-[n2:name]->(f_name), (k)-[:date_of_start]->(v) ' --where 'v > "^2010-01-01"' --return 'p_name, f_name, v'
kgtk query -i $GRAPH --match '(:Person)<-[t1:type]-(p)-[k1:FRIENDS_WITH]->(f1), (:Person)<-[t2:type]-(p)-[k2:FRIENDS_WITH]->(f2), (p)-[n1:name]->(p_name), (f2)-[n2:name]->(f_name), (k1)-[:date_of_start]->(v1), (k2)-[:date_of_start]->(v2) ' --where 'v2 > v1' --return 'p_name, f_name, v2'
kgtk query -i $GRAPH --match '(:Person)<-[t1:type]-(p1)-[k1:FRIENDS_WITH]->(p3)-[t2:type]->(:Person), (:Person)<-[t3:type]-(p2)-[k1:FRIENDS_WITH]->(p4)-[t4:type]->(:Person), (p1)-[n1:name]->(p1_name), (p2)-[n2:name]->(p2_name), (p3)-[n3:name]->(p3_name), (p4)-[n4:name]->(p4_name), (k1)-[:date_of_start]->(v1), (k2)-[:date_of_start]->(v2) ' --where 'v2 = v1 AND p1 != p2' --return 'p1_name, p2_name, p3_name, p4_name, v1, v2' --force
kgtk query -i $GRAPH --match '(:Person)<-[t1:type]-(p1)-[l1:LIVING_IN]->(c1)-[t2:type]->(:Country), (:Person)<-[t3:type]-(p2)-[l2:LIVING_IN]->(c2)-[t4:type]->(:Country), (p1)-[n1:name]->(p1_name), (p2)-[n2:name]->(p2_name), (c1)-[n3:name]->(c1_name), (c2)-[n4:name]->(c2_name), (l1)-[:date_of_start]->(v), (l2)-[:date_of_start]->(v) ' --where 'p1 != p2' --return 'p1_name, p2_name, c1_name, c2_name, v' --force
Porém a linguagem kypher não suporta consultas de caminho de tamanhos variáveis entre dois nós, como na tentativa abaixo (que não gera erro!!!).
kgtk query -i $GRAPH --match '(:Person)<-[t1:type]-(p1)-[k:FRIENDS_WITH*1..2]->(p2)-[t2:type]->(:Person), (k)-[:date_of_start]->(v)' --where 'v < "^2013-01-01"' --return 'k, v, p1, p2'
Nesse tipo de consulta se faz necessário utilizar outro recurso do toolkit kgtk: o comando paths. A saída desse comando gera um arquivo de caminhos possíveis entre pares de nós e a partir desse arquivo é possível realizar o filtro dos qualificadores.
kgtk query -i $GRAPH --match '()-[:FRIENDS_WITH]->()' -o $FRIENDS
kgtk paths --max_hops 2 --path-file pairs.tsv --path-mode NONE --path-source source --path-target target -i $FRIENDS -o $PATHS --statistics-only
kgtk query -i $GRAPH -i $PATHS --match 'p: (pathIDx)-[pathSeqx]->(pathEdgex), m: (pathEdgex)-[:date_of_start]->(vx)' --where 'vx >= "^2013-01-01"' --return 'pathIDx, pathSeqx.label, pathEdgex, pathSeqx' -o not_path.tsv
kgtk query -i $GRAPH --as g -i $PATHS -i not_path.tsv --as n --match 'n: (pathIDx)-[pathSeqx]->(pathEdgex), p: (pathIDy)-[pathSeqy]->(pathEdgey), g: (pathEdgey)-[:date_of_start]->(vy)' --where 'pathIDx != pathIDy' --return 'pathIDy, pathSeqy.label, pathEdgey, pathSeqy, vy' --force --order-by 'pathIDy, pathSeqy.label, pathEdgey, pathSeqy'
Mas essa solução funcionou pontualmente, principalmente a última query, pq só tem um caminho inválido
Consegui resolver esse caso da seguinte forma (filtrando as arestas antes de usar o path)
kgtk query -i $GRAPH --match '(n1)-[pathEdgex:FRIENDS_WITH]->(n2), (pathEdgex)-[:date_of_start]->(vx)' --where 'vx > "^2013-01-01"' --return 'pathEdgex, n1, pathEdgex.label , n2' -o query_path.tsv
kgtk paths --max_hops 2 --path-file pairs.tsv --path-mode NONE --path-source source --path-target target -i query_path.tsv -o $PATHS --statistics-only
Dados ... meta_inf.data
id node1 label node2
c1e1 c1 type Country
c1e2 c1 name "Germany"
c1e3 c1 language "German"
c1e4 c1 continent "Europe"
c1e5 c1 population 83000000
c2e1 c2 type Country
c2e2 c2 name "France"
c2e3 c2 language "French"
c2e4 c2 continent "Europe"
c2e5 c2 population 67000000
c3e1 c3 type Country
c3e2 c3 name "United Kingdom"
c3e3 c3 language "English"
c3e4 c3 continent "Europe"
c3e5 c3 population 66000000
p1e1 p1 type Person
p1e2 p1 name "John"
p2e1 p2 type Person
p2e2 p2 name "Harry"
p3e1 p3 type Person
p3e2 p3 name "Anna"
lp1c1 p1 LIVING_IN c1
lp2c1 p2 LIVING_IN c1
lp3c3 p3 LIVING_IN c3
lp3c1 p3 LIVING_IN c1
wp1c3 p1 WORKING_IN c3
wp2c1 p2 WORKING_IN c1
fp1p2 p1 FRIENDS_WITH p2
fp3p1 p3 FRIENDS_WITH p1
fp3p2 p3 FRIENDS_WITH p2
lp1c1q1 lp1c1 date_of_start ^2013-01-01
lp2c1q1 lp2c1 date_of_start ^2014-01-01
lp3c3q1 lp3c3 date_of_start ^2016-01-01
lp3c1q1 lp3c1 date_of_start ^2014-01-01
wp1c3q1 wp1c3 date_of_start ^2014-01-01
wp2c1q1 wp2c1 date_of_start ^2014-01-01
fp1p2q1 fp1p2 date_of_start ^2011-01-01
fp3p1q1 fp3p1 date_of_start ^2012-01-01
fp3p2q1 fp3p2 date_of_start ^2014-01-01
Comentários
Postar um comentário
Sinta-se a vontade para comentar. Críticas construtivas são sempre bem vindas.