Search in sources :

Example 1 with IProcessor

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

the class QueryResource method getRepresentation.

protected Representation getRepresentation(Variant variant) throws ResourceException {
    try {
        if (MediaType.APPLICATION_JAVA_OBJECT.equals(variant.getMediaType())) {
            if ((queryObject != null) && (queryObject instanceof Serializable))
                return new ObjectRepresentation((Serializable) returnQueryObject(), MediaType.APPLICATION_JAVA_OBJECT);
            else
                throw new ResourceException(Status.CLIENT_ERROR_NOT_ACCEPTABLE);
        }
        if (MediaType.APPLICATION_JAVASCRIPT.equals(variant.getMediaType())) {
            if (!isJSONPEnabled())
                throw new ResourceException(Status.CLIENT_ERROR_UNSUPPORTED_MEDIA_TYPE);
        }
        if (queryObject != null) {
            IProcessor<Q, Representation> convertor = null;
            Connection connection = null;
            int retry = 0;
            while (retry < maxRetry) {
                try {
                    DBConnection dbc = new DBConnection(getContext());
                    configureRDFWriterOption(((AmbitApplication) getApplication()).getProperties().getRDFwriter());
                    configureSDFLineSeparators(((AmbitApplication) getApplication()).getProperties().getConfigChangeLineSeparator());
                    configureDatasetMembersPrefixOption(((AmbitApplication) getApplication()).getProperties().isDatasetMembersPrefix());
                    convertor = createConvertor(variant);
                    if (convertor instanceof RepresentationConvertor)
                        ((RepresentationConvertor) convertor).setLicenseURI(getLicenseURI());
                    connection = dbc.getConnection();
                    Reporter reporter = ((RepresentationConvertor) convertor).getReporter();
                    if (reporter instanceof IDBProcessor)
                        ((IDBProcessor) reporter).setConnection(connection);
                    Representation r = convertor.process(queryObject);
                    r.setCharacterSet(CharacterSet.UTF_8);
                    return r;
                } catch (ResourceException x) {
                    throw x;
                } catch (NotFoundException x) {
                    Representation r = processNotFound(x, retry);
                    retry++;
                    if (r != null)
                        return r;
                } catch (BatchProcessingException x) {
                    if (x.getCause() instanceof NotFoundException) {
                        Representation r = processNotFound((NotFoundException) x.getCause(), retry);
                        retry++;
                        if (r != null)
                            return r;
                    } else {
                        Context.getCurrentLogger().severe(x.getMessage());
                        throw new RResourceException(Status.SERVER_ERROR_INTERNAL, x, variant);
                    }
                } catch (SQLException x) {
                    Representation r = processSQLError(x, retry, variant);
                    retry++;
                    if (r == null)
                        continue;
                    else
                        return r;
                } catch (Exception x) {
                    Context.getCurrentLogger().severe(x.getMessage());
                    throw new RResourceException(Status.SERVER_ERROR_INTERNAL, x, variant);
                } finally {
                // try { if (connection !=null) connection.close(); }
                // catch (Exception x) {};
                // try { if ((convertor !=null) &&
                // (convertor.getReporter() !=null))
                // convertor.getReporter().close(); } catch (Exception
                // x) {}
                }
            }
            return null;
        } else {
            if (variant.getMediaType().equals(MediaType.TEXT_HTML))
                try {
                    IProcessor<Q, Representation> convertor = createConvertor(variant);
                    Representation r = convertor.process(null);
                    return r;
                } catch (Exception x) {
                    throw new RResourceException(Status.CLIENT_ERROR_BAD_REQUEST, x, variant);
                }
            else {
                throw new RResourceException(Status.CLIENT_ERROR_BAD_REQUEST, error, variant);
            }
        }
    } catch (RResourceException x) {
        throw x;
    } catch (ResourceException x) {
        throw new RResourceException(x.getStatus(), x, variant);
    } catch (Exception x) {
        throw new RResourceException(Status.SERVER_ERROR_INTERNAL, x, variant);
    }
}
Also used : DBConnection(ambit2.rest.DBConnection) Serializable(java.io.Serializable) IDBProcessor(net.idea.modbcum.i.IDBProcessor) SQLException(java.sql.SQLException) Reporter(net.idea.modbcum.i.reporter.Reporter) QueryURIReporter(net.idea.restnet.db.QueryURIReporter) Connection(java.sql.Connection) DBConnection(ambit2.rest.DBConnection) AmbitApplication(ambit2.rest.AmbitApplication) NotFoundException(net.idea.modbcum.i.exceptions.NotFoundException) EmptyRepresentation(org.restlet.representation.EmptyRepresentation) ObjectRepresentation(org.restlet.representation.ObjectRepresentation) Representation(org.restlet.representation.Representation) IProcessor(net.idea.modbcum.i.processors.IProcessor) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) RResourceException(ambit2.rest.exception.RResourceException) ResourceException(org.restlet.resource.ResourceException) NotFoundException(net.idea.modbcum.i.exceptions.NotFoundException) BatchProcessingException(net.idea.modbcum.i.exceptions.BatchProcessingException) SQLException(java.sql.SQLException) ProcessorException(ambit2.base.processors.ProcessorException) RepresentationConvertor(net.idea.restnet.c.RepresentationConvertor) BatchProcessingException(net.idea.modbcum.i.exceptions.BatchProcessingException) RResourceException(ambit2.rest.exception.RResourceException) ResourceException(org.restlet.resource.ResourceException) RResourceException(ambit2.rest.exception.RResourceException) ObjectRepresentation(org.restlet.representation.ObjectRepresentation)

Example 2 with IProcessor

use of net.idea.modbcum.i.processors.IProcessor 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 3 with IProcessor

use of net.idea.modbcum.i.processors.IProcessor 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 IProcessor

use of net.idea.modbcum.i.processors.IProcessor 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 IProcessor

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

the class BatchProcessor method process.

/*
	protected Iterator getIterator(IInputState target) throws AmbitException {
		return target.getReader(); 
	}
	
	protected void closeIterator(Iterator iterator) throws AmbitException {
		iterator.close();
	}
	*/
public IBatchStatistics process(INPUT target) throws AmbitException {
    try {
        DefaultBatchStatistics stats = new DefaultBatchStatistics();
        stats.setResultCaption("Read");
        stats.setFrequency(1);
        Iterator reader = getIterator(target);
        ProcessorsChain<Target, IBatchStatistics, IProcessor> processor = getProcessorChain();
        if (processor == null)
            throw new AmbitException("Processor not defined");
        long started = System.currentTimeMillis();
        while (reader.hasNext() && !cancelled) {
            if ((stats.getRecords(IBatchStatistics.RECORDS_STATS.RECORDS_READ) % stats.getFrequency()) == 0)
                propertyChangeSupport.firePropertyChange(PROPERTY_BATCHSTATS, null, stats);
            Object object = null;
            long ms = System.currentTimeMillis();
            try {
                object = reader.next();
                stats.increment(IBatchStatistics.RECORDS_STATS.RECORDS_READ);
                stats.incrementTimeElapsed(IBatchStatistics.RECORDS_STATS.RECORDS_READ, System.currentTimeMillis() - ms);
            } catch (Exception x) {
                stats.increment(IBatchStatistics.RECORDS_STATS.RECORDS_ERROR);
                stats.incrementTimeElapsed(IBatchStatistics.RECORDS_STATS.RECORDS_ERROR, System.currentTimeMillis() - ms);
                continue;
            }
            ms = System.currentTimeMillis();
            try {
                processor.process((Target) object);
                stats.increment(IBatchStatistics.RECORDS_STATS.RECORDS_PROCESSED);
                stats.incrementTimeElapsed(IBatchStatistics.RECORDS_STATS.RECORDS_PROCESSED, System.currentTimeMillis() - ms);
            } catch (Exception x) {
                stats.increment(IBatchStatistics.RECORDS_STATS.RECORDS_ERROR);
                stats.incrementTimeElapsed(IBatchStatistics.RECORDS_STATS.RECORDS_ERROR, System.currentTimeMillis() - ms);
                continue;
            }
            long elapsed = System.currentTimeMillis() - started;
            if ((timeout > 0) && (elapsed > timeout)) {
                stats.increment(IBatchStatistics.RECORDS_STATS.RECORDS_ERROR);
                stats.incrementTimeElapsed(IBatchStatistics.RECORDS_STATS.RECORDS_ERROR, System.currentTimeMillis() - ms);
                break;
            }
        }
        propertyChangeSupport.firePropertyChange(PROPERTY_BATCHSTATS, null, stats);
        closeIterator(reader);
        return stats;
    } catch (Exception x) {
        throw new AmbitException(x);
    }
}
Also used : DefaultBatchStatistics(net.idea.modbcum.i.batch.DefaultBatchStatistics) Iterator(java.util.Iterator) IBatchStatistics(net.idea.modbcum.i.batch.IBatchStatistics) IProcessor(net.idea.modbcum.i.processors.IProcessor) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) AmbitException(net.idea.modbcum.i.exceptions.AmbitException)

Aggregations

IProcessor (net.idea.modbcum.i.processors.IProcessor)16 IBatchStatistics (net.idea.modbcum.i.batch.IBatchStatistics)14 IStructureRecord (ambit2.base.interfaces.IStructureRecord)12 AmbitException (net.idea.modbcum.i.exceptions.AmbitException)10 ProcessorsChain (net.idea.modbcum.i.processors.ProcessorsChain)10 ProcessorStructureRetrieval (ambit2.db.processors.ProcessorStructureRetrieval)7 RetrieveStructure (ambit2.db.readers.RetrieveStructure)5 FileInputState (ambit2.core.io.FileInputState)4 MoleculeReader (ambit2.core.processors.structure.MoleculeReader)4 BatchDBProcessor (ambit2.db.processors.BatchDBProcessor)3 RepositoryWriter (ambit2.db.processors.RepositoryWriter)3 SourceDataset (ambit2.base.data.SourceDataset)2 FileOutputState (ambit2.core.io.FileOutputState)2 FilesWithHeaderWriter (ambit2.core.io.FilesWithHeaderWriter)2 StructureNormalizer (ambit2.core.processors.StructureNormalizer)2 AtomConfigurator (ambit2.core.processors.structure.AtomConfigurator)2 FP1024Writer (ambit2.db.processors.FP1024Writer)2 QueryStructureByID (ambit2.db.search.structure.QueryStructureByID)2 SMARTSAcceleratorWriter (ambit2.db.update.qlabel.smarts.SMARTSAcceleratorWriter)2 FunctionalGroup (ambit2.descriptors.FunctionalGroup)2