/** * Converts a value of a lower domain numerical type to Long. * * @param value value * @param type type * * @return converted value */ private static long longValue(PropertyValue value, int type) { switch (type) { case SHORT: return value.getShort(); default: return value.getInt(); } }
/** * Converts a value of a lower domain numerical type to Float. * * @param value value * @param type type * * @return converted value */ private static float floatValue(PropertyValue value, int type) { switch (type) { case SHORT: return value.getShort(); case INT: return value.getInt(); default: return value.getLong(); } } }
/** * Converts a value of a lower domain numerical type to BigDecimal. * * @param value value * @param type type * * @return converted value */ private static BigDecimal bigDecimalValue(PropertyValue value, int type) { switch (type) { case SHORT: return BigDecimal.valueOf(value.getShort()); case INT: return BigDecimal.valueOf(value.getInt()); case LONG: return BigDecimal.valueOf(value.getLong()); case FLOAT: return BigDecimal.valueOf(value.getFloat()); default: return BigDecimal.valueOf(value.getDouble()); } }
/** * Converts a value of a lower domain numerical type to Double. * * @param value value * @param type type * * @return converted value */ private static double doubleValue(PropertyValue value, int type) { switch (type) { case SHORT: return value.getShort(); case INT: return value.getInt(); case LONG: return value.getLong(); default: return value.getFloat(); } }
@Override public GraphHead apply(GraphHead current, GraphHead transformed) { // property 'sum_birthday' is result of the aggregate function SumVertexProperty int sumAge = current.getPropertyValue("sum_birthday").getInt(); // property 'vertexCount' is result of the aggregate function VertexCount long numPerson = current.getPropertyValue("vertexCount").getLong(); current.setProperty(PROPERTY_KEY_MEAN_AGE, (double) sumAge / numPerson); return current; } }
@Test public void testGetInt() throws Exception { PropertyValue p = PropertyValue.create(INT_VAL_2); assertEquals(INT_VAL_2, p.getInt()); }
static Vertex transformVertex(Vertex current, Vertex transformed) { transformed.setLabel(current.getLabel()); if (current.getLabel().equals("A")) { transformed.setProperty("a", current.getPropertyValue("a").getInt() + 1); transformed.setProperty("b", current.getPropertyValue("b").getInt() - 1); } else if (current.getLabel().equals("B")) { transformed.setProperty("d", current.getPropertyValue("c")); } return transformed; }
static GraphHead transformGraphHead(GraphHead current, GraphHead transformed) { transformed.setLabel(current.getLabel()); transformed.setProperty("a", current.getPropertyValue("a").getInt() + 1); return transformed; }
@Test public void testSetInt() throws Exception { PropertyValue p = new PropertyValue(); p.setInt(INT_VAL_2); assertEquals(INT_VAL_2, p.getInt()); }
@Test public void testAddReturningInteger() { PropertyValue p; // int p = add(create(INT_VAL_2), create(INT_VAL_2)); assertTrue(p.isInt()); assertEquals(INT_VAL_2 + INT_VAL_2, p.getInt(), 0); // short p = add(create(INT_VAL_2), create(SHORT_VAL_e)); assertTrue(p.isInt()); assertEquals(INT_VAL_2 + SHORT_VAL_e, p.getInt(), 0); p = add(create(SHORT_VAL_e), create(INT_VAL_2)); assertTrue(p.isInt()); assertEquals(SHORT_VAL_e + INT_VAL_2, p.getInt(), 0); p = add(create(SHORT_VAL_e), create(SHORT_VAL_e)); assertTrue(p.isInt()); assertEquals(SHORT_VAL_e + SHORT_VAL_e, p.getInt(), 0); }
@Test public void testMultiplyReturningInteger() { PropertyValue p; // int p = multiply(create(INT_VAL_2), create(INT_VAL_2)); assertTrue(p.isInt()); assertEquals(INT_VAL_2 * INT_VAL_2, p.getInt(), 0); // short p = multiply(create(INT_VAL_2), create(SHORT_VAL_e)); assertTrue(p.isInt()); assertEquals(INT_VAL_2 * SHORT_VAL_e, p.getInt(), 0); p = multiply(create(SHORT_VAL_e), create(INT_VAL_2)); assertTrue(p.isInt()); assertEquals(SHORT_VAL_e * INT_VAL_2, p.getInt(), 0); p = multiply(create(SHORT_VAL_e), create(SHORT_VAL_e)); assertTrue(p.isInt()); assertEquals(SHORT_VAL_e * SHORT_VAL_e, p.getInt(), 0); }
@Test public void testSelectionWithEmptyResult() throws Exception { FlinkAsciiGraphLoader loader = getSocialNetworkLoader(); GraphCollection inputCollection = loader.getGraphCollectionByVariables("g0", "g1", "g2"); FilterFunction<GraphHead> predicateFunc = (FilterFunction<GraphHead>) entity -> entity.hasProperty("vertexCount") && entity.getPropertyValue("vertexCount").getInt() > 5; GraphCollection outputCollection = inputCollection.select(predicateFunc); collectAndAssertTrue(outputCollection.isEmpty()); } }
@Test public void testKeepOnlyRelevantVertices() throws Exception { FlinkAsciiGraphLoader loader = getLoaderFromString("source:G {source : \"graph\"}[" + " (a:Patent {author : \"asdf\", year: 2000, title: \"P1\"})-[:cite {difference : 0}]->(b:Patent {author : \"asdf\", year: 2000, title: \"P2\"})" + " (a)-[:cite {difference : 0}]->(c:Patent {author : \"asdf\", year: 2000, title: \"P3\"})" + " (b)-[:cite {difference : 0}]->(c)\n" + " (a)-[:cite {difference : 5}]->(d:Patent {author : \"zxcv\", year: 1995, title: \"Earlier...\"})" + " (b)-[:cite {difference : 5}]->(d)" + " (e:Patent {author : \"kdkdkd\", year: 1997, title: \"Once upon a time\"})-[e_d:cite {difference : 2}]->(d)" + "]"); GraphCollection sourceGraph = loader.getGraphCollectionByVariables("source"); // Caution: We can't use result.equalsByGraphElementIds because it internally uses a cross join // with equality of elements, which means, it ignores elements that are not within the other dataset // This means, the test would succeed even though we have too many vertices as a result of the // subgraph operator. org.junit.Assert.assertEquals(3, sourceGraph .apply(new ApplySubgraph(null, edge -> edge.getPropertyValue("difference").getInt() == 0)) .getVertices() .collect().size()); } }
@Test public void execute() throws Exception { GraphCollection collection = getTestCollection(); String propertyKey = "count"; GraphHeadReduceFunction countFunc = new CountGraphHeads(propertyKey); collection = collection.groupByIsomorphism(countFunc); List<GraphHead> graphHeads = collection.getGraphHeads().collect(); assertEquals(3, graphHeads.size()); for (GraphHead graphHead : graphHeads) { assertTrue(graphHead.hasProperty(propertyKey)); int count = graphHead.getPropertyValue(propertyKey).getInt(); String label = graphHead.getLabel(); if (label.equals("G")) { assertEquals(1, count); } else { assertEquals(2, count); } } } }
@Override public Edge<GradoopId, Double> map(org.gradoop.common.model.impl.pojo.Edge epgmEdge) throws Exception { reuseEdge.setSource(epgmEdge.getSourceId()); reuseEdge.setTarget(epgmEdge.getTargetId()); //cast incoming numeric value to double PropertyValue value = epgmEdge.getPropertyValue(propertyKey); if (value.isDouble()) { reuseEdge.setValue(value.getDouble()); } else if (value.isFloat()) { reuseEdge.setValue((double) value.getFloat()); } else if (value.isInt()) { reuseEdge.setValue((double) value.getInt()); } else if (value.isLong()) { reuseEdge.setValue((double) value.getLong()); } return reuseEdge; } }
@Override public E apply(E current, E transformed) { PropertyValue timePropertyValue = current.getPropertyValue(propertyKey); if (timePropertyValue == null) { return current; } LocalDateTime timeOfCall = LocalDateTime.ofEpochSecond( (long) timePropertyValue.getInt(), 0, OffsetDateTime.now().getOffset()); transformed.setLabel(current.getLabel()); transformed.setProperty("year", timeOfCall.getYear()); transformed.setProperty("month", timeOfCall.getMonth().getValue()); transformed.setProperty("day", timeOfCall.getDayOfMonth()); transformed.setProperty("hour", timeOfCall.getHour()); transformed.setProperty("minute", timeOfCall.getMinute()); return transformed; } }
@Override public Edge<GradoopId, Double> map(org.gradoop.common.model.impl.pojo.Edge epgmEdge) throws Exception { reuseEdge.setSource(epgmEdge.getSourceId()); reuseEdge.setTarget(epgmEdge.getTargetId()); //cast incoming numeric value to double PropertyValue value = epgmEdge.getPropertyValue(propertyKey); if (value.isDouble()) { reuseEdge.setValue(value.getDouble()); } else if (value.isFloat()) { reuseEdge.setValue((double) value.getFloat()); } else if (value.isInt()) { reuseEdge.setValue((double) value.getInt()); } else if (value.isLong()) { reuseEdge.setValue((double) value.getLong()); } return reuseEdge; } }
@Test public void testSelectionWithResult() throws Exception { FlinkAsciiGraphLoader loader = getSocialNetworkLoader(); GraphCollection inputCollection = loader.getGraphCollectionByVariables("g0", "g1", "g2"); GraphCollection expectedOutputCollection = loader.getGraphCollectionByVariables("g0", "g1"); FilterFunction<GraphHead> predicateFunc = (FilterFunction<GraphHead>) entity -> entity.hasProperty("vertexCount") && entity.getPropertyValue("vertexCount").getInt() == 3; GraphCollection outputCollection = inputCollection.select(predicateFunc); collectAndAssertTrue( expectedOutputCollection.equalsByGraphElementIds(outputCollection)); }
@Test public void testAppendSingleIdAndProperties() { GradoopId id = GradoopId.get(); Embedding embedding = new Embedding(); embedding.add(id, PropertyValue.create("String"), PropertyValue.create(42)); assertEquals(1, embedding.size()); assertEquals(id, embedding.getId(0)); assertEquals("String", embedding.getProperty(0).getString()); assertEquals(42, embedding.getProperty(1).getInt()); }
@Test public void testAppendProjectionEntryToExistingEmbedding() { GradoopId id = GradoopId.get(); Embedding embedding = createEmbedding(4); embedding.add(id, PropertyValue.create("String"), PropertyValue.create(42)); assertEquals(5, embedding.size()); assertNotEquals(id, embedding.getId(3)); assertEquals(id, embedding.getId(4)); assertEquals("String", embedding.getProperty(0).getString()); assertEquals(42, embedding.getProperty(1).getInt()); }