Search in sources :

Example 6 with Cardinality

use of eu.esdihumboldt.hale.common.schema.model.constraint.property.Cardinality in project hale by halestudio.

the class InstanceContextTester method testAllowAddIndex.

/**
 * Tests if for the given entity definition a new index context may be
 * created.
 *
 * @param entityDef the entity definition
 * @return if adding an index context is allowed
 */
private boolean testAllowAddIndex(EntityDefinition entityDef) {
    if (entityDef.getSchemaSpace().equals(SchemaSpaceID.TARGET)) {
        // only allowed on source properties
        return false;
    }
    // XXX for now only a simple test based on type and cardinality
    List<ChildContext> path = entityDef.getPropertyPath();
    if (path != null && !path.isEmpty()) {
        ChildContext lastContext = path.get(path.size() - 1);
        ChildDefinition<?> lastDef = lastContext.getChild();
        // test type & cardinality
        Cardinality cardinality;
        try {
            cardinality = DefinitionUtil.getCardinality(lastDef);
        } catch (Exception e) {
            return false;
        }
        if (cardinality.getMaxOccurs() == Cardinality.UNBOUNDED) {
            return true;
        }
        return cardinality.getMaxOccurs() > 1;
    }
    return false;
}
Also used : Cardinality(eu.esdihumboldt.hale.common.schema.model.constraint.property.Cardinality) ChildContext(eu.esdihumboldt.hale.common.align.model.ChildContext)

Example 7 with Cardinality

use of eu.esdihumboldt.hale.common.schema.model.constraint.property.Cardinality in project hale by halestudio.

the class InstanceContextTester method testAllowAddNamed.

/**
 * Tests if for the given entity definition a new instance context may be
 * created.
 *
 * @param entityDef the entity definition
 * @return if adding an instance context is allowed
 */
private boolean testAllowAddNamed(EntityDefinition entityDef) {
    if (entityDef.getSchemaSpace().equals(SchemaSpaceID.SOURCE)) {
        // only allowed on target properties
        return false;
    }
    // XXX for now only a simple test based on type and cardinality
    List<ChildContext> path = entityDef.getPropertyPath();
    if (path != null && !path.isEmpty()) {
        ChildContext lastContext = path.get(path.size() - 1);
        ChildDefinition<?> lastDef = lastContext.getChild();
        // test type & cardinality
        Cardinality cardinality;
        try {
            cardinality = DefinitionUtil.getCardinality(lastDef);
        } catch (Exception e) {
            return false;
        }
        if (cardinality.getMaxOccurs() == Cardinality.UNBOUNDED) {
            return true;
        }
        return cardinality.getMaxOccurs() > 1;
    }
    return false;
}
Also used : Cardinality(eu.esdihumboldt.hale.common.schema.model.constraint.property.Cardinality) ChildContext(eu.esdihumboldt.hale.common.align.model.ChildContext)

Example 8 with Cardinality

use of eu.esdihumboldt.hale.common.schema.model.constraint.property.Cardinality in project hale by halestudio.

the class StyledDefinitionLabelProvider method update.

/**
 * @see StyledCellLabelProvider#update(ViewerCell)
 */
@Override
public void update(ViewerCell cell) {
    Object element = cell.getElement();
    element = extractElement(element);
    StyledString text = new StyledString(defaultLabels.getText(element));
    cell.setImage(defaultLabels.getImage(element));
    String contextText = null;
    String countText = null;
    if (element instanceof EntityDefinition) {
        PopulationService ps = PlatformUI.getWorkbench().getService(PopulationService.class);
        if (ps != null) {
            Population pop = ps.getPopulation((EntityDefinition) element);
            int count = pop.getOverallCount();
            int parents = pop.getParentsCount();
            switch(count) {
                case Population.UNKNOWN:
                    countText = "\u00d7?";
                    break;
                case 0:
                    break;
                default:
                    countText = "\u00d7" + count;
                    if (parents != count) {
                        countText += " (" + parents + ")";
                    }
            }
        }
        contextText = AlignmentUtil.getContextText((EntityDefinition) element);
        element = ((EntityDefinition) element).getDefinition();
    }
    // append cardinality
    if (!suppressCardinality && element instanceof ChildDefinition<?>) {
        Cardinality cardinality = null;
        if (((ChildDefinition<?>) element).asGroup() != null) {
            cardinality = ((ChildDefinition<?>) element).asGroup().getConstraint(Cardinality.class);
        } else if (((ChildDefinition<?>) element).asProperty() != null) {
            cardinality = ((ChildDefinition<?>) element).asProperty().getConstraint(Cardinality.class);
        }
        if (cardinality != null) {
            // only append cardinality if it isn't 1/1
            if (cardinality.getMinOccurs() != 1 || cardinality.getMaxOccurs() != 1) {
                String card = " " + MessageFormat.format("({0}..{1})", new Object[] { Long.valueOf(cardinality.getMinOccurs()), (cardinality.getMaxOccurs() == Cardinality.UNBOUNDED) ? ("n") : (Long.valueOf(cardinality.getMaxOccurs())) });
                text.append(card, StyledString.COUNTER_STYLER);
            }
        }
    }
    if (contextText != null) {
        contextText = " " + contextText;
        text.append(contextText, StyledString.DECORATIONS_STYLER);
    }
    if (countText != null) {
        countText = " " + countText;
        text.append(countText, StyledString.QUALIFIER_STYLER);
    }
    cell.setText(text.toString());
    cell.setStyleRanges(text.getStyleRanges());
    Color foreground = getForeground(cell.getElement());
    cell.setForeground(foreground);
    Color background = getBackground(cell.getElement());
    cell.setBackground(background);
    super.update(cell);
}
Also used : EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition) Cardinality(eu.esdihumboldt.hale.common.schema.model.constraint.property.Cardinality) Color(org.eclipse.swt.graphics.Color) PopulationService(eu.esdihumboldt.hale.ui.common.service.population.PopulationService) ChildDefinition(eu.esdihumboldt.hale.common.schema.model.ChildDefinition) Population(eu.esdihumboldt.hale.ui.common.service.population.Population) StyledString(org.eclipse.jface.viewers.StyledString) StyledString(org.eclipse.jface.viewers.StyledString)

Example 9 with Cardinality

use of eu.esdihumboldt.hale.common.schema.model.constraint.property.Cardinality in project hale by halestudio.

the class GroupUtil method allowAddCheckCardinality.

/**
 * Determines if another value of the given property may be added to the
 * given group based on the cardinality of the property.
 *
 * @param group the group
 * @param propertyName the property name
 * @return if another property value may be added to the group based on the
 *         property cardinality
 */
private static boolean allowAddCheckCardinality(Group group, QName propertyName) {
    ChildDefinition<?> child = group.getDefinition().getChild(propertyName);
    Cardinality cardinality = DefinitionUtil.getCardinality(child);
    // check maximum
    long max = cardinality.getMaxOccurs();
    if (max == Cardinality.UNBOUNDED) {
        // add allowed in any case
        return true;
    } else if (max <= 0) {
        // add never allowed
        return false;
    }
    Object[] values = group.getProperty(propertyName);
    if (values == null) {
        // allowed because max is 1 or more
        return true;
    }
    return values.length < max;
}
Also used : Cardinality(eu.esdihumboldt.hale.common.schema.model.constraint.property.Cardinality)

Example 10 with Cardinality

use of eu.esdihumboldt.hale.common.schema.model.constraint.property.Cardinality in project hale by halestudio.

the class GroupUtil method isValidCardinality.

/**
 * Determines if a child is contained in a given group with a valid minimum
 * cardinality.
 *
 * @param group the group
 * @param childDef the child definition
 * @return if the minimum cardinality of the child definition is matched in
 *         the group
 */
static boolean isValidCardinality(Group group, ChildDefinition<?> childDef) {
    Cardinality cardinality = null;
    if (childDef.asProperty() != null) {
        cardinality = childDef.asProperty().getConstraint(Cardinality.class);
    } else if (childDef.asGroup() != null) {
        cardinality = childDef.asGroup().getConstraint(Cardinality.class);
    } else {
        log.error("Unrecognized child definition.");
    }
    if (cardinality != null) {
        // check minimum
        long min = cardinality.getMinOccurs();
        if (min > 0 && min != Cardinality.UNBOUNDED) {
            Object[] values = group.getProperty(childDef.getName());
            int count = (values == null) ? (0) : (values.length);
            if (min > count) {
                return false;
            }
        }
    }
    return true;
}
Also used : Cardinality(eu.esdihumboldt.hale.common.schema.model.constraint.property.Cardinality)

Aggregations

Cardinality (eu.esdihumboldt.hale.common.schema.model.constraint.property.Cardinality)14 GroupPropertyDefinition (eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition)7 PropertyDefinition (eu.esdihumboldt.hale.common.schema.model.PropertyDefinition)7 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)6 QName (javax.xml.namespace.QName)5 DefaultInputSupplier (eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier)4 XmlIndex (eu.esdihumboldt.hale.io.xsd.model.XmlIndex)4 URI (java.net.URI)4 Test (org.junit.Test)4 ChildContext (eu.esdihumboldt.hale.common.align.model.ChildContext)2 Binding (eu.esdihumboldt.hale.common.schema.model.constraint.type.Binding)2 Population (eu.esdihumboldt.hale.ui.common.service.population.Population)2 PopulationService (eu.esdihumboldt.hale.ui.common.service.population.PopulationService)2 MultiValue (eu.esdihumboldt.cst.MultiValue)1 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)1 TargetNode (eu.esdihumboldt.hale.common.align.model.transformation.tree.TargetNode)1 DataSet (eu.esdihumboldt.hale.common.instance.model.DataSet)1 Group (eu.esdihumboldt.hale.common.instance.model.Group)1 Classification (eu.esdihumboldt.hale.common.schema.Classification)1 ChildDefinition (eu.esdihumboldt.hale.common.schema.model.ChildDefinition)1