iterate over all database in prolog - foreach

I am new to prolog.I am trying to find topper from the database of student I need help with iteration over all the dataset to find a topper
my code is
student(name(kiran,kumar,b14122),[30,40,60]).
student(name(nimit,kalal,b14115),[10,30,40]).
student(name(ramesh,babu,b14222),[40,20,30]).
%student(name(fname(),lname(),roll_no(),markslist(p(),c(),m())
sum([],0).
sum([X|List],Sum) :-
sum(List,Sum1),plus(Sum1,X,Sum).
total(Roll_no,T) :-
student(name(Fname,Lname,Roll_no),Markslist),sum(Markslist,T).
topper(Fname,Lname,Totalmarks,Markslist) :-
thank you for every help

Related

create relationship between existing nodes in Py4neo

I have a list of gene names genes_list and a list of genes that target other genes (list of tuples) genes2, I successfully connected to my local database and created the 20244 nodes labeled GEN with name property.
I am trying to generate a script that automates the creation of relationships for any pair of nodes(usin variables tupla[0]and tupla[1]) in a Neo4j graph but I can't get the for loop to work for a list of tuples, any advice? Im still learning how to use this library any advices would be great! regards!
from py2neo import Node,Relationship,Graph, database,NodeMatcher
import time
import pandas as pd
genes_list=pd.read_csv("Gen_list.txt",delimiter="\t",header=None)
genes_list=genes_list[0].tolist()
for name in genes_list:
graph.run("CREATE(:GEN{name:$name})",name=name)
genes=pd.read_csv(r"C:\Users\espin\OneDrive\Escritorio\MCI\SCRIPTS\dorothea_final.csv", delimiter="\t",header=None)
genes2=list(genes.to_records(index=False))
for tupla in genes2:
existing_u1 = matcher.match("GEN").where(name=tupla[0]).first()
existing_u2 = matcher.match("GEN").where(name=tupla[1).first()
graph.merge(existing_u1,"REGULATES", existing_u2)
I figured out, For the people that want to try this implementation and is using py2neo V4, try using graph.run()
for tupla in genes2:
graph.run("MATCH(a:GEN{name:$name}) MATCH(b:GEN{name:$name1}) CREATE (a)-[:REGULATES]->(b)",name=tupla[0],name1=tupla[1])
remember that the query has to be in the first argument and then you declare the $variables separated by ","
at less this works when you have already created the nodes and does not duplicate the existing ones.

how can I refactor this py2neo v4 code to use neo4j 3.4 temporal data types?

I'm stuck trying to add a date_accepted property to an upload node that represents a scientific paper. Previously, I would have just added a timetree node. However, py2neo v4 no longer supports GregorianCalendar (shame). How would I convert this code snippet to use one of the new temporal data types? I've looked at the docs & online but I'm not yet savvy enough I'm afraid.
from datetime import date, datetime # ??? how to use this...
def getAccepted(year_accepted, month, day):
with open('/home/pmy/pdf/id.txt') as f:
id = f.read()
matcher = NodeMatcher(graph)
upload = matcher.match("Upload", id = id).first()
a = year_accepted+month+day
d = datetime.strptime(a, '%Y%m%d').strftime('%Y-%m-%d')
# >>> HOW TO CONVERT d TO A TEMPORAL DATA TYPE HERE? <<<
try:
graph.merge(upload)
upload['accepted_date']=d
graph.push(upload)
except IndexError as e:
print("type error: " + str(e))
pass
return 0
This works but it pushes the datetime string whereas I want to push a neotime temporal date...
It is possible to insert the datetime variable d above into something like this query below, which also works, but I'm winging this & suspect there's a better way...
query='''UNWIND [date({param})] AS date RETURN date'''
result=graph.run(query, param=d).data()
print(result)
which returns
[{'date': neotime.Date(2010, 10, 23)}]
So I could maybe extract the value & push that to the graph? Is this what the developers intended? The Docs are terse and aimed at proper programmers so IDK
Maybe
accepted=result[0].get('date') # <class 'neotime.Date'>
& push that to the graph perhaps?
The py2neo v4 neotime temporal types are very recent & there is not much documentation, or basic tutorials to adapt afaik. Hence this long-winded post. Anyone out there with experience care to comment?
Another user posted a similar question here: https://stackoverflow.com/a/61989193/13593049
Essentially, you need to use the neotime package for your dates and times if you want to use Neo4j data types in your graph. (Documentation)
neotime also has functionality to convert neotime objects into datetime objects.
import neotime
date_accepted = neotime.Date(2020, 05, 25)
print(date_accepted.to_native())
# # datetime.date(2020, 5, 25)

how to create multiple relationships in a single cypher statement

I must create a set of relationships, all having the same source and type, like in the following sample:
create (_1)-[:`typ`]->(:`x` {`name`:"Mark"})
create (_1)-[:`typ`]->(:`y` {`name`:"Jane"})
create (_1)-[:`typ`]->(:`z` {`name`:"John"})
...
I'd like to have a shorten way to write those statements, like following attempt?
create (_1)-[:`typ`]->[(:`x` {`name`:"Mark"}),
(:`y` {`name`:"Jane"}),
(:`z` {`name`:"John"})]
Any idea?
Thank you in advance.
Paolo
You could do it in a performant and easy way by this pattern:
{batch: [
{from:"alice#example.com",to:"bob#example.com",properties:{since:2012}},
{from:"alice#example.com",to:"charlie#example.com",properties:{since:2016}}]}
UNWIND {batch} as row
MATCH (from:Label {row.from})
MATCH (to:Label {row.to})
CREATE/MERGE (from)-[rel:KNOWS]->(to)
(ON CREATE) SET rel += row.properties
Taken with thanks from 5 Tips & Tricks for Fast Batched Updates of Graph Structures with Neo4j and Cypher by #MichaelHunger.

Parse a big file and populate a Neo4j database

I am working on a Ruby on Rails project that will read and parse somewhat big text file (around 100k lines) and build Neo4j nodes (I am using Neography) with that data.
This is the Neo4j related fraction of the code I wrote:
d= Neography::Rest.new.execute_query("MATCH (n:`Label`) WHERE (n.`name`='#{id}') RETURN n")
d= Neography::Node.load(d, #neo)
p= Neography::Rest.new.create_node("name" => "#{id}")
Neography::Rest.new.add_label(p, "LabelSample")
d=Neography::Rest.new.get_node(d)
Neography::Rest.new.create_relationship("belongs_to", p, d)
so, what I want to do is: a search in the already populated db for the node with the same "name" field as the parsed data, create a new node for this data and finally create a relationship between the two of them.
Obiously this code simply takes way too much time to be executed.
So I tried with Neography's batch, but I ran into some issues.
p = Neography::Rest.new.batch [:create_node, {"name" => "#{id}"}]
gave me a "undefined method `split' for nil:NilClass" in
id["self"].split('/').last
d=Neography::Rest.new.batch [:get_node, d]
gives me a Neography::UnknownBatchOptionException for get_node
I am not even sure this will save me enough time either.
I also tried different ways to do this, using Batch Import for example, but I couldn't find a way to get the already created node I need from the db.
As you can see I'm kinda new to this so any help will be appreciated.
Thanks in advance.
You might be able to do this with pure cypher (or neography generated cypher). Something like this perhaps:
MATCH (n:Label) WHERE n.name={id}
WITH n
CREATE (p:LabelSample {name: n.name})-[:belongs_to]->n
Not that I'm using CREATE, but if you don't want to create duplicate LabelSample nodes you could do:
MATCH (n:Label) WHERE n.name={id}
WITH n
MERGE (p:LabelSample {name: n.name})
CREATE p-[:belongs_to]->n
Note that I'm using params, which are generally recommended for performance (though this is just one query, so it's not as big of a deal)

Looping in SPSS to work through the cases

I have a data set in SPSS containing a sequence of six variables from which I have to create a new variable which should contain the last value present in the sequence. Let's say the data look like this: (the second row contains all missing values but represents a case to which I'll merge some other variables later, so I need this too.)
DATA LIST /V1 TO V6 1-6.
BEGIN DATA
423451
73453
929
0257
END DATA.
Now if I wish to generate a variable named lastscr which should have values 1, ., 3, 9, 7. Can anyone help me on how should I do it in SPSS? I could not find any clue about it. Thank you in advance for any help.
This can easily be done with the DO REPEAT command:
DO REPEAT Var = V1 TO V6.
IF NOT(SYSMIS(Var)) lastscr = Var.
END REPEAT.

Resources