@Test public void testEntityGraphEquals(){ SimpleSocialNetwork ssn1 = createSimpleSocialNetwork(); SimpleSocialNetwork ssn2 = createSimpleSocialNetwork(); swapFirstAndSecondElements(ssn1.users); assertGraphsEquality(ssn1, ssn2, false); swapFirstAndSecondElements(ssn1.users); assertGraphsEquality(ssn1, ssn2, true); swapFirstAndSecondElements(ssn1.users.get(0).hobbies); assertGraphsEquality(ssn1, ssn2, false); swapFirstAndSecondElements(ssn1.users.get(0).hobbies); assertGraphsEquality(ssn1, ssn2, true); }
public static boolean entityGraphEquals(NliRootEntity root1, NliRootEntity root2) { IdentityHashMap<NliEntity, NliEntity> entityInTree1ToEntityInTree2 = new IdentityHashMap<>(); ArrayList<NliEntity> nonPrimitiveEntitiesInTree1 = new ArrayList<>( getNonPrimitiveEntitiesReachableFromRoot(root1, true, true)); ArrayList<NliEntity> nonPrimitiveEntitiesInTree2 = new ArrayList<>( getNonPrimitiveEntitiesReachableFromRoot(root2, true, true)); if (nonPrimitiveEntitiesInTree1.size() != nonPrimitiveEntitiesInTree2.size()) return false; for (int i=0; i<nonPrimitiveEntitiesInTree1.size(); i++) { entityInTree1ToEntityInTree2.put(nonPrimitiveEntitiesInTree1.get(i), nonPrimitiveEntitiesInTree2.get(i)); } for (NliEntity entityOnTree1 : nonPrimitiveEntitiesInTree1){ NliEntity entityOnTree2 = entityInTree1ToEntityInTree2.get(entityOnTree1); if (!getPrimitiveEntitiesInOutgoingRelationFromGivenEntity(entityOnTree1,true).equals( getPrimitiveEntitiesInOutgoingRelationFromGivenEntity(entityOnTree2,true))) { return false; } ArrayList<Pair<Field, NliEntity>> actualPairsInTree2 = new ArrayList<>(getNonPrimitiveEntitiesInOutgoingRelationFromGivenEntity(entityOnTree2,true)); ArrayList<Pair<Field, NliEntity>> expectedPairsInTree2 = getNonPrimitiveEntitiesInOutgoingRelationFromGivenEntity(entityOnTree1,true).stream() .<Pair<Field, NliEntity>>map(fieldEntity->new ImmutablePair<>(fieldEntity.getLeft(), entityInTree1ToEntityInTree2.get(fieldEntity.getRight()))) .collect(Collectors.toCollection(ArrayList::new)); if (actualPairsInTree2.size() != expectedPairsInTree2.size()) return false; for (int i=0; i<actualPairsInTree2.size(); i++){ if (!actualPairsInTree2.get(i).getLeft().equals(expectedPairsInTree2.get(i).getLeft())) return false; if (actualPairsInTree2.get(i).getRight() != expectedPairsInTree2.get(i).getRight()) return false; } } return true; }
EntityGraphReflection { public static boolean entityGraphEquals(NliRootEntity root1, NliRootEntity root2) { IdentityHashMap<NliEntity, NliEntity> entityInTree1ToEntityInTree2 = new IdentityHashMap<>(); ArrayList<NliEntity> nonPrimitiveEntitiesInTree1 = new ArrayList<>( getNonPrimitiveEntitiesReachableFromRoot(root1, true, true)); ArrayList<NliEntity> nonPrimitiveEntitiesInTree2 = new ArrayList<>( getNonPrimitiveEntitiesReachableFromRoot(root2, true, true)); if (nonPrimitiveEntitiesInTree1.size() != nonPrimitiveEntitiesInTree2.size()) return false; for (int i=0; i<nonPrimitiveEntitiesInTree1.size(); i++) { entityInTree1ToEntityInTree2.put(nonPrimitiveEntitiesInTree1.get(i), nonPrimitiveEntitiesInTree2.get(i)); } for (NliEntity entityOnTree1 : nonPrimitiveEntitiesInTree1){ NliEntity entityOnTree2 = entityInTree1ToEntityInTree2.get(entityOnTree1); if (!getPrimitiveEntitiesInOutgoingRelationFromGivenEntity(entityOnTree1,true).equals( getPrimitiveEntitiesInOutgoingRelationFromGivenEntity(entityOnTree2,true))) { return false; } ArrayList<Pair<Field, NliEntity>> actualPairsInTree2 = new ArrayList<>(getNonPrimitiveEntitiesInOutgoingRelationFromGivenEntity(entityOnTree2,true)); ArrayList<Pair<Field, NliEntity>> expectedPairsInTree2 = getNonPrimitiveEntitiesInOutgoingRelationFromGivenEntity(entityOnTree1,true).stream() .<Pair<Field, NliEntity>>map(fieldEntity->new ImmutablePair<>(fieldEntity.getLeft(), entityInTree1ToEntityInTree2.get(fieldEntity.getRight()))) .collect(Collectors.toCollection(ArrayList::new)); if (actualPairsInTree2.size() != expectedPairsInTree2.size()) return false; for (int i=0; i<actualPairsInTree2.size(); i++){ if (!actualPairsInTree2.get(i).getLeft().equals(expectedPairsInTree2.get(i).getLeft())) return false; if (actualPairsInTree2.get(i).getRight() != expectedPairsInTree2.get(i).getRight()) return false; } } return true; } }
EntityGraphReflection { public static boolean entityGraphEquals(NliRootEntity root1, NliRootEntity root2) { IdentityHashMap<NliEntity, NliEntity> entityInTree1ToEntityInTree2 = new IdentityHashMap<>(); ArrayList<NliEntity> nonPrimitiveEntitiesInTree1 = new ArrayList<>( getNonPrimitiveEntitiesReachableFromRoot(root1, true, true)); ArrayList<NliEntity> nonPrimitiveEntitiesInTree2 = new ArrayList<>( getNonPrimitiveEntitiesReachableFromRoot(root2, true, true)); if (nonPrimitiveEntitiesInTree1.size() != nonPrimitiveEntitiesInTree2.size()) return false; for (int i=0; i<nonPrimitiveEntitiesInTree1.size(); i++) { entityInTree1ToEntityInTree2.put(nonPrimitiveEntitiesInTree1.get(i), nonPrimitiveEntitiesInTree2.get(i)); } for (NliEntity entityOnTree1 : nonPrimitiveEntitiesInTree1){ NliEntity entityOnTree2 = entityInTree1ToEntityInTree2.get(entityOnTree1); if (!getPrimitiveEntitiesInOutgoingRelationFromGivenEntity(entityOnTree1,true).equals( getPrimitiveEntitiesInOutgoingRelationFromGivenEntity(entityOnTree2,true))) { return false; } ArrayList<Pair<Field, NliEntity>> actualPairsInTree2 = new ArrayList<>(getNonPrimitiveEntitiesInOutgoingRelationFromGivenEntity(entityOnTree2,true)); ArrayList<Pair<Field, NliEntity>> expectedPairsInTree2 = getNonPrimitiveEntitiesInOutgoingRelationFromGivenEntity(entityOnTree1,true).stream() .<Pair<Field, NliEntity>>map(fieldEntity->new ImmutablePair<>(fieldEntity.getLeft(), entityInTree1ToEntityInTree2.get(fieldEntity.getRight()))) .collect(Collectors.toCollection(ArrayList::new)); if (actualPairsInTree2.size() != expectedPairsInTree2.size()) return false; for (int i=0; i<actualPairsInTree2.size(); i++){ if (!actualPairsInTree2.get(i).getLeft().equals(expectedPairsInTree2.get(i).getLeft())) return false; if (actualPairsInTree2.get(i).getRight() != expectedPairsInTree2.get(i).getRight()) return false; } } return true; }  }
EntityGraphReflection { public static boolean entityGraphEquals(NliRootEntity root1, NliRootEntity root2) { IdentityHashMap<NliEntity, NliEntity> entityInTree1ToEntityInTree2 = new IdentityHashMap<>(); ArrayList<NliEntity> nonPrimitiveEntitiesInTree1 = new ArrayList<>( getNonPrimitiveEntitiesReachableFromRoot(root1, true, true)); ArrayList<NliEntity> nonPrimitiveEntitiesInTree2 = new ArrayList<>( getNonPrimitiveEntitiesReachableFromRoot(root2, true, true)); if (nonPrimitiveEntitiesInTree1.size() != nonPrimitiveEntitiesInTree2.size()) return false; for (int i=0; i<nonPrimitiveEntitiesInTree1.size(); i++) { entityInTree1ToEntityInTree2.put(nonPrimitiveEntitiesInTree1.get(i), nonPrimitiveEntitiesInTree2.get(i)); } for (NliEntity entityOnTree1 : nonPrimitiveEntitiesInTree1){ NliEntity entityOnTree2 = entityInTree1ToEntityInTree2.get(entityOnTree1); if (!getPrimitiveEntitiesInOutgoingRelationFromGivenEntity(entityOnTree1,true).equals( getPrimitiveEntitiesInOutgoingRelationFromGivenEntity(entityOnTree2,true))) { return false; } ArrayList<Pair<Field, NliEntity>> actualPairsInTree2 = new ArrayList<>(getNonPrimitiveEntitiesInOutgoingRelationFromGivenEntity(entityOnTree2,true)); ArrayList<Pair<Field, NliEntity>> expectedPairsInTree2 = getNonPrimitiveEntitiesInOutgoingRelationFromGivenEntity(entityOnTree1,true).stream() .<Pair<Field, NliEntity>>map(fieldEntity->new ImmutablePair<>(fieldEntity.getLeft(), entityInTree1ToEntityInTree2.get(fieldEntity.getRight()))) .collect(Collectors.toCollection(ArrayList::new)); if (actualPairsInTree2.size() != expectedPairsInTree2.size()) return false; for (int i=0; i<actualPairsInTree2.size(); i++){ if (!actualPairsInTree2.get(i).getLeft().equals(expectedPairsInTree2.get(i).getLeft())) return false; if (actualPairsInTree2.get(i).getRight() != expectedPairsInTree2.get(i).getRight()) return false; } } return true; }  static List<Pair<Field, Entity>> getEntitiesInOutgoingRelationsFromGivenEntity(NliEntity fromEntity,
                                                                                          boolean deterministicOrder); static List<Entity> getEntitiesInOutgoingRelationFromGivenEntity(
            NliEntity fromEntity, boolean deterministicOrder, Field relation); static boolean isRelationField(Field field); static List<Field> getRelationFieldsOfNliEntityClass(Class<? extends NliEntity> clazz, boolean deterministic); static List<Pair<Field, PrimitiveEntity>> getPrimitiveEntitiesInOutgoingRelationFromGivenEntity(
            NliEntity fromEntity, boolean deterministicOrder); static List <Pair<Field, NliEntity>> getNonPrimitiveEntitiesInOutgoingRelationFromGivenEntity(
            NliEntity fromEntity, boolean deterministicOrder); static List<NliEntity> getNonPrimitiveEntitiesReachableFromRoot(
            NliRootEntity root, boolean includeRootEntity, boolean deterministicOrder); static Collection<Class<? extends NliEntity>> getPossiblyReachableNliEntityClasses(
            Class<? extends NliRootEntity> rootEntityClass, boolean includeRootInResult, boolean deterministic); static String entityGraphToHumanFriendlyString(NliRootEntity root, boolean deterministic); static boolean entityGraphEquals(NliRootEntity root1, NliRootEntity root2); }
EntityGraphReflection { public static boolean entityGraphEquals(NliRootEntity root1, NliRootEntity root2) { IdentityHashMap<NliEntity, NliEntity> entityInTree1ToEntityInTree2 = new IdentityHashMap<>(); ArrayList<NliEntity> nonPrimitiveEntitiesInTree1 = new ArrayList<>( getNonPrimitiveEntitiesReachableFromRoot(root1, true, true)); ArrayList<NliEntity> nonPrimitiveEntitiesInTree2 = new ArrayList<>( getNonPrimitiveEntitiesReachableFromRoot(root2, true, true)); if (nonPrimitiveEntitiesInTree1.size() != nonPrimitiveEntitiesInTree2.size()) return false; for (int i=0; i<nonPrimitiveEntitiesInTree1.size(); i++) { entityInTree1ToEntityInTree2.put(nonPrimitiveEntitiesInTree1.get(i), nonPrimitiveEntitiesInTree2.get(i)); } for (NliEntity entityOnTree1 : nonPrimitiveEntitiesInTree1){ NliEntity entityOnTree2 = entityInTree1ToEntityInTree2.get(entityOnTree1); if (!getPrimitiveEntitiesInOutgoingRelationFromGivenEntity(entityOnTree1,true).equals( getPrimitiveEntitiesInOutgoingRelationFromGivenEntity(entityOnTree2,true))) { return false; } ArrayList<Pair<Field, NliEntity>> actualPairsInTree2 = new ArrayList<>(getNonPrimitiveEntitiesInOutgoingRelationFromGivenEntity(entityOnTree2,true)); ArrayList<Pair<Field, NliEntity>> expectedPairsInTree2 = getNonPrimitiveEntitiesInOutgoingRelationFromGivenEntity(entityOnTree1,true).stream() .<Pair<Field, NliEntity>>map(fieldEntity->new ImmutablePair<>(fieldEntity.getLeft(), entityInTree1ToEntityInTree2.get(fieldEntity.getRight()))) .collect(Collectors.toCollection(ArrayList::new)); if (actualPairsInTree2.size() != expectedPairsInTree2.size()) return false; for (int i=0; i<actualPairsInTree2.size(); i++){ if (!actualPairsInTree2.get(i).getLeft().equals(expectedPairsInTree2.get(i).getLeft())) return false; if (actualPairsInTree2.get(i).getRight() != expectedPairsInTree2.get(i).getRight()) return false; } } return true; }  static List<Pair<Field, Entity>> getEntitiesInOutgoingRelationsFromGivenEntity(NliEntity fromEntity,
                                                                                          boolean deterministicOrder); static List<Entity> getEntitiesInOutgoingRelationFromGivenEntity(
            NliEntity fromEntity, boolean deterministicOrder, Field relation); static boolean isRelationField(Field field); static List<Field> getRelationFieldsOfNliEntityClass(Class<? extends NliEntity> clazz, boolean deterministic); static List<Pair<Field, PrimitiveEntity>> getPrimitiveEntitiesInOutgoingRelationFromGivenEntity(
            NliEntity fromEntity, boolean deterministicOrder); static List <Pair<Field, NliEntity>> getNonPrimitiveEntitiesInOutgoingRelationFromGivenEntity(
            NliEntity fromEntity, boolean deterministicOrder); static List<NliEntity> getNonPrimitiveEntitiesReachableFromRoot(
            NliRootEntity root, boolean includeRootEntity, boolean deterministicOrder); static Collection<Class<? extends NliEntity>> getPossiblyReachableNliEntityClasses(
            Class<? extends NliRootEntity> rootEntityClass, boolean includeRootInResult, boolean deterministic); static String entityGraphToHumanFriendlyString(NliRootEntity root, boolean deterministic); static boolean entityGraphEquals(NliRootEntity root1, NliRootEntity root2);  }