Search in sources :

Example 1 with BulkNeedEvent

use of won.matcher.service.common.event.BulkNeedEvent in project webofneeds by researchstudio-sat.

the class CrawlSparqlService method retrieveActiveNeedEvents.

public BulkNeedEvent retrieveActiveNeedEvents(long fromDate, long toDate, int offset, int limit, boolean sortAscending) {
    // query template to retrieve all alctive cralwed/saved needs in a certain date range
    String orderClause = sortAscending ? "ORDER BY ?date\n" : "ORDER BY DESC(?date)\n";
    log.debug("bulk load need data from sparql endpoint in date range: [{},{}]", fromDate, toDate);
    String queryTemplate = "SELECT ?needUri ?wonNodeUri ?date WHERE {  \n" + "  ?needUri a won:Need. \n" + "  ?needUri won:crawlDate ?date.  \n" + "  ?needUri won:isInState won:Active. \n" + "  ?needUri won:hasWonNode ?wonNodeUri. \n" + "  {?needUri won:crawlStatus 'SAVE'.} UNION {?needUri won:crawlStatus 'DONE'.}\n" + "  FILTER (?date >= ?fromDate && ?date < ?toDate ) \n" + "} " + orderClause + " OFFSET ?offset\n" + " LIMIT ?limit";
    ParameterizedSparqlString pps = new ParameterizedSparqlString();
    pps.setNsPrefix("won", "http://purl.org/webofneeds/model#");
    pps.setCommandText(queryTemplate);
    pps.setLiteral("fromDate", fromDate);
    pps.setLiteral("toDate", toDate);
    pps.setLiteral("offset", offset);
    pps.setLiteral("limit", limit);
    log.debug("Query SPARQL Endpoint: {}", sparqlEndpoint);
    log.debug("Execute query: {}", pps.toString());
    QueryExecution qexec = QueryExecutionFactory.sparqlService(sparqlEndpoint, pps.asQuery());
    ResultSet results = qexec.execSelect();
    // load all the needs into one bulk need event
    BulkNeedEvent bulkNeedEvent = new BulkNeedEvent();
    while (results.hasNext()) {
        QuerySolution qs = results.nextSolution();
        String needUri = qs.get("needUri").asResource().getURI();
        String wonNodeUri = qs.get("wonNodeUri").asResource().getURI();
        long crawlDate = qs.getLiteral("date").getLong();
        Dataset ds = retrieveNeedDataset(needUri);
        StringWriter sw = new StringWriter();
        RDFDataMgr.write(sw, ds, RDFFormat.TRIG.getLang());
        NeedEvent needEvent = new NeedEvent(needUri, wonNodeUri, NeedEvent.TYPE.ACTIVE, crawlDate, sw.toString(), RDFFormat.TRIG.getLang());
        bulkNeedEvent.addNeedEvent(needEvent);
    }
    qexec.close();
    log.debug("number of need events created: " + bulkNeedEvent.getNeedEvents().size());
    return bulkNeedEvent;
}
Also used : BulkNeedEvent(won.matcher.service.common.event.BulkNeedEvent) StringWriter(java.io.StringWriter) NeedEvent(won.matcher.service.common.event.NeedEvent) BulkNeedEvent(won.matcher.service.common.event.BulkNeedEvent)

Example 2 with BulkNeedEvent

use of won.matcher.service.common.event.BulkNeedEvent in project webofneeds by researchstudio-sat.

the class NeedEventLoaderActor method onReceive.

@Override
public void onReceive(final Object o) throws Throwable {
    if (o instanceof LoadNeedEvent) {
        LoadNeedEvent msg = (LoadNeedEvent) o;
        log.debug("received request to load needs events: {}", msg);
        BulkNeedEvent bulkNeedEvent;
        int offset = 0;
        do {
            // check if need event should be returned in time interval or last X need events
            if (msg.getLastXNeedEvents() == -1) {
                bulkNeedEvent = sparqlService.retrieveActiveNeedEvents(msg.getFromDate(), msg.getToDate(), offset, MAX_BULK_SIZE, true);
            } else {
                bulkNeedEvent = sparqlService.retrieveActiveNeedEvents(0, Long.MAX_VALUE, offset, Math.min(MAX_BULK_SIZE, msg.getLastXNeedEvents() - offset), false);
            }
            if (bulkNeedEvent.getNeedEvents().size() > 0) {
                log.debug("send bulk event of size {} back to requesting actor", bulkNeedEvent.getNeedEvents().size());
                getSender().tell(bulkNeedEvent, getSelf());
                offset += bulkNeedEvent.getNeedEvents().size();
            }
        } while (bulkNeedEvent.getNeedEvents().size() == MAX_BULK_SIZE);
    }
}
Also used : BulkNeedEvent(won.matcher.service.common.event.BulkNeedEvent) LoadNeedEvent(won.matcher.service.common.event.LoadNeedEvent)

Aggregations

BulkNeedEvent (won.matcher.service.common.event.BulkNeedEvent)2 StringWriter (java.io.StringWriter)1 LoadNeedEvent (won.matcher.service.common.event.LoadNeedEvent)1 NeedEvent (won.matcher.service.common.event.NeedEvent)1