Search in sources :

Example 66 with Instance

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();
    }
}
Also used : FamilyInstance(eu.esdihumboldt.hale.common.instance.model.FamilyInstance) MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) TransformationTree(eu.esdihumboldt.hale.common.align.model.transformation.tree.TransformationTree) InstanceVisitor(eu.esdihumboldt.hale.common.align.model.transformation.tree.visitor.InstanceVisitor) HashSet(java.util.HashSet) DuplicationVisitor(eu.esdihumboldt.hale.common.align.model.transformation.tree.visitor.DuplicationVisitor)

Example 67 with Instance

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;
    }
}
Also used : Instance(eu.esdihumboldt.hale.common.instance.model.Instance)

Example 68 with Instance

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);
}
Also used : MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) DefaultInstance(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) QName(javax.xml.namespace.QName) XmlAttributeFlag(eu.esdihumboldt.hale.io.xsd.constraint.XmlAttributeFlag)

Example 69 with Instance

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;
}
Also used : InstanceValidationReporter(eu.esdihumboldt.hale.common.instance.extension.validation.report.InstanceValidationReporter) DefaultInstanceValidationReporter(eu.esdihumboldt.hale.common.instance.extension.validation.report.impl.DefaultInstanceValidationReporter) MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) DefaultInstance(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) QName(javax.xml.namespace.QName) ATransaction(de.fhg.igd.slf4jplus.ATransaction) DefaultInstanceValidationReporter(eu.esdihumboldt.hale.common.instance.extension.validation.report.impl.DefaultInstanceValidationReporter) InstanceValidationContext(eu.esdihumboldt.hale.common.instance.extension.validation.InstanceValidationContext)

Example 70 with Instance

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);
            }
        }
    }
}
Also used : Group(eu.esdihumboldt.hale.common.instance.model.Group) MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) DefaultInstance(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) DefaultInstanceValidationMessage(eu.esdihumboldt.hale.common.instance.extension.validation.report.impl.DefaultInstanceValidationMessage) DefaultInstance(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance) HasValueFlag(eu.esdihumboldt.hale.common.schema.model.constraint.type.HasValueFlag) ArrayList(java.util.ArrayList) MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) ChildDefinition(eu.esdihumboldt.hale.common.schema.model.ChildDefinition)

Aggregations

Instance (eu.esdihumboldt.hale.common.instance.model.Instance)203 InstanceCollection (eu.esdihumboldt.hale.common.instance.model.InstanceCollection)131 Test (org.junit.Test)122 AbstractHandlerTest (eu.esdihumboldt.hale.io.gml.geometry.handler.internal.AbstractHandlerTest)97 QName (javax.xml.namespace.QName)29 ArrayList (java.util.ArrayList)26 MutableInstance (eu.esdihumboldt.hale.common.instance.model.MutableInstance)25 DefaultInstance (eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance)23 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)22 Group (eu.esdihumboldt.hale.common.instance.model.Group)15 Schema (eu.esdihumboldt.hale.common.schema.model.Schema)13 Coordinate (com.vividsolutions.jts.geom.Coordinate)12 Geometry (com.vividsolutions.jts.geom.Geometry)12 FamilyInstance (eu.esdihumboldt.hale.common.instance.model.FamilyInstance)10 Polygon (com.vividsolutions.jts.geom.Polygon)9 MultiPolygon (com.vividsolutions.jts.geom.MultiPolygon)8 TransformationException (eu.esdihumboldt.hale.common.align.transformation.function.TransformationException)8 GeometryProperty (eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty)8 HashSet (java.util.HashSet)8 Point (com.vividsolutions.jts.geom.Point)7