use of eu.esdihumboldt.hale.common.align.model.ChildContext 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.align.model.ChildContext in project hale by halestudio.
the class EntityAccessorUtil method createEntity.
/**
* Create an entity definition from a path.
*
* @param path the path, the topmost element has to represent an
* {@link EntityDefinition}, all other elements must represent
* {@link ChildContext}s
* @return the created entity definition or <code>null</code> if the path
* was <code>null</code>
*/
public static EntityDefinition createEntity(Path<PathElement> path) {
if (path == null) {
return null;
}
List<PathElement> elements = new ArrayList<>(path.getElements());
// create entity definition
PathElement top = elements.remove(0);
if (top.getRoot() == null) {
throw new IllegalArgumentException("Topmost path element must be an entity definition");
}
EntityDefinition entity = top.getRoot();
// collect type information
TypeDefinition type = entity.getType();
SchemaSpaceID schemaSpace = entity.getSchemaSpace();
Filter filter = entity.getFilter();
List<ChildContext> contextPath = new ArrayList<>(entity.getPropertyPath());
for (PathElement element : elements) {
ChildContext cc = element.getChild();
if (cc == null) {
throw new IllegalArgumentException("All child elements must be defined by a child context");
}
contextPath.add(cc);
}
return AlignmentUtil.createEntity(type, contextPath, schemaSpace, filter);
}
use of eu.esdihumboldt.hale.common.align.model.ChildContext 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.ChildContext in project hale by halestudio.
the class EntityDefinitionComparator method compare.
@Override
public int compare(EntityDefinition o1, EntityDefinition o2) {
// first, compare type
int compare = compareType(o1, o2);
if (compare != 0) {
return compare;
}
Iterator<ChildContext> o1iterator = o1.getPropertyPath().iterator();
Iterator<ChildContext> o2iterator = o2.getPropertyPath().iterator();
while (o1iterator.hasNext() && o2iterator.hasNext()) {
ChildContext o1c = null;
PropertyDefinition o1def = null;
// get first property definition (skip groups)
while (o1def == null && o1iterator.hasNext()) {
o1c = o1iterator.next();
o1def = o1c.getChild().asProperty();
}
ChildContext o2c = null;
PropertyDefinition o2def = null;
// get first property definition (skip groups)
while (o2def == null && o2iterator.hasNext()) {
o2c = o2iterator.next();
o2def = o2c.getChild().asProperty();
}
if (o1def != null && o2def != null) {
// compare local name
int comp = o1def.getName().getLocalPart().compareTo(o2def.getName().getLocalPart());
if (comp == 0) {
// compare namespace
comp = o1def.getName().getNamespaceURI().compareTo(o2def.getName().getNamespaceURI());
}
if (comp == 0) {
// compare context
comp = compareContext(o1c, o2c);
}
if (comp != 0) {
// properties are different
return comp;
}
// properties are the same, continue
} else if (o1def != null && o2def == null) {
// o2 after o1
return -1;
} else if (o1def == null && o2def != null) {
// o1 after o2
return 1;
}
}
// shorter paths first
if (o1iterator.hasNext() && !o2iterator.hasNext()) {
// o1 after o2
return 1;
} else if (!o1iterator.hasNext() && o2iterator.hasNext()) {
// o2 after o1
return -1;
}
return 0;
}
use of eu.esdihumboldt.hale.common.align.model.ChildContext in project hale by halestudio.
the class PropertyFunctionScriptPage method createDummyEntity.
private EntityDefinition createDummyEntity(DefaultCustomPropertyFunctionEntity entity, SchemaSpaceID ssid) {
DefaultTypeDefinition parent = new DefaultTypeDefinition(new QName("dummy"));
DefaultPropertyDefinition property = new DefaultPropertyDefinition(new QName(entity.getName()), parent, createDummyType(entity));
property.setConstraint(Cardinality.get(entity.getMinOccurrence(), entity.getMaxOccurrence()));
return new PropertyEntityDefinition(parent, Collections.singletonList(new ChildContext(property)), ssid, null);
}
Aggregations