Search in sources :

Example 1 with Definition

use of eu.esdihumboldt.hale.common.schema.model.Definition in project hale by halestudio.

the class GraphLabelProvider method getText.

/**
 * @see LabelProvider#getText(Object)
 */
@Override
public String getText(Object element) {
    if (element instanceof Entity) {
        element = ((Entity) element).getDefinition();
    }
    if (element instanceof EntityDefinition) {
        // use definition text
        return definitionLabels.getText(element);
    }
    if (element instanceof Definition<?>) {
        // use definition text
        return definitionLabels.getText(element);
    }
    if (element instanceof Cell) {
        // use function name if possible
        Cell cell = (Cell) element;
        String functionId = cell.getTransformationIdentifier();
        FunctionDefinition<?> function = FunctionUtil.getFunction(functionId, serviceProvider);
        if (function != null) {
            return functionLabels.getText(function);
        }
        return functionId;
    }
    if (element instanceof FunctionDefinition) {
        return functionLabels.getText(element);
    }
    return super.getText(element);
}
Also used : Entity(eu.esdihumboldt.hale.common.align.model.Entity) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition) Definition(eu.esdihumboldt.hale.common.schema.model.Definition) FunctionDefinition(eu.esdihumboldt.hale.common.align.extension.function.FunctionDefinition) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) FunctionDefinition(eu.esdihumboldt.hale.common.align.extension.function.FunctionDefinition) Cell(eu.esdihumboldt.hale.common.align.model.Cell)

Example 2 with Definition

use of eu.esdihumboldt.hale.common.schema.model.Definition in project hale by halestudio.

the class OptionalPropertiesFilter method areChildrenOptional.

/**
 * Determines if the given (nillable) entity can be considered optional in
 * respect to its children.
 *
 * @param entityDef the entity definition which children to check
 * @param alreadyChecked the set of definitions that have already been
 *            checked (excluding the given entity)
 * @return if all children are optional
 */
private boolean areChildrenOptional(EntityDefinition entityDef, Set<Definition<?>> alreadyChecked) {
    // get children without contexts
    Collection<? extends EntityDefinition> children = AlignmentUtil.getChildrenWithoutContexts(entityDef);
    if (children == null || children.isEmpty()) {
        return true;
    }
    for (EntityDefinition child : children) {
        Set<Definition<?>> checked = new HashSet<>(alreadyChecked);
        checked.add(entityDef.getDefinition());
        ChildDefinition<?> childDef = (ChildDefinition<?>) child.getDefinition();
        /*
			 * XML: We only need to check children that are attributes, if they
			 * are optional.
			 */
        if (childDef.asProperty() != null && childDef.asProperty().getConstraint(XmlAttributeFlag.class).isEnabled()) {
            // need to check if it is optional
            if (!isOptional(child, checked)) {
                return false;
            }
        }
    /*
			 * XXX does other special handling need to be done for other kinds
			 * of schemas?
			 */
    }
    return true;
}
Also used : EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition) GroupPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition) Definition(eu.esdihumboldt.hale.common.schema.model.Definition) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) ChildDefinition(eu.esdihumboldt.hale.common.schema.model.ChildDefinition) EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition) ChildDefinition(eu.esdihumboldt.hale.common.schema.model.ChildDefinition) HashSet(java.util.HashSet)

Example 3 with Definition

use of eu.esdihumboldt.hale.common.schema.model.Definition in project hale by halestudio.

the class InstanceBuilderCode method appendBuildProperties.

/**
 * Append example code to build the properties identified by the given path
 * tree.
 *
 * @param example the example code to append to
 * @param baseIndent the base indent to use
 * @param tree the path tree representing a specific segment
 * @param parent the parent of the segment
 * @param useBrackets if brackets should be used in the generated code
 * @param startWithIndent if at the beginning the indent should be added
 * @param endWithNewline if at the end a new line break should be added
 * @param useExampleValues if example values should be used
 * @return the relative offset where editing should be continued
 */
public static int appendBuildProperties(StringBuilder example, String baseIndent, PathTree tree, DefinitionGroup parent, final boolean useBrackets, final boolean startWithIndent, final boolean endWithNewline, final boolean useExampleValues) {
    Definition<?> def = (Definition<?>) tree.getSegment();
    final String indent = baseIndent;
    int cursor = 0;
    boolean opened = false;
    if (def instanceof PropertyDefinition) {
        // property name
        if (startWithIndent) {
            example.append(indent);
        }
        // TODO test if property must be accessed explicitly through
        // builder?
        example.append(def.getName().getLocalPart());
        // test if uniquely accessible from parent
        boolean useNamespace = true;
        if (parent instanceof Definition<?>) {
            try {
                new DefinitionAccessor((Definition<?>) parent).findChildren(def.getName().getLocalPart()).eval();
                useNamespace = false;
            } catch (IllegalStateException e) {
            // ignore - namespace needed
            }
        }
        boolean needComma = false;
        // add namespace if necessary
        if (useNamespace) {
            if (useBrackets && !needComma) {
                example.append('(');
            }
            example.append(" namespace: '");
            example.append(def.getName().getNamespaceURI());
            example.append('\'');
            needComma = true;
        }
        TypeDefinition propertyType = ((PropertyDefinition) def).getPropertyType();
        boolean hasValue = propertyType.getConstraint(HasValueFlag.class).isEnabled();
        if (hasValue) {
            // add an example value
            if (useBrackets && !needComma) {
                example.append('(');
            }
            if (needComma) {
                example.append(',');
            }
            example.append(' ');
            if (useExampleValues) {
                switch(Classification.getClassification(def)) {
                    case NUMERIC_PROPERTY:
                        example.append("42");
                        break;
                    case STRING_PROPERTY:
                        example.append("'some value'");
                        break;
                    default:
                        example.append("some_value");
                }
            }
            needComma = true;
        }
        if (DefinitionUtil.hasChildren(propertyType) && (!tree.getChildren().isEmpty() || !needComma || !hasValue)) {
            if (needComma) {
                if (useBrackets) {
                    example.append(" )");
                } else {
                    example.append(',');
                }
            }
            example.append(" {");
            example.append('\n');
            opened = true;
        } else {
            cursor = example.length();
            if (useBrackets && needComma) {
                example.append(" )");
            }
            if (endWithNewline) {
                example.append('\n');
            }
        }
    } else {
    // groups are ignored
    }
    if (opened) {
        // set the new parent
        parent = DefinitionUtil.getDefinitionGroup(def);
        // create child properties
        String newIndent = indent + createIndent(1);
        if (tree.getChildren().isEmpty()) {
            example.append(newIndent);
            cursor = example.length();
            example.append('\n');
        } else {
            for (PathTree child : tree.getChildren()) {
                cursor = appendBuildProperties(example, newIndent, child, parent, useBrackets, true, true, useExampleValues);
            }
        }
        // close bracket
        example.append(indent);
        example.append('}');
        if (endWithNewline) {
            example.append('\n');
        }
    }
    return cursor;
}
Also used : HasValueFlag(eu.esdihumboldt.hale.common.schema.model.constraint.type.HasValueFlag) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) Definition(eu.esdihumboldt.hale.common.schema.model.Definition) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) DefinitionAccessor(eu.esdihumboldt.hale.common.schema.groovy.DefinitionAccessor) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Example 4 with Definition

use of eu.esdihumboldt.hale.common.schema.model.Definition in project hale by halestudio.

the class TypeStructureTray method createSourceSample.

/**
 * Create sample code for a single tree path specifying a source property.
 *
 * @param path the tree path
 * @param types the types serving as input
 * @return the sample code or <code>null</code>
 */
private String createSourceSample(TreePath path, Collection<? extends TypeDefinition> types) {
    DefinitionGroup parent;
    int startIndex = 0;
    boolean hasPropDef = false;
    StringBuilder access = new StringBuilder();
    // determine parent type
    if (path.getFirstSegment() instanceof TypeDefinition) {
        // types are the top level elements
        parent = (DefinitionGroup) path.getFirstSegment();
        startIndex = 1;
        access.append(GroovyConstants.BINDING_SOURCE);
    } else {
        // types are not in the tree, single type must be root
        TypeDefinition type = types.iterator().next();
        parent = type;
        if (VARIABLES_TYPE_NAME.equals(type.getName())) {
            // Groovy property transformation
            // first segment is variable name
            Definition<?> def = (Definition<?>) path.getFirstSegment();
            startIndex++;
            access.append(def.getName().getLocalPart());
            // XXX add namespace if necessary
            // XXX currently groovy transformation does not support multiple
            // variables with the same name
            parent = DefinitionUtil.getDefinitionGroup(def);
        } else {
            // assuming Retype/Merge
            access.append(GroovyConstants.BINDING_SOURCE);
        }
    }
    // is a property or list of inputs referenced -> use accessor
    boolean propertyOrList = path.getSegmentCount() > startIndex || (path.getLastSegment() instanceof ChildDefinition<?> && canOccureMultipleTimes((ChildDefinition<?>) path.getLastSegment()));
    if (!propertyOrList) {
        if (parent instanceof TypeDefinition && canHaveValue((TypeDefinition) parent)) {
            if (!DefinitionUtil.hasChildren((Definition<?>) path.getLastSegment())) {
                // variable w/o children referenced
                return "// access variable\ndef value = " + access;
            } else {
                // variable w/ children referenced
                return "// access instance variable value\ndef value = " + access + ".value";
            }
        } else {
        // return null;
        }
    }
    if (path.getFirstSegment() instanceof TypeDefinition) {
        access.append(".links." + ((TypeDefinition) path.getFirstSegment()).getDisplayName());
    } else {
        access.append(".p");
        // check for encountering property definition, so that there should
        // not be 2 'p's appended.
        hasPropDef = true;
    }
    for (int i = startIndex; i < path.getSegmentCount(); i++) {
        Definition<?> def = (Definition<?>) path.getSegment(i);
        if (def instanceof PropertyDefinition) {
            for (int j = i - 1; j >= 0; j--) {
                Definition<?> def1 = (Definition<?>) path.getSegment(j);
                if (def1 instanceof TypeDefinition) {
                // do nothing
                } else {
                    hasPropDef = true;
                }
            }
            if (!hasPropDef) {
                access.append(".p");
            }
            // property name
            access.append('.');
            access.append(def.getName().getLocalPart());
            // test if uniquely accessible from parent
            boolean useNamespace = true;
            if (parent instanceof Definition<?>) {
                useNamespace = namespaceNeeded((Definition<?>) parent, def);
            }
            // add namespace if necessary
            if (useNamespace) {
                access.append("('");
                access.append(def.getName().getNamespaceURI());
                access.append("')");
            }
        } else if (def instanceof TypeDefinition) {
            access.append("." + ((TypeDefinition) def).getDisplayName());
        }
        // set the new parent
        parent = DefinitionUtil.getDefinitionGroup(def);
    }
    if (parent instanceof TypeDefinition) {
        // only properties at the end of the path are supported
        TypeDefinition propertyType = (TypeDefinition) parent;
        StringBuilder example = new StringBuilder();
        boolean canOccurMultipleTimes = false;
        if (path.getFirstSegment() instanceof TypeDefinition) {
            canOccurMultipleTimes = true;
        }
        /*
			 * Instances/values may occur multiple times if any element in the
			 * path may occur multiple times.
			 */
        for (int i = path.getSegmentCount() - 1; i >= 0 && !canOccurMultipleTimes; i--) {
            if (path.getSegment(i) instanceof ChildDefinition<?>) {
                canOccurMultipleTimes = canOccureMultipleTimes((ChildDefinition<?>) path.getSegment(i));
            }
        }
        if (canHaveValue(propertyType)) {
            // single value
            if (canOccurMultipleTimes) {
                example.append("// access first value\n");
            } else {
                example.append("// access value\n");
            }
            example.append("def value = ");
            example.append(access);
            example.append(".value()\n\n");
            // multiple values
            if (canOccurMultipleTimes) {
                example.append("// access all values as list\n");
                example.append("def valueList = ");
                example.append(access);
                example.append(".values()\n\n");
            }
        }
        if (DefinitionUtil.hasChildren(propertyType)) {
            // single instance
            if (canOccurMultipleTimes) {
                example.append("// access first instance\n");
            } else {
                example.append("// access instance\n");
            }
            example.append("def instance = ");
            example.append(access);
            example.append(".first()\n\n");
            if (canOccurMultipleTimes) {
                // multiple values
                example.append("// access all instances as list\n");
                example.append("def instanceList = ");
                example.append(access);
                example.append(".list()\n\n");
                // iterate over instances
                example.append("// iterate over instances\n");
                example.append(access);
                example.append(".each {\n");
                example.append("\tinstance ->\n");
                example.append("}\n\n");
            }
        } else if (canOccurMultipleTimes && propertyType.getConstraint(HasValueFlag.class).isEnabled()) {
            // iterate over values
            example.append("// iterate over values\n");
            example.append(access);
            example.append(".each {\n");
            example.append("\tvalue ->\n");
            example.append("}\n\n");
        }
        return example.toString();
    }
    return null;
}
Also used : HasValueFlag(eu.esdihumboldt.hale.common.schema.model.constraint.type.HasValueFlag) ChildDefinition(eu.esdihumboldt.hale.common.schema.model.ChildDefinition) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) Definition(eu.esdihumboldt.hale.common.schema.model.Definition) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) ChildDefinition(eu.esdihumboldt.hale.common.schema.model.ChildDefinition) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) DefinitionGroup(eu.esdihumboldt.hale.common.schema.model.DefinitionGroup) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Example 5 with Definition

use of eu.esdihumboldt.hale.common.schema.model.Definition in project hale by halestudio.

the class InstanceValidationReportDetailsContentProvider method getChildren.

/**
 * @see ITreePathContentProvider#getChildren(TreePath)
 */
@Override
public Object[] getChildren(TreePath parentPath) {
    Set<Object> children = childCache.get(parentPath);
    if (children == null) {
        Collection<InstanceValidationMessage> ivms = messages.get(parentPath);
        if (!ivms.isEmpty()) {
            children = new HashSet<Object>();
            // count of added messages
            int messageCount = 0;
            for (InstanceValidationMessage message : ivms) {
                if (message.getPath().size() > parentPath.getSegmentCount() - 1) {
                    // path not done, add next segment
                    QName name = message.getPath().get(parentPath.getSegmentCount() - 1);
                    Object child = name;
                    Object parent = parentPath.getLastSegment();
                    if (parent instanceof Definition<?>) {
                        ChildDefinition<?> childDef = DefinitionUtil.getChild((Definition<?>) parent, name);
                        if (childDef != null)
                            child = childDef;
                    }
                    children.add(child);
                    messages.put(parentPath.createChildPath(child), message);
                } else if (message.getPath().size() == parentPath.getSegmentCount() - 1) {
                    // path done, go by category
                    String category = message.getCategory();
                    children.add(category);
                    messages.put(parentPath.createChildPath(category), message);
                } else {
                    // all done, add as child
                    if (messageCount < LIMIT) {
                        children.add(message);
                        messageCount++;
                    } else {
                        limitedPaths.add(parentPath);
                    }
                }
            }
        } else
            children = Collections.emptySet();
        childCache.put(parentPath, children);
    }
    return children.toArray();
}
Also used : QName(javax.xml.namespace.QName) Definition(eu.esdihumboldt.hale.common.schema.model.Definition) ChildDefinition(eu.esdihumboldt.hale.common.schema.model.ChildDefinition) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) InstanceValidationMessage(eu.esdihumboldt.hale.common.instance.extension.validation.report.InstanceValidationMessage)

Aggregations

Definition (eu.esdihumboldt.hale.common.schema.model.Definition)16 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)10 ChildDefinition (eu.esdihumboldt.hale.common.schema.model.ChildDefinition)7 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)5 PropertyDefinition (eu.esdihumboldt.hale.common.schema.model.PropertyDefinition)4 QName (javax.xml.namespace.QName)4 DefinitionAccessor (eu.esdihumboldt.hale.common.schema.groovy.DefinitionAccessor)3 XmlElement (eu.esdihumboldt.hale.io.xsd.model.XmlElement)3 ChildContext (eu.esdihumboldt.hale.common.align.model.ChildContext)2 TypeEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition)2 InstanceValidationMessage (eu.esdihumboldt.hale.common.instance.extension.validation.report.InstanceValidationMessage)2 InstanceValidationReport (eu.esdihumboldt.hale.common.instance.extension.validation.report.InstanceValidationReport)2 DefinitionGroup (eu.esdihumboldt.hale.common.schema.model.DefinitionGroup)2 HasValueFlag (eu.esdihumboldt.hale.common.schema.model.constraint.type.HasValueFlag)2 Pair (eu.esdihumboldt.util.Pair)2 List (java.util.List)2 GridData (org.eclipse.swt.layout.GridData)2 FunctionDefinition (eu.esdihumboldt.hale.common.align.extension.function.FunctionDefinition)1 ChildContextType (eu.esdihumboldt.hale.common.align.io.impl.internal.generated.ChildContextType)1 Cell (eu.esdihumboldt.hale.common.align.model.Cell)1