rich:tree - programmatically set the selected node - java

I have a tree (a parent could have any number of child ) and an inputText for searching nodes by name. Assume the following tree:
A
--A1
----AA1
--A2
Where A has two children A1 and A2, A1 has one child AA1.
When i type A1 in input text , i want to set nodes (A1,AA1) programmatically to select and if it is necessary expands nodes.
(i have a list<T> for building my tree)

I'm not sure what exactly you're asking for but I'll assume you want to set the value of node A1 and all it's children when you type "A1".
First you need to check that the user input is formatted correctly (doesn't look like A?1 when only letters and numbers are used to identify nodes).
Second, you need to locate the indicated node.
Third, you need to set the value and check for children.
Finally, once children are discovered, go back to the third step for each child.
Recursion could make your method more sleek but would not be necessary if you don't like recursion.
Sorry for such a vague answer but without a more detailed question it's hard to give a more detailed solution.

You can use TreeNode of richfaces instead of list, so you can easily achieve your requirements.
because using it you can easily get parent-child relationship using key value

Related

How to remove nodes with java API for graphstream?

I currently use the Graphstream API for Java for my project.
I wan't to delete or add Nodes on command.
With JFrame & co. I initialized a console so I can just insert
"addNode()" or "removeNode(id)" in order to get the result.
A Interface shows the nodes with a number next to them(the ID).
When I delete one node, I want all nodes with higher ID to change their ID,
but I did not figure out a way jet to change the ID of one node.
F.e. I have:
graph.addNode(0);
graph.addNode(1);
graph.addNode(2);
When deleting a Node:
graph.removeNode(0);
I want 1,2 to be changed to 0,1 without reinitializing the complete graph.
Is there a way to achieve this behaviour? I thought about something like:
graph.getNode(1).setID(0);
Unfortunately I have only access to .getID() and can't manipulate it this way.
Thanks
Nodes ids are strings and they are immutable (no renaming, no setId()).
Now what you are doing in your example is different. You are using the index-based access to the nodes. Indices are integers and correspond to arbitrary nodes in the graph, they are not associated to the ids.
When you do graph.addNode(0), the integer is converted to the string "0". Then when you do graph.removeNode(0), you are removing a node that was indexed as the first of the list of nodes. But it does have to be the node this id "0".
You can remove nodes with index (integer) 0 as long as there are nodes in the graph (graph.removeNode(0)) but you can only remove the one node with id "0" once (graph.removeNode("0")).

what data structure should be used in neo4j database to store combinations of elements

I've combinations of data to save in database. For eg: A+B+C is one combination. B+C+D is other.
Conditions:
1. A+B+C is same as B+A+C, C+B+A etc.
2. Also, each node will have an attribute called "weight". This depends on combination(In A+B+C combination, A 5g, B 6g and C 7g. Please note the third node will also have weight. Hence "weight" cannot be relationship).
Issues:
First: I have decided to go with graph database. But don't know how to meet the above conditions. If I go with undirected graph, A-B-C is a combination. But it can't return B-A-C as other. Since there is no connection from A to C.
Second: "weight" can't be a property in node, Because weight differs as per the combination. Also it can't be a relationship. Since the last node will also have weight to be considered.
Please help me on this.
Each combination can have a Combination node with WEIGHT relationships to the nodes in that combination.

how to choose or write my own java data structure allowing multi attributes search

I have a pretty large list containing many instances of one class, this class has many attributes(member variables). My problem is to find a feasible data structure to store these instances that allow searches based on multiple attributes like database search(i.e. A Student class, each student has age, date of birth, grade and GPA.find all 2nd year students whose ages are between 20 and 23). The Map seems not applicable as it only allow single key and if I create multi attribute index for searching, the big O is still not decreased. I also considered using trees like AVL tree, and I don't think it would work.
I'd be grateful if someone could give me some hints.
I think what you are looking for is an Inverted Index (using attribute name + value as keys) or possibly one Inverted Index per attribute. A search would build the intersection of all results found for each attribute.
You could do this:
Build an AVL tree with objects sorted by the most recurrent attribute (just one, e.g. "id" or "name").
Then create a search function that instead of taking a value, takes a Java lambda expression F (so your seacrh condition must be something like F(myObj) == true instead of myObj.deFaultAttribute == searchParameter)
For the example you gave, F could be something like ((myObj) -> myObj.year==2 && myObj.age>=20 && myObj.age<=23)
I hope it helps.

what is the best way to find longest path between set of nodes(certain nodes) in neo4j?

I have set of nodes which will be declared by user(I mean user enters some words and those which exist among my nodes will be included in my set of nodes),and I want to find the longest path which exists between just these nodes(does not have to include all of the nodes in set ,but should not include a node which is not exist in my set of nodes)in neo4j embedded java database.(I do not know what is the start node,all of them can be the start node.)
First I thought maybe I have to make a subgraph of these nodes and relations between them,and then find the longest path in that subgraph,but I don't know what is the right and best thing to do?
Then i thought,I can directly find longest path between the set of nodes(can that be done?).
Or if this can't be done,how about finding the relations of the certain nodes(set of nodes) and make a set of relations,and then find the longest path between set of relations?
I want the optimum and fastest way in performance,and I don't want cypher query,because I am using java core api(it is faster),so please tell me what is the best way?
Consider it as a large scale database,with so many nodes.
Thanks in advance.
i'm afraid this cannot be done by any standard algorithm except traversing the whole graph. thus, there is no optimum way in performance. neo4j has no build-in option for it.
this is an cypher query (sorry i don't know other input languages in neo4j):
start n1=node(*), n2=node(user)
match n1--n2 //filter only the just declared user nodes, or any other matching condition
with n1, n2
match p=n1-[r:..*]-m, m--n2 //all paths between declared user nodes
return p
order by length(r) desc
limit 1

Searching for the first matching element after a specific node (XPath and ITunes XML)

it's not nessesary to post my full code because I have just a short questions. I'm searching with XPath in a XML Doc for a text Value. I have a XML Like
<key>Name</key>
<string>Dat Ass</string>
<key>Artist</key>
<string>Earl Sweatshirt</string>
<key>Album</key>
<string>Kitchen Cutlery</string>
<key>Kind</key>
<string>MPEG-Audiodatei</string>
I have an Expression like this:
//string[preceding-sibling::key[text()[contains(., 'Name')]]]/text()
but this gives me ALL following string-tags, I just want the first one with the Song-Title.
greets Alex
Use:
(//string[preceding-sibling::key[1] = 'Name'])[1]/text()
Alternatively, one can use a forward-only expression:
(//key[. = 'Name'])[1]/following-sibling::string[1]/text()
Do note:
This is a common error. Any expression of the kind:
//someExpr[1]
Doesn't select "the first node in the document from all nodes selected by //someExpr". In fact it can select many nodes.
The above expression selects any node that is selected by //someExpr and that is the first such child of its parent.
This is why, without brackets, the other answer to this question is generally incorrect.
You can just add another predicate [1] to select the first matching node. The nested predicate using text() should be unneccessary:
//string[preceding-sibling::key[contains(., 'Name')]][1]/text()
Another, perhaps more efficient, way to select this node would be
//key[contains(., 'Name')]/following-sibling::*[1][self::string]
This selects the first node (with any name) following the wanted key node and tests if its name is string.

Categories