Search in sources :

Example 1 with ProcessorsChain

use of net.idea.modbcum.i.processors.ProcessorsChain in project ambit-mirror by ideaconsult.

the class CallableFileImport method importFile.

public TaskResult importFile(File file) throws Exception {
    try {
        // if target dataset is not defined, create new dataset
        final SourceDataset dataset = targetDataset != null ? targetDataset : datasetMeta(file);
        if (targetDataset == null)
            dataset.setId(-1);
        final BatchDBProcessor<String> batch = new BatchDBProcessor<String>() {

            /**
             */
            private static final long serialVersionUID = -7971761364143510120L;

            @Override
            public Iterator<String> getIterator(IInputState target) throws AmbitException {
                try {
                    File file = ((FileInputState) target).getFile();
                    RDFIteratingReader i = getRDFIterator(file, getReporter().getBaseReference().toString());
                    if (i == null) {
                        IIteratingChemObjectReader ni = getNanoCMLIterator(file, getReporter().getBaseReference().toString());
                        if (ni == null)
                            return super.getIterator(target);
                        else
                            return ni;
                    } else {
                        /*
			     * RDFMetaDatasetIterator datasets = null; try {
			     * datasets = new
			     * RDFMetaDatasetIterator(i.getJenaModel());
			     * datasets
			     * .setBaseReference(getReporter().getBaseReference
			     * ()); while (datasets.hasNext()) { SourceDataset d
			     * = datasets.next(); dataset.setId(d.getId());
			     * dataset.setName(d.getName());
			     * dataset.setTitle(d.getTitle());
			     * dataset.setURL(d.getURL()); } } catch (Exception
			     * x) { x.printStackTrace(); } finally { try {
			     * datasets.close();} catch (Exception x) {} }
			     */
                        return i;
                    }
                } catch (AmbitException x) {
                    throw x;
                } catch (Exception x) {
                    throw new AmbitException(x);
                }
            }

            @Override
            public void onItemProcessed(String input, Object output, IBatchStatistics stats) {
                super.onItemProcessed(input, output, stats);
                if (firstCompoundOnly && (stats.getRecords(RECORDS_STATS.RECORDS_PROCESSED) >= 1)) {
                    cancelled = true;
                    if (output != null)
                        if ((output instanceof ArrayList) && ((ArrayList) output).size() > 0) {
                            if (((ArrayList) output).get(0) instanceof IStructureRecord)
                                recordImported = (IStructureRecord) ((ArrayList) output).get(0);
                        } else if (output instanceof IStructureRecord)
                            recordImported = (IStructureRecord) output;
                }
            }
        };
        batch.setReference(dataset.getReference());
        batch.setConnection(connection);
        final RepositoryWriter writer = new RepositoryWriter();
        writer.setUseExistingStructure(isPropertyOnly());
        writer.setPropertyKey(getMatcher());
        writer.setDataset(dataset);
        final ProcessorsChain<String, IBatchStatistics, IProcessor> chain = new ProcessorsChain<String, IBatchStatistics, IProcessor>();
        chain.add(writer);
        batch.setProcessorChain(chain);
        writer.setConnection(connection);
        FileInputState fin = new FileInputState(file);
        IBatchStatistics stats = batch.process(fin);
        if (firstCompoundOnly) {
            if (recordImported == null)
                throw new Exception("No compound imported");
            if (compoundReporter == null)
                compoundReporter = new ConformerURIReporter("", null, false);
            try {
                batch.close();
            } catch (Exception xx) {
            }
            return new TaskResult(compoundReporter.getURI(recordImported));
        } else {
            ReadDataset q = new ReadDataset();
            q.setValue(dataset);
            QueryExecutor<ReadDataset> x = new QueryExecutor<ReadDataset>();
            x.setConnection(connection);
            ResultSet rs = x.process(q);
            ISourceDataset newDataset = null;
            while (rs.next()) {
                newDataset = q.getObject(rs);
                break;
            }
            x.closeResults(rs);
            x.setConnection(null);
            if (newDataset == null)
                throw new ResourceException(Status.SUCCESS_NO_CONTENT);
            if (reporter == null)
                reporter = new DatasetURIReporter<IQueryRetrieval<ISourceDataset>, ISourceDataset>();
            try {
                batch.close();
            } catch (Exception xx) {
            }
            return new TaskResult(reporter.getURI(newDataset));
        }
    } catch (ResourceException x) {
        throw x;
    } catch (Exception x) {
        throw new ResourceException(new Status(Status.SERVER_ERROR_INTERNAL, x.getMessage()));
    } finally {
        try {
            connection.close();
        } catch (Exception x) {
        }
        connection = null;
    }
}
Also used : RDFIteratingReader(ambit2.rest.dataset.RDFIteratingReader) ArrayList(java.util.ArrayList) RepositoryWriter(ambit2.db.processors.RepositoryWriter) IStructureRecord(ambit2.base.interfaces.IStructureRecord) ISourceDataset(ambit2.base.data.ISourceDataset) ProcessorsChain(net.idea.modbcum.i.processors.ProcessorsChain) IInputState(ambit2.core.io.IInputState) QueryExecutor(ambit2.db.search.QueryExecutor) ResultSet(java.sql.ResultSet) ResourceException(org.restlet.resource.ResourceException) Status(org.restlet.data.Status) IIteratingChemObjectReader(org.openscience.cdk.io.iterator.IIteratingChemObjectReader) IBatchStatistics(net.idea.modbcum.i.batch.IBatchStatistics) IProcessor(net.idea.modbcum.i.processors.IProcessor) BatchDBProcessor(ambit2.db.processors.BatchDBProcessor) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) CDKException(org.openscience.cdk.exception.CDKException) ResourceException(org.restlet.resource.ResourceException) ConformerURIReporter(ambit2.rest.structure.ConformerURIReporter) ISourceDataset(ambit2.base.data.ISourceDataset) SourceDataset(ambit2.base.data.SourceDataset) File(java.io.File) FileInputState(ambit2.core.io.FileInputState) ReadDataset(ambit2.db.update.dataset.ReadDataset) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) DatasetURIReporter(ambit2.rest.dataset.DatasetURIReporter)

Example 2 with ProcessorsChain

use of net.idea.modbcum.i.processors.ProcessorsChain in project ambit-mirror by ideaconsult.

the class SubstanceExportResource method createJSONReporter.

@Override
protected IProcessor<Q, Representation> createJSONReporter(String filenamePrefix) {
    String jsonpcallback = getParams().getFirstValue("jsonp");
    if (jsonpcallback == null)
        jsonpcallback = getParams().getFirstValue("callback");
    String command = "results";
    try {
        if (Boolean.parseBoolean(getParams().getFirstValue("array").toString()))
            command = null;
    } catch (Exception x) {
    }
    ProcessorsChain chain = new ProcessorsChain<>();
    chain.add(new SubstanceStudyDetailsProcessor());
    getCompositionProcessors(chain);
    SubstanceRecordAnnotationProcessor annotator = null;
    try {
        annotator = new SubstanceRecordAnnotationProcessor(new File(((AmbitFreeMarkerApplication) getApplication()).getProperties().getMapFolder()), false);
    } catch (Exception x) {
        Logger.getGlobal().log(Level.WARNING, x.getMessage());
        annotator = null;
    }
    return new OutputWriterConvertor<SubstanceRecord, Q>((QueryAbstractReporter<SubstanceRecord, Q, Writer>) new Substance2BucketJsonReporter(command, chain, jsonmode, summaryMeasurement, dbTag, annotator), jsonpcallback == null ? MediaType.APPLICATION_JSON : MediaType.APPLICATION_JAVASCRIPT, filenamePrefix);
}
Also used : ProcessorsChain(net.idea.modbcum.i.processors.ProcessorsChain) OutputWriterConvertor(net.idea.restnet.db.convertors.OutputWriterConvertor) SubstanceStudyDetailsProcessor(ambit2.db.substance.study.SubstanceStudyDetailsProcessor) SubstanceRecordAnnotationProcessor(ambit2.base.ro.SubstanceRecordAnnotationProcessor) SubstanceRecord(ambit2.base.data.SubstanceRecord) AmbitFreeMarkerApplication(ambit2.rest.AmbitFreeMarkerApplication) File(java.io.File) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) ResourceException(org.restlet.resource.ResourceException) Writer(java.io.Writer)

Example 3 with ProcessorsChain

use of net.idea.modbcum.i.processors.ProcessorsChain in project ambit-mirror by ideaconsult.

the class CallableFingerprintsModelCreator method createProcessors.

protected ProcessorsChain<IStructureRecord, IBatchStatistics, IProcessor> createProcessors() throws Exception {
    ProcessorsChain<IStructureRecord, IBatchStatistics, IProcessor> p1 = new ProcessorsChain<IStructureRecord, IBatchStatistics, IProcessor>();
    p1.add(new ProcessorStructureRetrieval());
    p1.add(new MoleculeReader());
    p1.add(new FingerprintGenerator(new Fingerprinter()));
    p1.add(new DefaultAmbitProcessor<BitSet, BitSet>() {

        /**
         */
        private static final long serialVersionUID = -2978697147490545478L;

        public BitSet process(BitSet target) throws AmbitException {
            builder.getTrainingData().add(target);
            return target;
        }
    });
    p1.setAbortOnError(true);
    return p1;
}
Also used : IBatchStatistics(net.idea.modbcum.i.batch.IBatchStatistics) BitSet(java.util.BitSet) IProcessor(net.idea.modbcum.i.processors.IProcessor) FingerprintGenerator(ambit2.core.processors.structure.FingerprintGenerator) IStructureRecord(ambit2.base.interfaces.IStructureRecord) MoleculeReader(ambit2.core.processors.structure.MoleculeReader) Fingerprinter(org.openscience.cdk.fingerprint.Fingerprinter) ProcessorsChain(net.idea.modbcum.i.processors.ProcessorsChain) ProcessorStructureRetrieval(ambit2.db.processors.ProcessorStructureRetrieval) AmbitException(net.idea.modbcum.i.exceptions.AmbitException)

Example 4 with ProcessorsChain

use of net.idea.modbcum.i.processors.ProcessorsChain in project ambit-mirror by ideaconsult.

the class CallableUpdateDataset method createProcessors.

@Override
protected ProcessorsChain<String, IBatchStatistics, IProcessor> createProcessors() throws Exception {
    final RepositoryWriter writer = new RepositoryWriter();
    writer.setDataset(dataset);
    final ProcessorsChain<String, IBatchStatistics, IProcessor> chain = new ProcessorsChain<String, IBatchStatistics, IProcessor>();
    chain.add(writer);
    return chain;
}
Also used : ProcessorsChain(net.idea.modbcum.i.processors.ProcessorsChain) RepositoryWriter(ambit2.db.processors.RepositoryWriter) IBatchStatistics(net.idea.modbcum.i.batch.IBatchStatistics) IProcessor(net.idea.modbcum.i.processors.IProcessor)

Example 5 with ProcessorsChain

use of net.idea.modbcum.i.processors.ProcessorsChain in project ambit-mirror by ideaconsult.

the class DbReaderTest method testProcess.

@Test
public void testProcess() throws Exception {
    setUpDatabaseFromResource("ambit2/db/processors/test/descriptors-datasets.xml");
    IDatabaseConnection c = getConnection();
    ITable names = c.createQueryTable("EXPECTED_NAMES", "SELECT * FROM properties");
    Assert.assertEquals(5, names.getRowCount());
    ITable values = c.createQueryTable("EXPECTED_VALUES", "SELECT * FROM property_values");
    Assert.assertEquals(2, values.getRowCount());
    ProcessorsChain<IStructureRecord, IBatchStatistics, IProcessor> p = new ProcessorsChain<IStructureRecord, IBatchStatistics, IProcessor>();
    p.add(new ProcessorStructureRetrieval());
    p.add(new IProcessor<IStructureRecord, IStructureRecord>() {

        public boolean isEnabled() {
            return true;
        }

        public IStructureRecord process(IStructureRecord target) throws AmbitException {
            // System.out.println(target.getContent());
            return target;
        }

        public void setEnabled(boolean value) {
        }

        public long getID() {
            return 0;
        }

        @Override
        public void open() throws Exception {
        }

        @Override
        public void close() throws Exception {
        }
    });
    p.add(new MoleculeReader());
    p.add(new AtomConfigurator());
    p.add(new FunctionalGroup("Test", "P", "Test"));
    p.add(new IProcessor<VerboseDescriptorResult, String>() {

        /**
         */
        private static final long serialVersionUID = -3923864774580483348L;

        public String process(VerboseDescriptorResult target) throws AmbitException {
            Assert.assertEquals("1", target.getResult().toString());
            return target.toString();
        }

        public boolean isEnabled() {
            return true;
        }

        public void setEnabled(boolean value) {
        }

        public long getID() {
            return 0;
        }

        @Override
        public void open() throws Exception {
        }

        @Override
        public void close() throws Exception {
        }
    });
    batch.setProcessorChain(p);
    QueryStructureByID query = new QueryStructureByID(100215);
    batch.setConnection(c.getConnection());
    batch.open();
    IBatchStatistics stats = batch.process(query);
    Assert.assertEquals(1, stats.getRecords(IBatchStatistics.RECORDS_STATS.RECORDS_READ));
    Assert.assertEquals(1, stats.getRecords(IBatchStatistics.RECORDS_STATS.RECORDS_PROCESSED));
// C20H20BrP
}
Also used : IBatchStatistics(net.idea.modbcum.i.batch.IBatchStatistics) IProcessor(net.idea.modbcum.i.processors.IProcessor) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) QueryStructureByID(ambit2.db.search.structure.QueryStructureByID) IStructureRecord(ambit2.base.interfaces.IStructureRecord) FunctionalGroup(ambit2.descriptors.FunctionalGroup) MoleculeReader(ambit2.core.processors.structure.MoleculeReader) ProcessorsChain(net.idea.modbcum.i.processors.ProcessorsChain) ITable(org.dbunit.dataset.ITable) ProcessorStructureRetrieval(ambit2.db.processors.ProcessorStructureRetrieval) AtomConfigurator(ambit2.core.processors.structure.AtomConfigurator) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) VerboseDescriptorResult(ambit2.descriptors.VerboseDescriptorResult) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) DbUnitTest(ambit2.db.processors.test.DbUnitTest) Test(org.junit.Test)

Aggregations

ProcessorsChain (net.idea.modbcum.i.processors.ProcessorsChain)11 IBatchStatistics (net.idea.modbcum.i.batch.IBatchStatistics)10 IProcessor (net.idea.modbcum.i.processors.IProcessor)10 IStructureRecord (ambit2.base.interfaces.IStructureRecord)8 ProcessorStructureRetrieval (ambit2.db.processors.ProcessorStructureRetrieval)7 AmbitException (net.idea.modbcum.i.exceptions.AmbitException)7 RetrieveStructure (ambit2.db.readers.RetrieveStructure)4 MoleculeReader (ambit2.core.processors.structure.MoleculeReader)3 RepositoryWriter (ambit2.db.processors.RepositoryWriter)3 ResourceException (org.restlet.resource.ResourceException)3 SourceDataset (ambit2.base.data.SourceDataset)2 FileInputState (ambit2.core.io.FileInputState)2 AtomConfigurator (ambit2.core.processors.structure.AtomConfigurator)2 QueryStructureByID (ambit2.db.search.structure.QueryStructureByID)2 FunctionalGroup (ambit2.descriptors.FunctionalGroup)2 VerboseDescriptorResult (ambit2.descriptors.VerboseDescriptorResult)2 File (java.io.File)2 Test (org.junit.Test)2 ISourceDataset (ambit2.base.data.ISourceDataset)1 LiteratureEntry (ambit2.base.data.LiteratureEntry)1