/** @see Alignment#getCells(EntityDefinition, boolean) */ @Override public Collection<? extends Cell> getCells( EntityDefinition entityDefinition, boolean includeInherited) { if (!includeInherited) return Collections.unmodifiableCollection(cellsPerEntity.get(entityDefinition)); else { // Set for safety to return each cell only once. // Duplicates shouldn't happen in usual cases, though. Collection<Cell> cells = new HashSet<Cell>(); EntityDefinition e = entityDefinition; do { cells.addAll(cellsPerEntity.get(e)); if (e.getFilter() != null) { cells.addAll( cellsPerEntity.get( AlignmentUtil.createEntity( e.getType(), e.getPropertyPath(), e.getSchemaSpace(), null))); } TypeDefinition superType = e.getType().getSuperType(); e = superType == null ? null : AlignmentUtil.createEntity( superType, e.getPropertyPath(), e.getSchemaSpace(), e.getFilter()); } while (e != null); return cells; } }
/** @see Alignment#getPropertyCells(Cell, boolean, boolean) */ @Override public Collection<? extends Cell> getPropertyCells( Cell typeCell, boolean includeDisabled, boolean ignoreEmptySource) { if (CellUtil.getFirstEntity(typeCell.getTarget()) == null) { if (CellUtil.getFirstEntity(typeCell.getSource()) == null) return Collections.emptySet(); else if (!(typeCell.getSource().values().iterator().next() instanceof Type)) throw new IllegalArgumentException("Given cell is not a type cell."); // query with sources only Collection<TypeEntityDefinition> sourceTypes = new ArrayList<TypeEntityDefinition>(); Iterator<? extends Entity> it = typeCell.getSource().values().iterator(); while (it.hasNext()) sourceTypes.add((TypeEntityDefinition) it.next().getDefinition()); List<Cell> result = new ArrayList<Cell>(); for (Cell cell : cells) if (!AlignmentUtil.isTypeCell(cell) && matchesSources(cell.getSource(), sourceTypes)) result.add(cell); return result; } if (!AlignmentUtil.isTypeCell(typeCell)) throw new IllegalArgumentException("Given cell is not a type cell."); List<Cell> result = new ArrayList<Cell>(); TypeDefinition typeCellType = typeCell.getTarget().values().iterator().next().getDefinition().getType(); // collect source entity definitions Collection<TypeEntityDefinition> sourceTypes = new ArrayList<TypeEntityDefinition>(); // null check in case only target type is in question if (typeCell.getSource() != null) { Iterator<? extends Entity> it = typeCell.getSource().values().iterator(); while (it.hasNext()) sourceTypes.add((TypeEntityDefinition) it.next().getDefinition()); } while (typeCellType != null) { // select all cells of the target type for (Cell cell : cellsPerTargetType.get(typeCellType)) { // check all cells associated to the target type if (!AlignmentUtil.isTypeCell(cell) && (includeDisabled || !cell.getDisabledFor().contains(typeCell.getId()))) { // cell is a property cell that isn't disabled // the target type matches, too if (AlignmentUtil.isAugmentation(cell) || (ignoreEmptySource && sourceTypes.isEmpty()) || matchesSources(cell.getSource(), sourceTypes)) { // cell matches on the source side, too result.add(cell); } } } // continue with super type for inheritance typeCellType = typeCellType.getSuperType(); } return result; }
/** @see Alignment#getTypeCells(Cell) */ @Override public Collection<? extends Cell> getTypeCells(Cell queryCell) { Set<TypeEntityDefinition> sources = new HashSet<TypeEntityDefinition>(); if (queryCell.getSource() != null) { Iterator<? extends Entity> it = queryCell.getSource().values().iterator(); while (it.hasNext()) sources.add(AlignmentUtil.getTypeEntity(it.next().getDefinition())); } Entity targetEntity = CellUtil.getFirstEntity(queryCell.getTarget()); TypeDefinition target = targetEntity == null ? null : targetEntity.getDefinition().getType(); if (sources.isEmpty() && target == null) return getTypeCells(); List<Cell> result = new ArrayList<Cell>(); for (Cell typeCell : typeCells) { TypeDefinition typeCellTarget = CellUtil.getFirstEntity(typeCell.getTarget()).getDefinition().getType(); if (target == null || DefinitionUtil.isSuperType(target, typeCellTarget)) { // target matches if (sources.isEmpty() || matchesSources(typeCell.getSource(), sources)) { // source matches, too result.add(typeCell); } } } return result; }
/** * Add a cell to the various internal containers. * * @param cell the cell to add */ private void internalAdd(Cell cell) { cells.add(cell); idToCell.put(cell.getId(), cell); // check if cell is a type cell if (AlignmentUtil.isTypeCell(cell)) { typeCells.add(cell); } // add to maps internalAddToMaps(cell.getSource(), cell); internalAddToMaps(cell.getTarget(), cell); }
/** * Determines if all of the given entities are associated to at least one of the given type entity * definitions. * * @param testCellSources the entities * @param typeCellTypes the type entity definitions * @return whether all entities are associated to at least one of the types */ private boolean matchesSources( ListMultimap<String, ? extends Entity> testCellSources, Iterable<TypeEntityDefinition> typeCellTypes) { if (testCellSources == null) return true; for (Entity entity : testCellSources.values()) { // if one of the property cell's sources is not part of the type // cell it should not be included // XXX this is only true for the transformation? if (!matchesSources(AlignmentUtil.getTypeEntity(entity.getDefinition()), typeCellTypes)) return false; } return true; }