use of eu.esdihumboldt.hale.common.schema.model.TypeDefinition in project hale by halestudio.
the class CSVSchemaReaderTest method testRead3.
/**
* Test for given property names and property types with point as a decimal
* divisor
*
* @throws Exception the Exception thrown if the test fails
*/
@Test
public void testRead3() throws Exception {
String props = "A,B,C,D,E";
CSVSchemaReader schemaReader = new CSVSchemaReader();
schemaReader.setSource(new DefaultInputSupplier(getClass().getResource("/data/test3-pointdecimal.csv").toURI()));
schemaReader.setParameter(CommonSchemaConstants.PARAM_TYPENAME, Value.of("TestTyp"));
schemaReader.setParameter(CSVSchemaReader.PARAM_PROPERTY, Value.of(props));
schemaReader.setParameter(CSVSchemaReader.PARAM_PROPERTYTYPE, Value.of("java.lang.Integer,java.lang.String,java.lang.Float,java.lang.Float,java.lang.String"));
schemaReader.setParameter(CSVSchemaReader.PARAM_SEPARATOR, Value.of(";"));
schemaReader.setParameter(CSVSchemaReader.PARAM_QUOTE, null);
schemaReader.setParameter(CSVSchemaReader.PARAM_ESCAPE, null);
schemaReader.setParameter(CSVSchemaReader.PARAM_DECIMAL, Value.of("."));
IOReport report = schemaReader.execute(new LogProgressIndicator());
assertTrue(report.isSuccess());
Schema schema = schemaReader.getSchema();
assertEquals(1, schema.getMappingRelevantTypes().size());
TypeDefinition type = schema.getMappingRelevantTypes().iterator().next();
assertTrue(type.getName().getLocalPart().equals("TestTyp"));
Iterator<? extends ChildDefinition<?>> it = type.getChildren().iterator();
while (it.hasNext()) {
assertTrue(props.contains(it.next().getName().getLocalPart()));
}
}
use of eu.esdihumboldt.hale.common.schema.model.TypeDefinition in project hale by halestudio.
the class CSVSchemaReaderTest method testRead.
/**
* Test for given property names and property types
*
* @throws Exception the Exception thrown if the test fails
*/
@Test
public void testRead() throws Exception {
String props = "muh,kuh,bla,blub";
CSVSchemaReader schemaReader = new CSVSchemaReader();
schemaReader.setSource(new DefaultInputSupplier(getClass().getResource("/data/test1.csv").toURI()));
schemaReader.setParameter(CommonSchemaConstants.PARAM_TYPENAME, Value.of("TestTyp"));
schemaReader.setParameter(CSVSchemaReader.PARAM_PROPERTY, Value.of(props));
schemaReader.setParameter(CSVSchemaReader.PARAM_PROPERTYTYPE, Value.of("java.lang.String,java.lang.String,java.lang.String,java.lang.String"));
schemaReader.setParameter(CSVSchemaReader.PARAM_SEPARATOR, null);
schemaReader.setParameter(CSVSchemaReader.PARAM_QUOTE, null);
schemaReader.setParameter(CSVSchemaReader.PARAM_ESCAPE, null);
IOReport report = schemaReader.execute(new LogProgressIndicator());
assertTrue(report.isSuccess());
Schema schema = schemaReader.getSchema();
assertEquals(1, schema.getMappingRelevantTypes().size());
TypeDefinition type = schema.getMappingRelevantTypes().iterator().next();
assertTrue(type.getName().getLocalPart().equals("TestTyp"));
Iterator<? extends ChildDefinition<?>> it = type.getChildren().iterator();
while (it.hasNext()) {
assertTrue(props.contains(it.next().getName().getLocalPart()));
}
}
use of eu.esdihumboldt.hale.common.schema.model.TypeDefinition in project hale by halestudio.
the class PropertyBean method createEntityDefinition.
/**
* @see EntityBean#createEntityDefinition(TypeIndex, SchemaSpaceID)
*/
@Override
protected PropertyEntityDefinition createEntityDefinition(TypeIndex index, SchemaSpaceID schemaSpace) {
TypeDefinition typeDef = index.getType(getTypeName());
if (typeDef == null) {
throw new IllegalStateException(MessageFormat.format("TypeDefinition for type {0} not found", getTypeName()));
}
List<ChildContext> path = new ArrayList<ChildContext>();
DefinitionGroup parent = typeDef;
for (ChildContextBean childContext : properties) {
if (parent == null) {
throw new IllegalStateException("Could not resolve property entity definition: child not present");
}
Pair<ChildDefinition<?>, List<ChildDefinition<?>>> childs = findChild(parent, childContext.getChildName());
ChildDefinition<?> child = childs.getFirst();
// if the child is still null throw an exception
if (child == null) {
throw new IllegalStateException("Could not resolve property entity definition: child not found");
}
if (childs.getSecond() != null) {
for (ChildDefinition<?> pathElems : childs.getSecond()) {
path.add(new ChildContext(childContext.getContextName(), childContext.getContextIndex(), createCondition(childContext.getConditionFilter()), pathElems));
}
}
path.add(new ChildContext(childContext.getContextName(), childContext.getContextIndex(), createCondition(childContext.getConditionFilter()), child));
if (child instanceof DefinitionGroup) {
parent = (DefinitionGroup) child;
} else if (child.asProperty() != null) {
parent = child.asProperty().getPropertyType();
} else {
parent = null;
}
}
return new PropertyEntityDefinition(typeDef, path, schemaSpace, FilterDefinitionManager.getInstance().parse(getFilter()));
}
use of eu.esdihumboldt.hale.common.schema.model.TypeDefinition in project hale by halestudio.
the class AlignmentUtil method reparentCell.
/**
* Returns a cell like the given property cell with all source and target
* types matching those off the given type cell.<br>
* If the types already match they are unchanged. If the types are sub types
* of the types of the type cell they are changed. If no change is necessary
* the cell itself is returned.
*
* @param propertyCell the property cell to update
* @param typeCell the type cell with the target types
* @param strict If false and the target type cell has no sources or target,
* the property cell is updated to have the sources/target in
* their declaring type. If true, said properties are left
* unchanged. Does not matter for complete type cells, since they
* have sources and a target.
* @return the updated cell or <code>null</code> if an update isn't possible
*/
public static Cell reparentCell(Cell propertyCell, Cell typeCell, boolean strict) {
ListMultimap<String, Entity> sources = ArrayListMultimap.create();
ListMultimap<String, Entity> targets = ArrayListMultimap.create();
boolean updateNecessary = false;
// XXX are updates to the property path needed?
// Currently not, since ChildDefinitions are compared by their names
// only.
// TARGETS
Entity targetEntity = CellUtil.getFirstEntity(typeCell.getTarget());
if (targetEntity != null) {
TypeDefinition typeCellTargetType = ((Type) targetEntity).getDefinition().getDefinition();
for (Entry<String, ? extends Entity> target : propertyCell.getTarget().entries()) {
TypeDefinition propertyCellTargetType = target.getValue().getDefinition().getType();
if (propertyCellTargetType.equals(typeCellTargetType))
targets.put(target.getKey(), target.getValue());
else if (DefinitionUtil.isSuperType(typeCellTargetType, propertyCellTargetType)) {
PropertyEntityDefinition oldDef = (PropertyEntityDefinition) target.getValue().getDefinition();
targets.put(target.getKey(), new DefaultProperty(new PropertyEntityDefinition(typeCellTargetType, oldDef.getPropertyPath(), SchemaSpaceID.TARGET, null)));
updateNecessary = true;
} else {
// a cell with targets in more than one type
return null;
}
}
} else if (!strict)
updateNecessary |= reparentToDeclaring(propertyCell.getTarget(), targets);
else
targets.putAll(propertyCell.getTarget());
if (propertyCell.getSource() != null && !propertyCell.getSource().isEmpty()) {
if (typeCell.getSource() != null && !typeCell.getSource().isEmpty()) {
// collect source entity definitions
Collection<TypeEntityDefinition> typeCellSourceTypes = new ArrayList<TypeEntityDefinition>();
for (Entity entity : typeCell.getSource().values()) typeCellSourceTypes.add((TypeEntityDefinition) entity.getDefinition());
for (Entry<String, ? extends Entity> source : propertyCell.getSource().entries()) {
TypeEntityDefinition propertyCellSourceType = getTypeEntity(source.getValue().getDefinition());
if (typeCellSourceTypes.contains(propertyCellSourceType))
sources.put(source.getKey(), source.getValue());
else {
boolean matchFound = false;
// maybe the whole cell should be duplicated?
for (TypeEntityDefinition typeCellSourceType : typeCellSourceTypes) {
if (DefinitionUtil.isSuperType(typeCellSourceType.getDefinition(), propertyCellSourceType.getDefinition()) && (propertyCellSourceType.getFilter() == null || propertyCellSourceType.getFilter().equals(typeCellSourceType.getFilter()))) {
if (matchFound)
log.warn("Inherited property cell source matches multiple sources of type cell.");
matchFound = true;
PropertyEntityDefinition oldDef = (PropertyEntityDefinition) source.getValue().getDefinition();
sources.put(source.getKey(), new DefaultProperty(new PropertyEntityDefinition(typeCellSourceType.getDefinition(), oldDef.getPropertyPath(), SchemaSpaceID.SOURCE, typeCellSourceType.getFilter())));
updateNecessary = true;
// XXX break; if only one match should be added
}
}
if (!matchFound) {
// cell
return null;
}
}
}
} else if (!strict)
updateNecessary |= reparentToDeclaring(propertyCell.getSource(), sources);
else
targets.putAll(propertyCell.getTarget());
}
if (updateNecessary) {
MutableCell copy = new DefaultCell(propertyCell);
copy.setSource(sources);
copy.setTarget(targets);
propertyCell = copy;
}
return propertyCell;
}
use of eu.esdihumboldt.hale.common.schema.model.TypeDefinition in project hale by halestudio.
the class AlignmentUtil method reparentToDeclaring.
/**
* Copies the properties from original to modified, modifying properties to
* their declaring type, if the type has no filter set.
*
* @param original the original Multimap of properties
* @param modified the target Multimap
* @return true, if a property was changed, false otherwise
*/
private static boolean reparentToDeclaring(ListMultimap<String, ? extends Entity> original, ListMultimap<String, Entity> modified) {
boolean changed = false;
for (Entry<String, ? extends Entity> oEntity : original.entries()) {
PropertyEntityDefinition property = (PropertyEntityDefinition) oEntity.getValue().getDefinition();
ChildDefinition<?> childDef = property.getPropertyPath().get(0).getChild();
if (Objects.equals(childDef.getDeclaringGroup(), childDef.getParentType()) || property.getFilter() != null)
modified.put(oEntity.getKey(), oEntity.getValue());
else if (childDef.getDeclaringGroup() instanceof TypeDefinition) {
modified.put(oEntity.getKey(), new DefaultProperty(new PropertyEntityDefinition((TypeDefinition) childDef.getDeclaringGroup(), property.getPropertyPath(), property.getSchemaSpace(), null)));
changed = true;
} else {
// declaring group of first level property no type
// definition?
// simply add it without change, shouldn't happen
modified.put(oEntity.getKey(), oEntity.getValue());
}
}
return changed;
}
Aggregations