@Test public void serverCpuUsageQuery() { StatefulKnowledgeSession ksession = createKnowledgeSession(); Server winServer = new Server("winServer", 4, 4096, 2048, 25); ksession.insert(winServer); Server ubuntuServer = new Server("ubuntuServer", 4, 2048, 1024, 70); FactHandle ubuntuServerFactHandle = ksession.insert(ubuntuServer); Server debianServer = new Server("debianServer", 4, 2048, 1024, 10); ksession.insert(debianServer); CustomViewChangedEventListener listener = new CustomViewChangedEventListener(); LiveQuery query = ksession.openLiveQuery("serverCpuUsage", new Object[] {20}, listener); assertEquals(1, listener.getCurrentServers().size()); assertEquals(0, listener.getRemovedServers().size()); assertEquals(0, listener.getUpdatedServers().size()); ubuntuServer.setCpuUsage(10); ksession.update(ubuntuServerFactHandle, ubuntuServer); assertEquals(2, listener.getCurrentServers().size()); assertEquals(0, listener.getRemovedServers().size()); assertEquals(0, listener.getUpdatedServers().size()); ubuntuServer.setCpuUsage(5); ksession.update(ubuntuServerFactHandle, ubuntuServer); assertEquals(2, listener.getCurrentServers().size()); assertEquals(0, listener.getRemovedServers().size()); assertEquals(1, listener.getUpdatedServers().size()); query.close(); ksession.dispose(); }
@Test public void serverCpuUsageGlazedListQuery() { StatefulKnowledgeSession ksession = createKnowledgeSession(); Server winServer = new Server("winServer", 4, 4096, 2048, 25); FactHandle winServerFactHandle = ksession.insert(winServer); Server ubuntuServer = new Server("ubuntuServer", 4, 2048, 1024, 70); FactHandle ubuntuServerFactHandle = ksession.insert(ubuntuServer); Server debianServer = new Server("debianServer", 4, 2048, 1024, 10); ksession.insert(debianServer); GlazedListViewChangedEventListener listener = new GlazedListViewChangedEventListener(); LiveQuery query = ksession.openLiveQuery("serverCpuUsage", new Object[] {20}, listener); SortedList<Row> serverSortedList = new SortedList<Row>( listener, new Comparator<Row>() { public int compare(Row r1, Row r2) { Server server1 = (Server) r1.get("$server"); Server server2 = (Server) r2.get("$server"); return (server1.getCpuUsage() - server2.getCpuUsage()); } }); System.out.println( "#######################################################################################"); for (Row row : serverSortedList) { System.out.println(row.get("$server")); } assertEquals(10, ((Server) serverSortedList.get(0).get("$server")).getCpuUsage()); ubuntuServer.setCpuUsage(13); ksession.update(ubuntuServerFactHandle, ubuntuServer); System.out.println( "#######################################################################################"); for (Row row : serverSortedList) { System.out.println(row.get("$server")); } assertEquals(10, ((Server) serverSortedList.get(0).get("$server")).getCpuUsage()); assertEquals(13, ((Server) serverSortedList.get(1).get("$server")).getCpuUsage()); ubuntuServer.setCpuUsage(5); ksession.update(ubuntuServerFactHandle, ubuntuServer); winServer.setCpuUsage(0); ksession.update(winServerFactHandle, winServer); System.out.println( "#######################################################################################"); for (Row row : serverSortedList) { System.out.println(row.get("$server")); } assertEquals(0, ((Server) serverSortedList.get(0).get("$server")).getCpuUsage()); assertEquals(5, ((Server) serverSortedList.get(1).get("$server")).getCpuUsage()); assertEquals(10, ((Server) serverSortedList.get(2).get("$server")).getCpuUsage()); query.close(); ksession.dispose(); }