Search in sources :

Example 1 with Coordinate

use of won.protocol.model.Coordinate in project webofneeds by researchstudio-sat.

the class DefaultNeedModelWrapper method getLocationCoordinate.

public Coordinate getLocationCoordinate(Resource contentNode) {
    Model needModel = getNeedModel();
    Property geoProperty = needModel.createProperty("http://schema.org/", "geo");
    Property longitudeProperty = needModel.createProperty("http://schema.org/", "longitude");
    Property latitudeProperty = needModel.createProperty("http://schema.org/", "latitude");
    RDFNode locationNode = RdfUtils.findOnePropertyFromResource(needModel, contentNode, WON.HAS_LOCATION);
    RDFNode geoNode = (locationNode != null && locationNode.isResource()) ? RdfUtils.findOnePropertyFromResource(needModel, locationNode.asResource(), geoProperty) : null;
    RDFNode lat = (geoNode != null && geoNode.isResource()) ? RdfUtils.findOnePropertyFromResource(needModel, geoNode.asResource(), latitudeProperty) : null;
    RDFNode lon = (geoNode != null && geoNode.isResource()) ? RdfUtils.findOnePropertyFromResource(needModel, geoNode.asResource(), longitudeProperty) : null;
    if (lat == null || lon == null) {
        return null;
    }
    Float latitude = Float.valueOf(lat.asLiteral().getString());
    Float longitude = Float.valueOf(lon.asLiteral().getString());
    return new Coordinate(latitude, longitude);
}
Also used : Coordinate(won.protocol.model.Coordinate) Model(org.apache.jena.rdf.model.Model) Property(org.apache.jena.rdf.model.Property) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 2 with Coordinate

use of won.protocol.model.Coordinate in project webofneeds by researchstudio-sat.

the class GoalInstantiationTest method exampleTaxi_validity.

@Test
public void exampleTaxi_validity() throws IOException {
    Dataset taxiOffer = loadDataset(baseFolder + "ex6_taxioffer.trig");
    Dataset taxiDemand = loadDataset(baseFolder + "ex6_taxi.trig");
    Dataset taxiDemandNoLoc = loadDataset(baseFolder + "ex6_taxi_noloc.trig");
    Dataset taxiDemandTwoLoc = loadDataset(baseFolder + "ex6_taxi_twoloc.trig");
    GoalInstantiationProducer goalInstantiation = new GoalInstantiationProducer(taxiOffer, taxiDemand, null, "http://example.org/", "http://example.org/blended/");
    Collection<GoalInstantiationResult> results = goalInstantiation.createGoalInstantiationResultsForNeed1();
    for (GoalInstantiationResult res : results) {
        System.out.println("Result::::::::::::::::::::::::::::::" + res.isConform());
        System.out.println(res.toString());
        if (res.isConform()) {
            Coordinate departureAddress = getAddress(loadSparqlQuery("/won/utils/goals/extraction/address/fromLocationQuery.rq"), res.getInstanceModel());
            Coordinate destinationAddress = getAddress(loadSparqlQuery("/won/utils/goals/extraction/address/toLocationQuery.rq"), res.getInstanceModel());
            Assert.assertEquals(departureAddress, new Coordinate(10.0f, 11.0f));
            Assert.assertEquals(destinationAddress, new Coordinate(12.0f, 13.0f));
        }
    }
    NeedModelWrapper needWrapper1 = new NeedModelWrapper(taxiOffer);
    Resource goal = needWrapper1.getGoals().iterator().next();
    GoalInstantiationResult result = goalInstantiation.findInstantiationForGoal(goal);
    Assert.assertTrue(result.isConform());
    GoalInstantiationResult recheckResultModel = GoalInstantiationProducer.findInstantiationForGoalInDataset(taxiOffer, goal, result.getInstanceModel());
    Assert.assertTrue(recheckResultModel.isConform());
    goalInstantiation = new GoalInstantiationProducer(taxiOffer, taxiDemandNoLoc, null, "http://example.org/", "http://example.org/blended/");
    results = goalInstantiation.createGoalInstantiationResultsForNeed1();
    for (GoalInstantiationResult res : results) {
        Assert.assertFalse(res.isConform());
    }
    goalInstantiation = new GoalInstantiationProducer(taxiOffer, taxiDemandTwoLoc, null, "http://example.org/", "http://example.org/blended/");
    results = goalInstantiation.createGoalInstantiationResultsForNeed1();
    for (GoalInstantiationResult res : results) {
        Assert.assertFalse(res.isConform());
    }
}
Also used : GoalInstantiationResult(won.utils.goals.GoalInstantiationResult) Coordinate(won.protocol.model.Coordinate) Dataset(org.apache.jena.query.Dataset) GoalInstantiationProducer(won.utils.goals.GoalInstantiationProducer) Resource(org.apache.jena.rdf.model.Resource) NeedModelWrapper(won.protocol.util.NeedModelWrapper) DefaultNeedModelWrapper(won.protocol.util.DefaultNeedModelWrapper) Test(org.junit.Test)

Example 3 with Coordinate

use of won.protocol.model.Coordinate in project webofneeds by researchstudio-sat.

the class NeedIndexer method indexNeedModel.

public void indexNeedModel(Model needModel, String id, boolean useTestCore) throws IOException, JsonLdError {
    // create the json from rdf model
    StringWriter sw = new StringWriter();
    RDFDataMgr.write(sw, needModel, Lang.JSONLD);
    String jsonld = sw.toString();
    Object jsonObject = JsonUtils.fromString(jsonld);
    Object frame = JsonUtils.fromString(" {\"@type\": \"" + WON.NEED + "\"} ");
    JsonLdOptions options = new JsonLdOptions();
    Map<String, Object> framed = JsonLdProcessor.frame(jsonObject, frame, options);
    // add the uri of the need as id field to avoid multiple adding of needs but instead allow updates
    framed.put("id", id);
    // add latitude and longitude values in one field for Solr spatial queries
    DefaultNeedModelWrapper needModelWrapper = new DefaultNeedModelWrapper(needModel, null);
    for (Resource contentNode : needModelWrapper.getContentNodes(NeedContentPropertyType.IS)) {
        Coordinate coordinate = needModelWrapper.getLocationCoordinate(contentNode);
        if (coordinate != null) {
            framed.put(SOLR_IS_LOCATION_COORDINATES_FIELD, String.valueOf(coordinate.getLatitude()) + "," + String.valueOf(coordinate.getLongitude()));
        }
    }
    for (Resource contentNode : needModelWrapper.getContentNodes(NeedContentPropertyType.SEEKS)) {
        Coordinate coordinate = needModelWrapper.getLocationCoordinate(contentNode);
        if (coordinate != null) {
            framed.put(SOLR_SEEKS_LOCATION_COORDINATES_FIELD, String.valueOf(coordinate.getLatitude()) + "," + String.valueOf(coordinate.getLongitude()));
        }
    }
    for (Resource contentNode : needModelWrapper.getContentNodes(NeedContentPropertyType.SEEKS_SEEKS)) {
        Coordinate coordinate = needModelWrapper.getLocationCoordinate(contentNode);
        if (coordinate != null) {
            framed.put(SOLR_SEEKS_SEEKS_LOCATION_COORDINATES_FIELD, String.valueOf(coordinate.getLatitude()) + "," + String.valueOf(coordinate.getLongitude()));
        }
    }
    // write the final json string
    sw = new StringWriter();
    JsonUtils.writePrettyPrint(sw, framed);
    String needJson = sw.toString();
    // post the need to the solr index
    String indexUri = config.getSolrEndpointUri(useTestCore);
    indexUri += "update/json/docs";
    if (config.isCommitIndexedNeedImmediately()) {
        indexUri += "?commit=" + config.isCommitIndexedNeedImmediately();
    }
    log.debug("Post need to solr index. \n Solr URI: {} \n Need (JSON): {}", indexUri, needJson);
    httpService.postJsonRequest(indexUri, needJson);
}
Also used : DefaultNeedModelWrapper(won.protocol.util.DefaultNeedModelWrapper) StringWriter(java.io.StringWriter) JsonLdOptions(com.github.jsonldjava.core.JsonLdOptions) Coordinate(won.protocol.model.Coordinate) Resource(org.apache.jena.rdf.model.Resource)

Example 4 with Coordinate

use of won.protocol.model.Coordinate in project webofneeds by researchstudio-sat.

the class GoalInstantiationTest method exampleTaxiFakeLocation_validity.

@Test
public void exampleTaxiFakeLocation_validity() throws IOException {
    Dataset taxiOffer = loadDataset(baseFolder + "ex7_taxioffer.trig");
    Dataset taxiDemand = loadDataset(baseFolder + "ex7_taxi.trig");
    GoalInstantiationProducer goalInstantiation = new GoalInstantiationProducer(taxiOffer, taxiDemand, null, "http://example.org/", "http://example.org/blended/");
    Collection<GoalInstantiationResult> results = goalInstantiation.createGoalInstantiationResultsForNeed1();
    for (GoalInstantiationResult res : results) {
        res.getInstanceModel().write(System.out, "TRIG");
        Assert.assertTrue(res.isConform());
        Coordinate departureAddress = getAddress(loadSparqlQuery("/won/utils/goals/extraction/address/northWestCornerQuery.rq"), res.getInstanceModel());
        Coordinate destinationAddress = getAddress(loadSparqlQuery("/won/utils/goals/extraction/address/southEastCornerQuery.rq"), res.getInstanceModel());
        Assert.assertEquals(departureAddress, new Coordinate(48.218727f, 16.360141f));
        Assert.assertEquals(destinationAddress, new Coordinate(48.218828f, 16.360241f));
    }
    NeedModelWrapper needWrapper1 = new NeedModelWrapper(taxiOffer);
    Resource goal = needWrapper1.getGoals().iterator().next();
    GoalInstantiationResult result = goalInstantiation.findInstantiationForGoal(goal);
    Assert.assertTrue(result.isConform());
    GoalInstantiationResult recheckResultModel = GoalInstantiationProducer.findInstantiationForGoalInDataset(taxiOffer, goal, result.getInstanceModel());
    Assert.assertTrue(recheckResultModel.isConform());
}
Also used : GoalInstantiationResult(won.utils.goals.GoalInstantiationResult) Coordinate(won.protocol.model.Coordinate) Dataset(org.apache.jena.query.Dataset) GoalInstantiationProducer(won.utils.goals.GoalInstantiationProducer) Resource(org.apache.jena.rdf.model.Resource) NeedModelWrapper(won.protocol.util.NeedModelWrapper) DefaultNeedModelWrapper(won.protocol.util.DefaultNeedModelWrapper) Test(org.junit.Test)

Aggregations

Coordinate (won.protocol.model.Coordinate)4 Resource (org.apache.jena.rdf.model.Resource)3 DefaultNeedModelWrapper (won.protocol.util.DefaultNeedModelWrapper)3 Dataset (org.apache.jena.query.Dataset)2 Test (org.junit.Test)2 NeedModelWrapper (won.protocol.util.NeedModelWrapper)2 GoalInstantiationProducer (won.utils.goals.GoalInstantiationProducer)2 GoalInstantiationResult (won.utils.goals.GoalInstantiationResult)2 JsonLdOptions (com.github.jsonldjava.core.JsonLdOptions)1 StringWriter (java.io.StringWriter)1 Model (org.apache.jena.rdf.model.Model)1 Property (org.apache.jena.rdf.model.Property)1 RDFNode (org.apache.jena.rdf.model.RDFNode)1