private static <T extends HierarchicalType> void addToResult(T type, List<T> result, Set<T> processed, Map<String, T> typesByName) { if (processed.contains(type)) { return; } processed.add(type); ImmutableSet<String> superTypeNames = type.superTypes; for (String superTypeName : superTypeNames) { // Recursively add any supertypes first to the result. T superType = typesByName.get(superTypeName); if (superType != null) { addToResult(superType, result, processed, typesByName); } } result.add(type); } }
private void orderAndConstructTypes() throws AtlasException { List<TraitType> traitTypes = new ArrayList<>(); for (String traitTypeName : traitNameToDefMap.keySet()) { traitTypes.add(getDataType(TraitType.class, traitTypeName)); } traitTypes = HierarchicalTypeDependencySorter.sortTypes(traitTypes); List<ClassType> classTypes = new ArrayList<>(); for (String classTypeName : classNameToDefMap.keySet()) { classTypes.add(getDataType(ClassType.class, classTypeName)); } classTypes = HierarchicalTypeDependencySorter.sortTypes(classTypes); for (StructTypeDefinition structDef : structDefs) { constructStructureType(structDef); } for (TraitType traitType : traitTypes) { constructHierarchicalType(TraitType.class, traitNameToDefMap.get(traitType.getName())); } for (ClassType classType : classTypes) { constructHierarchicalType(ClassType.class, classNameToDefMap.get(classType.getName())); } }
private void orderAndConstructTypes() throws AtlasException { List<TraitType> traitTypes = new ArrayList<>(); for (String traitTypeName : traitNameToDefMap.keySet()) { traitTypes.add(getDataType(TraitType.class, traitTypeName)); } traitTypes = HierarchicalTypeDependencySorter.sortTypes(traitTypes); List<ClassType> classTypes = new ArrayList<>(); for (String classTypeName : classNameToDefMap.keySet()) { classTypes.add(getDataType(ClassType.class, classTypeName)); } classTypes = HierarchicalTypeDependencySorter.sortTypes(classTypes); for (StructTypeDefinition structDef : structDefs) { constructStructureType(structDef); } for (TraitType traitType : traitTypes) { constructHierarchicalType(TraitType.class, traitNameToDefMap.get(traitType.getName())); } for (ClassType classType : classTypes) { constructHierarchicalType(ClassType.class, classNameToDefMap.get(classType.getName())); } }
@SuppressWarnings("rawtypes") @Test public void testSimpleModel() throws AtlasException { TypeSystem ts = TypeSystem.getInstance(); HierarchicalType a = new ClassType(ts, "a", null, ImmutableSet.<String>of(), 0); HierarchicalType b = new ClassType(ts, "B", null, ImmutableSet.of("a"), 0); HierarchicalType c = new ClassType(ts, "C", null, ImmutableSet.of("B"), 0); List<HierarchicalType> unsortedTypes = Arrays.asList(c, a, b); List<HierarchicalType> sortedTypes = HierarchicalTypeDependencySorter.sortTypes(unsortedTypes); Assert.assertEquals(sortedTypes.size(), 3); Assert.assertEquals(sortedTypes.indexOf(a), 0); Assert.assertEquals(sortedTypes.indexOf(b), 1); Assert.assertEquals(sortedTypes.indexOf(c), 2); }
private static <T extends HierarchicalType> void addToResult(T type, List<T> result, Set<T> processed, Map<String, T> typesByName) { if (processed.contains(type)) { return; } processed.add(type); ImmutableSet<String> superTypeNames = type.superTypes; for (String superTypeName : superTypeNames) { // Recursively add any supertypes first to the result. T superType = typesByName.get(superTypeName); if (superType != null) { addToResult(superType, result, processed, typesByName); } } result.add(type); } }
@SuppressWarnings("rawtypes") @Test public void testSimpleModel() throws AtlasException { TypeSystem ts = TypeSystem.getInstance(); HierarchicalType a = new ClassType(ts, "a", null, ImmutableSet.<String>of(), 0); HierarchicalType b = new ClassType(ts, "B", null, ImmutableSet.of("a"), 0); HierarchicalType c = new ClassType(ts, "C", null, ImmutableSet.of("B"), 0); List<HierarchicalType> unsortedTypes = Arrays.asList(c, a, b); List<HierarchicalType> sortedTypes = HierarchicalTypeDependencySorter.sortTypes(unsortedTypes); Assert.assertEquals(sortedTypes.size(), 3); Assert.assertEquals(sortedTypes.indexOf(a), 0); Assert.assertEquals(sortedTypes.indexOf(b), 1); Assert.assertEquals(sortedTypes.indexOf(c), 2); }
/** * Sorts the specified hierarchical types by supertype dependencies, * such that any type A which is a supertype of type B * will always be located earlier in the result list; that is, the supertype * A would be found at some index i and subtype B would be found at some index j, * and i < j. * * @param types hierarchical types to be sorted * @return hierarchical types sorted by supertype dependency */ public static <T extends HierarchicalType> List<T> sortTypes(List<T> types) { Map<String, T> typesByName = new HashMap<>(); for (T type : types) { typesByName.put(type.name, type); } List<T> result = new ArrayList<>(types.size()); Set<T> processed = new HashSet<>(); for (T type : types) { addToResult(type, result, processed, typesByName); } return result; }
@SuppressWarnings("rawtypes") @Test public void testLargerModel() throws Exception { TypeSystem ts = TypeSystem.getInstance(); HierarchicalType testObjectType = new ClassType(ts, "TestObject", null, ImmutableSet.<String>of(), 0); HierarchicalType testDataSetType = new ClassType(ts, "TestDataSet", null, ImmutableSet.of("TestObject"), 0); HierarchicalType testColumnType = new ClassType(ts, "TestColumn", null, ImmutableSet.of("TestObject"), 0); HierarchicalType testRelationalDataSetType = new ClassType(ts, "TestRelationalDataSet", null, ImmutableSet.of("TestDataSet"), 0); HierarchicalType testTableType = new ClassType(ts, "Table", null, ImmutableSet.of("TestDataSet"), 0); HierarchicalType testDataFileType = new ClassType(ts, "TestDataFile", null, ImmutableSet.of("TestRelationalDataSet"), 0); HierarchicalType testDocumentType = new ClassType(ts, "TestDocument", null, ImmutableSet.of("TestDataSet"), 0); HierarchicalType testAnnotationType = new ClassType(ts, "TestAnnotation", null, ImmutableSet.<String>of(), 0); HierarchicalType myNewAnnotationType = new ClassType(ts, "MyNewAnnotation", null, ImmutableSet.of("TestAnnotation"), 0); List<HierarchicalType> unsortedTypes = Arrays.asList(testTableType, testColumnType, myNewAnnotationType, testDataSetType, testDataFileType, testAnnotationType, testRelationalDataSetType, testObjectType, testDocumentType); List<HierarchicalType> sortedTypes = HierarchicalTypeDependencySorter.sortTypes(unsortedTypes); // Verify that super types were sorted before their subtypes. Assert.assertTrue(sortedTypes.indexOf(testObjectType) < sortedTypes.indexOf(testDataSetType)); Assert.assertTrue(sortedTypes.indexOf(testObjectType) < sortedTypes.indexOf(testColumnType)); Assert.assertTrue(sortedTypes.indexOf(testDataSetType) < sortedTypes.indexOf(testRelationalDataSetType)); Assert.assertTrue(sortedTypes.indexOf(testDataSetType) < sortedTypes.indexOf(testDocumentType)); Assert.assertTrue(sortedTypes.indexOf(testDataSetType) < sortedTypes.indexOf(testTableType)); Assert.assertTrue(sortedTypes.indexOf(testRelationalDataSetType) < sortedTypes.indexOf(testDataFileType)); Assert.assertTrue(sortedTypes.indexOf(testAnnotationType) < sortedTypes.indexOf(myNewAnnotationType)); } }
/** * Sorts the specified hierarchical types by supertype dependencies, * such that any type A which is a supertype of type B * will always be located earlier in the result list; that is, the supertype * A would be found at some index i and subtype B would be found at some index j, * and i < j. * * @param types hierarchical types to be sorted * @return hierarchical types sorted by supertype dependency */ public static <T extends HierarchicalType> List<T> sortTypes(List<T> types) { Map<String, T> typesByName = new HashMap<>(); for (T type : types) { typesByName.put(type.name, type); } List<T> result = new ArrayList<>(types.size()); Set<T> processed = new HashSet<>(); for (T type : types) { addToResult(type, result, processed, typesByName); } return result; }
@SuppressWarnings("rawtypes") @Test public void testLargerModel() throws Exception { TypeSystem ts = TypeSystem.getInstance(); HierarchicalType testObjectType = new ClassType(ts, "TestObject", null, ImmutableSet.<String>of(), 0); HierarchicalType testDataSetType = new ClassType(ts, "TestDataSet", null, ImmutableSet.of("TestObject"), 0); HierarchicalType testColumnType = new ClassType(ts, "TestColumn", null, ImmutableSet.of("TestObject"), 0); HierarchicalType testRelationalDataSetType = new ClassType(ts, "TestRelationalDataSet", null, ImmutableSet.of("TestDataSet"), 0); HierarchicalType testTableType = new ClassType(ts, "Table", null, ImmutableSet.of("TestDataSet"), 0); HierarchicalType testDataFileType = new ClassType(ts, "TestDataFile", null, ImmutableSet.of("TestRelationalDataSet"), 0); HierarchicalType testDocumentType = new ClassType(ts, "TestDocument", null, ImmutableSet.of("TestDataSet"), 0); HierarchicalType testAnnotationType = new ClassType(ts, "TestAnnotation", null, ImmutableSet.<String>of(), 0); HierarchicalType myNewAnnotationType = new ClassType(ts, "MyNewAnnotation", null, ImmutableSet.of("TestAnnotation"), 0); List<HierarchicalType> unsortedTypes = Arrays.asList(testTableType, testColumnType, myNewAnnotationType, testDataSetType, testDataFileType, testAnnotationType, testRelationalDataSetType, testObjectType, testDocumentType); List<HierarchicalType> sortedTypes = HierarchicalTypeDependencySorter.sortTypes(unsortedTypes); // Verify that super types were sorted before their subtypes. Assert.assertTrue(sortedTypes.indexOf(testObjectType) < sortedTypes.indexOf(testDataSetType)); Assert.assertTrue(sortedTypes.indexOf(testObjectType) < sortedTypes.indexOf(testColumnType)); Assert.assertTrue(sortedTypes.indexOf(testDataSetType) < sortedTypes.indexOf(testRelationalDataSetType)); Assert.assertTrue(sortedTypes.indexOf(testDataSetType) < sortedTypes.indexOf(testDocumentType)); Assert.assertTrue(sortedTypes.indexOf(testDataSetType) < sortedTypes.indexOf(testTableType)); Assert.assertTrue(sortedTypes.indexOf(testRelationalDataSetType) < sortedTypes.indexOf(testDataFileType)); Assert.assertTrue(sortedTypes.indexOf(testAnnotationType) < sortedTypes.indexOf(myNewAnnotationType)); } }