Search in sources :

Example 1 with Filter

use of eu.esdihumboldt.hale.common.instance.model.Filter 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 2 with Filter

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

the class FilterTest method testLoadShiporderCQL.

/**
 * Test loading a simple XML file with one instance
 *
 * @throws Exception if an error occurs
 */
@Ignore
// element)
@Test
public void testLoadShiporderCQL() throws Exception {
    InstanceCollection instances = loadXMLInstances(getClass().getResource("/testdata/shiporder/shiporder.xsd").toURI(), getClass().getResource("/testdata/shiporder/shiporder.xml").toURI());
    ResourceIterator<Instance> it = instances.iterator();
    try {
        assertTrue(it.hasNext());
        boolean foundIt = false;
        boolean stayFalse = false;
        boolean stayFalseToo = false;
        boolean foundIt2 = false;
        Filter cqlfilter = new FilterGeoCqlImpl("shipto.city = '4000 Stavanger'");
        Filter foulfilter = new FilterGeoCqlImpl("HERP = 'DERP'");
        Filter foulfilter1 = new FilterGeoCqlImpl("shipto.city = 'HURR'");
        Filter cqlfilter2 = new FilterGeoCqlImpl("\"{http://www.example.com}shipto.{http://www.example.com}city\" = '4000 Stavanger'");
        while (it.hasNext()) {
            Instance instance = it.next();
            assertNotNull(instance);
            if (cqlfilter.match(instance)) {
                foundIt = true;
            }
            if (foulfilter.match(instance)) {
                stayFalse = true;
            }
            if (foulfilter1.match(instance)) {
                stayFalseToo = true;
            }
            if (cqlfilter2.match(instance)) {
                foundIt2 = true;
            }
        }
        assertTrue(foundIt);
        assertTrue(foundIt2);
        assertFalse(stayFalse);
        assertFalse(stayFalseToo);
    } finally {
        it.close();
    }
}
Also used : MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) DefaultInstance(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance) Filter(eu.esdihumboldt.hale.common.instance.model.Filter) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with Filter

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

the class FilterTest method simpleFilterTestCQL.

@Test
public void simpleFilterTestCQL() 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 FilterGeoCqlImpl("Name = 'Mein Porsche'");
    assertTrue(filter.match(auto));
    Filter filter1 = new FilterGeoCqlImpl("Name like 'Porsche'");
    assertFalse(filter1.match(auto));
    Filter filter2 = new FilterGeoCqlImpl("Name like '%Porsche'");
    assertTrue(filter2.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 4 with Filter

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

the class FilterTest method simpleSchemaTestCQL.

@Test
public void simpleSchemaTestCQL() throws Exception {
    ShapeSchemaReader schemaReader = new ShapeSchemaReader();
    schemaReader.setSource(new DefaultInputSupplier(getClass().getResource("/testdata/GN_Point/GN_Point.shp").toURI()));
    schemaReader.validate();
    IOReport report = schemaReader.execute(null);
    assertTrue(report.isSuccess());
    Schema schema = schemaReader.getSchema();
    ShapeInstanceReader instanceReader = new ShapeInstanceReader();
    instanceReader.setSource(new DefaultInputSupplier(getClass().getResource("/testdata/GN_Point/GN_Point.shp").toURI()));
    instanceReader.setSourceSchema(schema);
    instanceReader.validate();
    report = instanceReader.execute(null);
    assertTrue(report.isSuccess());
    InstanceCollection instances = instanceReader.getInstances();
    assertFalse(instances.isEmpty());
    ResourceIterator<Instance> ri = instances.iterator();
    try {
        boolean foundIt = false;
        boolean stayFalse = false;
        boolean stayFalseToo = false;
        Filter cqlfilter = new FilterGeoCqlImpl("NEV = 'Piritulus'");
        Filter foulfilter = new FilterGeoCqlImpl("HERP = 'DERP'");
        Filter foulfilter1 = new FilterGeoCqlImpl("NEV = 'HURR'");
        while (ri.hasNext()) {
            Instance inst = ri.next();
            assertNotNull(inst);
            if (cqlfilter.match(inst)) {
                foundIt = true;
            }
            if (foulfilter.match(inst)) {
                stayFalse = true;
            }
            if (foulfilter1.match(inst)) {
                stayFalseToo = true;
            }
        }
        assertTrue(foundIt);
        assertFalse(stayFalse);
        assertFalse(stayFalseToo);
    } finally {
        ri.close();
    }
}
Also used : ShapeInstanceReader(eu.esdihumboldt.hale.io.shp.reader.internal.ShapeInstanceReader) DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) DefaultInstance(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance) Filter(eu.esdihumboldt.hale.common.instance.model.Filter) Schema(eu.esdihumboldt.hale.common.schema.model.Schema) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) ShapeSchemaReader(eu.esdihumboldt.hale.io.shp.reader.internal.ShapeSchemaReader) Test(org.junit.Test)

Example 5 with Filter

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

the class DefaultAlignment method matchesSources.

/**
 * Determines if the given type entity definition of the test cell is
 * associated to at least one of the given type entity definitions of a type
 * cell.
 *
 * @param testCellType type entity definition of the test cell
 * @param typeCellTypes type entity definitions of a type cell
 * @return whether the entity definition is associated to at least one of
 *         the others
 */
private boolean matchesSources(TypeEntityDefinition testCellType, Iterable<TypeEntityDefinition> typeCellTypes) {
    TypeDefinition def = testCellType.getDefinition();
    Filter filter = testCellType.getFilter();
    for (TypeEntityDefinition typeCellType : typeCellTypes) {
        if (DefinitionUtil.isSuperType(typeCellType.getDefinition(), def) && (filter == null || filter.equals(typeCellType.getFilter())))
            return true;
    }
    return false;
}
Also used : Filter(eu.esdihumboldt.hale.common.instance.model.Filter) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Aggregations

Filter (eu.esdihumboldt.hale.common.instance.model.Filter)22 Test (org.junit.Test)9 MutableInstance (eu.esdihumboldt.hale.common.instance.model.MutableInstance)8 DefaultInstance (eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance)8 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)8 TypeEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition)7 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)7 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)5 InstanceCollection (eu.esdihumboldt.hale.common.instance.model.InstanceCollection)5 DefaultTypeDefinition (eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition)5 QName (javax.xml.namespace.QName)5 ChildContext (eu.esdihumboldt.hale.common.align.model.ChildContext)4 Condition (eu.esdihumboldt.hale.common.align.model.Condition)4 DefaultPropertyDefinition (eu.esdihumboldt.hale.common.schema.model.impl.DefaultPropertyDefinition)3 Cell (eu.esdihumboldt.hale.common.align.model.Cell)2 MutableCell (eu.esdihumboldt.hale.common.align.model.MutableCell)2 ParameterValue (eu.esdihumboldt.hale.common.align.model.ParameterValue)2 Type (eu.esdihumboldt.hale.common.align.model.Type)2 DefaultCell (eu.esdihumboldt.hale.common.align.model.impl.DefaultCell)2 DefaultType (eu.esdihumboldt.hale.common.align.model.impl.DefaultType)2