public static TreeRecord convertToTreeRecord(final Tree tree) { TreeRecord treeRecord = new TreeRecord(); treeRecord.setName(tree.getName()); treeRecord.setDescription(tree.getDescription()); Map<Long, Node> nodes = new HashMap<>(); List<NodeRecord> nodeRecords = new ArrayList<>(); nodes.put(0L, tree.getRootTransition().getDestinationNode(null, null)); Long transitionId = 0L; Long nodeId = 0L; Long count = 1L; while (!nodes.isEmpty()) { Node next = nodes.get(nodeId); NodeRecord converted = convertToNodeRecord(next, nodeId); converted.setTransitions(new ArrayList<TransitionRecord>()); for (Map.Entry<String, ITransition> entry : next.getTransitions().entrySet()) { TransitionRecord transitionRecord = new TransitionRecord(); transitionRecord.setId(transitionId++); transitionRecord.setKey(entry.getKey()); transitionRecord.setNode(count); nodes.put(count, entry.getValue().getDestinationNode(null, null)); converted.getTransitions().add(transitionRecord); ++count; } nodeRecords.add(converted); nodes.remove(nodeId); ++nodeId; } treeRecord.setNodes(nodeRecords); return treeRecord; }
return new Tree() .setName(tree.getName()) .setDescription(tree.getDescription()) .setRootTransition(new Transition().setDestinationNode(convertedNodes.get(rootId)));
@RequestMapping(value = "/tree/example", method = RequestMethod.POST) @ResponseStatus(HttpStatus.OK) public void createExampleTree() { decisionTreeService.saveDecisionTree(new Tree() .setName("Illness Tree") .setRootTransition(new Transition().setDestinationNode(new Node() .setPrompts(new TextToSpeechPrompt().setMessage("if you feel sick select 1, if not select 2")) .setTransitions(new Object[][]{ {"1", new Transition().setName("pressed1") .setDestinationNode(new Node() .setPrompts(new TextToSpeechPrompt().setMessage("if you have a fever select 1, if not select 3")) .setTransitions(new Object[][]{ {"1", new Transition().setName("pressed1").setDestinationNode( new Node().setPrompts(new TextToSpeechPrompt().setMessage("come to the hospital now")) )}, {"3", new Transition().setName("pressed3").setDestinationNode( new Node().setPrompts(new TextToSpeechPrompt().setMessage("be patient, we will call you")) )} }) )}, {"2", new Transition().setName("pressed2") .setDestinationNode(new Node().setPrompts(new TextToSpeechPrompt().setMessage("Check with us again")))} }) ))); }