1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
public class TreeTest { private final Logger log = LoggerFactory.getLogger(TreeTest.class);
private Tree tree;
@Before public void initTree() { TreeNode rootNode = new TreeNode(); rootNode.setNodeId(1L); rootNode.setNodeType("branch");
TreeNode rootChildOne = new TreeNode(); rootChildOne.setNodeId(11L); rootChildOne.setNodeType("branch"); rootChildOne.setFromNodeId(rootNode.getNodeId()); TreeNode rootChildTwo = new TreeNode(); rootChildTwo.setNodeId(12L); rootChildTwo.setNodeType("branch"); rootChildTwo.setFromNodeId(rootNode.getNodeId());
TreeNode grandChildrenOne = new TreeNode(); grandChildrenOne.setNodeId(111L); grandChildrenOne.setNodeType("leaf"); grandChildrenOne.setNodeValue("推送二次元类文章"); grandChildrenOne.setFromNodeId(rootChildOne.getFromNodeId()); TreeNode grandChildrenTwo = new TreeNode(); grandChildrenTwo.setNodeId(112L); grandChildrenTwo.setNodeType("leaf"); grandChildrenTwo.setNodeValue("推送财经类文章"); grandChildrenTwo.setFromNodeId(rootChildOne.getFromNodeId()); TreeNode grandChildrenThree = new TreeNode(); grandChildrenThree.setNodeId(121L); grandChildrenThree.setNodeType("leaf"); grandChildrenThree.setNodeValue("推送A类文章"); grandChildrenThree.setFromNodeId(rootChildTwo.getFromNodeId()); TreeNode grandChildrenFour = new TreeNode(); grandChildrenFour.setNodeId(122L); grandChildrenFour.setNodeType("leaf"); grandChildrenFour.setNodeValue("推送B类文章"); grandChildrenFour.setFromNodeId(rootChildOne.getFromNodeId());
String rootDecisionName = "gender"; rootNode.setDecisionName(rootDecisionName); TreeNodeDecision manDecision = new TreeNodeDecision(); manDecision.setDecisionType("eq"); manDecision.setDecisionValue("man"); manDecision.setFromNode(rootNode.getNodeId()); manDecision.setToNode(rootChildOne.getNodeId()); TreeNodeDecision womanDecision = new TreeNodeDecision(); womanDecision.setDecisionType("eq"); womanDecision.setDecisionValue("woman"); womanDecision.setFromNode(rootNode.getNodeId()); womanDecision.setToNode(rootChildTwo.getNodeId()); List<TreeNodeDecision> rootNodeDecisionList = new ArrayList<>(2); rootNodeDecisionList.add(manDecision); rootNodeDecisionList.add(womanDecision); rootNode.setTreeNodeDecisionList(rootNodeDecisionList);
String childDecisionName = "age"; rootChildOne.setDecisionName(childDecisionName); rootChildTwo.setDecisionName(childDecisionName); TreeNodeDecision ageDecisionOne = new TreeNodeDecision(); ageDecisionOne.setDecisionType("lte"); ageDecisionOne.setDecisionValue("24"); ageDecisionOne.setFromNode(rootChildOne.getNodeId()); ageDecisionOne.setToNode(grandChildrenOne.getNodeId()); TreeNodeDecision ageDecisionTwo = new TreeNodeDecision(); ageDecisionTwo.setDecisionType("gt"); ageDecisionTwo.setDecisionValue("24"); ageDecisionTwo.setFromNode(rootChildOne.getNodeId()); ageDecisionTwo.setToNode(grandChildrenTwo.getNodeId()); List<TreeNodeDecision> rootChildOneDecisionList = new ArrayList<>(2); rootChildOneDecisionList.add(ageDecisionOne); rootChildOneDecisionList.add(ageDecisionTwo); rootChildOne.setTreeNodeDecisionList(rootChildOneDecisionList);
TreeNodeDecision ageDecisionThree = new TreeNodeDecision(); ageDecisionThree.setDecisionType("lte"); ageDecisionThree.setDecisionValue("35"); ageDecisionThree.setFromNode(rootChildTwo.getNodeId()); ageDecisionThree.setToNode(grandChildrenThree.getNodeId()); TreeNodeDecision ageDecisionFour = new TreeNodeDecision(); ageDecisionFour.setDecisionType("gt"); ageDecisionFour.setDecisionValue("35"); ageDecisionFour.setFromNode(rootChildTwo.getNodeId()); ageDecisionFour.setToNode(grandChildrenFour.getNodeId()); List<TreeNodeDecision> rootChildTwoDecisionList = new ArrayList<>(2); rootChildTwoDecisionList.add(ageDecisionThree); rootChildTwoDecisionList.add(ageDecisionFour); rootChildTwo.setTreeNodeDecisionList(rootChildTwoDecisionList);
tree = new Tree(); tree.setId(1L); tree.setName("精准化运营消息推送决策树"); tree.setTreeRootNode(rootNode); Map<Long, TreeNode> treeNodeMap = new TreeMap<>(); treeNodeMap.put(rootNode.getNodeId(), rootNode); treeNodeMap.put(rootChildOne.getNodeId(), rootChildOne); treeNodeMap.put(rootChildTwo.getNodeId(), rootChildTwo); treeNodeMap.put(grandChildrenOne.getNodeId(), grandChildrenOne); treeNodeMap.put(grandChildrenTwo.getNodeId(), grandChildrenTwo); treeNodeMap.put(grandChildrenThree.getNodeId(), grandChildrenThree); treeNodeMap.put(grandChildrenFour.getNodeId(), grandChildrenFour); tree.setTreeNodeMap(treeNodeMap); }
@Test public void testUserPushDecisionTree() { UserPushDecisionTreeService userPushDecisionTreeService = new UserPushDecisionTreeService(); Map<String, String> userData = new HashMap<>(); userData.put("gender", "man"); userData.put("age", "29"); DecisionResult result = userPushDecisionTreeService.process(tree, "yiyufxst", userData); log.info("性别:{},年龄:{}的用户推送{}", userData.get("gender"), userData.get("age"), result.getResultNodeValue()); userData.put("age", "24"); result = userPushDecisionTreeService.process(tree, "yiyufxst", userData); log.info("性别:{},年龄:{}的用户推送{}", userData.get("gender"), userData.get("age"), result.getResultNodeValue()); userData.put("gender", "woman"); userData.put("age", "35"); result = userPushDecisionTreeService.process(tree, "马冬梅", userData); log.info("性别:{},年龄:{}的用户推送{}", userData.get("gender"), userData.get("age"), result.getResultNodeValue()); userData.put("age", "40"); result = userPushDecisionTreeService.process(tree, "马冬梅", userData); log.info("性别:{},年龄:{}的用户推送{}", userData.get("gender"), userData.get("age"), result.getResultNodeValue()); } }
|