Search in sources :

Example 96 with Cell

use of eu.esdihumboldt.hale.common.align.model.Cell in project hale by halestudio.

the class XsltGenerator method write.

/**
 * Generate the XSLT transformation and write it to the given target.
 *
 * @param target the target output supplier
 * @return the report
 * @throws Exception if a unrecoverable error occurs during the process
 */
public IOReport write(LocatableOutputSupplier<? extends OutputStream> target) throws Exception {
    Template root = ve.getTemplate(Templates.ROOT, "UTF-8");
    VelocityContext context = XslTransformationUtil.createStrictVelocityContext();
    // project info
    context.put("info", ProjectXslInfo.getInfo(projectInfo));
    // collects IDs of type cells
    Set<String> typeIds = new HashSet<String>();
    // type cells
    for (Cell typeCell : alignment.getTypeCells()) {
        if (typeCell.getTransformationMode() != TransformationMode.disabled) {
            // ignore disabled cells
            Entity targetEntity = CellUtil.getFirstEntity(typeCell.getTarget());
            if (targetEntity != null) {
                // assign identifiers for type transformations
                String targetName = targetEntity.getDefinition().getDefinition().getName().getLocalPart();
                String id = cellIdentifiers.getId(typeCell, targetName);
                typeIds.add(id);
            } else {
                reporter.warn(new IOMessageImpl("Ignoring type relation without target type", null));
            }
        }
    }
    // collects IDs of type cells mapped to target element names
    Map<String, QName> targetElements = new HashMap<String, QName>();
    // container
    File container = new File(workDir, "container.xsl");
    progress.setCurrentTask("Generating container");
    generateContainer(typeIds, container, targetElements);
    Set<String> passiveCellIds = new HashSet<String>(typeIds);
    progress.setCurrentTask("Generate type transformations");
    // all active cells templates
    for (Entry<String, QName> entry : targetElements.entrySet()) {
        // generate XSL fragments for type transformations
        String id = entry.getKey();
        QName elementName = entry.getValue();
        Cell typeCell = cellIdentifiers.getObject(id);
        // this is not a passive cell
        passiveCellIds.remove(id);
        XmlElement targetElement = targetSchema.getElements().get(elementName);
        String filename = "_" + id + ".xsl";
        File file = new File(workDir, filename);
        includes.add(filename);
        generateTypeTransformation(id, targetElement, typeCell, file);
    }
    // all passive cell templates
    for (String passiveId : passiveCellIds) {
        Cell typeCell = cellIdentifiers.getObject(passiveId);
        String filename = "_" + passiveId + ".xsl";
        File file = new File(workDir, filename);
        includes.add(filename);
        // XXX dummy target element
        XmlElement targetElement = new XmlElement(new QName(NS_XSL_DEFINITIONS, "dummy"), null, null);
        generateTypeTransformation(passiveId, targetElement, typeCell, file);
        // for passive cells no variables should be created
        typeIds.remove(passiveId);
    }
    // namespaces that occur additionally to the fixed namespaces
    Map<String, String> additionalNamespaces = new HashMap<String, String>(prefixes.asMap());
    for (String fixedPrefix : FIXED_PREFIXES.keySet()) {
        additionalNamespaces.remove(fixedPrefix);
    }
    context.put("additionalNamespaces", additionalNamespaces);
    // types cells
    /*
		 * The type identifiers are used as variable name to store the result of
		 * the equally named template.
		 */
    context.put("targets", typeIds);
    // includes
    // TODO check if files to include are actually there?
    context.put("includes", includes);
    OutputStream out = target.getOutput();
    XMLPrettyPrinter printer = new XMLPrettyPrinter(out);
    Future<?> ready = printer.start();
    Writer writer = new OutputStreamWriter(printer, "UTF-8");
    try {
        root.merge(context, writer);
        writer.flush();
    } finally {
        writer.close();
        ready.get();
        out.close();
    }
    reporter.setSuccess(reporter.getErrors().isEmpty());
    return reporter;
}
Also used : Entity(eu.esdihumboldt.hale.common.align.model.Entity) HashMap(java.util.HashMap) VelocityContext(org.apache.velocity.VelocityContext) QName(javax.xml.namespace.QName) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) Template(org.apache.velocity.Template) XmlElement(eu.esdihumboldt.hale.io.xsd.model.XmlElement) OutputStreamWriter(java.io.OutputStreamWriter) Cell(eu.esdihumboldt.hale.common.align.model.Cell) File(java.io.File) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 97 with Cell

use of eu.esdihumboldt.hale.common.align.model.Cell in project hale by halestudio.

the class MathematicalExpressionExplanation method getExplanation.

@Override
protected String getExplanation(Cell cell, boolean html, ServiceProvider services, Locale locale) {
    Entity target = CellUtil.getFirstEntity(cell.getTarget());
    String expression = CellUtil.getFirstParameter(cell, MathematicalExpression.PARAMETER_EXPRESSION).as(String.class);
    List<? extends Entity> sources = cell.getSource().get(MathematicalExpression.ENTITY_VARIABLE);
    if (target != null && expression != null) {
        if (html)
            expression = "<pre>" + expression + "</pre>";
        String explanation = MessageFormat.format(getMessage("main", locale), formatEntity(target, html, true, locale), expression);
        if (html)
            explanation = explanation.replaceAll("\n", "<br />");
        if (html) {
            Map<String, String> varToProperty = sources.stream().collect(Collectors.toMap(entity -> {
                return getEntityNameWithoutCondition(entity);
            }, entity -> {
                return formatEntity(entity, true, false, locale);
            }));
            explanation += buildReplacementTable(varToProperty, locale);
        }
        return explanation;
    }
    return null;
}
Also used : List(java.util.List) Cell(eu.esdihumboldt.hale.common.align.model.Cell) Locale(java.util.Locale) Map(java.util.Map) ServiceProvider(eu.esdihumboldt.hale.common.core.service.ServiceProvider) Entity(eu.esdihumboldt.hale.common.align.model.Entity) AbstractCellExplanation(eu.esdihumboldt.hale.common.align.model.impl.AbstractCellExplanation) Collectors(java.util.stream.Collectors) CellUtil(eu.esdihumboldt.hale.common.align.model.CellUtil) MessageFormat(java.text.MessageFormat) Entity(eu.esdihumboldt.hale.common.align.model.Entity)

Example 98 with Cell

use of eu.esdihumboldt.hale.common.align.model.Cell in project hale by halestudio.

the class DefaultAlignmentMigrator method updateAligmment.

@Override
public MutableAlignment updateAligmment(Alignment originalAlignment, AlignmentMigration migration, MigrationOptions options, SimpleLog log) {
    MutableAlignment result = new DefaultAlignment(originalAlignment);
    // XXX TODO adapt custom functions?!
    // result.getCustomPropertyFunctions();
    Collection<? extends Cell> cellList = new ArrayList<>(result.getCells());
    for (Cell cell : cellList) {
        // XXX
        if (cell instanceof MutableCell) {
            CellMigrator cm = getCellMigrator(cell.getTransformationIdentifier());
            MutableCell newCell = cm.updateCell(cell, migration, options, log);
            MigrationUtil.removeIdPrefix(newCell, options.transferBase(), options.transferBase());
            result.removeCell(cell);
            if (newCell != null) {
                result.addCell(newCell);
            }
        } else {
            // XXX can we deal with other cases? (Base alignment cells)
            if (options.transferBase()) {
                // include base alignment cell as mutable mapping cell
                CellMigrator cm = getCellMigrator(cell.getTransformationIdentifier());
                MutableCell newCell = cm.updateCell(cell, migration, options, log);
                MigrationUtil.removeIdPrefix(newCell, true, true);
                result.removeCell(cell);
                if (newCell != null) {
                    result.addCell(newCell);
                }
            }
        }
    }
    if (options.transferBase()) {
        MigrationUtil.removeBaseCells(result);
    } else {
    // does something need to be done to correctly retain base
    // alignments?
    }
    return result;
}
Also used : MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) ArrayList(java.util.ArrayList) MutableAlignment(eu.esdihumboldt.hale.common.align.model.MutableAlignment) DefaultAlignment(eu.esdihumboldt.hale.common.align.model.impl.DefaultAlignment) CellMigrator(eu.esdihumboldt.hale.common.align.migrate.CellMigrator) Cell(eu.esdihumboldt.hale.common.align.model.Cell) MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell)

Example 99 with Cell

use of eu.esdihumboldt.hale.common.align.model.Cell in project hale by halestudio.

the class EffectiveMapping method expand.

/**
 * Convert an alignment into an expanded version where all effective
 * mappings are defined explicitly, i.e. mappings effective due to
 * inheritance are converted to use the respective sub-types. Also, base
 * alignment cells are converted into normal cells.
 *
 * @param alignment the alignment to convert
 * @return the expanded copy of the alignment
 */
public static MutableAlignment expand(Alignment alignment) {
    MutableAlignment result = new DefaultAlignment(alignment);
    // remove base alignment cells keeping custom functions
    MigrationUtil.removeBaseCells(result);
    // remove other cells
    result.clearCells();
    // transfer cells based on effective mapping
    // set of all cells used as they are in the resulting alignment
    Set<Cell> usedAsIs = new HashSet<>();
    for (Cell typeCell : alignment.getTypeCells()) {
        // transfer type cell unchanged
        MutableCell typeCellNew = new DefaultCell(typeCell);
        MigrationUtil.removeIdPrefix(typeCellNew, true, true);
        result.addCell(typeCellNew);
        usedAsIs.add(typeCell);
        Collection<? extends Cell> propertyCells = alignment.getPropertyCells(typeCell, true, false);
        for (Cell propertyCell : propertyCells) {
            // FIXME what does this do in case of a join where there are
            // potentially multiple cells to be handled?
            Cell reparented = AlignmentUtil.reparentCell(propertyCell, typeCell, true);
            if (reparented == propertyCell) {
                // use as is
                if (!usedAsIs.contains(propertyCell)) {
                    // only add if not done yet
                    // transfer unchanged
                    MutableCell newCell = new DefaultCell(propertyCell);
                    MigrationUtil.removeIdPrefix(newCell, true, true);
                    result.addCell(newCell);
                    usedAsIs.add(propertyCell);
                }
            } else {
                // inherited cell
                // add the reparented cell
                // TODO check if similar cell has been added already?
                MutableCell rCell = (MutableCell) reparented;
                MigrationUtil.removeIdPrefix(rCell, true, true);
                // avoid ID collision
                // no updates needed in other places because it's a property
                // cell
                rCell.setId(rCell.getId() + '_' + typeCellNew.getId());
                result.addCell(rCell);
            }
        }
    }
    return result;
}
Also used : MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) DefaultCell(eu.esdihumboldt.hale.common.align.model.impl.DefaultCell) MutableAlignment(eu.esdihumboldt.hale.common.align.model.MutableAlignment) DefaultAlignment(eu.esdihumboldt.hale.common.align.model.impl.DefaultAlignment) Cell(eu.esdihumboldt.hale.common.align.model.Cell) DefaultCell(eu.esdihumboldt.hale.common.align.model.impl.DefaultCell) MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) HashSet(java.util.HashSet)

Example 100 with Cell

use of eu.esdihumboldt.hale.common.align.model.Cell in project hale by halestudio.

the class FormattedStringExplanation method getExplanation.

@Override
protected String getExplanation(Cell cell, boolean html, ServiceProvider services, Locale locale) {
    Entity target = CellUtil.getFirstEntity(cell.getTarget());
    String pattern = CellUtil.getFirstParameter(cell, PARAMETER_PATTERN).as(String.class);
    List<? extends Entity> sources = null;
    if (cell.getSource() != null) {
        sources = cell.getSource().get(ENTITY_VARIABLE);
    }
    if (target != null && pattern != null) {
        if (html)
            pattern = "<pre>" + pattern + "</pre>";
        String explanation = MessageFormat.format(getMessage("main", locale), formatEntity(target, html, true, locale), pattern);
        if (html)
            explanation = explanation.replaceAll("\n", "<br />");
        if (sources != null && html) {
            Map<String, String> varToProperty = sources.stream().collect(Collectors.toMap(entity -> {
                return '{' + getEntityNameWithoutCondition(entity) + '}';
            }, entity -> {
                return formatEntity(entity, true, false, locale);
            }));
            explanation += buildReplacementTable(varToProperty, locale);
        }
        return explanation;
    }
    return null;
}
Also used : List(java.util.List) Cell(eu.esdihumboldt.hale.common.align.model.Cell) Locale(java.util.Locale) Map(java.util.Map) ServiceProvider(eu.esdihumboldt.hale.common.core.service.ServiceProvider) Entity(eu.esdihumboldt.hale.common.align.model.Entity) FormattedStringFunction(eu.esdihumboldt.hale.common.align.model.functions.FormattedStringFunction) AbstractCellExplanation(eu.esdihumboldt.hale.common.align.model.impl.AbstractCellExplanation) Collectors(java.util.stream.Collectors) CellUtil(eu.esdihumboldt.hale.common.align.model.CellUtil) MessageFormat(java.text.MessageFormat) Entity(eu.esdihumboldt.hale.common.align.model.Entity)

Aggregations

Cell (eu.esdihumboldt.hale.common.align.model.Cell)123 ArrayList (java.util.ArrayList)33 MutableCell (eu.esdihumboldt.hale.common.align.model.MutableCell)28 ParameterValue (eu.esdihumboldt.hale.common.align.model.ParameterValue)28 Test (org.junit.Test)27 Entity (eu.esdihumboldt.hale.common.align.model.Entity)24 DefaultCell (eu.esdihumboldt.hale.common.align.model.impl.DefaultCell)24 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)18 BaseAlignmentCell (eu.esdihumboldt.hale.common.align.model.BaseAlignmentCell)16 TypeEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition)16 AlignmentService (eu.esdihumboldt.hale.ui.service.align.AlignmentService)16 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)15 Alignment (eu.esdihumboldt.hale.common.align.model.Alignment)13 HashSet (java.util.HashSet)13 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)12 Type (eu.esdihumboldt.hale.common.align.model.Type)11 List (java.util.List)11 ModifiableCell (eu.esdihumboldt.hale.common.align.model.ModifiableCell)9 PropertyEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition)9 Property (eu.esdihumboldt.hale.common.align.model.Property)8