This page outlines the process of converting a TiQL query into a Cypher query, aiding users familiar with TiQL in understanding the semantics of Cypher more effectively.
While this page focuses on query conversion, a more detailed description of the Cypher and TiQL query languages can be found in TiQLToCypher and Avatars_search, respectively.
In TiQL, some keywords are reserved to point out the avatar properties : $classes
, $inheritance
, $label
, $iri
, $lastUpdated
, $domain
, $uuid
, $cluster
, $acl
, $group
, $visibility
, $static
.
In Cypher for Thing'in, these properties could be accessed through dedicated node properties :
$classes
→ classes
$inheritance
at true
with $classes
constraint → inherited_classes
$label
→ label
$iri
→ iri
$lastUpdated
→ lastUpdated
$domain
→ domain
$uuid
→ uuid
$cluster
→ cluster
$acl
→ acl
$group
→ group
$visibility
→ visibility
$static
→ static
Find the avatar with the given IRI
TiQL
{
"$iri" : "IRI",
}
Cypher for Thing'in
MATCH ({iri:`IRI`})
RETURN *
Find the avatars with multiple constraints
TiQL
{
"$label" : {"$contains" : "UN_LABEL"},
"$domain" : "MON_DOMAIN",
"$visibility" : {"$geq" : 0}
}
Cypher for Thing'in
MATCH (a:`UN_LABEL` {domain:'MON_DOMAIN'})
WHERE a.visibility >= 0
RETURN *
Here conditions should be express in the WHERE clause
Find the rooms (ontology concept
ROOM
) that are in (ontology conceptisIn
) a building having an iriB_IRI
TiQL
{
"query" : [
{
"$alias" : "b",
"$iri" : "B_IRI",
},
{
"$alias" : "r",
"$classes" : {"$contains" : "ROOM"},
"->isIn" : "b"
}
],
"return" : "r"
Cypher for Thing'in
MATCH ({iri:`B_IRI`})-[:isIn]->(r:ROOM)
RETURN r
Find the sensors connected to a device D_IRI between 8:00 and 12:00
TiQL
{
"query" : [
{
"$alias" : "d",
"$iri" : "D_IRI",
},
{
"$alias" : "s",
"$classes" : {"$contains" : "SENSOR"},
"$relationships":[
{
"START_TIME": {"$geq": 8},
"END_TIME": {"$lte": 12},
"$avatar":"d",
"$label":"CONNECTED_TO",
"$direction":"OUT"}
]
}
],
"return" : "s"
Cypher for Thing'in
MATCH (s:SENSOR)-[c:CONNECTED_TO]->({iri:`D_IRI`})
WHERE c.START_TIME >= 0 AND c.END_TIME <= 12
RETURN s
Find the sensors connected to a device via MQTT protocol and for a duration of 4 minutes.
TiQL
{
"query" : [ {
"$alias" : "s",
"$label" : {
"$contains_all" : [ "SENSOR" ]
},
"$relationships" : [ {
"duration" : {
"$eq" : "PT4M"
},
"protocol" : {
"$eq" : "MQTT"
},
"$avatar" : "d",
"$label" : "CONNECTED_TO",
"$direction" : "OUT"
} ]
}, {
"$alias" : "d",
"$label" : {
"$contains_all" : [ "DEVICE" ]
}
} ],
"return" : [ "s" ]
}
Cypher
MATCH (s:SENSOR) -[r:CONNECTED_TO { protocol: 'MQTT', duration: 'PT4M' }]-> (d:DEVICE)
RETURN s
Find the sensors that are connected to a device via a path of depth 2.
TiQL
{
"query" : [ {
"$alias" : "s",
"$label" : {
"$contains_all" : [ "SENSOR" ]
},
"$relationships" : [ {
"$avatar" : "d",
"$label" : "connectedTo",
"$direction" : "ANY",
"$maxDepth " : 2
} ]
}, {
"$alias" : "d",
"$label" : {
"$contains_all" : [ "DEVICE" ]
}
} ],
"return" : [ "s" ]
}
Cypher
MATCH (s:SENSOR) -[r:connectedTo*..2]- (d:DEVICE)
RETURN s
Find sensors that are connected to a device located in a room X.
TiQL
{
"query" : [ {
"$alias" : "s",
"$label" : {
"$contains_all" : [ "SENSOR" ]
},
"$relationships" : [ {
"$avatar" : "d",
"$label" : "CONNECTED_TO",
"$direction" : "OUT"
} ]
}, {
"$alias" : "d",
"$label" : {
"$contains_all" : [ "DEVICE" ]
},
"$relationships" : [ {
"$avatar" : "ro",
"$label" : "isIn",
"$direction" : "OUT"
} ]
}, {
"$alias" : "ro",
"$label" : {
"$contains_all" : [ "ROOM" ]
},
"IRI" : {
"$eq" : "X"
}
} ],
"return" : [ "s" ]
}
Cypher
MATCH (s:SENSOR) -[r:CONNECTED_TO]-> (d:DEVICE) -[i:isIn]-> (ro:ROOM)
WHERE ro.IRI = 'X'
RETURN s
Find sensors that are located in room X and connected to a device.
TiQL
{
"query" : [ {
"$alias" : "s",
"$label" : {
"$contains_all" : [ "SENSOR" ]
},
"$relationships" : [ {
"$avatar" : "d",
"$label" : "CONNECTED_TO",
"$direction" : "OUT"
}, {
"$avatar" : "ro",
"$label" : "isIn",
"$direction" : "OUT"
} ]
}, {
"$alias" : "d",
"$label" : {
"$contains_all" : [ "DEVICE" ]
}
}, {
"$alias" : "ro",
"$label" : {
"$contains_all" : [ "ROOM" ]
},
"IRI" : {
"$eq" : "X"
}
} ],
"return" : [ "s" ]
}
Cypher
MATCH (s:SENSOR) -[r:CONNECTED_TO]-> (d:DEVICE), (s) -[i:isIn]-> (ro:ROOM)
WHERE ro.IRI = 'X'
RETURN s
The OPTIONAL MATCH
clause is used to retrieve nodes and relationships based on a pattern, but unlike the regular MATCH
clause, it allows for the possibility of not finding a match. In other words, if the pattern specified in the OPTIONAL MATCH
clause does not match any nodes or relationships in the graph, the query will still return the result of the preceding MATCH
clause. The optional relationship matching is also supported in TiQL with the use of token ?
(please refer to Avatars_search for more details).
Find nodes labeled as "head" with a specific domain. Then, optionally look for a relationship labeled "hasRouterLink" between that node and another node labeled "tail" with a certain property value. If the relationship and the property match are found, it returns the "head" node. If not, it still returns the "head" node without the relationship.
TiQL
{
"query" : [ {
"$alias" : "head",
"$domain" : {
"$eq" : "http://www.example.com/optional/"
},
"-?>" : [ {
"$avatar" : "tail",
"$label" : "http://www.disit.org/km4city/schema#hasRouterLink",
"$alias" : "r1"
} ]
}, {
"$alias" : "tail",
"http://knoesis.wright.edu/ssw/ont/sensor-observation.owl#distance" : {
"$eq" : 300.0
}
} ],
"return" : [ "head" ]
}
Cypher query
MATCH (head {domain: 'http://www.example.com/optional/'})
OPTIONAL MATCH (head)-[r1: `http://www.disit.org/km4city/schema#hasRouterLink`]->
(tail {`http://knoesis.wright.edu/ssw/ont/sensor-observation.owl#distance` : 300.0})
RETURN head
Find the sensors for which the sum of properties x and y exceeds 3.
TiQL
{
"query" : [ {
"$alias" : "s",
"$label" : {
"$contains_all" : [ "SENSOR" ]
},
"$condition" : {
"$expr" : "$gt",
"left" : {
"$expr" : "$add",
"left" : {
"$expr" : "$dp",
"alias" : "s",
"property" : "x"
},
"right" : {
"$expr" : "$dp",
"alias" : "s",
"property" : "y"
}
},
"right" : 3
}
} ],
"return" : [ "s" ]
}
Cypher
MATCH (s:SENSOR)
WHERE s.x + s.y > 3
RETURN s
Find the sensors connected to a device whose IRI starts with 'D'.
TiQL
{
"query" : [ {
"$alias" : "s",
"$label" : {
"$contains_all" : [ "SENSOR" ]
},
"$relationships" : [ {
"$avatar" : "d",
"$label" : "CONNECTED",
"$direction" : "OUT"
} ]
}, {
"$alias" : "d",
"$label" : {
"$contains_all" : [ "DEVICE" ]
},
"IRI" : {
"$startswith" : "D"
}
} ],
"return" : [ "s" ]
}
Cypher
MATCH (s:SENSOR) -[:CONNECTED]-> (d:DEVICE)
WHERE d.IRI STARTS WITH 'D'
RETURN s
Find sensors connected to a device with non-empty provider ID.
TiQL
{
"query" : [ {
"$alias" : "s",
"$label" : {
"$contains_all" : [ "SENSOR" ]
},
"$relationships" : [ {
"$avatar" : "d",
"$label" : "CONNECTED",
"$direction" : "OUT"
} ]
}, {
"$alias" : "d",
"$label" : {
"$contains_all" : [ "DEVICE" ]
},
"PROVIDER_ID" : {
"$exists" : true
}
} ],
"return" : [ "s" ]
}
Cypher
MATCH (s:SENSOR) -[:CONNECTED]-> (d:DEVICE)
WHERE d.PROVIDER_ID IS NOT NULL
RETURN s
Find sensors connected to an object containing 'Device' in its list of classes.
TiQL
{
"query" : [ {
"$alias" : "s",
"$label" : {
"$contains_all" : [ "SENSOR" ]
},
"$relationships" : [ {
"$avatar" : "d",
"$label" : "CONNECTED",
"$direction" : "OUT"
} ]
}, {
"$alias" : "d",
"$classes" : {
"$contains" : "DEVICE"
}
} ],
"return" : [ "s" ]
}
Cypher
MATCH (s:SENSOR) -[:CONNECTED]-> (d)
WHERE 'DEVICE' IN d.classes
RETURN s
Keep in mind that the IN operator can be used in two ways:
- Before a property lookup (e.g., 'class_x' IN n.classes) to check if the value class_x exists in the list property class.
- After a property lookup (e.g., n.classes IN ['class_x', 'class_y']) to check if the list property is included in the provided list value ['class_x', 'class_y'].
Find sensors that are connected to devices whose provider IDs are either 'X' or 'Y'.
TiQL
{
"query" : [ {
"$alias" : "s",
"$label" : {
"$contains_all" : [ "SENSOR" ]
},
"$relationships" : [ {
"$avatar" : "d",
"$label" : "CONNECTED",
"$direction" : "OUT"
} ]
}, {
"$alias" : "d",
"$label" : {
"$contains_all" : [ "DEVICE" ]
},
"$condition" : {
"$expr" : "$or",
"left" : {
"$expr" : "$eq",
"left" : {
"$expr" : "$dp",
"alias" : "d",
"property" : "PROVIDER_ID"
},
"right" : "x"
},
"right" : {
"$expr" : "$eq",
"left" : {
"$expr" : "$dp",
"alias" : "d",
"property" : "PROVIDER_ID"
},
"right" : "y"
}
}
} ],
"return" : [ "s" ]
}
Cypher
MATCH (s:SENSOR) -[:CONNECTED]-> (d:DEVICE)
WHERE d.PROVIDER_ID = 'x' OR d.PROVIDER_ID = 'y'
RETURN s