{
"identity": 7,
"labels": [
"Parent"
],
"properties": {
"name": "foo1"
}
},
{
"identity": 8,
"labels": [
"Child"
],
"properties": {
"name": "bar2"
}
},
{
"identity": 9,
"labels": [
"Child"
],
"properties": {
"name": "bar1"
}
},
{
"identity": 10,
"labels": [
"Parent"
],
"properties": {
"name": "foo2"
}
}
I want to select the Parents which do not have any child with name='abc'
Expected Behaviour : I should get both Parents (foo1 and foo2) as result
Query1 :
Match (x1:Parent) with x1
optional Match (x1)-[:CHILD]-(x2:Child) with x1 , collect(x2) as x3
UNWIND x3 as x2
WITH x1 , x2 , x3
where none (x IN x3 where x.name IN ['abc'])
return DISTINCT x1
This query is returning me only 1 Parent(foo1) but it should return both parents and 2nd parent(foo2) is not connected to any child.
PS : Reason for using UNWIND is to use the variables for further WHERE clauses on variable x2.
This will get all Parents without a child name: 'abc' or without any child
MATCH (p:Parent)
WHERE NOT EXISTS((p)-[:CHILD]->(:Child {name: 'abc'}))
RETURN p
Check if the given parent, p has a child not named: 'abc'
==========
EDITED:
Personally I dislike this query because it is collecting x2 then unwinding it later. But for sake of answering the SO question,
Match (x1:Parent) with x1
optional Match (x1)-[:CHILD]->(x2:Child) with x1 , collect({x1:x1, x2:x2}) as x3
UNWIND x3 as x2
WITH x2.x1 as x1, x2.x2 as x2, x3
where NOT x2.name IN ['abc'] OR x2 is NULL
RETURN distinct x1
I collected BOTH x1 and x2 then UNWIND the collection by getting x1
and x2 during unwind. This will include other nodes that has no x2
(without a CHILD). I also simplified the where condition because x2 is also accessible rather than looping again on x3
Related
I have this issue where i need a new list with, all the unique elements from list a, but the issue is that the list have different lengths.
[ 1; 2; 4; 5 ] |> List.except [ 2; 3; 5 ] // [ 1; 4 ]
That is, the following should do what you want
let c = a |> List.except b
Update
As #rcoy pointed out, List.except returns distinct elements only, i.e.
[ 1; 1 ] |> List.except [] = [ 1 ] // true
To keep duplicates, a straightforward way is
let b = [ 2 ]
let toRemove x = not(List.contains x b)
[ 1; 1; 2 ] |> List.filter toRemove // [ 1; 1 ]
Depending on the size of the b list and equivalency function (structural vs. referential), i.e. the cost of traversing and comparing, changing the datastructures might be beneficial. E.g.
open System.Collections.Generic
let b = [ 2 ]
let toRemove = HashSet(b)
[ 1; 1; 2 ] |> List.filter (fun x -> not(toRemove.Contains(x))) // [ 1; 1 ]
which is similar to what List.except does internally.
I want to ask about the method of solving a case I ran into. I have data with various variations with each index interrelated.
Data Example:
1 :
x1 = [ 'S' , 'M' , 'L', 'XL', 'XXL']
x2 = [ 10 , 30 , 20, 30, 20]
pattern = A
2 :
x1 = [ 'H' , 'X' , 'A', 'DL', 'AXL']
x2 = [ 10 , 40 , 10, 30, 10]
pattern = G
3 :
x1 = [ 'SS' , 'AM' , 'L', 'XL', 'SXL']
x2 = [ 90 , 30 , 30, 50, 10]
pattern = B
.
.
.
.
.
(n data)
x1 -> type of clothes
x2 -> qty of clothes
pattern = the result ( A, B, C, D, E, F, G)
Altogether I have about 10,000 data with a pattern like that and I want to find the formula (r) using x1 array and x2 array. What method or algorithm can I use or what kind of solution exists for this case?
I am trying to get my head around Z3. Tthough I understand the basic principles and the examples for solving basic problems.
I am creating a symbolic dynamic execution tool and use Z3 as solver. In the sample program under test there's is a condition table.Rows.Count == 1, which I have successfully manually translated into a Z3 model with a solution:
(declare-datatypes () ((Type (Char) (Decimal) (String) (Bool) (Int))))
(declare-datatypes (T S) ((Column (mkcol (first T) (second S)))))
(declare-datatypes () ((Row (Array (String (Column Type String))))))
(declare-datatypes () ((Table (Array (Int (Row))))))
(declare-const a Int)
(declare-const row Row)
(declare-const column (Column String String))
(declare-const c Row)
(declare-const x Int)
(declare-const table Table)
(declare-const table.Rows (List Row))
(declare-const list2 (List Row))
(assert (not (= table.Rows nil))) ; an actual instance (not null)
(assert (= (head table.Rows) row)) ; firt row
(check-sat)
(get-model)
And the solution
sat
(model
(define-fun table.Rows () (List Row)
(insert (Array (mkcol String "")) nil))
(define-fun row () Row
(Array (mkcol String "")))
)
I don't think my input model is perfect and then I don't know how to model the constraint (int)table.Rows[0]["name"], i.e. the named cell contains a int value.
So my question is how to model this and how to approach such translations from these more complicated code constraints written in code to Z3 constraints (i.e. type mapping). And answering basic questions like
Should the Rows property be modeled as (declare-const table.Rows (List Row)) on the table variable?
Or the Rows property should be modeled using custom sort?
Should Count be also declared or it can be "by-passed" by multiple head and tail assertions?
If you can recommend any paper or post or project, that would be awesome :)
Thanks,
Karel
With the help of the project I was able to come up with a solution
from model import *
from z3 import *
import yaml
import pprint
import inspect
import linecache
import timeit
import itertools
classes_yaml = """
-
name: DataColumn
attribute: [{name: Value, type: Integer}]
-
name: DataTable
reference: [
{name: Rows, type: DataRowCollection}
]
-
name: DataRow
reference: [
{name: Columns, type: DataColumn, multiple: true}
]
-
name: DataRowCollection
reference: [
{name: Row, type: DataRow, multiple: true}
]
"""
classes = yaml.load(classes_yaml)
DataColumn, DataTable, DataRow, DataRowCollection = load_all_classes(classes)
dc = DefineObject('col1', DataColumn)
drc = DefineObject('drc', DataRowCollection)
dt = DefineObject('dt', DataTable).force_value('Rows', drc)
dr1 = DefineObject('dr1', DataRow)
dr2 = DefineObject('dr2', DataRow)
dr3 = DefineObject('dr3', DataRow)
generate_meta_constraints()
generate_config_constraints()
solver = Optimize()
solver = Solver()
solver.add(*get_all_meta_facts())
solver.add(*get_all_config_facts())
solver.add(dt.isinstance(DataTable))
solver.add(dt['Rows'] == drc)
solver.add(drc['Row'].count() == 1)
solver.add(dc['Value'] > 5);
solver.add(dr1['Columns'].count() == 1)
print(solver)
print(solver.check())
print(cast_all_objects(solver.model()))
The problem the solver is solving is to find a DataTable with a single DataRow that has a value greater than 5 in column with the name Value.
The resulting model is
{
"dt":{
"name": "dt", "type": "DataTable", "alive": True,
"Rows": "drc"
},
"dr1":{
"name": "dr1", "type": "DataRow", "alive": True,
"Columns":[
"col1"
]
},
"col1": {
"name": "col1", "type":"DataColumn", "alive": True,
"Value":6
},
"dr3": {
"name": "dr3", "type": "DataRow", "alive": True,
"Columns": []
},
"dr2": {
"name": "dr2", "type": "DataRow", "alive": True,
"Columns": []
},
"drc": {
"name": "drc", "type": "DataRowCollection", "alive": True,
"Row": [
"dr2"
]
}
}
This is not a generic solution and the assumptions class have to be recreated every time the code constraint is different, i.e. the assumptions and initilizations will be different for "2 rows with two distinct values table" problem. Also, the class definitions for DataRow will be different as well (but there might be a trick how to generalize it a little bit).
I'm trying to break out of a nested foreach loop using 2 lists of sorted turtles.
But instead of just leaving the inner loop, netlogo breaks out of the whole procedure.
I have a code like the one below (this one is made up, but has exactly the same structure):
to do-something
let xturtles sort-by [ [a b] -> [birthday] of a > [birthday] of b ] turtles
;; construct an ordered set
foreach xturtles [ the-xturtle ->
ask the-xturtle [
let xage birthday
let yturtles sort-by [ [a b] -> [birthday] of a > [birthday] of b ] turtles with [birthday < xage]
;; construct a second ordered set
foreach yturtles [ the-yturtle ->
let breakout-condition? false
ask the-yturtle [
if (hidden? ) [
set breakout-condition? true
]
]
if breakout-condition? [ stop ]
]
]
]
end
However, when the stop condition is reached, netlogo breaks out of the whole procedure, instead of continuing with the outer loop (the xturtles loop)?
Is that the expected behavior? If so, what is a good practice in this case?
Thanks!
Felix
It looks even nesting the stop within an extra ask procedure in the same procedure doesn't help. However, if you need a quick fix I think you can replace the second foreach loop with a standalone procedure that contains the stop as a workaround. For example, this procedure follows a similar format to yours and the same problem comes up- as soon as stop is called the broader foreach is exited.
to nest-foreach-example
ca
crt 1
let xs [ 1 2 3 4 ]
let ys [ 1 2 3 4 ]
foreach xs [
x ->
foreach ys [
y ->
ask turtles [
if y > x [
stop
]
]
print word x y
]
]
end
This prints out 11.
However, if you make a custom procedure to take the place of your "y" foreach loop, it works (with or without the ask turtles):
to nest-foreach-example-turtles
ca
crt 1
let xs [ 1 2 3 4 ]
let ys [ 1 2 3 4 ]
foreach xs [
x ->
for-y x ys
]
end
to for-y [ x_ ys ]
foreach ys [
y ->
ask turtles [
if y > x_ [
stop
]
]
print word x_ y
]
end
Outputs:
11
21
22
31
32
33
41
42
43
44
[
[
"x1", "x2" ,
[
["n1", "n2"],["x3", "x4"],["x5", "x6"]
]
],
[
"y1", "y2"
],
[
"z1", "z2"
]
]
I want to access the x3 and x4 items , how to access with dustjs
It mostly depends on what you mean by "access", but here are some example templates that show you what happens if you use the special . reference, which in Dust means "the current iteration context"
{#.}
Item {.}
{/.}
Output:
Item x1,x2,n1,n2,x3,x4,x5,x6 Item y1,y2 Item z1,z2
By iterating one level into the current context, you see that the top-level array elements are iterated on. One step deeper:
{#.}{#.}
Item {.}
{/.}{/.}
Output:
Item x1 Item x2 Item n1,n2,x3,x4,x5,x6 Item y1 Item y2 Item z1 Item z2
Notice how the depth at which we traverse nested arrays increases the more levels of context you tell Dust to inspect. So for your above example, four levels will suffice:
{#.}{#.}{#.}{#.}
Item {.}
{/.}{/.}{/.}{/.}
Output:
Item x1 Item x2 Item n1 Item n2 Item x3 Item x4 Item x5 Item x6 Item y1 Item y2 Item z1 Item z2