use of eu.esdihumboldt.hale.common.align.model.BaseAlignmentCell in project hale by halestudio.
the class AlignmentView method menuAboutToShow.
/**
* @see eu.esdihumboldt.hale.ui.views.mapping.AbstractMappingView#menuAboutToShow(org.eclipse.jface.action.IMenuManager)
*/
@Override
protected void menuAboutToShow(IMenuManager manager) {
ISelection cellSelection = getViewer().getSelection();
final Cell typeCell = sourceTargetSelector.getSelectedCell();
// is a type relation selected
if (!sourceTargetSelector.isCellSelected())
return;
// is a cell selected?
if (!(cellSelection instanceof IStructuredSelection) || ((IStructuredSelection) cellSelection).size() != 1 || !(((IStructuredSelection) cellSelection).getFirstElement() instanceof Cell))
return;
final Cell selectedCell = (Cell) ((IStructuredSelection) cellSelection).getFirstElement();
// ignore type cell
if (AlignmentUtil.isTypeCell(selectedCell))
return;
// check current disable status
if (!selectedCell.getDisabledFor().contains(typeCell.getId())) {
manager.add(new Action("Disable") {
/**
* @see org.eclipse.jface.action.Action#run()
*/
@Override
public void run() {
AlignmentService as = PlatformUI.getWorkbench().getService(AlignmentService.class);
as.setCellProperty(selectedCell.getId(), Cell.PROPERTY_DISABLE_FOR, typeCell);
}
});
} else {
manager.add(new Action("Enable") {
/**
* @see org.eclipse.jface.action.Action#run()
*/
@Override
public void run() {
AlignmentService as = PlatformUI.getWorkbench().getService(AlignmentService.class);
as.setCellProperty(selectedCell.getId(), Cell.PROPERTY_ENABLE_FOR, typeCell);
}
/**
* @see org.eclipse.jface.action.Action#isEnabled()
*/
@Override
public boolean isEnabled() {
// Still show the action for clarity.
if (selectedCell instanceof BaseAlignmentCell)
return !((BaseAlignmentCell) selectedCell).getBaseDisabledFor().contains(typeCell.getId());
return true;
}
});
}
}
use of eu.esdihumboldt.hale.common.align.model.BaseAlignmentCell in project hale by halestudio.
the class AlignmentServiceImpl method addOrUpdateAlignment.
/**
* @see AlignmentService#addOrUpdateAlignment(MutableAlignment)
*/
@Override
public void addOrUpdateAlignment(MutableAlignment alignment) {
Collection<Cell> added = new ArrayList<Cell>();
/*
* XXX Updates for disables not supported
*
* One problem is, that base alignments shouldn't be added twice, so if
* the given alignment contains a base alignment that is also contained
* in the current alignment it won't be added.
*
* Now what about cells in the given alignment, that reference those
* base alignments (-> disabled for)? They need to be changed to the
* base alignment cells of the current alignment.
*
* Those cells can be obtained by replacing the prefix part of the cell
* and using getCell(String).
*
* XXX Addition of base alignments not supported
*
* Here the problem is, that it is not enough to gather the base
* alignment cells and change their prefix to a new one, since the base
* cell could be another base alignment cell, which would need to
* change, too. They would have to be added in order. An option would be
* to load them with the base alignment loading mechanism, which would
* result in a loss of additional disables.
*
* XXX Changes of conflicting base alignment cells not handled
*/
// if (!alignment.getBaseAlignments().isEmpty()) {
// _log.warn("Adding alignments currently does not support merging of base alignments. Import base alignments independently.");
// }
boolean addedBaseCells = false;
// add cells
synchronized (this) {
for (Cell cell : alignment.getCells()) {
if (cell instanceof MutableCell) {
this.alignment.addCell((MutableCell) cell);
added.add(cell);
} else if (!(cell instanceof BaseAlignmentCell)) {
throw new IllegalStateException("The given alignment contained a cell which is neither mutable nor from a base alignment.");
} else {
addedBaseCells = true;
}
}
}
synchronized (this) {
// XXX this needs merging, see above
boolean first = true;
for (Entry<String, URI> baseAlignment : alignment.getBaseAlignments().entrySet()) {
Collection<CustomPropertyFunction> baseFunctions;
if (first) {
// XXX hack - insert all base functions with the first base
// alignment
baseFunctions = alignment.getBasePropertyFunctions().values();
} else {
// base functions should only be added once
baseFunctions = Collections.<CustomPropertyFunction>emptyList();
}
this.alignment.addBaseAlignment(baseAlignment.getKey(), baseAlignment.getValue(), //
alignment.getBaseAlignmentCells(baseAlignment.getValue()), baseFunctions);
}
}
// add custom functions
boolean addedFunctions = false;
synchronized (this) {
for (CustomPropertyFunction cf : alignment.getCustomPropertyFunctions().values()) {
this.alignment.addCustomPropertyFunction(cf);
addedFunctions = true;
}
}
if (addedBaseCells || (!added.isEmpty() && addedFunctions)) {
// only emit one combined event (not to trigger multiple
// transformations)
notifyAlignmentChanged();
} else {
if (!added.isEmpty()) {
notifyCellsAdded(added);
}
if (addedFunctions) {
notifyCustomFunctionsChanged();
}
}
}
use of eu.esdihumboldt.hale.common.align.model.BaseAlignmentCell in project hale by halestudio.
the class DefaultAlignmentIOTest method testBaseAlignmentSaveLoad.
/**
* Tests base alignment add, save and load.
*
* @throws Exception if an error occurs
*/
@Test
public void testBaseAlignmentSaveLoad() throws Exception {
DefaultAlignment baseAlignment = new DefaultAlignment();
MutableAlignment alignment = new DefaultAlignment();
Schema schema = TestUtil.loadSchema(getClass().getResource("/testdata/simple/t1.xsd").toURI());
Iterator<? extends TypeDefinition> iter = schema.getMappingRelevantTypes().iterator();
TypeDefinition t = iter.next();
if (!t.getName().getLocalPart().equals("T1")) {
t = iter.next();
}
DefaultCell cell1 = new DefaultCell();
cell1.setTransformationIdentifier("trans1");
ListMultimap<String, Type> source = ArrayListMultimap.create();
source.put(null, new DefaultType(new TypeEntityDefinition(t, SchemaSpaceID.SOURCE, null)));
cell1.setSource(source);
ListMultimap<String, Type> target = ArrayListMultimap.create();
target.put(null, new DefaultType(new TypeEntityDefinition(t, SchemaSpaceID.TARGET, null)));
cell1.setTarget(target);
DefaultCell cell2 = new DefaultCell();
cell2.setTransformationIdentifier("trans2");
List<ChildContext> childContext = new ArrayList<ChildContext>();
PropertyDefinition child = DefinitionUtil.getChild(t, new QName("a1")).asProperty();
childContext.add(new ChildContext(child));
ListMultimap<String, Property> source2 = ArrayListMultimap.create();
source2.put(null, new DefaultProperty(new PropertyEntityDefinition(t, childContext, SchemaSpaceID.SOURCE, null)));
cell2.setSource(source2);
ListMultimap<String, Property> target2 = ArrayListMultimap.create();
target2.put(null, new DefaultProperty(new PropertyEntityDefinition(t, childContext, SchemaSpaceID.TARGET, null)));
cell2.setTarget(target2);
// add cell1 to base alignment
baseAlignment.addCell(cell1);
// save base alignment
File baseAlignmentFile = tmp.newFile("alignment_base.xml");
System.out.println(baseAlignmentFile.getAbsolutePath());
saveAlignment(baseAlignment, new BufferedOutputStream(new FileOutputStream(baseAlignmentFile)));
// add as base alignment to extended alignment
addBaseAlignment(alignment, baseAlignmentFile.toURI(), schema, schema);
assertEquals(1, alignment.getBaseAlignments().size());
String usedPrefix = alignment.getBaseAlignments().keySet().iterator().next();
assertEquals(1, alignment.getCells().size());
assertEquals(usedPrefix + ":" + cell1.getId(), alignment.getCells().iterator().next().getId());
// add cell2 to extended alignment
alignment.addCell(cell2);
assertEquals(2, alignment.getCells().size());
assertEquals(1, alignment.getPropertyCells(cell1).size());
// save extended alignment
File alignmentFile = tmp.newFile("alignment_extended.xml");
System.out.println(alignmentFile.getAbsolutePath());
saveAlignment(alignment, new BufferedOutputStream(new FileOutputStream(alignmentFile)));
// load extended
MutableAlignment alignment2 = loadAlignment(new FileInputStream(alignmentFile), schema, schema);
assertEquals(2, alignment2.getCells().size());
assertEquals(1, alignment2.getTypeCells().size());
Cell typeCell = alignment2.getTypeCells().iterator().next();
assertTrue(typeCell instanceof BaseAlignmentCell);
assertEquals(usedPrefix + ":" + cell1.getId(), typeCell.getId());
assertEquals(1, alignment2.getPropertyCells(typeCell).size());
assertFalse(alignment2.getPropertyCells(typeCell).iterator().next() instanceof BaseAlignmentCell);
}
use of eu.esdihumboldt.hale.common.align.model.BaseAlignmentCell in project hale by halestudio.
the class AbstractBaseAlignmentLoader method processBaseAlignments.
/**
* Creates and adds cells and modifiers of the base alignments to the main
* alignment.
*
* @param alignment the alignment to add base alignments to
* @param sourceTypes the source types to use for resolving definition
* references
* @param targetTypes the target types to use for resolving definition
* references
* @param prefixMapping gets filled with a mapping from local to global
* prefixes
* @param alignmentToInfo gets filled with a mapping from base alignment
* representations to prefixes and URIs
* @param reporter the I/O reporter to report any errors to, may be
* <code>null</code>
* @throws IOException if one of the base alignments does not have cell ids
*/
private void processBaseAlignments(MutableAlignment alignment, TypeIndex sourceTypes, TypeIndex targetTypes, Map<A, Map<String, String>> prefixMapping, Map<A, AlignmentInfo> alignmentToInfo, IOReporter reporter) throws IOException {
for (Entry<A, AlignmentInfo> base : alignmentToInfo.entrySet()) {
Collection<C> baseCells = getCells(base.getKey());
boolean hasIds = true;
for (C baseCell : baseCells) if (Strings.isNullOrEmpty(getCellId(baseCell))) {
hasIds = false;
break;
}
if (!hasIds) {
throw new IOException("At least one base alignment (" + base.getValue().uri.absoluteURI + ") has no cell ids. Please load and save it to generate them.");
}
}
for (Entry<A, AlignmentInfo> base : alignmentToInfo.entrySet()) {
if (alignment.getBaseAlignments().containsValue(base.getValue().uri.usedURI)) {
// base alignment already present
// can currently happen with base alignments included in base
// alignments
reporter.warn(new IOMessageImpl("Base alignment at " + base.getValue().uri.usedURI + " has already been added", null));
} else {
Collection<CustomPropertyFunction> baseFunctions = getPropertyFunctions(base.getKey(), sourceTypes, targetTypes);
Collection<C> baseCells = getCells(base.getKey());
Collection<BaseAlignmentCell> createdBaseCells = new ArrayList<BaseAlignmentCell>(baseCells.size());
List<MutableCell> createdCells = new ArrayList<>(baseCells.size());
for (C baseCell : baseCells) {
// add cells of base alignments
MutableCell cell = createCell(baseCell, sourceTypes, targetTypes, reporter);
if (cell != null) {
createdCells.add(cell);
}
}
// Migrate UnmigratedCells
migrateCells(createdCells, reporter);
for (MutableCell cell : createdCells) {
createdBaseCells.add(new BaseAlignmentCell(cell, base.getValue().uri.usedURI, base.getValue().prefix));
}
alignment.addBaseAlignment(base.getValue().prefix, base.getValue().uri.usedURI, createdBaseCells, baseFunctions);
}
}
// add modifiers of base alignments
for (Entry<A, AlignmentInfo> base : alignmentToInfo.entrySet()) applyModifiers(alignment, getModifiers(base.getKey()), prefixMapping.get(base.getKey()), base.getValue().prefix, true, reporter);
}
use of eu.esdihumboldt.hale.common.align.model.BaseAlignmentCell in project hale by halestudio.
the class DefaultAlignmentIOTest method testCellDisableSaveLoad.
/**
* Tests cell disable save and load.
*
* @throws Exception if an error occurs
*/
@Test
public void testCellDisableSaveLoad() throws Exception {
DefaultAlignment baseAlignment = new DefaultAlignment();
MutableAlignment alignment = new DefaultAlignment();
Schema schema = TestUtil.loadSchema(getClass().getResource("/testdata/simple/t1.xsd").toURI());
Iterator<? extends TypeDefinition> iter = schema.getMappingRelevantTypes().iterator();
TypeDefinition t = iter.next();
if (!t.getName().getLocalPart().equals("T1")) {
t = iter.next();
}
// generate base alignment
DefaultCell cell1 = new DefaultCell();
cell1.setTransformationIdentifier("trans1");
ListMultimap<String, Type> source = ArrayListMultimap.create();
source.put(null, new DefaultType(new TypeEntityDefinition(t, SchemaSpaceID.SOURCE, null)));
cell1.setSource(source);
ListMultimap<String, Type> target = ArrayListMultimap.create();
target.put(null, new DefaultType(new TypeEntityDefinition(t, SchemaSpaceID.TARGET, null)));
cell1.setTarget(target);
DefaultCell cell2 = new DefaultCell();
cell2.setTransformationIdentifier("trans2");
List<ChildContext> childContext2 = new ArrayList<ChildContext>();
PropertyDefinition child2 = DefinitionUtil.getChild(t, new QName("a1")).asProperty();
childContext2.add(new ChildContext(child2));
ListMultimap<String, Property> source2 = ArrayListMultimap.create();
source2.put(null, new DefaultProperty(new PropertyEntityDefinition(t, childContext2, SchemaSpaceID.SOURCE, null)));
cell2.setSource(source2);
ListMultimap<String, Property> target2 = ArrayListMultimap.create();
target2.put(null, new DefaultProperty(new PropertyEntityDefinition(t, childContext2, SchemaSpaceID.TARGET, null)));
cell2.setTarget(target2);
DefaultCell cell3 = new DefaultCell();
cell3.setTransformationIdentifier("trans3");
List<ChildContext> childContext3 = new ArrayList<ChildContext>();
PropertyDefinition child3 = DefinitionUtil.getChild(t, new QName("b1")).asProperty();
childContext3.add(new ChildContext(child3));
ListMultimap<String, Property> source3 = ArrayListMultimap.create();
source3.put(null, new DefaultProperty(new PropertyEntityDefinition(t, childContext3, SchemaSpaceID.SOURCE, null)));
cell3.setSource(source3);
ListMultimap<String, Property> target3 = ArrayListMultimap.create();
target3.put(null, new DefaultProperty(new PropertyEntityDefinition(t, childContext3, SchemaSpaceID.TARGET, null)));
cell3.setTarget(target3);
baseAlignment.addCell(cell1);
baseAlignment.addCell(cell2);
String baseDisableCellId = cell2.getId();
baseAlignment.addCell(cell3);
String extendedDisableCellId = cell3.getId();
assertEquals(3, baseAlignment.getCells().size());
Cell typeCell = baseAlignment.getTypeCells().iterator().next();
assertEquals(2, baseAlignment.getPropertyCells(typeCell).size());
// test disable, it should not be with the related property cells
cell2.setDisabledFor(cell1, true);
assertEquals(1, baseAlignment.getPropertyCells(typeCell).size());
assertTrue(cell2.getDisabledFor().contains(cell1.getId()));
cell2.setDisabledFor(cell1, false);
assertFalse(cell2.getDisabledFor().contains(cell1.getId()));
cell2.setDisabledFor(cell1, true);
assertEquals(1, baseAlignment.getPropertyCells(typeCell).size());
// save base alignment
File baseAlignmentFile = tmp.newFile("alignment_base.xml");
System.out.println(baseAlignmentFile.getAbsolutePath());
saveAlignment(baseAlignment, new BufferedOutputStream(new FileOutputStream(baseAlignmentFile)));
// load base alignment
MutableAlignment baseAlignment2 = loadAlignment(new FileInputStream(baseAlignmentFile), schema, schema);
typeCell = baseAlignment2.getTypeCells().iterator().next();
assertEquals(3, baseAlignment2.getCells().size());
// test again that it is still disabled
assertEquals(1, baseAlignment2.getPropertyCells(typeCell).size());
// disable the remaining enabled cell in extended alignment
addBaseAlignment(alignment, baseAlignmentFile.toURI(), schema, schema);
assertEquals(1, alignment.getBaseAlignments().size());
String usedPrefix = alignment.getBaseAlignments().keySet().iterator().next();
File alignmentFile = tmp.newFile("alignment_extended.xml");
// check cells
typeCell = alignment.getTypeCells().iterator().next();
assertEquals(3, alignment.getCells().size());
assertEquals(1, alignment.getPropertyCells(typeCell).size());
// disable remaining cell
((ModifiableCell) alignment.getPropertyCells(typeCell, false, false).iterator().next()).setDisabledFor(typeCell, true);
assertEquals(0, alignment.getPropertyCells(typeCell).size());
// save / load extended alignment
System.out.println(alignmentFile.getAbsolutePath());
saveAlignment(alignment, new BufferedOutputStream(new FileOutputStream(alignmentFile)));
// load extended
MutableAlignment alignment2 = loadAlignment(new FileInputStream(alignmentFile), schema, schema);
typeCell = alignment2.getTypeCells().iterator().next();
// test disabled again
assertEquals(3, alignment2.getCells().size());
// test again that it is still disabled
assertEquals(0, alignment2.getPropertyCells(typeCell).size());
// more specifically test whether the disables come from base alignment
// or extended alignment
Cell baseDisableCell = alignment2.getCell(usedPrefix + ":" + baseDisableCellId);
Cell extendedDisableCell = alignment2.getCell(usedPrefix + ":" + extendedDisableCellId);
assertTrue(baseDisableCell instanceof BaseAlignmentCell);
assertEquals(1, baseDisableCell.getDisabledFor().size());
assertEquals(1, ((BaseAlignmentCell) baseDisableCell).getBaseDisabledFor().size());
assertEquals(0, ((BaseAlignmentCell) baseDisableCell).getAdditionalDisabledFor().size());
assertTrue(extendedDisableCell instanceof BaseAlignmentCell);
assertEquals(1, extendedDisableCell.getDisabledFor().size());
assertEquals(0, ((BaseAlignmentCell) extendedDisableCell).getBaseDisabledFor().size());
assertEquals(1, ((BaseAlignmentCell) extendedDisableCell).getAdditionalDisabledFor().size());
}
Aggregations