OrientDB traverse specific path - orientdb

I have a couple of vertices (with property value) connected with edges in OrientDB such as:
X[value=0] ------------->Y[value=0]
^ \
/ v
A[value=1] -> B[value=1] -> C[value=1] -> D[value=1] -> E[value=1]
I now want to have the following traverse condition:
continue following vertices with value=1
if you find 2 paths, only follow path along value=0
The query on the vertices above should therefore return:
A,B,X,Y,D,E (C ignored and not visited)
How is that realizable in orientdb?

Related

Calculating recursively all possible List of positions on a board scala

I am trying to write a method, which, using a path and a dimension of a board, compute recursively the number of possible path that can travel through the board, once at each case.
For this I created the method : legal_moves(dim: Int, path : Path, x : Pos)
this method returns a list of all possible moves from position x on board of dimension dim that are not in the path yet.
info on types : type Pos = (Int,Int)
type Path = List[Pos]
this methods works correctly.
Then I use the following recursive method to calculate all possible tours from a given path :
def count_tours(dim: Int, path : Path) : Int = {
if (path.length == dim * dim) 1
val x = for (m <- legal_moves(dim,path,path.head)) yield {
count_tours(dim,m::path)
}
x.sum
}
But for some reasons, this function will only return 0
Edit :
Example of input and output :
Let's say we have the 3 following positions : (0,0) (1,1) (1,2) and that in this case, a move allow us to travel between them as we want (e.g. : we can go from (0,0) to (1,1) but also from (0,0) to (1,2) etc ...
we can I call count_tours(dim,(0,0))
I want the output to be 2.
why ? we start from (0,0) (starting path) then we do : -> (1,1) -> (1,2) this is one path
or we can do (0,0) -> (1,2) -> (1,1) this is another path
2 possible paths so output is 2.

Adding edge attribute causes TypeError: 'AtlasView' object does not support item assignment

Using networkx 2.0 I try to dynamically add an additional edge attribute by looping through all the edges. The graph is a MultiDiGraph.
According to the tutorial it seems to be possible to add edge attributes the way I do in the code below:
g = nx.read_gpickle("../pickles/" + gname)
yearmonth = gname[:7]
g.name = yearmonth # works
for source, target in g.edges():
g[source][target]['yearmonth'] = yearmonth
This code throws the following error:
TypeError: 'AtlasView' object does not support item assignment
What am I doing wrong?
That should happen if your graph is a nx.MultiGraph. From which case you need an extra index going from 0 to n where n is the number of edges between the two nodes.
Try:
for source, target in g.edges():
g[source][target][0]['yearmonth'] = yearmonth
The tutorial example is intended for a nx.Graph.

StackOverflowError when executing cypher queries through Neo4J REST API

I'm using Neo4j version 3.0.7
I'm reading a list of edges from a dataset and I need to pass those edges batch-wise using the REST API.
I used the following query format to create multiple nodes (if they already don't exist) and their relationships in Neo4j through a single Cypher query via the REST API. I obtain the two vertices of an edge and the node properties are set according to the vertex IDs of those vertices.
{
"query":
"MATCH (n { name: 0 }), (m { name:1 })
CREATE (n)-[:X]->(m)
WITH count(*) as dummy
MATCH (n { name: 0 }), (m { name: 6309 })
CREATE (n)-[:X]->(m)"
}
This approach works correctly for a batch of 10 edges but when I try to send a batch of 1000 edges (nodes and their relationships) through a single Cypher query, I get a StackOverflowError exception. Is there a better approach to achieve this task?
Thank you for your help.
The error obtained from the response:
{
"exception" : "StackOverflowError",
"fullname" : "java.lang.StackOverflowError",
"stackTrace" : [ "scala.collection.TraversableOnce$class.$div$colon(TraversableOnce.scala:151) ..."
}
You can use UNWIND to get a single query:
UNWIND [[0,1], [0,6309]] AS pair
MATCH (n {name: pair[0]}), (m {name: pair[1]})
CREATE (n)-[:X]->(m)
Insert your node pairs after UNWIND as a list of two-element lists. As the query uses the name property for finding the nodes, it is worth adding an index to it. For example, if you haven Person nodes, index them with:
CREATE INDEX ON :Person(name)
(See also the Cypher reference card.)

spark-graphx finding the most active user?

I have a graph of this form:
_ 3 _
/' '\
(1) (1)
/ \
1--(2)--->2
I want to count the most active user (who follow the most,here it's user 1 who follows two times user 2 and one time user 3).
My graph is of this form Graph[Int,Int]
val edges = Array(Edge(1,10,1), Edge(10,1,1), Edge(11,1,1), Edge(1,11,1), Edge(1,12,1))
val vertices = Array((12L,12), (10L,10), (11L,11), (1L,1))
val graph = Graph(sc.parallelize(vertices),sc.parallelize(edges),0)
My idea is to use to group srcId for the edges and to count using the iterator and then to sort but I have issues to use the iterator, the type are quite complex:
graph.edges.groupBy(_.dstId).collect() has type:
Array[(org.apache.spark.graphx.VertexId,Iterable[org.apache.spark.graphx.Edge[Int]])]
Any ideas ?
Your idea of grouping by srcId is good, since you are looking for the relation follows and not is followed by (your example uses dstId by the way)
val group = graph.edges.groupBy(_.srcId)
group now contains the edges going out of each vertex. We can now take the sum of the attributes to get the total time the user follows any user.
val followCount = group.map{
case (vertex, edges) => (vertex, edges.map(_.attr).sum)
}.collect
Which produces
Array((10,1), (11,1), (1,3))
Now if you want to extract the user which follows the most, you can simply sort it by descending order and take the head of the list, which will give the most active user.
val mostActiveUser = followCount.sortBy(- _._2).head

How make logic OR between vertex Indexes in Titan 1.0 / TP3 3.01 using predicate Text

During my migration from TP2 0.54 -> TP3 titan 1.0 / Tinkerpop 3.01
I'm trying to build gremlin query which make "logical OR" with Predicate Text , between properties on different Vertex indexes
Something like:
------------------- PRE-DEFINED ES INDEXES: ------------------
tg = TitanFactory.open('../conf/titan-cassandra-es.properties')
tm = tg.openManagement();
g=tg.traversal();
PropertyKey pNodeType = createPropertyKey(tm, "nodeType", String.class, Cardinality.SINGLE);
PropertyKey userContent = createPropertyKey(tm, "storyContent", String.class, Cardinality.SINGLE);
PropertyKey storyContent = createPropertyKey(tm, "userContent", String.class, Cardinality.SINGLE);
//"storyContent" : is elasticsearch backend index - mixed
tm.buildIndex(indexName, Vertex.class).addKey(storyContent, Mapping.TEXTSTRING.asParameter()).ib.addKey(pNodeType, Mapping.TEXTSTRING.asParameter()).buildMixedIndex("search");
//"userContent" : is elasticsearch backend index - mixed
tm.buildIndex(indexName, Vertex.class).addKey(userContent, Mapping.TEXTSTRING.asParameter()).ib.addKey(pNodeType, Mapping.TEXTSTRING.asParameter()).buildMixedIndex("search");
v1= g.addVertex()
v1.property("nodeType","USER")
v1.property("userContent" , "dccsdsadas")
v2= g.addVertex()
v2.property("nodeType","STORY")
v2.property("storyContent" , "abdsds")
v3= g.addVertex()
v3.property("nodeType","STORY")
v3.property("storyContent" , "xxxx")
v4= g.addVertex()
v4.property("nodeType","STORY")
v4.property("storyContent" , "abdsds") , etc'...
------------------- EXPECTED RESULT: -----------
I want to return all vertexes with property "storyContent" match text contains prefix , OR all vertexes with property "userContent" matching its case.
in this case return v1 and v2 , because v3 doesn't match and v4 duplicated so it must be ignored by dedup step
g.V().has("storyContent", textContainsPrefix("ab")) "OR" has("userContent", textContainsPrefix("dc"))
or maybe :
g.V().or(_().has('storyContent', textContainsPrefix("abc")), _().has('userContent', textContainsPrefix("dcc")))
PS,
I thought use TP3 OR step with dedup , but gremlin throws error ...
Thanks for any help
Vitaly
How about something along those lines:
g.V().or(
has('storyContent', textContainsPrefix("abc")),
has('userContent', textContainsPrefix("dcc"))
)
Edit - as mentioned in the comments, this query won't use any index. It must be split into two separate queries.
See TinkerPop v3.0.1 Drop Step documentation and Titan v1.0.0 Ch. 20 - Index Parameters and Full-Text Search documentation.
With Titan, you might have to import text predicates before:
import static com.thinkaurelius.titan.core.attribute.Text.*
_.() is TinkerPop2 material and no longer used in TinkerPop3. You now use anonymous traversals as predicates, which sometimes have to start with __. for steps named with reserved keywords in Groovy (for ex. __.in()).