use of eu.esdihumboldt.hale.common.align.model.impl.DefaultProperty in project hale by halestudio.
the class DefaultsVisitor method addDefaultCell.
/**
* Add a cell assigning a default value to the given entity.
*
* @param ped the property entity definition
* @param value the value to assign or <code>null</code> if it should be
* auto-detected
*/
private void addDefaultCell(PropertyEntityDefinition ped, String value) {
String note;
// determine value to assign
if (value == null) {
value = determineDefaultValue(ped.getDefinition().getPropertyType());
note = "Generated default value based on property type.";
} else {
note = "Generated cell with specified default value.";
}
if (value == null) {
return;
}
// create cell template
MutableCell cell = new DefaultCell();
cell.setPriority(Priority.LOWEST);
ListMultimap<String, Entity> target = ArrayListMultimap.create();
cell.setTarget(target);
ListMultimap<String, ParameterValue> parameters = ArrayListMultimap.create();
cell.setTransformationParameters(parameters);
// set transformation identifier (Assign)
cell.setTransformationIdentifier(AssignFunction.ID);
// set cell target (Property)
target.put(null, new DefaultProperty(ped));
// set cell parameters (Value)
parameters.put(AssignFunction.PARAMETER_VALUE, new ParameterValue(value));
BGISAppUtil.appendNote(cell, note);
cells.add(cell);
}
use of eu.esdihumboldt.hale.common.align.model.impl.DefaultProperty in project hale by halestudio.
the class DefaultCellMigrator method updateCell.
@Override
public MutableCell updateCell(final Cell originalCell, final AlignmentMigration migration, final MigrationOptions options, SimpleLog log) {
MutableCell result = new DefaultCell(originalCell);
SimpleLog cellLog = SimpleLog.all(log, new CellLog(result, CELL_LOG_CATEGORY));
final AtomicBoolean replacedEntities = new AtomicBoolean(false);
EntryTransformer<String, Entity, Entity> entityTransformer = new EntryTransformer<String, Entity, Entity>() {
@Override
public Entity transformEntry(String key, Entity value) {
EntityDefinition org = value.getDefinition();
Optional<EntityDefinition> replace = migration.entityReplacement(org, cellLog);
EntityDefinition entity = replace.orElse(org);
if (!Objects.equal(entity, org)) {
replacedEntities.set(true);
}
if (entity instanceof PropertyEntityDefinition) {
return new DefaultProperty((PropertyEntityDefinition) entity);
} else if (entity instanceof TypeEntityDefinition) {
return new DefaultType((TypeEntityDefinition) entity);
} else {
throw new IllegalStateException("Invalid entity definition for creating entity");
}
}
};
// update source entities
if (options.updateSource() && result.getSource() != null && !result.getSource().isEmpty()) {
result.setSource(ArrayListMultimap.create(Multimaps.transformEntries(result.getSource(), entityTransformer)));
}
// update target entities
if (options.updateTarget() && result.getTarget() != null && !result.getTarget().isEmpty()) {
result.setTarget(ArrayListMultimap.create(Multimaps.transformEntries(result.getTarget(), entityTransformer)));
}
// TODO anything else?
postUpdateCell(result, options, replacedEntities.get(), cellLog);
return result;
}
use of eu.esdihumboldt.hale.common.align.model.impl.DefaultProperty 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());
}
use of eu.esdihumboldt.hale.common.align.model.impl.DefaultProperty in project hale by halestudio.
the class PropertyEntitySelector method createFilters.
private static ViewerFilter[] createFilters(PropertyParameterDefinition field) {
// if no condition is present add a filter that allows all properties
ViewerFilter propertyFilter = new ViewerFilter() {
@Override
public boolean select(Viewer viewer, Object parentElement, Object element) {
return element instanceof PropertyEntityDefinition;
}
};
if (field == null) {
return new ViewerFilter[] { propertyFilter };
}
List<PropertyCondition> conditions = field.getConditions();
if (conditions == null || conditions.isEmpty())
return new ViewerFilter[] { propertyFilter };
ViewerFilter[] filters = new ViewerFilter[conditions.size()];
int i = 0;
for (final PropertyCondition condition : conditions) {
filters[i] = new ViewerFilter() {
@Override
public boolean select(Viewer viewer, Object parentElement, Object element) {
if (element instanceof PropertyEntityDefinition) {
Property property = new DefaultProperty((PropertyEntityDefinition) element);
return condition.accept(property);
} else
return false;
}
};
}
return filters;
}
use of eu.esdihumboldt.hale.common.align.model.impl.DefaultProperty in project hale by halestudio.
the class EntityDefinitionServiceImpl method replace.
/**
* Creates a new ListMultimap with all occurrences of originalDef replaced
* by newDef. newDef must be a sibling of originalDef.
*
* @param entities the original list
* @param originalDef the entity definition to be replaced
* @param newDef the entity definition to use
* @return a new list
*/
private ListMultimap<String, ? extends Entity> replace(ListMultimap<String, ? extends Entity> entities, EntityDefinition originalDef, EntityDefinition newDef) {
ListMultimap<String, Entity> newList = ArrayListMultimap.create();
for (Entry<String, ? extends Entity> entry : entities.entries()) {
EntityDefinition entryDef = entry.getValue().getDefinition();
Entity newEntry;
if (AlignmentUtil.isParent(originalDef, entryDef)) {
if (entry.getValue() instanceof Type) {
// entry is a Type, so the changed Definition must be a
// Type, too.
newEntry = new DefaultType((TypeEntityDefinition) newDef);
} else if (entry.getValue() instanceof Property) {
// entry is a Property, check changed Definition.
if (originalDef.getPropertyPath().isEmpty()) {
// Type changed.
newEntry = new DefaultProperty(new PropertyEntityDefinition(newDef.getType(), entryDef.getPropertyPath(), entryDef.getSchemaSpace(), newDef.getFilter()));
} else {
// Some element of the property path changed.
List<ChildContext> newPath = new ArrayList<ChildContext>(entryDef.getPropertyPath());
int lastIndexOfChangedDef = newDef.getPropertyPath().size() - 1;
newPath.set(lastIndexOfChangedDef, newDef.getPropertyPath().get(lastIndexOfChangedDef));
newEntry = new DefaultProperty(new PropertyEntityDefinition(entryDef.getType(), newPath, entryDef.getSchemaSpace(), entryDef.getFilter()));
}
} else {
throw new IllegalStateException("Entity is neither a Type nor a Property.");
}
} else {
newEntry = entry.getValue();
}
newList.put(entry.getKey(), newEntry);
}
return newList;
}
Aggregations