Search in sources :

Example 1 with ReferenceURIReporter

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

the class PropertyResourceTest method testCreateEntry.

/**
 * <pre>
 * <rdf:RDF
 *     xmlns:j.0="http://purl.org/net/nknouf/ns/bibtex#"
 *     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
 *     xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
 *     xmlns:ot="http://www.opentox.org/api/1.1#"
 *     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
 *     xmlns:owl="http://www.w3.org/2002/07/owl#"
 *     xmlns:dc="http://purl.org/dc/elements/1.1/" >
 *   <rdf:Description rdf:about="http://www.opentox.org/api/1.1#units">
 *     <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#DatatypeProperty"/>
 *   </rdf:Description>
 *   <rdf:Description rdf:about="http://purl.org/net/nknouf/ns/bibtex#Entry">
 *     <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
 *   </rdf:Description>
 *   <rdf:Description rdf:about="http://www.opentox.org/api/1.1#hasSource">
 *     <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#ObjectProperty"/>
 *   </rdf:Description>
 *   <rdf:Description rdf:nodeID="A0">
 *     <rdfs:seeAlso>bbb</rdfs:seeAlso>
 *     <dc:title>aaa</dc:title>
 *     <rdf:type rdf:resource="http://purl.org/net/nknouf/ns/bibtex#Entry"/>
 *   </rdf:Description>
 *   <rdf:Description rdf:about="http://www.opentox.org/api/1.1#Feature">
 *     <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
 *   </rdf:Description>
 *   <rdf:Description rdf:nodeID="A1">
 *     <dc:type>http://www.w3.org/2001/XMLSchema#string</dc:type>
 *     <ot:hasSource rdf:nodeID="A0"/>
 *     <owl:sameAs>CasRN</owl:sameAs>
 *     <ot:units></ot:units>
 *     <dc:title>cas</dc:title>
 *     <rdf:type rdf:resource="http://www.opentox.org/api/1.1#Feature"/>
 *   </rdf:Description>
 * </rdf:RDF>
 * </pre>
 *
 * @throws Exception
 */
@Test
public void testCreateEntry() throws Exception {
    OntModel model = OT.createModel();
    Property p = new Property("cas", new LiteratureEntry("aaa", "bbb"));
    p.setNominal(true);
    PropertyRDFReporter.addToModel(model, p, new PropertyURIReporter(new Reference(String.format("http://localhost:%d%s", port, PropertyResource.featuredef))), new ReferenceURIReporter());
    StringWriter writer = new StringWriter();
    model.write(writer, "RDF/XML");
    System.out.println(writer.toString());
    Response response = testPost(String.format("http://localhost:%d%s", port, PropertyResource.featuredef), MediaType.APPLICATION_RDF_XML, writer.toString());
    Assert.assertEquals(Status.SUCCESS_OK, response.getStatus());
    // Assert.assertEquals("http://localhost:8181/feature/4",
    // response.getLocationRef().toString());
    IDatabaseConnection c = getConnection();
    ITable table = c.createQueryTable("EXPECTED", String.format("SELECT * FROM properties join catalog_references using(idreference) where name='cas' and comments='%s' and title='aaa' and url='bbb' and isLocal=1", Property.opentox_CAS));
    Assert.assertEquals(1, table.getRowCount());
    c.close();
}
Also used : Response(org.restlet.Response) StringWriter(java.io.StringWriter) ReferenceURIReporter(ambit2.rest.reference.ReferenceURIReporter) LiteratureEntry(ambit2.base.data.LiteratureEntry) Reference(org.restlet.data.Reference) OntModel(com.hp.hpl.jena.ontology.OntModel) ITable(org.dbunit.dataset.ITable) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) Property(ambit2.base.data.Property) PropertyURIReporter(ambit2.rest.property.PropertyURIReporter) Test(org.junit.Test) ResourceTest(ambit2.rest.test.ResourceTest)

Example 2 with ReferenceURIReporter

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

the class PropertyResourceTest method testUpdateExistingEntry.

@Test
public void testUpdateExistingEntry() throws Exception {
    // now we can only update by PUT, not POST
    Property p = new Property("Property 1", new LiteratureEntry("Dummy", "NA"));
    p.setLabel("Test");
    p.setNominal(true);
    OntModel model = OT.createModel();
    PropertyRDFReporter.addToModel(model, p, new PropertyURIReporter(new Reference(String.format("http://localhost:%d%s", port, PropertyResource.featuredef))), new ReferenceURIReporter());
    StringWriter writer = new StringWriter();
    model.write(writer, "RDF/XML");
    Response response = testPut(String.format("http://localhost:%d%s/1", port, PropertyResource.featuredef), MediaType.TEXT_URI_LIST, new StringRepresentation(writer.toString(), MediaType.APPLICATION_RDF_XML));
    Assert.assertEquals(Status.SUCCESS_OK, response.getStatus());
    IDatabaseConnection c = getConnection();
    ITable table = c.createQueryTable("EXPECTED", String.format("SELECT isLocal FROM properties join catalog_references using(idreference) where name='Property 1' and comments='%s' and title='Dummy' and url='NA'", "http://www.opentox.org/api/1.1#Test"));
    Assert.assertEquals(1, table.getRowCount());
    Assert.assertEquals(true, table.getValue(0, "isLocal"));
    c.close();
    Assert.assertEquals("http://localhost:8181/feature/1", response.getLocationRef().toString());
}
Also used : Response(org.restlet.Response) StringWriter(java.io.StringWriter) ReferenceURIReporter(ambit2.rest.reference.ReferenceURIReporter) StringRepresentation(org.restlet.representation.StringRepresentation) LiteratureEntry(ambit2.base.data.LiteratureEntry) Reference(org.restlet.data.Reference) OntModel(com.hp.hpl.jena.ontology.OntModel) ITable(org.dbunit.dataset.ITable) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) Property(ambit2.base.data.Property) PropertyURIReporter(ambit2.rest.property.PropertyURIReporter) Test(org.junit.Test) ResourceTest(ambit2.rest.test.ResourceTest)

Example 3 with ReferenceURIReporter

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

the class PropertyResourceTest method testUpdateExistingEntry1.

// this fails because of mysql foreign key constraints
public void testUpdateExistingEntry1() throws Exception {
    Property p = new Property("Property 1", new LiteratureEntry("Dummy 1", "NA"));
    p.setLabel("Test");
    p.setId(1);
    OntModel model = OT.createModel();
    PropertyRDFReporter.addToModel(model, p, new PropertyURIReporter(new Reference(String.format("http://localhost:%d%s", port))), new ReferenceURIReporter());
    StringWriter writer = new StringWriter();
    model.write(writer, "RDF/XML");
    Response response = testPost(String.format("http://localhost:%d%s/2", port, PropertyResource.featuredef), MediaType.APPLICATION_RDF_XML, writer.toString());
    Assert.assertEquals(Status.SUCCESS_OK, response.getStatus());
    IDatabaseConnection c = getConnection();
    ITable table = c.createQueryTable("EXPECTED", String.format("SELECT * FROM properties join catalog_references using(idreference) where name='Property 1' and comments='%s' and title='Dummy' and url='NA'", "http://www.opentox.org/api/1.1#Test"));
    Assert.assertEquals(1, table.getRowCount());
    c.close();
    Assert.assertEquals("http://localhost:8181/feature/2", response.getLocationRef().toString());
}
Also used : Response(org.restlet.Response) StringWriter(java.io.StringWriter) ReferenceURIReporter(ambit2.rest.reference.ReferenceURIReporter) LiteratureEntry(ambit2.base.data.LiteratureEntry) Reference(org.restlet.data.Reference) OntModel(com.hp.hpl.jena.ontology.OntModel) ITable(org.dbunit.dataset.ITable) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) Property(ambit2.base.data.Property) PropertyURIReporter(ambit2.rest.property.PropertyURIReporter)

Example 4 with ReferenceURIReporter

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

the class PropertyRDFReporter method addToModel.

public static Individual addToModel(OntModel jenaModel, Property item, QueryURIReporter<Property, IQueryRetrieval<Property>> uriReporter, ReferenceURIReporter referenceReporter) {
    Individual feature = null;
    OTClass featureType = OTClass.Feature;
    String id = uriReporter.getURI(item);
    if ((uriReporter == null) || (uriReporter.getBaseReference() == null) || (item.getId() < 0)) {
        if (item.getClazz() == Dictionary.class) {
            feature = jenaModel.createIndividual(id, featureType.getOntClass(jenaModel));
        } else
            feature = jenaModel.createIndividual(featureType.getOntClass(jenaModel));
    } else {
        feature = jenaModel.createIndividual(id, featureType.getOntClass(jenaModel));
    }
    if (item.isNominal())
        feature.addOntClass(OTClass.NominalFeature.getOntClass(jenaModel));
    if (item.getClazz() == Number.class)
        feature.addOntClass(OTClass.NumericFeature.getOntClass(jenaModel));
    else if (item.getClazz() == Double.class)
        feature.addOntClass(OTClass.NumericFeature.getOntClass(jenaModel));
    else if (item.getClazz() == Float.class)
        feature.addOntClass(OTClass.NumericFeature.getOntClass(jenaModel));
    else if (item.getClazz() == Integer.class)
        feature.addOntClass(OTClass.NumericFeature.getOntClass(jenaModel));
    else if (item.getClazz() == Long.class)
        feature.addOntClass(OTClass.NumericFeature.getOntClass(jenaModel));
    else if (item.getClazz() == Dictionary.class)
        feature.addOntClass(OTClass.TupleFeature.getOntClass(jenaModel));
    if (item.getName() != null)
        feature.addProperty(DC.title, item.getName());
    if (item.getUnits() != null)
        feature.addProperty(OT.DataProperty.units.createProperty(jenaModel), item.getUnits());
    String uri = item.getLabel();
    if (uri == null)
        uri = Property.guessLabel(item.getName());
    if ((uri != null) && (uri.indexOf("http://") < 0) && (uri.indexOf("https://") < 0)) {
        uri = String.format("%s%s", OT.NS, Reference.encode(uri));
    }
    feature.addProperty(OWL.sameAs, jenaModel.createResource(uri));
    // ot:hasSource ; reference.title used as source URI, reference.url used
    // as object type -
    // somewhat awkward, but title is the unique field in the
    // catalog_references table
    uri = item.getTitle();
    // catch all
    if (uri.indexOf("/model/") > 0)
        feature.addOntClass(OT.OTClass.ModelPredictionFeature.getOntClass(jenaModel));
    // drop using /reference objects
    if ((uri.indexOf("http://") < 0) && (uri.indexOf("https://") < 0)) {
        Individual source = null;
        if (_type.Algorithm.equals(item.getReference().getType())) {
            uri = String.format("%s/algorithm/%s", uriReporter.getBaseReference(), Reference.encode(uri));
            source = jenaModel.createIndividual(uri, OT.OTClass.Algorithm.createOntClass(jenaModel));
            feature.addProperty(OT.OTProperty.hasSource.createProperty(jenaModel), source);
        } else if (_type.Model.equals(item.getReference().getType())) {
            uri = String.format("%s/model/%s", uriReporter.getBaseReference(), Reference.encode(uri));
            source = jenaModel.createIndividual(uri, OT.OTClass.Model.createOntClass(jenaModel));
            feature.addProperty(OT.OTProperty.hasSource.createProperty(jenaModel), source);
            feature.addOntClass(OT.OTClass.ModelPredictionFeature.getOntClass(jenaModel));
        } else if (_type.Feature.equals(item.getReference().getType())) {
            uri = String.format("%s/feature/%s", uriReporter.getBaseReference(), Reference.encode(uri));
            source = jenaModel.createIndividual(uri, OT.OTClass.Feature.createOntClass(jenaModel));
            feature.addProperty(OT.OTProperty.hasSource.createProperty(jenaModel), source);
        } else if (_type.Dataset.equals(item.getReference().getType())) {
            // this seems to confuse everybody's else parsers ...
            uri = String.format("%s/dataset/%s", uriReporter.getBaseReference(), Reference.encode(uri));
            Resource src = jenaModel.createResource(uri);
            feature.addProperty(OT.OTProperty.hasSource.createProperty(jenaModel), src);
        } else {
            feature.addProperty(OT.OTProperty.hasSource.createProperty(jenaModel), uri);
        }
        feature.addProperty(DC.creator, item.getReference().getURL());
    } else {
        feature.addProperty(OT.OTProperty.hasSource.createProperty(jenaModel), jenaModel.createResource(uri));
        feature.addProperty(DC.creator, item.getReference().getURL());
    }
    if (item.getAnnotations() != null)
        for (PropertyAnnotation a : item.getAnnotations()) try {
            PropertyAnnotationRDFReporter.annotation2RDF(a, jenaModel, feature, uriReporter.getBaseReference().toString());
        } catch (Exception x) {
            x.printStackTrace();
        }
    return feature;
}
Also used : Dictionary(ambit2.base.data.Dictionary) OTClass(net.idea.restnet.rdf.ns.OT.OTClass) Individual(com.hp.hpl.jena.ontology.Individual) Resource(com.hp.hpl.jena.rdf.model.Resource) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) DbAmbitException(net.idea.modbcum.i.exceptions.DbAmbitException) PropertyAnnotation(ambit2.base.data.PropertyAnnotation)

Example 5 with ReferenceURIReporter

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

the class PropertyAnnotationResourceTest method testCreateEntry.

@Test
public void testCreateEntry() throws Exception {
    OntModel model = OT.createModel();
    Property p = new Property("cas", new LiteratureEntry("aaa", "bbb"));
    p.setNominal(true);
    PropertyRDFReporter.addToModel(model, p, new PropertyURIReporter(new Reference(String.format("http://localhost:%d%s", port, PropertyResource.featuredef))), new ReferenceURIReporter());
    StringWriter writer = new StringWriter();
    model.write(writer, "RDF/XML");
    // System.out.println(writer.toString());
    Response response = testPost(String.format("http://localhost:%d%s", port, PropertyResource.featuredef), MediaType.APPLICATION_RDF_XML, writer.toString());
    Assert.assertEquals(Status.SUCCESS_OK, response.getStatus());
    // Assert.assertEquals("http://localhost:8181/feature/4",
    // response.getLocationRef().toString());
    IDatabaseConnection c = getConnection();
    ITable table = c.createQueryTable("EXPECTED", String.format("SELECT * FROM properties join catalog_references using(idreference) where name='cas' and comments='%s' and title='aaa' and url='bbb' and isLocal=1", Property.opentox_CAS));
    Assert.assertEquals(1, table.getRowCount());
    c.close();
}
Also used : Response(org.restlet.Response) StringWriter(java.io.StringWriter) ReferenceURIReporter(ambit2.rest.reference.ReferenceURIReporter) LiteratureEntry(ambit2.base.data.LiteratureEntry) Reference(org.restlet.data.Reference) OntModel(com.hp.hpl.jena.ontology.OntModel) ITable(org.dbunit.dataset.ITable) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) Property(ambit2.base.data.Property) PropertyURIReporter(ambit2.rest.property.PropertyURIReporter) Test(org.junit.Test) ResourceTest(ambit2.rest.test.ResourceTest)

Aggregations

Property (ambit2.base.data.Property)7 PropertyURIReporter (ambit2.rest.property.PropertyURIReporter)7 ReferenceURIReporter (ambit2.rest.reference.ReferenceURIReporter)7 OntModel (com.hp.hpl.jena.ontology.OntModel)7 StringWriter (java.io.StringWriter)7 IDatabaseConnection (org.dbunit.database.IDatabaseConnection)7 ITable (org.dbunit.dataset.ITable)7 Response (org.restlet.Response)7 Reference (org.restlet.data.Reference)7 LiteratureEntry (ambit2.base.data.LiteratureEntry)6 ResourceTest (ambit2.rest.test.ResourceTest)6 Test (org.junit.Test)6 StringRepresentation (org.restlet.representation.StringRepresentation)2 Dictionary (ambit2.base.data.Dictionary)1 PropertyAnnotation (ambit2.base.data.PropertyAnnotation)1 Individual (com.hp.hpl.jena.ontology.Individual)1 Resource (com.hp.hpl.jena.rdf.model.Resource)1 AmbitException (net.idea.modbcum.i.exceptions.AmbitException)1 DbAmbitException (net.idea.modbcum.i.exceptions.DbAmbitException)1 OTClass (net.idea.restnet.rdf.ns.OT.OTClass)1