Depth traversal Orientdb - orientdb

How can I get depth traversal of a graph in Orientdb .
Using the documentation here is what I tried , yet when I run in I get an error here is the query .
EXPLAIN SELECT FROM (TRAVERSE any("Edge1") FROM P_H WHILE $depth <= 3) WHERE p ='SP00000000001';
The goal is the get the equivalent of this Neo4j Query :
MATCH (n:Node{NodeID:"SP00000000001"})-[:Edge1*1..3]-(d) RETURN Distinct d, n
Any help would be appreciated

The easiest thing is using a MATCH statement: http://orientdb.com/docs/2.2.x/SQL-Match.html
MATCH
{class:Node, as:n, where:(NodeID = "SP00000000001") -EdgeClass- {as:d, while:($depth < 3), where: ($matched.n != $currentMatch)} }
RETURN d, n
Or RETURN $elements if you want the vertices expanded

Related

NDepend: how to limit JustMyCode to the methods used by x?

The official documentation explains how to limit JustMyCode by exclusion using notmycode
notmycode from m in Methods where
m.SourceFileDeclAvailable &&
m.SourceDecls.First().SourceFile.FileName.ToLower().EndsWith(".designer.cs")
select m
I'd like to limit JustMyCode to only the methods returned by this query
from m in Methods
let depth0 = m.DepthOfIsUsedBy("InsertMuk(Int32,Int32,BetCoupon,Boolean,Boolean,Boolean,DateTime&,Boolean)")
where depth0 >= 0 orderby depth0
select new { m, depth0 }
Will it redefine the codebase for all the other queries?
This query should work, assuming you disable all other notmycode queries and also that you provide Namespace.ClassName in the string:
notmycode
let methodsUsed = Methods.Where(m1 => m1.DepthOfIsUsedBy("Namespace.ClassName.InsertMuk(Int32,Int32,BetCoupon,Boolean,Boolean,Boolean,DateTime&,Boolean)") >= 0)
from m in Methods.Except(methodsUsed )
select m

How to merge aql query with iterative traversal

I want to query a collection in ArangoDB using AQL, and at each node in the query, expand the node using a traversal.
I have attempted to do this by calling the traversal as a subquery using a LET statement within the collection query.
The result set for the traversal is empty, even though the query completes.
FOR ne IN energy
FILTER ne.identifier == "12345"
LET ne_edges = (
FOR v, e IN 1..1 ANY ne relation
RETURN e
)
RETURN MERGE(ne, {"edges": ne_edges})
[
{
"value": 123.99,
"edges": []
}
]
I have verified there are edges, and the traversal returns correctly when it is not executed as a subquery.
It seems as if the initial query is completing before a result is returned from the subquery, giving the result below.
What am I missing? or is there a better way?
I can think of two way to do this. This first is easier to understand but the second is more compact. For the examples below, I have a vertex collection test2 and an edge collection testEdge that links parent and child items within test2
Using Collect:
let seed = (FOR testItem IN test2
FILTER testItem._id in ['test2/Q1', 'test2/Q3']
RETURN testItem._id)
let traversal = (FOR seedItem in seed
FOR v, e IN 1..1 ANY seedItem
testEdge
RETURN {seed: seedItem, e_to: e._to})
for t in traversal
COLLECT seeds = t.seed INTO groups = t.e_to
return {myseed: seeds, mygroups: groups}
Above we first get the items we want to traverse through (seed), then we perform the traversal and get an object that has the seed .id and the related edges
Then we finally use collect into to group the results
Using array expansion
FOR testItem IN test2
FILTER testItem._id in ['test2/Q1', 'test2/Q3']
LET testEdges = (
FOR v, e IN 1..1 ANY testItem testEdge
RETURN e
)
RETURN {myseed: testItem._id, mygroups: testEdges[*]._to}
This time we combine the seed search and the traversal by using the let statement. then we use array expansion to group items
In either case, I end up with something that looks like this:
[
{
"myseed": "test2/Q1",
"mygroups": [
"test2/Q1-P5-2",
"test2/Q1-P6-3",
"test2/Q1-P4-1"
]
},
{
"myseed": "test2/Q3",
"mygroups": [
"test2/Q3",
"test2/Q3"
]
}
]

How to get the available connected vertex in OrientDB

I have a case: I want to get all the connected vertex (including the middle vertex) from a base vertex.
For example, the graph as below
enter image description here
I want to query all the connected vertexes from vertex ("giggs"), and I also want to query the connected path. ex: "giggs"->"192.168.0.1"->"ronaldo"->"192.168.0.2"->"veri". I used query as below:
MATCH {class: ic, as: s, where: (title = 'giggs')}.(outE(){where: 'some condition'}.inV().inE(){where: 'some condition'}.outV()){class: %s, as: t, while: ($depth <= 5), where: ($matched.s != $currentMatch)} RETURN $paths
I can get all the target nodes, ex: "veri", but I don't know the preceding vertex of "veri" and the edge between "veri" and its preceding vertex.
So how I can write the query? Thanks in advance.
Try this:
TRAVERSE both() FROM (SELECT EXPAND(s) FROM (MATCH {CLASS:ic, AS:s, WHERE:(name='giggs')} RETURN s))
Hope it helps
Regards

In Linq to EF 4.0, I want to return rows matching a list or all rows if the list is empty. How do I do this in an elegant way?

This sort of thing:
Dim MatchingValues() As Integer = {5, 6, 7}
Return From e in context.entity
Where MatchingValues.Contains(e.Id)
...works great. However, in my case, the values in MatchingValues are provided by the user. If none are provided, all rows ought to be returned. It would be wonderful if I could do this:
Return From e in context.entity
Where (MatchingValues.Length = 0) OrElse (MatchingValues.Contains(e.Id))
Alas, the array length test cannot be converted to SQL. I could, of course, code this:
If MatchingValues.Length = 0 Then
Return From e in context.entity
Else
Return From e in context.entity
Where MatchingValues.Contains(e.Id)
End If
This solution doesn't scale well. My application needs to work with 5 such lists, which means I'd need to code 32 queries, one for every situation.
I could also fill MatchingValues with every existing value when the user doesn't want to use the filter. However, there could be thousands of values in each of the five lists. Again, that's not optimal.
There must be a better way. Ideas?
Give this a try: (Sorry for the C# code, but you get the idea)
IQueryable<T> query = context.Entity;
if (matchingValues.Length < 0) {
query = query.Where(e => matchingValues.Contains(e.Id));
}
You could do this with the other lists aswell.

Add a Date in Linq to Entities

With Linq to Entities, I am trying to query a Log table to find rows near a matching row. I am having trouble with adding a date inside the query. This is what I have so far.
from
l in objectSet.Logs
let
match = objectSet.Logs.Where(whatever).FirstOrDefault()
where
l.Timestamp > (match.Timestamp - twoHours)
&& l.Timestamp < (match.Timestamp + twoHours)
select
l
Leaving out the "whatever" condition that finds the row I'm interested in, "twoHours" has variably been a time span, a .AddHours() function and so forth. I haven't found the right way that EF can generate SQL that adds the value from a field (match.Timestamp) to a constant.
The obvious solution is to do the "match" query first and then use the literal value in a second query, but I have simplified the code example here to the main problem (adding dates in the query) and in actual fact my query is more complex and this would not be ideal.
Cheers
You can generate an AddHours using the EntityFunctions class.
from
l in objectSet.Logs
let
match = objectSet.Logs.Where(whatever).FirstOrDefault()
where
(l.Timestamp > EntityFunctions.AddHours(match.Timestamp, -1 * twoHours))
&& // ...
select
l
However, don't expect this WHERE to be optimized with an index unless you have an expression index on the column.
EntityFunctions is deprecated in favor of DbFunctions
public int GetNumUsersByDay(DateTime Date)
{
using (var context = db)
{
var DateDay = new DateTime(Date.Year, Date.Month, Date.Day);
var DateDayTomorrow = DateDay.AddDays(1);
return context.Users.Where(m => DbFunctions.AddHours(m.DateCreated,-5) >= DateDay && m.DateCreated < DateDayTomorrow).Count();
}
}
As it was described in this article - http://www.devart.com/blogs/dotconnect/?p=2982#first, use parameters (declare variable) instead of DateTime using in your queries.