use of eu.esdihumboldt.hale.common.instance.model.Instance in project hale by halestudio.
the class TreePropertyTransformer method publish.
/**
* @see PropertyTransformer#publish(FamilyInstance, MutableInstance,
* TransformationLog, Cell)
*/
@Override
public void publish(final FamilyInstance source, final MutableInstance target, final TransformationLog typeLog, final Cell typeCell) {
instanceCounter.adjustOrPutValue(typeCell, 1, 1);
// increase output type counter
reporter.stats().at("createdPerType").at(target.getDefinition().getName().toString()).next();
Runnable job = new Runnable() {
@Override
public void run() {
try {
SimpleLogContext.withLog(typeLog, () -> {
// Add the meta data ID of the source as SourceID to the
// target
Collection<Instance> sources = InstanceUtil.getInstanceOutOfFamily(source);
Set<Object> ids = new HashSet<Object>();
for (Instance inst : sources) {
// Merge instances may have multiple IDs
List<Object> sourceIDs = inst.getMetaData(InstanceMetadata.METADATA_ID);
if (sourceIDs != null) {
ids.addAll(sourceIDs);
}
}
InstanceMetadata.setSourceID(target, ids.toArray());
// identify transformations to be executed on given
// instances
// create/get a transformation tree
TransformationTree tree = treePool.getTree(typeCell);
// State: base tree
HooksUtil.executeTreeHooks(treeHooks, TreeState.MINIMAL, tree, target);
// apply instance value to transformation tree
InstanceVisitor instanceVisitor = new InstanceVisitor(source, tree, typeLog);
tree.accept(instanceVisitor);
// State: basic source populated tree
// duplicate subtree as necessary
DuplicationVisitor duplicationVisitor = new DuplicationVisitor(tree, typeLog);
tree.accept(duplicationVisitor);
duplicationVisitor.doAugmentationTrackback();
// State: source populated tree (duplication complete)
HooksUtil.executeTreeHooks(treeHooks, TreeState.SOURCE_POPULATED, tree, target);
// apply functions
for (FunctionExecutor functionExecutor : executors) {
functionExecutor.setTypeCell(typeCell);
tree.accept(functionExecutor);
}
// State: full tree (target populated)
HooksUtil.executeTreeHooks(treeHooks, TreeState.FULL, tree, target);
// fill instance
builder.populate(target, tree, typeLog);
// generate the rest of the metadatas
metaworkerthread.get().generate(target);
// XXX ok to add to sink in any thread?!
// XXX addInstance and close were made synchronized in
// OrientInstanceSink
// XXX instead collect instances and write them in only
// one
// thread?
// after property transformations, publish target
// instance
sink.addInstance(target);
// and release the tree for further use
treePool.releaseTree(tree);
});
} catch (Throwable e) {
/*
* Catch any error, as exceptions in the executor service
* will only result in a message on the console.
*/
typeLog.error(typeLog.createMessage("Error performing property transformations", e));
}
}
};
if (executorService != null) {
executorService.execute(job);
} else {
job.run();
}
}
use of eu.esdihumboldt.hale.common.instance.model.Instance in project hale by halestudio.
the class FullInstanceIteratorSupport method doNext.
private Instance doNext() {
proceedToNext();
if (super.supportsTypePeek()) {
/*
* Type peek supported - next is equivalent to decoratee
*/
return super.next();
} else {
/*
* No type peek supported - next is cached next instance
*/
Instance next = peekInstance;
peekInstance = null;
return next;
}
}
use of eu.esdihumboldt.hale.common.instance.model.Instance in project hale by halestudio.
the class InstanceValidator method validateGroupChildren.
/**
* Validates the given {@link Group}'s children against the {@link Group}'s
* definition.
*
* @param group the group to validate
* @param reporter the reporter to report to
* @param type the top level type
* @param path the current property path
* @param onlyCheckExistingChildren whether to only validate existing
* children (in case of a choice) or not
* @param reference the instance reference
* @param context the instance validation context
* @param presentIn the child definition this group is present in, if
* applicable
* @param groupEntity the group's entity definition or <code>null</code>
*/
private void validateGroupChildren(Group group, InstanceValidationReporter reporter, QName type, List<QName> path, boolean onlyCheckExistingChildren, InstanceReference reference, InstanceValidationContext context, @Nullable ChildDefinition<?> presentIn, EntityDefinition groupEntity) {
Collection<? extends ChildDefinition<?>> childDefs = DefinitionUtil.getAllChildren(group.getDefinition());
// check only existing children (=attributes)
if (group instanceof Instance && presentIn != null && presentIn.asProperty() != null) {
Instance instance = (Instance) group;
if (presentIn.asProperty().getConstraint(NillableFlag.class).isEnabled() && instance.getValue() == null) {
// test if all properties present are attributes
boolean onlyAttributes = true;
// but there must be an attribute present (otherwise we are not
// sure this is XML)
boolean foundAttribute = false;
for (QName propertyName : group.getPropertyNames()) {
ChildDefinition<?> childDef = presentIn.asProperty().getPropertyType().getChild(propertyName);
if (childDef == null || childDef.asProperty() == null || !childDef.asProperty().getConstraint(XmlAttributeFlag.class).isEnabled()) {
onlyAttributes = false;
break;
} else {
foundAttribute = true;
}
}
if (onlyAttributes && foundAttribute) {
onlyCheckExistingChildren = true;
}
}
}
validateGroupChildren(group, childDefs, reporter, type, path, onlyCheckExistingChildren, reference, context, groupEntity);
}
use of eu.esdihumboldt.hale.common.instance.model.Instance in project hale by halestudio.
the class InstanceValidator method validateInstances.
/**
* Validates the given instances using all constraints that are validatable.
*
* @param instances the instances to validate
* @param monitor the progress monitor
* @return a report of the validation
*/
public InstanceValidationReport validateInstances(InstanceCollection instances, IProgressMonitor monitor) {
monitor.beginTask("Instance validation", instances.hasSize() ? instances.size() : IProgressMonitor.UNKNOWN);
InstanceValidationReporter reporter = new DefaultInstanceValidationReporter(false);
reporter.setSuccess(false);
ATransaction trans = log.begin("Instance validation");
InstanceValidationContext context = new InstanceValidationContext();
ResourceIterator<Instance> iterator = instances.iterator();
try {
while (iterator.hasNext()) {
if (monitor.isCanceled())
return reporter;
Instance instance = iterator.next();
validateInstance(instance, reporter, instance.getDefinition().getName(), new ArrayList<QName>(), false, instances.getReference(instance), context, null, null);
monitor.worked(1);
}
} finally {
iterator.close();
trans.end();
}
validateContext(context, reporter);
reporter.setSuccess(true);
return reporter;
}
use of eu.esdihumboldt.hale.common.instance.model.Instance in project hale by halestudio.
the class InstanceValidator method validateChildren.
/**
* Validates the given property values (their values - as instances - and/or
* group children).
*
* @param properties the array of existing properties, may be null
* @param childDef their definition
* @param reporter the reporter to report to
* @param type the top level type
* @param path the current property path
* @param onlyCheckExistingChildren whether to only validate existing
* children (in case of a choice) or not
* @param reference the instance reference
* @param context the instance validation context
* @param entity the entity definition related to the property values or
* <code>null</code>
*/
private void validateChildren(Object[] properties, ChildDefinition<?> childDef, InstanceValidationReporter reporter, QName type, List<QName> path, boolean onlyCheckExistingChildren, InstanceReference reference, InstanceValidationContext context, @Nullable EntityDefinition entity) {
if (properties != null && properties.length > 0) {
for (Object property : properties) {
if (property instanceof Instance) {
validateInstance((Instance) property, reporter, type, path, onlyCheckExistingChildren, reference, context, childDef, entity);
} else if (property instanceof Group) {
validateGroupChildren((Group) property, reporter, type, path, onlyCheckExistingChildren, reference, context, childDef, entity);
} else {
if (childDef.asGroup() != null)
reporter.warn(new DefaultInstanceValidationMessage(reference, type, new ArrayList<QName>(path), "Wrong group", "A property is no group"));
else if (childDef.asProperty() != null) {
if (!skipValidation(childDef.asProperty().getPropertyType(), property)) {
// don't skip property
// wrap value in dummy instance for type validation
MutableInstance instance = new DefaultInstance(childDef.asProperty().getPropertyType(), null);
instance.setValue(property);
validateInstance(instance, reporter, type, path, onlyCheckExistingChildren, reference, context, childDef, entity);
}
}
}
}
} else {
/*
* Special case: No property value, but a combination of minimum
* cardinality greater than zero and NillableFlag is set. Then there
* can be sub-properties that are required.
*
* Applicable for XML (simple) types with mandatory attributes.
*/
if (childDef.asProperty() != null && childDef.asProperty().getConstraint(Cardinality.class).getMinOccurs() > 0 && childDef.asProperty().getConstraint(NillableFlag.class).isEnabled() && childDef.asProperty().getPropertyType().getConstraint(HasValueFlag.class).isEnabled() && !childDef.asProperty().getPropertyType().getChildren().isEmpty()) {
// collect XML attribute children
List<ChildDefinition<?>> attributes = new ArrayList<ChildDefinition<?>>();
for (ChildDefinition<?> child : childDef.asProperty().getPropertyType().getChildren()) {
if (child.asProperty() != null && child.asProperty().getConstraint(XmlAttributeFlag.class).isEnabled()) {
attributes.add(child);
}
}
if (!attributes.isEmpty()) {
// create an empty dummy instance
Instance instance = new DefaultInstance(childDef.asProperty().getPropertyType(), null);
validateGroupChildren(instance, attributes, reporter, type, path, onlyCheckExistingChildren, reference, context, entity);
}
}
}
}
Aggregations