Search in sources :

Example 1 with ProgressMonitorIndicator

use of eu.esdihumboldt.hale.common.core.io.ProgressMonitorIndicator in project hale by halestudio.

the class OrientInstanceService method performTransformation.

/**
 * Perform the transformation
 *
 * @return if the transformation was successful
 */
protected boolean performTransformation() {
    final TransformationService ts = getTransformationService();
    if (ts == null) {
        log.userError("No transformation service available");
        return false;
    }
    final AtomicBoolean transformationFinished = new AtomicBoolean(false);
    final AtomicBoolean transformationCanceled = new AtomicBoolean(false);
    IRunnableWithProgress op = new IRunnableWithProgress() {

        @Override
        public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
            try {
                Alignment alignment = getAlignmentService().getAlignment();
                if (alignment.getActiveTypeCells().isEmpty()) {
                    // early exit if there are no type relations
                    return;
                }
                // determine if there are any active type cells w/o source
                boolean transformEmpty = false;
                for (Cell cell : alignment.getActiveTypeCells()) {
                    if (cell.getSource() == null || cell.getSource().isEmpty()) {
                        transformEmpty = true;
                        break;
                    }
                }
                InstanceCollection sources = getInstances(DataSet.SOURCE);
                if (!transformEmpty && sources.isEmpty()) {
                    return;
                }
                HaleOrientInstanceSink sink = new HaleOrientInstanceSink(transformed, true);
                TransformationReport report;
                ATransaction trans = log.begin("Instance transformation");
                try {
                    report = ts.transform(alignment, sources, sink, HaleUI.getServiceProvider(), new ProgressMonitorIndicator(monitor));
                    // publish report
                    ReportService rs = PlatformUI.getWorkbench().getService(ReportService.class);
                    rs.addReport(report);
                } finally {
                    try {
                        sink.close();
                    } catch (IOException e) {
                    // ignore
                    }
                    trans.end();
                }
            } finally {
                // remember if canceled
                if (monitor.isCanceled()) {
                    transformationCanceled.set(true);
                }
                // transformation finished
                transformationFinished.set(true);
            }
        }
    };
    try {
        ThreadProgressMonitor.runWithProgressDialog(op, ts.isCancelable());
    } catch (Throwable e) {
        log.error("Error starting transformation process", e);
    }
    // wait for transformation to complete
    HaleUI.waitFor(transformationFinished);
    return !transformationCanceled.get();
}
Also used : TransformationReport(eu.esdihumboldt.hale.common.align.transformation.report.TransformationReport) ProgressMonitorIndicator(eu.esdihumboldt.hale.common.core.io.ProgressMonitorIndicator) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) BrowseOrientInstanceCollection(eu.esdihumboldt.hale.common.instance.orient.storage.BrowseOrientInstanceCollection) FilteredInstanceCollection(eu.esdihumboldt.hale.common.instance.model.impl.FilteredInstanceCollection) IOException(java.io.IOException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Alignment(eu.esdihumboldt.hale.common.align.model.Alignment) ReportService(eu.esdihumboldt.hale.ui.service.report.ReportService) ATransaction(de.fhg.igd.slf4jplus.ATransaction) TransformationService(eu.esdihumboldt.hale.common.align.transformation.service.TransformationService) Cell(eu.esdihumboldt.hale.common.align.model.Cell)

Example 2 with ProgressMonitorIndicator

use of eu.esdihumboldt.hale.common.core.io.ProgressMonitorIndicator in project hale by halestudio.

the class ExportJob method run.

/**
 * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
 */
@Override
protected IStatus run(IProgressMonitor monitor) {
    IOReporter defaultReporter = writer.createReporter();
    defaultReporter.setSuccess(false);
    IOReport report = defaultReporter;
    try {
        ATransaction trans = log.begin(defaultReporter.getTaskName());
        try {
            IOReport result = writer.execute(new ProgressMonitorIndicator(monitor));
            if (result != null) {
                report = result;
            } else {
                defaultReporter.setSuccess(true);
            }
        } catch (Throwable e) {
            defaultReporter.error(new IOMessageImpl(e.getLocalizedMessage(), e));
        } finally {
            trans.end();
        }
    } catch (Throwable e) {
        defaultReporter.error(new IOMessageImpl(e.getLocalizedMessage(), e));
    }
    if (monitor.isCanceled()) {
        reset();
        return Status.CANCEL_STATUS;
    }
    // add report to report service
    reportHandler.publishReport(report);
    // show message to user
    if (report.isSuccess()) {
        // no message, we rely on the report being
        // shown/processed
        // let advisor handle results
        advisor.handleResults(writer);
        reset();
        return Status.OK_STATUS;
    } else {
        reset();
        log.error(report.getSummary());
        return new Status(Status.ERROR, "eu.esdihumboldt.hale.common.headless", report.getSummary(), null);
    }
}
Also used : Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) IOReporter(eu.esdihumboldt.hale.common.core.io.report.IOReporter) ProgressMonitorIndicator(eu.esdihumboldt.hale.common.core.io.ProgressMonitorIndicator) ATransaction(de.fhg.igd.slf4jplus.ATransaction) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport)

Example 3 with ProgressMonitorIndicator

use of eu.esdihumboldt.hale.common.core.io.ProgressMonitorIndicator in project hale by halestudio.

the class Transformation method transform.

/**
 * Transform the given instances, according to the given alignment.
 *
 * @param sources the collection of source instances
 * @param targetSink the target sink
 * @param exportJob the export job
 * @param validationJob the validation job, may be <code>null</code>
 * @param alignment the alignment, may not be changed outside this method
 * @param sourceSchema the source schema
 * @param reportHandler the report handler
 * @param serviceProvider the service provider in the transformation context
 * @param processId the identifier for the transformation process, may be
 *            <code>null</code> if grouping the jobs to a job family is not
 *            necessary
 * @return the future representing the successful completion of the
 *         transformation (note that a successful completion doesn't
 *         necessary mean there weren't any internal transformation errors)
 */
public static ListenableFuture<Boolean> transform(InstanceCollection sources, final TransformationSink targetSink, final ExportJob exportJob, final ValidationJob validationJob, final Alignment alignment, SchemaSpace sourceSchema, final ReportHandler reportHandler, final ServiceProvider serviceProvider, final Object processId) {
    final SettableFuture<Boolean> result = SettableFuture.create();
    final InstanceCollection sourceToUse;
    // Check whether to create a temporary database or not.
    // Currently do not create a temporary DB is there are Retypes/Creates
    // only.
    boolean useTempDatabase = false;
    final LocalOrientDB db;
    for (Cell cell : alignment.getActiveTypeCells()) if (!isStreamingTypeTransformation(cell.getTransformationIdentifier())) {
        useTempDatabase = true;
        break;
    }
    // Create temporary database if necessary.
    if (useTempDatabase) {
        // create db
        File tmpDir = Files.createTempDir();
        db = new LocalOrientDB(tmpDir);
        tmpDir.deleteOnExit();
        // get instance collection
        // sourceToUse = new BrowseOrientInstanceCollection(db, sourceSchema, DataSet.SOURCE);
        // only yield instances that were actually inserted
        // this is also done in OrientInstanceService
        // TODO make configurable?
        sourceToUse = FilteredInstanceCollection.applyFilter(new BrowseOrientInstanceCollection(db, sourceSchema, DataSet.SOURCE), new Filter() {

            @Override
            public boolean match(Instance instance) {
                if (instance instanceof OInstance) {
                    return ((OInstance) instance).isInserted();
                }
                return true;
            }
        });
    } else {
        sourceToUse = new StatsCountInstanceCollection(sources, reportHandler);
        db = null;
    }
    // create transformation job
    final AbstractTransformationJob transformJob = new AbstractTransformationJob("Transformation") {

        /**
         * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
         */
        @Override
        protected IStatus run(IProgressMonitor monitor) {
            TransformationService transformationService = HalePlatform.getService(TransformationService.class);
            TransformationReport report = transformationService.transform(alignment, sourceToUse, targetSink, serviceProvider, new ProgressMonitorIndicator(monitor));
            try {
                // publish report
                reportHandler.publishReport(report);
                if (report.isSuccess()) {
                    return Status.OK_STATUS;
                } else {
                    return ERROR_STATUS;
                }
            } finally {
                // and may lead to the transformation report being lost
                if (monitor.isCanceled()) {
                    targetSink.done(true);
                    return Status.CANCEL_STATUS;
                } else {
                    targetSink.done(false);
                }
            }
        }
    };
    // set process IDs to group jobs in a job family
    if (processId != null) {
        transformJob.setProcessId(processId);
        exportJob.setProcessId(processId);
        if (validationJob != null) {
            validationJob.setProcessId(processId);
        }
    }
    exportJob.setUser(true);
    // the jobs should cancel each other
    transformJob.addJobChangeListener(new JobChangeAdapter() {

        @Override
        public void done(IJobChangeEvent event) {
            if (!event.getResult().isOK()) {
                // log transformation job error (because it otherwise gets
                // lost)
                String msg = "Error during transformation";
                if (event.getResult().getMessage() != null) {
                    msg = ": " + event.getResult().getMessage();
                }
                log.error(msg, event.getResult().getException());
                // failing transformation is done by cancelling the export
                exportJob.cancel();
            }
            if (db != null) {
                db.delete();
            }
        }
    });
    // after export is done, validation should run
    exportJob.addJobChangeListener(new JobChangeAdapter() {

        @Override
        public void done(IJobChangeEvent event) {
            if (!event.getResult().isOK()) {
                transformJob.cancel();
                // failure
                failure(result, event);
            } else {
                if (validationJob == null) {
                    // success
                    result.set(true);
                } else {
                    // schedule the validation job
                    validationJob.schedule();
                }
            }
        }
    });
    // validation ends the process
    if (validationJob != null) {
        validationJob.addJobChangeListener(new JobChangeAdapter() {

            @Override
            public void done(IJobChangeEvent event) {
                if (!event.getResult().isOK()) {
                    // failure
                    failure(result, event);
                } else {
                    // success
                    result.set(true);
                }
            }
        });
    }
    if (useTempDatabase) {
        // Initialize instance index with alignment
        InstanceIndexService indexService = serviceProvider.getService(InstanceIndexService.class);
        indexService.addPropertyMappings(alignment.getActiveTypeCells(), serviceProvider);
        // run store instance job first...
        Job storeJob = new StoreInstancesJob("Load source instances into temporary database", db, sources, serviceProvider, reportHandler, true) {

            @Override
            protected void onComplete() {
            // onComplete is also called if monitor is cancelled...
            }

            @Override
            public boolean belongsTo(Object family) {
                if (processId == null) {
                    return super.belongsTo(family);
                }
                return AbstractTransformationJob.createFamily(processId).equals(family);
            }
        };
        // and schedule jobs on successful completion
        storeJob.addJobChangeListener(new JobChangeAdapter() {

            @Override
            public void done(IJobChangeEvent event) {
                if (event.getResult().isOK()) {
                    exportJob.schedule();
                    transformJob.schedule();
                } else {
                    failure(result, event);
                }
            }
        });
        storeJob.schedule();
    } else {
        // otherwise feed InstanceProcessors directly from the
        // InstanceCollection...
        // TODO Implement differently, not w/ PseudoInstanceReference which
        // will cause memory problems
        // final InstanceProcessingExtension ext = new InstanceProcessingExtension(
        // serviceProvider);
        // final List<InstanceProcessor> processors = ext.getInstanceProcessors();
        // 
        // ResourceIterator<Instance> it = sourceToUse.iterator();
        // try {
        // while (it.hasNext()) {
        // Instance instance = it.next();
        // 
        // ResolvableInstanceReference resolvableRef = new ResolvableInstanceReference(
        // new PseudoInstanceReference(instance), sourceToUse);
        // processors.forEach(p -> p.process(instance, resolvableRef));
        // 
        // }
        // } finally {
        // it.close();
        // }
        // ...and schedule jobs
        exportJob.schedule();
        transformJob.schedule();
    }
    return result;
}
Also used : OInstance(eu.esdihumboldt.hale.common.instance.orient.OInstance) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) OInstance(eu.esdihumboldt.hale.common.instance.orient.OInstance) TransformationService(eu.esdihumboldt.hale.common.align.transformation.service.TransformationService) StoreInstancesJob(eu.esdihumboldt.hale.common.instance.orient.storage.StoreInstancesJob) Job(org.eclipse.core.runtime.jobs.Job) Cell(eu.esdihumboldt.hale.common.align.model.Cell) InstanceIndexService(eu.esdihumboldt.hale.common.instance.index.InstanceIndexService) TransformationReport(eu.esdihumboldt.hale.common.align.transformation.report.TransformationReport) ProgressMonitorIndicator(eu.esdihumboldt.hale.common.core.io.ProgressMonitorIndicator) StoreInstancesJob(eu.esdihumboldt.hale.common.instance.orient.storage.StoreInstancesJob) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) BrowseOrientInstanceCollection(eu.esdihumboldt.hale.common.instance.orient.storage.BrowseOrientInstanceCollection) MultiInstanceCollection(eu.esdihumboldt.hale.common.instance.model.impl.MultiInstanceCollection) FilteredInstanceCollection(eu.esdihumboldt.hale.common.instance.model.impl.FilteredInstanceCollection) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent) BrowseOrientInstanceCollection(eu.esdihumboldt.hale.common.instance.orient.storage.BrowseOrientInstanceCollection) LocalOrientDB(eu.esdihumboldt.hale.common.instance.orient.storage.LocalOrientDB) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Filter(eu.esdihumboldt.hale.common.instance.model.Filter) File(java.io.File)

Example 4 with ProgressMonitorIndicator

use of eu.esdihumboldt.hale.common.core.io.ProgressMonitorIndicator in project hale by halestudio.

the class ValidationJob method run.

/**
 * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
 */
@Override
protected IStatus run(IProgressMonitor monitor) {
    boolean successful = true;
    for (InstanceValidator validator : this.validators) {
        IOReporter defaultReporter = validator.createReporter();
        defaultReporter.setSuccess(false);
        IOReport report = defaultReporter;
        try {
            ATransaction trans = log.begin(defaultReporter.getTaskName());
            try {
                if (writer != null) {
                    // set validation schemas (may have been determined only
                    // during writer execution)
                    // set schemas
                    List<? extends Locatable> schemas = writer.getValidationSchemas();
                    validator.setSchemas(schemas.toArray(new Locatable[schemas.size()]));
                }
                validator.setServiceProvider(serviceProvider);
                IOReport result = validator.execute(new ProgressMonitorIndicator(monitor));
                if (result != null) {
                    report = result;
                } else {
                    defaultReporter.setSuccess(true);
                }
            } catch (Throwable e) {
                defaultReporter.error(new IOMessageImpl(e.getLocalizedMessage(), e));
            } finally {
                trans.end();
            }
        } catch (Throwable e) {
            defaultReporter.error(new IOMessageImpl(e.getLocalizedMessage(), e));
        }
        if (monitor.isCanceled()) {
            reset();
            return Status.CANCEL_STATUS;
        }
        // add report to report service
        reportHandler.publishReport(report);
        // show message to user
        if (report.isSuccess()) {
            // info message
            log.info(report.getSummary());
        } else {
            // error message
            log.error(report.getSummary());
            successful = false;
        }
    }
    reset();
    if (successful) {
        log.userInfo("All validations completed successfully.");
        return Status.OK_STATUS;
    } else {
        log.userError("There were validation failures. Please check the reports for details.");
        return ERROR_STATUS;
    }
}
Also used : InstanceValidator(eu.esdihumboldt.hale.common.instance.io.InstanceValidator) IOReporter(eu.esdihumboldt.hale.common.core.io.report.IOReporter) ProgressMonitorIndicator(eu.esdihumboldt.hale.common.core.io.ProgressMonitorIndicator) ATransaction(de.fhg.igd.slf4jplus.ATransaction) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) Locatable(eu.esdihumboldt.hale.common.core.io.supplier.Locatable)

Example 5 with ProgressMonitorIndicator

use of eu.esdihumboldt.hale.common.core.io.ProgressMonitorIndicator in project hale by halestudio.

the class IOWizard method execute.

/**
 * Execute the given provider
 *
 * @param provider the I/O provider
 * @param defaultReporter the default reporter that is used if the provider
 *            doesn't supply a report
 * @return the execution report, if null it will not give feedback to the
 *         user and the advisor's handleResult method won't be called either
 */
protected IOReport execute(final IOProvider provider, final IOReporter defaultReporter) {
    // execute provider
    final AtomicReference<IOReport> report = new AtomicReference<IOReport>(defaultReporter);
    defaultReporter.setSuccess(false);
    try {
        getContainer().run(true, provider.isCancelable(), new IRunnableWithProgress() {

            @Override
            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                ATransaction trans = log.begin(defaultReporter.getTaskName());
                try {
                    IOReport result = provider.execute(new ProgressMonitorIndicator(monitor));
                    if (result != null) {
                        report.set(result);
                    } else {
                        defaultReporter.setSuccess(true);
                    }
                } catch (Throwable e) {
                    defaultReporter.error(new IOMessageImpl(e.getLocalizedMessage(), e));
                } finally {
                    trans.end();
                }
            }
        });
    } catch (Throwable e) {
        defaultReporter.error(new IOMessageImpl(e.getLocalizedMessage(), e));
    }
    return report.get();
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ProgressMonitorIndicator(eu.esdihumboldt.hale.common.core.io.ProgressMonitorIndicator) ATransaction(de.fhg.igd.slf4jplus.ATransaction) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) AtomicReference(java.util.concurrent.atomic.AtomicReference) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress)

Aggregations

ProgressMonitorIndicator (eu.esdihumboldt.hale.common.core.io.ProgressMonitorIndicator)6 ATransaction (de.fhg.igd.slf4jplus.ATransaction)5 IOReport (eu.esdihumboldt.hale.common.core.io.report.IOReport)4 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)4 IOReporter (eu.esdihumboldt.hale.common.core.io.report.IOReporter)3 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)3 IRunnableWithProgress (org.eclipse.jface.operation.IRunnableWithProgress)3 Cell (eu.esdihumboldt.hale.common.align.model.Cell)2 TransformationReport (eu.esdihumboldt.hale.common.align.transformation.report.TransformationReport)2 TransformationService (eu.esdihumboldt.hale.common.align.transformation.service.TransformationService)2 InstanceCollection (eu.esdihumboldt.hale.common.instance.model.InstanceCollection)2 FilteredInstanceCollection (eu.esdihumboldt.hale.common.instance.model.impl.FilteredInstanceCollection)2 BrowseOrientInstanceCollection (eu.esdihumboldt.hale.common.instance.orient.storage.BrowseOrientInstanceCollection)2 ReportService (eu.esdihumboldt.hale.ui.service.report.ReportService)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Alignment (eu.esdihumboldt.hale.common.align.model.Alignment)1 CachingImportProvider (eu.esdihumboldt.hale.common.core.io.CachingImportProvider)1 Value (eu.esdihumboldt.hale.common.core.io.Value)1 Locatable (eu.esdihumboldt.hale.common.core.io.supplier.Locatable)1 InstanceIndexService (eu.esdihumboldt.hale.common.instance.index.InstanceIndexService)1