@Test public void movingWithOnlyOnePoint() { Transform movingTransform = new MetricMappingTransform(new MovingValueMapping()); Map<Long, String> datapoints = new HashMap<Long, String>(); datapoints.put(0L, "3.0"); Map<Long, String> actual = new HashMap<Long, String>(); actual.put(0L, "3.0"); Metric metric = new Metric(TEST_SCOPE, TEST_METRIC); metric.setDatapoints(datapoints); List<Metric> metrics = new ArrayList<Metric>(); metrics.add(metric); List<String> constants = new ArrayList<String>(1); constants.add("120s"); constants.add("avg"); List<Metric> result = movingTransform.transform(metrics, constants); assertEquals(result.get(0).getDatapoints().size(), 1); assertEquals(result.get(0).getDatapoints(), actual); }
/* (non-Javadoc) * @see org.dyn4j.geometry.Convex#getFarthestPoint(org.dyn4j.geometry.Vector, org.dyn4j.geometry.Transform) */ @Override public Vector2 getFarthestPoint(Vector2 n, Transform transform) { // transform the normal into local space Vector2 localn = transform.getInverseTransformedR(n); Vector2 point = new Vector2(); // set the farthest point to the first one point.set(this.vertices[0]); // prime the projection amount double max = localn.dot(this.vertices[0]); // loop through the rest of the vertices to find a further point along the axis int size = this.vertices.length; for (int i = 1; i < size; i++) { // get the current vertex Vector2 v = this.vertices[i]; // project the vertex onto the axis double projection = localn.dot(v); // check to see if the projection is greater than the last if (projection > max) { // otherwise this point is the farthest so far so clear the array and add it point.set(v); // set the new maximum max = projection; } } // transform the point into world space transform.transform(point); return point; }
@Test public void testNonSquareMatrix() { int[][] testInput = { {6, 0, 5}, {0, 1, 1}, {1, 5, 0}, {0, 1, 0}, {1, 5, 7}, {1, 1, 1} }; double[][] testOutput = {{}}; double error = .00001; Matrix inputMatrix = new YaleSparseMatrix(6, 3); for (int row = 0; row < 6; ++row) for (int col = 0; col < 3; ++col) inputMatrix.set(row, col, testInput[row][col]); Transform transform = new TfIdfTransform(); Matrix outputMatrix = transform.transform(inputMatrix); for (int row = 0; row < 6; ++row) { for (int col = 0; col < 3; ++col) System.out.println(outputMatrix.get(row, col)); // assertEquals(testOutput[row][col], // outputMatrix.get(row, col), error); } }
@Test public void movingRunOutOfPointsBeforeHittingwindow() { Transform movingTransform = new MetricMappingTransform(new MovingValueMapping()); Map<Long, String> datapoints = new HashMap<Long, String>(); datapoints.put(0L, "3.0"); datapoints.put(60000L, "6.0"); datapoints.put(120000L, "9.0"); Map<Long, String> actual = new HashMap<Long, String>(); actual.put(0L, "3.0"); actual.put(60000L, "4.5"); actual.put(120000L, "7.5"); Metric metric = new Metric(TEST_SCOPE, TEST_METRIC); metric.setDatapoints(datapoints); List<Metric> metrics = new ArrayList<Metric>(); metrics.add(metric); List<String> constants = new ArrayList<String>(1); constants.add("120s"); constants.add("avg"); List<Metric> result = movingTransform.transform(metrics, constants); assertEquals(result.get(0).getDatapoints().size(), 3); assertEquals(result.get(0).getDatapoints(), actual); }
@Test public void testJoinTransformWithOneMetric() { Transform joinTransform = new JoinTransform(); Map<Long, String> datapoints_1 = new HashMap<Long, String>(); datapoints_1.put(1000L, "1"); datapoints_1.put(2000L, "2"); datapoints_1.put(3000L, "3"); Metric metric_1 = new Metric(TEST_SCOPE, TEST_METRIC); metric_1.setDatapoints(datapoints_1); List<Metric> metrics = new ArrayList<Metric>(); metrics.add(metric_1); Map<Long, String> expected = new HashMap<Long, String>(); expected.put(1000L, "1"); expected.put(2000L, "2"); expected.put(3000L, "3"); List<Metric> result = joinTransform.transform(metrics); assertEquals(result.get(0).getDatapoints().size(), 3); assertEquals(expected, result.get(0).getDatapoints()); }
/** * Converts a list of one type into a list of another type. * * @param xs The list to convert. * @param t How to transform each element. * @return A new list of type Y. */ public static <X, Y> List<Y> map(List<X> xs, Transform<X, Y> t) { List<Y> ys = new ArrayList<Y>(); for (X x : xs) { ys.add(t.transform(x)); } return ys; }
public void delete() throws ParserConfigurationException, SAXException, IOException, TransformerException { Document document = new GetDocument().getDocument(); Element rootElement = document.getDocumentElement(); NodeList contacts = rootElement.getChildNodes(); System.out.println("Enter the name of the contact to delete: "); BufferedReader inItem = new BufferedReader(new InputStreamReader(System.in)); String contactToDelete = inItem.readLine(); boolean isContactFound = false; for (int i = 0; i < contacts.getLength(); i++) { Node contact = contacts.item(i); NodeList contactData = contact.getChildNodes(); for (int j = 0; j < contactData.getLength(); j++) { Node dataNode = contactData.item(j); if (dataNode.getNodeName().equals("name") && dataNode.getTextContent().equals(contactToDelete)) { contact.getParentNode().removeChild(contact); isContactFound = true; System.out.println("The contact " + contactToDelete + " has been deleted."); } } } Transform.transform(document); if (!isContactFound) System.out.println("No such contact found."); }
@Test(expected = UnsupportedOperationException.class) public void testJoinTransformWithConstant() { Transform joinTransform = new JoinTransform(); List<Metric> metrics = new ArrayList<Metric>(); List<String> constants = new ArrayList<String>(); joinTransform.transform(metrics, constants); }
@Test public void testJoinTransformWithEmptyMetricsLists() { Transform joinTransform = new JoinTransform(); List<Metric> metrics_1 = new ArrayList<Metric>(); List<Metric> metrics_2 = new ArrayList<Metric>(); List<Metric> result = joinTransform.transform(metrics_1, metrics_2); assertEquals(result.size(), 0); }
@Test(expected = UnsupportedOperationException.class) public void transform_ShouldThrowUnsupportedOperationExceptionWhenNoWindowSizeSpecified() { List<Metric> metrics = new ArrayList<Metric>(); metrics.add(new Metric(TEST_SCOPE, TEST_METRIC)); Transform movingTransform = new MetricMappingTransform(new MovingValueMapping()); movingTransform.transform(metrics); }
@Test(expected = UnsupportedOperationException.class) public void transform_ShouldThrowUnsupportedOperationExceptionWhenTypeIsInvalid() { List<Metric> metrics = new ArrayList<Metric>(); metrics.add(new Metric(TEST_SCOPE, TEST_METRIC)); Transform movingTransform = new MetricMappingTransform(new MovingValueMapping()); List<String> constants = new ArrayList<String>(); constants.add("2"); constants.add("foobar"); movingTransform.transform(metrics); }
/* (non-Javadoc) * @see org.dyn4j.geometry.Convex#getFarthestFeature(org.dyn4j.geometry.Vector, org.dyn4j.geometry.Transform) */ @Override public Edge getFarthestFeature(Vector2 n, Transform transform) { // transform the normal into local space Vector2 localn = transform.getInverseTransformedR(n); Vector2 maximum = new Vector2(); double max = -Double.MAX_VALUE; int index = 0; // find the vertex on the polygon that is further along on the penetration axis int count = this.vertices.length; for (int i = 0; i < count; i++) { // get the current vertex Vector2 v = this.vertices[i]; // get the scalar projection of v onto axis double projection = localn.dot(v); // keep the maximum projection point if (projection > max) { // set the max point maximum.set(v); // set the new maximum max = projection; // save the index index = i; } } // once we have the point of maximum // see which edge is most perpendicular int l = index + 1 == count ? 0 : index + 1; int r = index - 1 < 0 ? count - 1 : index - 1; Vector2 leftN = this.normals[index == 0 ? count - 1 : index - 1]; Vector2 rightN = this.normals[index]; // create the maximum point for the feature (transform the maximum into world space) transform.transform(maximum); Vertex vm = new Vertex(maximum, index); // is the left or right edge more perpendicular? if (leftN.dot(localn) < rightN.dot(localn)) { Vector2 left = transform.getTransformed(this.vertices[l]); Vertex vl = new Vertex(left, l); // make sure the edge is the right winding return new Edge(vm, vl, vm, maximum.to(left), index + 1); } else { Vector2 right = transform.getTransformed(this.vertices[r]); Vertex vr = new Vertex(right, r); // make sure the edge is the right winding return new Edge(vr, vm, vm, right.to(maximum), index); } }
@Test public void testMatrixTransformSize() { int[][] testInput = { {0, 0, 0, 0, 1, 1, 2, 4, 5, 0}, {0, 1, 1, 2, 0, 1, 5, 2, 8, 10}, {1, 5, 0, 0, 1, 0, 6, 3, 7, 9}, {0, 1, 0, 1, 0, 1, 2, 0, 3, 0}, {1, 5, 7, 0, 0, 1, 6, 10, 2, 45}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1} }; double[][] testOutput = { { 0.00000, 0.00000, 0.00000, 0.00000, 0.17028, 0.10217, 0.04644, 0.10217, 0.09824, 0.00000, }, { 0.00000, 0.00810, 0.01171, 0.05268, 0.00000, 0.02107, 0.02395, 0.01054, 0.03242, 0.01621, }, { 0.07438, 0.08582, 0.00000, 0.00000, 0.07438, 0.00000, 0.06086, 0.03347, 0.06008, 0.03090, }, { 0.00000, 0.03929, 0.00000, 0.12771, 0.00000, 0.10217, 0.04644, 0.00000, 0.05894, 0.00000, }, { 0.03512, 0.04052, 0.08195, 0.00000, 0.00000, 0.02107, 0.02873, 0.05268, 0.00810, 0.07294, }, { -0.03177, -0.00733, -0.01059, -0.02383, -0.03177, -0.01906, -0.00433, -0.00477, -0.00367, -0.00147, } }; double error = .00001; Matrix inputMatrix = new YaleSparseMatrix(6, 10); for (int row = 0; row < 6; ++row) for (int col = 0; col < 10; ++col) inputMatrix.set(row, col, testInput[row][col]); Transform transform = new TfIdfTransform(); Matrix outputMatrix = transform.transform(inputMatrix); for (int row = 0; row < 6; ++row) { for (int col = 0; col < 10; ++col) assertEquals(testOutput[row][col], outputMatrix.get(row, col), error); } }
@Test public void testMovingMedianTransformWithTimeInterval() { Transform movingTransform = new MetricMappingTransform(new MovingValueMapping()); Map<Long, String> datapoints = new HashMap<Long, String>(); datapoints.put(1000L, "1"); datapoints.put(2000L, "2"); datapoints.put(3000L, "3"); datapoints.put(5000L, "10"); datapoints.put(6000L, "2"); datapoints.put(7000L, "3"); datapoints.put(10000L, "15"); Map<Long, String> actual = new HashMap<Long, String>(); actual.put(1000L, "1.0"); actual.put(2000L, "1.5"); actual.put(3000L, "2.5"); actual.put(5000L, "10.0"); actual.put(6000L, "6.0"); actual.put(7000L, "2.5"); actual.put(10000L, "15.0"); Metric metric = new Metric(TEST_SCOPE, TEST_METRIC); metric.setDatapoints(datapoints); List<Metric> metrics = new ArrayList<Metric>(); metrics.add(metric); List<String> constants = new ArrayList<String>(1); constants.add("2s"); constants.add("median"); List<Metric> result = movingTransform.transform(metrics, constants); assertEquals(result.get(0).getDatapoints().size(), actual.size()); assertEquals(result.get(0).getDatapoints(), actual); }
@Override public int currentSegment(double[] coords) { if (isDone()) { throw new NoSuchElementException("Iterator out of bounds"); } if (index == 5) { return SEG_CLOSE; } int type; if (index == 0) { type = SEG_MOVETO; coords[0] = x; coords[1] = y; } else { type = SEG_LINETO; switch (index) { case 1: coords[0] = x + width; coords[1] = y; break; case 2: coords[0] = x + width; coords[1] = y + height; break; case 3: coords[0] = x; coords[1] = y + height; break; case 4: coords[0] = x; coords[1] = y; break; } } if (t != null) { t.transform(coords, 0, coords, 0, 1); } return type; }
@Test public void testJoinTransformWithMultipleMetrics() { Transform joinTransform = new JoinTransform(); Map<Long, String> datapoints_1 = new HashMap<Long, String>(); datapoints_1.put(1000L, "1"); datapoints_1.put(2000L, "2"); datapoints_1.put(3000L, "3"); Metric metric_1 = new Metric(TEST_SCOPE, TEST_METRIC); metric_1.setDatapoints(datapoints_1); Map<Long, String> datapoints_2 = new HashMap<Long, String>(); datapoints_2.put(4000L, "10"); datapoints_2.put(5000L, "100"); datapoints_2.put(6000L, "1000"); Metric metric_2 = new Metric(TEST_SCOPE, TEST_METRIC); metric_2.setDatapoints(datapoints_2); Map<Long, String> datapoints_3 = new HashMap<Long, String>(); datapoints_3.put(7000L, "0.1"); datapoints_3.put(8000L, "0.01"); datapoints_3.put(9000L, "0.0001"); Metric metric_3 = new Metric(TEST_SCOPE, TEST_METRIC); metric_3.setDatapoints(datapoints_3); final List<Metric> metrics_1 = new ArrayList<Metric>(); final List<Metric> metrics_2 = new ArrayList<Metric>(); final List<Metric> metrics_3 = new ArrayList<Metric>(); metrics_1.add(metric_1); metrics_2.add(metric_2); metrics_3.add(metric_3); Map<Long, String> expected_1 = new HashMap<Long, String>(); Map<Long, String> expected_2 = new HashMap<Long, String>(); Map<Long, String> expected_3 = new HashMap<Long, String>(); expected_1.put(1000L, "1"); expected_1.put(2000L, "2"); expected_1.put(3000L, "3"); expected_2.put(4000L, "10"); expected_2.put(5000L, "100"); expected_2.put(6000L, "1000"); expected_3.put(7000L, "0.1"); expected_3.put(8000L, "0.01"); expected_3.put(9000L, "0.0001"); List<Metric> result = joinTransform.transform(metrics_1, metrics_2, metrics_3); assertEquals(result.size(), 3); assertEquals(expected_1, result.get(0).getDatapoints()); assertEquals(expected_2, result.get(1).getDatapoints()); assertEquals(expected_3, result.get(2).getDatapoints()); }
private XYChart.Data<Number, Number> xform(Number time, Number value) { return new XYChart.Data<>(xXform.transform(time), yXform.transform(value)); }