Titan GraphDB query within a set - titan

I have edges that has a property called type that stores the different type of relationships between two vertices. The type property is a set. I was wondering how can I query to get the edges that contain that type?
For example:
Types = [A, B, C]
Edge1.type = [A, B]
Edge2.type = [B, C]
Edge3.type = [A, C]
If I query for edges with type A, I would get Edge1 and Edge3.

Not efficient, but the only way I can think of:
In TP2:
g.E().filter { it.getProperty("type").contains("A") }
In TP3:
g.E().filter { it.get().value("type").contains("A") }

Related

Guava Graph library ElementOrder on Edges instead of Nodes

I have this straight-forward Graph structure using the Guava Graph library and I'd like to understand better if that is possible to sort the adjacents/edges (not the node order). For the sake of clarification:
import com.google.common.graph.ElementOrder;
import com.google.common.graph.GraphBuilder;
import com.google.common.graph.MutableGraph;
public class MyNodeTest {
public static void main(String[] args) {
MutableGraph<String> graph = GraphBuilder.undirected().nodeOrder(ElementOrder.insertion()).build();
graph.addNode("A");
graph.addNode("C");
graph.addNode("D");
graph.addNode("B");
graph.addNode("E");
graph.putEdge("A", "B");
graph.putEdge("A", "C");
graph.putEdge("A", "D");
graph.putEdge("A", "E");
System.out.println("My default Insertion.order Nodes: " + graph.nodes());
System.out.println("Adj. Order that I couldn't understand: " + graph.adjacentNodes("A"));
System.out.println("Successor. Order that I couldn't understand: " + graph.successors("A"));
System.out.println("Pred. Order that I couldn't understand: " + graph.predecessors("A"));
}
}
My outcome is:
My default Insertion.order Nodes: [A, C, D, B, E]
Adj. Order that I couldn't understand: [D, E, B, C]
Successor. Order that I couldn't understand: [D, E, B, C]
Pred. Order that I couldn't understand: [D, E, B, C]
Without further ado, what I mean is:
Using .nodeOrder(ElementOrder.insertion()) it is possible to sort the nodes themselves. Nonetheless, I'm more interested in sorting the edges associated with a given node in a way that if I used the putEdge respectively from A with B, C, D, E the outcome is precisely this instead of the above shown.
Any insight?
Thanks in advance.
In case someone faces the same question, here is how I solved it (disclaimer: not the optimal, but a working solution).
MutableNetwork<String, UUID> graph = NetworkBuilder.undirected().edgeOrder(ElementOrder.insertion()).build();
graph.addNode("A");
graph.addNode("C");
graph.addNode("D");
graph.addNode("B");
graph.addNode("E");
graph.addEdge("A", "B", UUID.randomUUID());
graph.addEdge("A", "C", UUID.randomUUID());
graph.addEdge("A", "D", UUID.randomUUID());
graph.addEdge("A", "E", UUID.randomUUID());
System.out.println("My default Insertion.order Nodes: " + graph.nodes());
System.out.println("Adj. Order that I couldn't understand: " + graph.adjacentNodes("A"));
System.out.println("Successor. Order that I couldn't understand: " + graph.successors("A"));
System.out.println("Pred. Order that I couldn't understand: " + graph.predecessors("A"));
And the results:
My default Insertion.order Nodes: [A, C, D, B, E]
Adj. Order that I couldn't understand: [B, C, D, E]
Successor. Order that I couldn't understand: [B, C, D, E]
Pred. Order that I couldn't understand: [B, C, D, E]
The MutableNetwork has the .edgeOrder(ElementOrder.insertion()) that does the trick. The cons here are associated with the K,V needed to create this data structure.
Regards

[AMPL]Impose a sum to be equal to the number of elements

I'm trying to impose a simple constrain that however doesn't work in any way I try. I'd to impose:
subject to myConstrain:
sum { a1 in A, a2 in A } myVar[a1,a2] = *<<<number of elements of the set A>>>*
How can I do that? Is there a function to use? I try in this way but it doesn't work.
subject to myConstrain:
sum { a1 in A, a2 in A } myVar[a1,a2] = sum {a in A} 1;
Thanks
You can write such constraint as follows:
subject to myConstrain:
sum{a1 in A, a2 in A} myVar[a1, a2] = card(A);

Flatten DStream of List type

Problem
Hey, I have a DStream with type List[A], what's the best way to transform this DStream into type A?
To help illustrate my goal, I want
List(A, A, A, ....), List(A, A, ...), List(A, A, A, ...), ...
to be
A, A, A, A, A, ...
Basically it's very similar to a flatten operation in concept. Thanks!
Update:
I think I figured it out, a simple flatMap should do it. Thanks anyways!
Just in case anyone want the answer.
If x is some DStream of List[A] then applying a flat map on x where the transformation function simply returns the list, will flatten those lists into a DStream of A.
val x: DStream[List[A]] = ...
val y: DStream[A] = x.flatmap(k => k)

New collection or nested array?

Supposed we have a db called A. The structure of A can be:
1) A( a, b, c, d).
a, b, c, d are collections.
And the element in each collection is like { _id:id, data : data }
2) A(k).
k(a, b, c, d)
k is a colletion. and a, b, c, d are elements inside k.
a, b, c, d are like
{
type : 'a / b / c / d',
data : [
{_id : id1, data : data1 },
{_id : id2, data : data2},
...
]
}
the daily operations are { get, inserting element into, empty element of } a, b, c and d.
Which one is better in terms of efficiency?
#Markus-W-Mahlberg is right about your actual-use-case.
As you are using mongodb and it uses documents not tabular data structure (such as ms-sql), your both approaches work fine and if you define right index, u get same performance.
But in my opinion if your types (a, b, c and d ) have different structures (different properties, different queries, different update scenarios, aggregation plans and ...) Use way1, other wise use Way2 with right index.

Scala infix type aliasing for >2 type parameters?

I know in Scala, you can do type ===>[A, B] = Map[A, B] and then you can use infix notation to define def foo: String ===> Int which is same as saying def foo: Map[String, Int]. Is there any way to exploit this infix notation to create types with >2 arguments? For example, I want something like this:
type A ~> B ~~~> C to be an alias of say Map[A, Pair[B, C]] ?
Is there anyway I can write something line this:
type A to B -> C as alias for (A, B, C) type?
Interestingly operator precedence as defined for symbolic methods doesn't seem to hold for symbolic type aliases. Instead infix type aliases are always evaluated left associative:
type -[A,B] = Map[A,B]
type /[A,B] = Map[A,B] // '/' has higher precedence than '-' as an operator
classOf[A - B / C] // Class[/[-[A,B],C]]
classOf[A / B - C] // Class[-[/[A,B],C]]
Unfortunately that means it will never be possible to do what you ask for without parentheses like this:
classOf[A - (B / C)] // Class[-[A,/[B,C]]
So the closest answer is the following:
type ~>[A,B] = Map[A,B]
type ~~~>[A,B] = Pair[A,B]
classOf[A ~> (B ~~~> C)] // Map[A,Pair[B,C]]
Ommitting the parentheses will only be possible if you use right associative aliases (ending with :)
type ~:[A,B] = Map[A,B]
type ~~~:[A,B] = Pair[A,B]
classOf[A ~: B ~~~: C] // Map[A,Pair[B,C]]
Again, unfortunately since all type aliases have the same precedence it is not possible to mix right and left associative aliases without parentheses.
Concerning the second part of your question: (A,B,C) is syntactic sugar for Tuple3[A,B,C] which is a type with three parameters. As infix types only take two parameters, I'm afraid that I believe there is no way to represent this type solely with infix types. You would always end up with nested two parameter types (e.g. (A,(B,C)) or ((A,B),C).
Short answer: no. A ~> B ~> C cannot mean Map[A, Pair[B, C]].
It could be made to mean Map[A, Map[B, C]], though, or Pair[A, Pair[B, C]].