Search in sources :

Example 1 with AmbitApplication

use of ambit2.rest.AmbitApplication in project ambit-mirror by ideaconsult.

the class CallableDomainCreatorTest method test.

@Test
public void test() throws Exception {
    Reference app = new Reference(String.format("http://localhost:%d", port));
    Form form = new Form();
    form.add("dataset_uri", String.format("http://localhost:%d/dataset/1", port));
    Algorithm alg = new Algorithm();
    alg.setContent(DataCoverageLeverage.class.getName());
    ModelURIReporter uriReporter = new ModelURIReporter(null);
    CallableNumericalModelCreator c = new CallableNumericalModelCreator(form, app, component.getApplication() == null ? null : ((AmbitApplication) component.getApplication()).getContext(), alg, uriReporter, new AlgorithmURIReporter(null), null, "test", null);
    TaskResult ref = c.call();
}
Also used : CallableNumericalModelCreator(ambit2.rest.task.CallableNumericalModelCreator) Form(org.restlet.data.Form) Reference(org.restlet.data.Reference) AmbitApplication(ambit2.rest.AmbitApplication) DataCoverageLeverage(ambit2.model.numeric.DataCoverageLeverage) TaskResult(ambit2.rest.task.TaskResult) ModelURIReporter(ambit2.rest.model.ModelURIReporter) AlgorithmURIReporter(ambit2.rest.algorithm.AlgorithmURIReporter) Algorithm(ambit2.core.data.model.Algorithm) Test(org.junit.Test) ResourceTest(ambit2.rest.test.ResourceTest)

Example 2 with AmbitApplication

use of ambit2.rest.AmbitApplication in project ambit-mirror by ideaconsult.

the class CallableWekaModelCreatorTest method test.

public void test() throws Exception {
    Reference app = new Reference(String.format("http://localhost:%d", port));
    Form form = new Form();
    form.add("dataset_uri", DatasetReporterTest.class.getResource("/input.rdf").toString());
    form.add("target", "http://ambit.uni-plovdiv.bg:8080/ambit2/feature/11938");
    ModelURIReporter uriReporter = new ModelURIReporter(null, new ResourceDoc());
    CallableWekaModelCreator c = new CallableWekaModelCreator(form, app, ((AmbitApplication) component.getApplication()).getContext(), (Algorithm) null, uriReporter, new AlgorithmURIReporter(null), null, "test", null);
    TaskResult ref = c.call();
}
Also used : ResourceDoc(net.idea.restnet.c.ResourceDoc) CallableWekaModelCreator(ambit2.rest.task.weka.CallableWekaModelCreator) Form(org.restlet.data.Form) Reference(org.restlet.data.Reference) TaskResult(ambit2.rest.task.TaskResult) ModelURIReporter(ambit2.rest.model.ModelURIReporter) AlgorithmURIReporter(ambit2.rest.algorithm.AlgorithmURIReporter)

Example 3 with AmbitApplication

use of ambit2.rest.AmbitApplication in project ambit-mirror by ideaconsult.

the class SimilarityResource method createQuery.

@Override
protected Q createQuery(Context context, Request request, Response response) throws ResourceException {
    Form form = getResourceRef(getRequest()).getQueryAsForm();
    try {
        Object bundleURI = OpenTox.params.bundle_uri.getFirstValue(form);
        Integer idbundle = bundleURI == null ? null : getIdBundle(bundleURI, request);
        SubstanceEndpointsBundle bundle = new SubstanceEndpointsBundle(idbundle);
        bundles = new SubstanceEndpointsBundle[1];
        bundles[0] = bundle;
    } catch (Exception x) {
        bundles = null;
    }
    try {
        includeMol = "true".equals(form.getFirstValue("mol"));
    } catch (Exception x) {
        includeMol = false;
    }
    folders = form.getValuesArray("folder");
    filterBySubstance = false;
    try {
        String filter = form.getFirstValue("filterBySubstance");
        if (filter != null) {
            filter = filter.toLowerCase();
            filterBySubstance = "yes".equals(filter) || "on".equals(filter) || "true".equals(filter);
        }
    } catch (Exception x) {
        filterBySubstance = false;
    }
    QueryAtomEnvironment.q_modifier ae = null;
    try {
        String filter = form.getFirstValue("mode");
        if (filter != null) {
            filter = filter.toLowerCase();
            ae = QueryAtomEnvironment.q_modifier.valueOf(filter.replace("ae", ""));
        }
    } catch (Exception x) {
        ae = null;
    }
    mol = getMolecule(form);
    if ((mol == null) || (mol.getAtomCount() == 0))
        throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "Empty molecule");
    threshold = 0.0;
    try {
        threshold = new Double(Reference.decode(form.getFirstValue("threshold")));
    } catch (Exception x) {
        threshold = 0.9;
    }
    QuerySimilarity q = null;
    if (ae != null) {
        q = new QueryAtomEnvironment(ae);
        try {
            q.setValue(getAE(mol));
        } catch (Exception x) {
            throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, x);
        }
    } else
        try {
            q = new QuerySimilarityBitset();
            q.setValue(getBitset(mol));
        } catch (Exception x) {
            throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, x);
        }
    q.setChemicalsOnly(true);
    q.setThreshold(threshold);
    q.setCondition(NumberCondition.getInstance(">"));
    q.setName("Similarity");
    try {
        q.setForceOrdering(((AmbitApplication) getApplication()).isSimilarityOrder());
    } catch (Exception x) {
        q.setForceOrdering(true);
    }
    try {
        if (filterBySubstance) {
            ChemicalBySubstanceRelation qa = new ChemicalBySubstanceRelation();
            QueryCombinedStructure qc = new QueryCombinedStructure();
            qc.add(q);
            qc.setChemicalsOnly(true);
            qc.setScope(qa);
            setTemplate(createTemplate(context, request, response));
            setGroupProperties(context, request, response);
            return (Q) qc;
        } else if ((folders != null) && (folders.length > 0)) {
            ChemicalByQueryFolder qa = new ChemicalByQueryFolder(folders);
            QueryCombinedStructure qc = new QueryCombinedStructure();
            qc.add(q);
            qc.setChemicalsOnly(true);
            qc.setScope(qa);
            setTemplate(createTemplate(context, request, response));
            setGroupProperties(context, request, response);
            return (Q) qc;
        } else {
            Object datasetURI = OpenTox.params.dataset_uri.getFirstValue(form);
            ISourceDataset srcdataset = null;
            if (datasetURI != null)
                try {
                    srcdataset = getDataset(datasetURI.toString());
                    QueryCombinedStructure qc = new QueryCombinedStructure();
                    qc.add(q);
                    qc.setChemicalsOnly(true);
                    if (srcdataset instanceof SourceDataset) {
                        ChemicalByDataset cd = new ChemicalByDataset(new Integer(srcdataset.getID()));
                        qc.setScope(cd);
                        setTemplate(createTemplate(context, request, response));
                        setGroupProperties(context, request, response);
                        return (Q) qc;
                    } else {
                    // TODO, resort to all db
                    }
                } catch (Exception x) {
                    srcdataset = null;
                }
            QueryCombinedStructure qc = null;
            try {
                this.dataset_id = Reference.decode(getRequest().getAttributes().get(DatasetResource.datasetKey).toString());
                qc = new QueryCombinedStructure();
                qc.add(q);
                qc.setChemicalsOnly(true);
                ChemicalByDataset cd = new ChemicalByDataset(new Integer(dataset_id));
                qc.setScope(cd);
                setTemplate(createTemplate(context, request, response));
                setGroupProperties(context, request, response);
                return (Q) qc;
            } catch (Exception x) {
                setTemplate(createTemplate(context, request, response));
                setGroupProperties(context, request, response);
                return (Q) q;
            }
        }
    } catch (Exception x) {
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, x);
    }
}
Also used : ChemicalByQueryFolder(ambit2.db.search.structure.ChemicalByQueryFolder) QuerySimilarityBitset(ambit2.db.search.structure.QuerySimilarityBitset) Form(org.restlet.data.Form) ChemicalBySubstanceRelation(ambit2.db.substance.relation.ChemicalBySubstanceRelation) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) ResourceException(org.restlet.resource.ResourceException) InvalidResourceIDException(ambit2.rest.error.InvalidResourceIDException) QueryAtomEnvironment(ambit2.db.search.structure.QueryAtomEnvironment) ISourceDataset(ambit2.base.data.ISourceDataset) ChemicalByDataset(ambit2.db.update.structure.ChemicalByDataset) SubstanceEndpointsBundle(ambit2.base.data.substance.SubstanceEndpointsBundle) ResourceException(org.restlet.resource.ResourceException) ISourceDataset(ambit2.base.data.ISourceDataset) SourceDataset(ambit2.base.data.SourceDataset) QuerySimilarity(ambit2.db.search.structure.QuerySimilarity) QueryCombinedStructure(ambit2.db.search.structure.QueryCombinedStructure)

Example 4 with AmbitApplication

use of ambit2.rest.AmbitApplication 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 5 with AmbitApplication

use of ambit2.rest.AmbitApplication in project ambit-mirror by ideaconsult.

the class DatasetCompoundResource method doInit.

@Override
protected void doInit() throws ResourceException {
    DBConnection dbc = new DBConnection(getContext());
    configureDatasetMembersPrefixOption(((AmbitApplication) getApplication()).getProperties().isDatasetMembersPrefix());
    super.doInit();
}
Also used : DBConnection(ambit2.rest.DBConnection) AmbitApplication(ambit2.rest.AmbitApplication)

Aggregations

ResourceException (org.restlet.resource.ResourceException)5 AmbitApplication (ambit2.rest.AmbitApplication)4 AmbitException (net.idea.modbcum.i.exceptions.AmbitException)4 Form (org.restlet.data.Form)4 DBConnection (ambit2.rest.DBConnection)3 Connection (java.sql.Connection)3 ISourceDataset (ambit2.base.data.ISourceDataset)2 ProcessorException (ambit2.base.processors.ProcessorException)2 AlgorithmURIReporter (ambit2.rest.algorithm.AlgorithmURIReporter)2 RResourceException (ambit2.rest.exception.RResourceException)2 ModelURIReporter (ambit2.rest.model.ModelURIReporter)2 TaskResult (ambit2.rest.task.TaskResult)2 SQLException (java.sql.SQLException)2 BatchProcessingException (net.idea.modbcum.i.exceptions.BatchProcessingException)2 NotFoundException (net.idea.modbcum.i.exceptions.NotFoundException)2 Reference (org.restlet.data.Reference)2 SourceDataset (ambit2.base.data.SourceDataset)1 SubstanceEndpointsBundle (ambit2.base.data.substance.SubstanceEndpointsBundle)1 IStructureRecord (ambit2.base.interfaces.IStructureRecord)1 Algorithm (ambit2.core.data.model.Algorithm)1