Comparing properties in Gremlin - gremlin

I have a simple graph, with parents and children being vertices.
Parents have the relationship "isParentOf" to their children.
The vertices all have one property: the "familyName".
I want to use gremlin to match all the parents whose child's familyName is different from theirs.
Note: I cannot use the Groovy syntax of Gremlin. I must use pure Java code only.

The GremlinPipeline should look like this:
find all parents
follow the "isParentOf" relationship and get all the children
filter the children through a PipeFunction that compares the parent's "familyName" with the child's "familyName"
The problem is in the last step. How to retrieve the parent's "familyName", when this pipeline step only has access to (what is coming from the previous step, that is to say) the children?
My answer:
Accessing previous steps of a GremlinPipeline in a filter's PipeFunction is not possible. BUT it is possible if you use a PipesFunction instead (note the 's' !).
Let's look at the javadoc here:
public PipesFunction extends PipeFunction{
public AsMap getAsMap();
}
So you should setup the GremlinPipeline like this:
find all parents
name that step as "theParent"
follow the "isParentOf" relationship and get all the children
filter the children with a PipesFunction like this:
.filter(new PipesFunction<Vertex,Boolean>()
{
public Boolean compute(Vertex child) {
return parentHasDifferentFamilyName(child);
}
private Boolean parentHasDifferentFamilyName(child){
Vertex theParent = getAsMap().get("theParent");
String theParentFamilyName = theParent.getProperty("familyName");
String childFamilyName = child.getParameter("familyName");
return !(childFamilyName.equals(parentFamilyName));
}
})
Note: in the step 4, we could retrieve the "theParent" vertex thanks to the getAsMap() method, and thanks to the step 2 (that implicitly filled the "As" map).

Related

How to bind lists like an updating ForEach?

Here is a sample code:
public class Example3 {
class Point {
int x, y; // these can be properties if it matters
}
class PointRepresentation {
Point point; // this can be a property if it matters
public PointRepresentation(Point point) {
this.point = point;
}
}
Example3() {
ObservableList<Point> points = FXCollections.observableArrayList();
ObservableList<PointRepresentation> representations = FXCollections.observableArrayList();
points.forEach(point -> representations.add(new PointRepresentation(point)));
}
}
I have a data holder Point and a data representor PointRepresentation. I have a list of points and i would like that for each point in the list there would be an equivalent representation object in the second list. The code I gave works for the initialization but if there is any change later the above will not update.
What I am doing now is using a change listener to synchronize the lists (add and remove elements based on the change object) and it's OK but i am wondering if there's a simpler solution. I was looking for something like a "for each bind" that means: for each element in one list there is one in the other with the specified relation between them [in my case its that constructor]. In pseudocode:
representations.bindForEach(points, point -> new PointRepresentation(point));
Things I looked at: extractors for the list but that sends updates when a property in the objects they hold change and not when the list itself changes. So in my case if x in the point changes i can make an extractor that notifies it. Another thing I looked at is http://docs.oracle.com/javase/8/javafx/api/javafx/beans/binding/ListBinding.html, so maybe a custom binding does it but I don't know if it's simpler.
Also is there a similar solution for arrays instead of lists? i saw the http://docs.oracle.com/javase/8/javafx/api/javafx/collections/ObservableArray.html as a possibility.
The third-party library ReactFX has functionality for this. You can do
ObservableList<Point> points = FXCollections.observableArrayList();
ObservableList<PointRepresentation> representations = LiveList.map(points, PointRepresentation::new);
This will update representations automatically on add/remove etc changes to points.

Flex: Filter HierarchicalData child rows

I have an ArrayCollection of objects used as the source for a HierarchicalData object. My object looks roughly like this:
ObjectName (String)
SubCollection (ArrayCollection)
I am using the HierarchicalData in an AdvancedDataGrid to display the data in a grouped format.
I am able to filter the data in the ArrayCollection using a filterFunction. What I want to do now is also filter the records in the SubCollection as well so that only the items that match the filter are displayed in the AdvancedDataGrid.
Can anyone tell me how I can filter the child rows in a HierarchicalData?
This answer isn't a direct answer to your question, but it should help with some of the background. Essentially I am in the same position as you, where I need to show a specific data set depending on what type of parent node I have.
In this case, starting with an override to HierarchicalData.getChildren(node:Object):Object this will give you access to filter the first level children, and will also give you the ability to call a filtered method for sub-children to any n-th level.
You then use your extended class as the source to the ADG.
A pseudo-code example:
Class MyCollection extends HierarchicalData
override public function getChildren(node:Object):Object
{
if (node is a TopLevelObject)
(node.children as ArrayCollection).filterFunction = filterSub;
node.children.refresh();
else if (node is a SubCollectionObject)
(node.children as ArrayCollection).filterFunction = filterGrandChildren;
node.children.refresh();
// - OR -
//a more complex process of allowing the sub-node to determine it's filter
return node.filterSubCollectionGrandChildren();
return node;
}

RPN with operators other than *,/,+,-

I have a class called Node.
public Node
{
public int data;
public Node primaryNext;
public Node secondaryNext;
}
I have a Node root = null; And when the first value is received from the input, it then runs something like this.
root = new Node;
root.data = /*input*/ ;
root.primaryNext = null;
root.secondaryNext = null;
The next step is adding a new Node at the end of the list, by pointing root.primaryNext or root.secondaryNext to a new Node while filling the "pointer" tree by levels. So I need to do something like this:
GIF of the idea.
I think that this could be done using ||, &&, |, & operators applied to each level of nodes with a recursive method. So:
How do I operate in C# like the RPN?
If I can, which would be the best way to do it? I understand recursion pretty well, but I might not do the best possible method.
Thanks.
Suggestion:
One command to push a single node onto a stack.
Another command to take the two topmost nodes from the stack, combine them and push the result back on the stack.

Getting a tree node using the name and not the position

How to get a child tree node using the name and not the range of the node in the child node list .
i found this method but it uses the position of the element in the children list:
selectedNode.getChildren().get(i).
Many thanks
The collection you receive by calling getChildren() is a standard Java collection IIRC and is not indexed by name. The only ways I can think of realizing this is to create your own Node implementation or to iterate over the collection (which I think is the easiest solution).
public Node getNodeByName(String name)
{
for (Node n : selectedNode.getChildren())
{
if (name.equals(n.getName())
{ return n; }
}
return null;
}

Deleting Child Entities in JDO

I would like to know if the following is possible in JDO.
I have a 1-N relationship between a Parent and a Child class. So my classes look like
#PersistenceCapable
public class Parent {
#Persistent
private String name;
#Elements(mappedBy = "parent", dependent = "true")
private List<Children> children;
}
#PersistenceCapable
public class Child {
#Persistent
private String name;
#Persistent
private Parent parent;
}
Cascading deletes work fine. I can delete a parent and all its children will be removed from the data store.
If I query the data store for a particular child and have that query delete it, then the child is removed from the table of Child objects but its Parent's list of children will contain a null entry.
I guess this is a fairly dumb question but is there a way to get JDO to update the parent's list on deletion of a child or do I have to do this myself?
Thanks for your replies.
I recommend db4o without the DataNucleus layer. It is just getting in the way of a better performing soluton. We've done testing and found that if you use db4o directly it performs much better and uses less resources.

Resources