I am trying to wrap my head around how to solve this problem in WPF using an MVVM pattern.
I am converting a win32 tree control into WPF. The old tree control uses a Node class hierarchy along these lines (BaseNode being the base class and each following item inheriting from it and extending slightly):
BaseNode, GroupNode, VehicleNode, PersonNode, EquipmentNode, SupplyNodes (etc, etc.)
Going from this, A GroupNode will have a list of child nodes which could be one or more GroupNodes, one or more VehicleNodes and one or more PersonNodes.
A VehicleNode would have a list of PersonNodes that would be the crew for operating the vehicle. The VehicleNode would also have a list of PersonNodes that would be passengers in the vehicle. Each of these is under a 'Dummy' node labeled Crew and Passengers (though they are both PersonNode Types).
Outside of this, a VehicleNode and PersonNode would each have EquipmentNodes and SupplyNodes.
The tree will have several "Group" nodes that will be expanded to list any items of that type.
Hopefully, this text diagram will help solve illustrate the problem.
Ground Fleet (GroupNode)
West Coast (GroupNode)
East Coast (GroupNode)
Truck 1 (VehicleNode)
Truck 2 (VehicleNode)
Crew (DummyNode)
Bill the Driver (PersonNode)
Passengers (DummyNode)
Passenger 1 (PersonNode)
Passenger 2 (PersonNode)
Equipment (DummyNode)
Camera (EquipmentNode)
Sunglasses (EquipmentNode)
Supplies (DummyNode)
Apple (SupplyNode)
Water Bottle (SupplyNode)
Equipment (DummyNode)
Jack (EquipmentNode)
Tire Iron (EquipmentNode)
Supplies (DummyNode)
SpareTire (SupplyNode)
Personnel (DummyNode)
Salesman Tom (PersonNode)
District Manager Sally (PersonNode)
So the instance of Truck 2 (VehicleNode) has six child nodes (list of BaseNodes) of various types:
List children; // {Bill The Driver(PersonNode), Passenger 1 (PersonNode), Passenger 2 (PersonNode), Jack (EquipmentNode), Tire Iron (EquipmentNode), SpareTire (SupplyNode)}
In our existing win32 tree, when we add the Truck 2 node, we manually cycle through the child nodes and add the dummy nodes (As required) and children to create the tree based on the type of nodes in the children list and potentially a property on the child node - the PersonNode has a flag on it to indicate if its a passenger or crew, so we know which parent node to add it should belong to.
I'm struggling with how this can be represented in an MVVM approach on a tree that would allow us to retain the Dummy Nodes as above.
Any help would be greatly appreciated!
Use HierarchicalDataTemplate, see a decent example here.
You would expose a list of nodes from your ViewModel, and bind that to an ItemsSource of a TreeView. Each node will also have a child property that is another list of nodes.
Each of you nodes could just be a simple class, or could actually be ViewModels themselves if you need to put ICommands/Actions/Methods at each node.
Each node can be setup however you wish in C# in your ViewModel, since HierarchicalDataTemplate does alot of binding work for you.
Related
Hi im having a bit of trouble understanding what should be a subclass or just merely an instance of a class in OWL ontologies
If i use an example of World Leaders
You have Presidents, Prime ministers, Chancellors etc
Every one of these is a world leader
So far i have tried to suggest that
All three of these are a subclass of World leader
But if i want to throw in Head Of State its where i start to get confused because some world leaders are also heads of state but some arent for example some prime ministers arent head of state
So if i were to throw in head of state would i be correct in assuming that
Head of state would be a subclass of world leader because not every leader is a head of state but every head of state is a leader?
And would President be an instance of head of state or its own subclass? And Also would Prime minister and chancellor be seperated from President like that so Prime minister and Chancellor be subclasses of World leader or actually instances of
Is there a general rule of thumb or guideline of how best to approach this?
Any help would be appreciated thanks
World Leaders, Presidents, Prime ministers, Chancellors, Heads of State - all of these represent sets which can have many members. Whenever what you are modelling can have multiple members, it is usually best to model using class axioms like subclass rather than individuals.
If you have A subclass B and C subclass D there is nothing stopping you from having an individual x that is a member of both A and D, except if you have an axiom stating for example stating A disjoint C or B disjoint D.
React newbie again.
I have and admin page for a forum. It's a tree structure with potentially infinite depth, ordered by order_id field.
I can get it from the api either as a tree (with a children array field holding the children of each node) or as a flat array with an ancestry (a string that holds the whole path to the node, separated by a /) and a parent_id field.
I need to be able to move them around, expand and collapse subtrees and be able to add a node in any place.
Now I have several questions:
Is there a good out-of-the-box UI library to be able to drag-drop them to rearrange? I found this one: https://github.com/frontend-collective/react-sortable-tree but I can't get it to not have a limited height frame, and also customizing those themes looks too complicated.
If I am to do it from scratch using react-dnd, what is the more efficient way of working with this tree - have it as a tree (how do I add/update the nodes?) or store it flat and build the tree on render (easy to manage the collection, but complicates rendering and sorting logic).
This sounds like a complicated and interesting problem. If the library cannot do what you want it to stylistically then you will have to build it from scratch.
I would store all of your node objects in an Array with a children property that holds a reference to all of it's children; you could use their order_id as a reference if it is guaranteed to be unique.
I would also store the parent node's order_id as well, as it will help when you manipulate the tree.
Nodes = [
{order_id: 1, name:"order1", children_ids:[2,3], parent_id: 0},
{order_id: 2, name:"order2", children_ids:[], parent_id: 1},
{order_id: 3, name:"order3", children_ids:[], parent_id: 1},
]
You will have to build the tree on render, it may be a brain twister but it's more than doable with a bit of work.
When you add a new node, you will have to update the parent and children of it's new spot in the tree.
When you rearrange a node, you will have to travel up to the parent and remove it from it's children. You will also have to travel down to it's children and set their parents to the rearranged node's old parent. This removes the node from the tree. Then, add the rearranged node to the tree.
Any manipulation you do will involve manipulating the parent and children of that node - this is why I recommend storing both children's id and the parent's id. It saves you from having to do too many tree traversals which is not computationally efficient.
I don't recommend using an ancestry field as you mentioned. The logic for moving nodes around will be too complicated and you would have to traverse and edit potentially the entire tree if you do it this way.
The first step is to integrate with react-dnd; make each node draggable. When you drop it onto another node to rearrange, you trigger your rearrange handler.
Best of luck.
I am new to JGraphT, and i have a use case, say a simple graph, A->B->C, and have (timed) events representing visiting the vertices, and they can come out of order, for example, event B (visits B) comes in first with event time (9:02), since it is the first, create a instance of the graph 1 (A->B->C) with B noted as visited. then event A comes in next with event time (9:00), since A is before (event time) B, and within a range (say 10 minute), A and B belong to the same graph instance, so I add A to instance 1, so now instance 1 has two vertices visited A and B.
I am wondering if anyone can give some suggestions/guide/direction as how JGraphT can help, with my custom implementation if necessary.
What I need;
Check if a new visit event (vertex) fits in an existing graph (instance) with my own logic to check event time.
Give me all the vertices visited for a graph (instance).
I want to divide my map in clusters and implement HPA*. Where do I start, each time I try this I run into problems. I need to implement this on a random and dynamically changing map.
I am unsure how to write an algorithm that places these "nodes", to connect the parts, in between sections/clusters of the map and update them. I guess every time open tiles lie in between closed tiles on the edge of a cluster/section there should be a node since inside the cluster it could be that multiple openings into the cluster do not connect to each other within this section.
Normally I would just have a big Tile[,] map. I guess I could just leave this be and create a cluster/section class that holds all the paths and nodes. And have a node class/struct that holds the 2 tiles that are connected between sections. I have read several articles about HPA* but I just can not wrap my head around implementing this correctly on a random and dynamical map. I hope to get some good pointers here although the question is not very clear.
-edit-
What i am trying to do is making cluster class that holds 10x10 tiles/nodes with on each side an entry point (several if there is a obstruction on the edge). The entries link to the next cluster.
For some reason, a treeview I'm buildling in Silverlight has decided it no longer wants to display the triangle associated with the root level. It still functions correctly though. Pictures below:
As you can see, it is just the root level that is exhibiting this behavior. Any ideas on what could be causing this?
After banging my head into a wall for awhile about this one. I got it, here's the source:
public void HandleGroupData(ObservableCollection<Group> groupTree)
{
foreach (var group in groupTree)
{
var groupNode = new TreeNode(group.DisplayText, ENodeType.Group, group.Id);
GetSubitemsOfGroup(group, groupNode);
RootLevel.Add(groupNode);
}
}
We build the TreeView from the database. Originally the RootLevel.Add and GetSubitems calls were in reverse order. GetSubitems is a routine that recursively is called building the tree in a DFS. What I believe was happening was that we were adding a node to the tree that had no children, so initially the Silverlight GUI builder thought that they had no child nodes so didn't give them little triangles.
Moral of the story: Watch the ordering of tree view creation!