Search in sources :

Example 16 with MutableInstance

use of eu.esdihumboldt.hale.common.instance.model.MutableInstance in project hale by halestudio.

the class GroovyCreate method execute.

@Override
public void execute(String transformationIdentifier, TransformationEngine engine, Map<String, String> executionParameters, TransformationLog log, Cell cell) throws TransformationException {
    // get number of executions
    int num;
    String numberExpr = getOptionalParameter(PARAM_NUMBER, Value.of(1)).as(String.class);
    if (numberExpr != null) {
        // replace variables
        numberExpr = getExecutionContext().getVariables().replaceVariables(numberExpr);
        try {
            num = Integer.parseInt(numberExpr);
        } catch (NumberFormatException e) {
            log.error(log.createMessage("Unable to parse expression for number of instances to create", e));
            num = 1;
        }
    } else {
        num = 1;
    }
    TypeDefinition targetType = getTarget().values().iterator().next().getDefinition().getDefinition();
    for (int i = 0; i < num; i++) {
        Iterable<MutableInstance> target = createInstances(targetType, log, cell, i);
        for (MutableInstance instance : target) {
            getPropertyTransformer().publish(null, instance, log, cell);
        }
    }
}
Also used : MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Example 17 with MutableInstance

use of eu.esdihumboldt.hale.common.instance.model.MutableInstance in project hale by halestudio.

the class TargetCollector method toMultiValue.

/**
 * Transforms the closures added to this collector to a {@link MultiValue}
 * using the supplied builder.
 *
 * @param builder the instance builder for creating target instances
 * @param type the type of the instance to create
 * @param log the log
 * @return a result value for all closures added to this collector
 * @throws TransformationException if some of the collected targets do not
 *             match the specified type
 */
public MultiValue toMultiValue(InstanceBuilder builder, TypeDefinition type, SimpleLog log) throws TransformationException {
    MultiValue result = new MultiValue(size());
    // a) closures not allowed if the target is no instance
    if (containsClosures && type.getChildren().isEmpty()) {
        throw new TransformationException("An instance is not applicable for the target.");
    }
    // b) values not allowed if the target may not have a value
    if (containsValues && !type.getConstraint(HasValueFlag.class).isEnabled() && !type.getConstraint(AugmentedValueFlag.class).isEnabled()) {
        // this may be desired, e.g. when producing geometries for GML
        if (containsGeometries) {
            // only warning message for geometries
            log.warn("Value provided for target that does not allow a value according to the schema, contains geometries");
        } else {
            // instead of a hard error, we just log an error
            log.error("Value provided for target that does not allow a value according to the schema");
        }
    }
    for (TargetData data : targetData) {
        Object value;
        if (data.instance != null) {
            Instance instance = data.instance;
            // value as instance value
            if (data.value != null && instance instanceof MutableInstance) {
                ((MutableInstance) instance).setValue(data.value);
            }
            value = instance;
        } else {
            value = data.value;
        }
        result.add(value);
    }
    return result;
}
Also used : TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) AugmentedValueFlag(eu.esdihumboldt.hale.common.schema.model.constraint.type.AugmentedValueFlag) MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) MultiValue(eu.esdihumboldt.cst.MultiValue)

Example 18 with MutableInstance

use of eu.esdihumboldt.hale.common.instance.model.MutableInstance in project hale by halestudio.

the class FilterTest method simpleFilterTestECQL.

@Test
public void simpleFilterTestECQL() throws CQLException {
    DefaultTypeDefinition stringType = new DefaultTypeDefinition(new QName("StringType"));
    stringType.setConstraint(Binding.get(String.class));
    DefaultTypeDefinition personDef = new DefaultTypeDefinition(new QName("PersonType"));
    personDef.addChild(new DefaultPropertyDefinition(new QName("Name"), personDef, stringType));
    DefaultTypeDefinition autoDef = new DefaultTypeDefinition(new QName("AutoType"));
    autoDef.addChild(new DefaultPropertyDefinition(new QName("Name"), autoDef, stringType));
    autoDef.addChild(new DefaultPropertyDefinition(new QName("Besitzer"), autoDef, personDef));
    MutableInstance auto = new DefaultInstance(autoDef, null);
    auto.addProperty(new QName("Name"), "Mein Porsche");
    MutableInstance ich = new DefaultInstance(personDef, null);
    ich.addProperty(new QName("Name"), "Ich");
    auto.addProperty(new QName("Besitzer"), ich);
    Filter filter;
    filter = new FilterGeoECqlImpl("Name = 'Mein Porsche'");
    assertTrue(filter.match(auto));
    Filter filter1 = new FilterGeoECqlImpl("Name like '%Porsche%'");
    assertTrue(filter1.match(auto));
}
Also used : DefaultTypeDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition) DefaultPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultPropertyDefinition) Filter(eu.esdihumboldt.hale.common.instance.model.Filter) QName(javax.xml.namespace.QName) DefaultInstance(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance) MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) Test(org.junit.Test)

Example 19 with MutableInstance

use of eu.esdihumboldt.hale.common.instance.model.MutableInstance in project hale by halestudio.

the class AlignmentUtil method matchCondition.

/**
 * Match a property condition against a property value.
 *
 * @param condition the property condition
 * @param value the property value
 * @param parent the parent of the property value, may be <code>null</code>
 *            if there is none
 * @return if the value matched the property condition
 */
public static boolean matchCondition(Condition condition, Object value, Object parent) {
    // create dummy instance
    MutableInstance dummy = new DefaultInstance(null, null);
    // add value as property
    dummy.addProperty(new QName("value"), value);
    // add parent value as property
    if (parent != null) {
        dummy.addProperty(new QName("parent"), parent);
    }
    return condition.getFilter().match(dummy);
}
Also used : QName(javax.xml.namespace.QName) DefaultInstance(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance) MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance)

Example 20 with MutableInstance

use of eu.esdihumboldt.hale.common.instance.model.MutableInstance in project hale by halestudio.

the class Rename method structuralRename.

/**
 * Performs a structural rename on the given source object to the given
 * target definition.
 *
 * @param source the source value (or group/instance)
 * @param targetDefinition the target definition
 * @param allowIgnoreNamespaces if for the structure comparison, namespaces
 *            may be ignored
 * @param instanceFactory the instance factory
 * @param copyGeometries specifies if geometry objects should be copied
 * @return the transformed value (or group/instance) or NO_MATCH
 */
public static Object structuralRename(Object source, ChildDefinition<?> targetDefinition, boolean allowIgnoreNamespaces, InstanceFactory instanceFactory, boolean copyGeometries) {
    if (!(source instanceof Group)) {
        // source simple value
        if (targetDefinition.asProperty() != null) {
            // target can have value
            TypeDefinition propertyType = targetDefinition.asProperty().getPropertyType();
            if (copyGeometries || !isGeometry(source)) {
                if (propertyType.getChildren().isEmpty()) {
                    // simple value
                    return convertValue(source, targetDefinition.asProperty().getPropertyType());
                } else {
                    // instance with value
                    MutableInstance instance = instanceFactory.createInstance(propertyType);
                    instance.setDataSet(DataSet.TRANSFORMED);
                    instance.setValue(convertValue(source, propertyType));
                    return instance;
                }
            } else {
                return Result.NO_MATCH;
            }
        }
    }
    // source is group or instance
    if (targetDefinition.asProperty() != null) {
        // target can have value
        TypeDefinition propertyType = targetDefinition.asProperty().getPropertyType();
        if (source instanceof Instance) {
            // source has value
            if (propertyType.getChildren().isEmpty()) {
                // simple value
                return convertValue(((Instance) source).getValue(), targetDefinition.asProperty().getPropertyType());
            } else {
                // instance with value
                MutableInstance instance = instanceFactory.createInstance(targetDefinition.asProperty().getPropertyType());
                instance.setDataSet(DataSet.TRANSFORMED);
                if (copyGeometries || !isGeometry(((Instance) source).getValue())) {
                    instance.setValue(convertValue(((Instance) source).getValue(), targetDefinition.asProperty().getPropertyType()));
                }
                renameChildren((Group) source, instance, targetDefinition, allowIgnoreNamespaces, instanceFactory, copyGeometries);
                return instance;
            }
        } else {
            // source has no value
            if (targetDefinition.asProperty().getPropertyType().getChildren().isEmpty())
                // no match possible
                return Result.NO_MATCH;
            else {
                // instance with no value set
                MutableInstance instance = instanceFactory.createInstance(targetDefinition.asProperty().getPropertyType());
                instance.setDataSet(DataSet.TRANSFORMED);
                if (renameChildren((Group) source, instance, targetDefinition, allowIgnoreNamespaces, instanceFactory, copyGeometries))
                    return instance;
                else
                    // no child matched and no value
                    return Result.NO_MATCH;
            }
        }
    } else if (targetDefinition.asGroup() != null) {
        // target can not have a value
        if (targetDefinition.asGroup().getDeclaredChildren().isEmpty())
            // target neither has a value nor
            return Result.NO_MATCH;
        else // children?
        {
            // group
            MutableGroup group = new DefaultGroup(targetDefinition.asGroup());
            if (renameChildren((Group) source, group, targetDefinition, allowIgnoreNamespaces, instanceFactory, copyGeometries))
                return group;
            else
                // no child matched and no value
                return Result.NO_MATCH;
        }
    } else {
        // neither asProperty nor asGroup -> illegal ChildDefinition
        throw new IllegalStateException("Illegal child type.");
    }
}
Also used : Group(eu.esdihumboldt.hale.common.instance.model.Group) MutableGroup(eu.esdihumboldt.hale.common.instance.model.MutableGroup) DefaultGroup(eu.esdihumboldt.hale.common.instance.model.impl.DefaultGroup) MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) DefaultGroup(eu.esdihumboldt.hale.common.instance.model.impl.DefaultGroup) MutableGroup(eu.esdihumboldt.hale.common.instance.model.MutableGroup) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Aggregations

MutableInstance (eu.esdihumboldt.hale.common.instance.model.MutableInstance)22 DefaultInstance (eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance)10 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)9 QName (javax.xml.namespace.QName)8 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)7 DefaultTypeDefinition (eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition)4 MultiValue (eu.esdihumboldt.cst.MultiValue)3 TransformationException (eu.esdihumboldt.hale.common.align.transformation.function.TransformationException)3 Group (eu.esdihumboldt.hale.common.instance.model.Group)3 MutableGroup (eu.esdihumboldt.hale.common.instance.model.MutableGroup)3 DefaultGroup (eu.esdihumboldt.hale.common.instance.model.impl.DefaultGroup)3 DefaultPropertyDefinition (eu.esdihumboldt.hale.common.schema.model.impl.DefaultPropertyDefinition)3 Geometry (com.vividsolutions.jts.geom.Geometry)2 NoResultException (eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException)2 FamilyInstance (eu.esdihumboldt.hale.common.instance.model.FamilyInstance)2 Filter (eu.esdihumboldt.hale.common.instance.model.Filter)2 InstanceCollection (eu.esdihumboldt.hale.common.instance.model.InstanceCollection)2 GeometryProperty (eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty)2 PropertyDefinition (eu.esdihumboldt.hale.common.schema.model.PropertyDefinition)2 HasValueFlag (eu.esdihumboldt.hale.common.schema.model.constraint.type.HasValueFlag)2