Search in sources :

Example 1 with Predicate

use of ddf.catalog.pubsub.predicate.Predicate in project ddf by codice.

the class EventProcessorImpl method createSubscription.

@Override
public void createSubscription(Subscription subscription, String subscriptionId) throws InvalidSubscriptionException, SubscriptionExistsException {
    String methodName = "createSubscription";
    LOGGER.debug("ENTERING: {}", methodName);
    LOGGER.debug("Creating Evaluation Criteria... ");
    try {
        for (PreSubscriptionPlugin plugin : preSubscription) {
            LOGGER.debug("Processing subscription with preSubscription plugin");
            subscription = plugin.process(subscription);
        }
        SubscriptionFilterVisitor visitor = new SubscriptionFilterVisitor();
        Predicate finalPredicate = (Predicate) subscription.accept(visitor, null);
        LOGGER.debug("predicate from filter visitor: {}", finalPredicate);
        String[] topics = new String[] { PubSubConstants.PUBLISHED_EVENT_TOPIC_NAME };
        Dictionary<String, String[]> props = new Hashtable<>(1, 1);
        props.put(EventConstants.EVENT_TOPIC, topics);
        ServiceRegistration serviceRegistration = bundleContext.registerService(EventHandler.class.getName(), new PublishedEventHandler(finalPredicate, subscription, preDelivery, catalog, threadPool), props);
        existingSubscriptions.put(subscriptionId, serviceRegistration);
        LOGGER.debug("Subscription {} created.", subscriptionId);
    } catch (Exception e) {
        LOGGER.info("Error while creating subscription predicate: ", e);
        throw new InvalidSubscriptionException(e);
    }
    LOGGER.debug("EXITING: {}", methodName);
}
Also used : InvalidSubscriptionException(ddf.catalog.event.InvalidSubscriptionException) SubscriptionFilterVisitor(ddf.catalog.pubsub.internal.SubscriptionFilterVisitor) Hashtable(java.util.Hashtable) PreSubscriptionPlugin(ddf.catalog.plugin.PreSubscriptionPlugin) EventHandler(org.osgi.service.event.EventHandler) SubscriptionNotFoundException(ddf.catalog.event.SubscriptionNotFoundException) SubscriptionExistsException(ddf.catalog.event.SubscriptionExistsException) PluginExecutionException(ddf.catalog.plugin.PluginExecutionException) InvalidSubscriptionException(ddf.catalog.event.InvalidSubscriptionException) Predicate(ddf.catalog.pubsub.predicate.Predicate) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 2 with Predicate

use of ddf.catalog.pubsub.predicate.Predicate in project ddf by codice.

the class SubscriptionFilterVisitor method visit.

@Override
public Object visit(And filter, Object data) {
    LOGGER.debug("ENTERING: AND filter");
    Predicate returnPredicate = null;
    List<Predicate> predList = new ArrayList<Predicate>();
    List<Filter> childList = filter.getChildren();
    if (childList != null) {
        for (Filter child : childList) {
            if (child == null) {
                continue;
            }
            predList.add((Predicate) child.accept(this, data));
        }
    }
    ContentTypePredicate currentContentTypePred = null;
    LOGGER.debug("predicate list size: {}", predList.size());
    for (Predicate p : predList) {
        if (p == null) {
            // filterless subscription
            LOGGER.debug("Found null predicate.  Indicates Filterless Subscription.");
        } else if (p instanceof ContentTypePredicate) {
            LOGGER.debug("Found ContentType Predicate.");
            if (currentContentTypePred == null) {
                currentContentTypePred = (ContentTypePredicate) p;
            } else {
                ContentTypePredicate incomingContentTypePred = (ContentTypePredicate) p;
                String currentType = currentContentTypePred.getType();
                String currentVersion = currentContentTypePred.getVersion();
                String incomingType = incomingContentTypePred.getType();
                String incomingVersion = incomingContentTypePred.getVersion();
                // Combine the two.
                if (currentType != null && incomingType == null && incomingVersion != null) {
                    currentContentTypePred.setVersion(incomingVersion);
                // Case 2
                // First ContentTypePredicate has no type but has a version. Second
                // ContentTypePredicate has no version, but it has a type.
                } else if (currentType == null && currentVersion != null && incomingType != null) {
                    currentContentTypePred.setType(incomingType);
                }
            }
            if (returnPredicate == null) {
                LOGGER.debug("first return predicate");
                returnPredicate = currentContentTypePred;
            } else {
                LOGGER.debug("ANDing the predicates. Pred1: {} Pred2: {}", returnPredicate, currentContentTypePred);
                returnPredicate = and(returnPredicate, currentContentTypePred);
            }
        } else {
            if (returnPredicate == null) {
                LOGGER.debug("first return predicate");
                returnPredicate = p;
            } else {
                LOGGER.debug("ANDing the predicates. Pred1: {} Pred2: {}", returnPredicate, p);
                returnPredicate = and(returnPredicate, p);
            }
        }
    }
    LOGGER.debug("EXITING: AND filter");
    return returnPredicate;
}
Also used : IncludeFilter(org.opengis.filter.IncludeFilter) Filter(org.opengis.filter.Filter) ArrayList(java.util.ArrayList) ContentTypePredicate(ddf.catalog.pubsub.predicate.ContentTypePredicate) GeospatialPredicate(ddf.catalog.pubsub.predicate.GeospatialPredicate) ContentTypePredicate(ddf.catalog.pubsub.predicate.ContentTypePredicate) EntryPredicate(ddf.catalog.pubsub.predicate.EntryPredicate) TemporalPredicate(ddf.catalog.pubsub.predicate.TemporalPredicate) ContextualPredicate(ddf.catalog.pubsub.predicate.ContextualPredicate) Predicate(ddf.catalog.pubsub.predicate.Predicate)

Example 3 with Predicate

use of ddf.catalog.pubsub.predicate.Predicate in project ddf by codice.

the class PredicateTest method testContextualQueryLeadingWildcard.

@Test
public void testContextualQueryLeadingWildcard() throws Exception {
    Predicate predicate = getPredicate("*test");
    for (String term : Arrays.asList(LEADING_TERM, EMBEDDED_TERM_REVERSED)) {
        for (Character specialChar : ContextualTokenizer.SPECIAL_CHARACTERS_SET) {
            String phrase = String.format(term, specialChar);
            String metadata = String.format(METADATA_FORMAT, StringEscapeUtils.escapeXml(phrase));
            Event testEvent = getEvent(metadata);
            assertThat(phrase + " not matched", predicate.matches(testEvent), is(equalTo(true)));
        }
    }
    for (String term : Arrays.asList(TRAILING_TERM, EMBEDDED_TERM)) {
        for (Character specialChar : ContextualTokenizer.SPECIAL_CHARACTERS_SET) {
            String phrase = String.format(term, specialChar);
            String metadata = String.format(METADATA_FORMAT, StringEscapeUtils.escapeXml(phrase));
            Event testEvent = getEvent(metadata);
            assertThat(phrase + " matched", predicate.matches(testEvent), is(equalTo(false)));
        }
    }
}
Also used : Event(org.osgi.service.event.Event) GeospatialPredicate(ddf.catalog.pubsub.predicate.GeospatialPredicate) ContentTypePredicate(ddf.catalog.pubsub.predicate.ContentTypePredicate) Predicate(ddf.catalog.pubsub.predicate.Predicate) Test(org.junit.Test)

Example 4 with Predicate

use of ddf.catalog.pubsub.predicate.Predicate in project ddf by codice.

the class PredicateTest method testContextualQuery.

@Test
public void testContextualQuery() throws Exception {
    String methodName = "testContextualQuery";
    LOGGER.debug("***************  START: {}  *****************", methodName);
    String searchPhrase = "serengeti event";
    MockQuery query = new MockQuery();
    query.addContextualFilter(searchPhrase, null);
    SubscriptionFilterVisitor visitor = new SubscriptionFilterVisitor();
    Predicate predicate = (Predicate) query.getFilter().accept(visitor, null);
    MetacardImpl metacard = new MetacardImpl();
    metacard.setId("ABC123");
    metacard.setMetadata(TestDataLibrary.getCatAndDogEntry());
    HashMap<String, Object> properties = new HashMap<>();
    properties.put(PubSubConstants.HEADER_ID_KEY, metacard.getId());
    properties.put(PubSubConstants.HEADER_ENTRY_KEY, metacard);
    properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
    Map<String, Object> contextualMap = constructContextualMap(metacard);
    properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
    // Above Pulled from PubSubProviderImpl
    Event testEvent = new Event("topic", properties);
    assertTrue(predicate.matches(testEvent));
    contextualMap.clear();
    properties.clear();
    metacard.setMetadata(TestDataLibrary.getDogEntry());
    Directory index1 = ContextualEvaluator.buildIndex(metacard.getMetadata());
    contextualMap.put("DEFAULT_INDEX", index1);
    contextualMap.put("METADATA", metacard.getMetadata());
    properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
    properties.put(PubSubConstants.HEADER_ID_KEY, metacard.getId());
    properties.put(PubSubConstants.HEADER_ENTRY_KEY, metacard);
    properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
    testEvent = new Event("topic", properties);
    assertFalse(predicate.matches(testEvent));
    LOGGER.debug("***************  END: {}  *****************", methodName);
}
Also used : SubscriptionFilterVisitor(ddf.catalog.pubsub.internal.SubscriptionFilterVisitor) HashMap(java.util.HashMap) Event(org.osgi.service.event.Event) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) GeospatialPredicate(ddf.catalog.pubsub.predicate.GeospatialPredicate) ContentTypePredicate(ddf.catalog.pubsub.predicate.ContentTypePredicate) Predicate(ddf.catalog.pubsub.predicate.Predicate) Directory(org.apache.lucene.store.Directory) Test(org.junit.Test)

Example 5 with Predicate

use of ddf.catalog.pubsub.predicate.Predicate in project ddf by codice.

the class SubscriptionFilterVisitor method visit.

/**
     * During filter maps to a Temporal (Absolute and Modified) search criteria.
     */
@Override
public Object visit(During filter, Object data) {
    LOGGER.debug("ENTERING: During filter");
    AttributeExpressionImpl temporalTypeAttribute = (AttributeExpressionImpl) filter.getExpression1();
    String temporalType = temporalTypeAttribute.getPropertyName();
    LiteralExpressionImpl timePeriodLiteral = (LiteralExpressionImpl) filter.getExpression2();
    Object literal = timePeriodLiteral.getValue();
    Predicate returnPredicate = null;
    if (literal instanceof Period) {
        Period timePeriod = (Period) literal;
        // Extract the start and end dates from the OGC TOverlaps filter
        Date start = timePeriod.getBeginning().getPosition().getDate();
        Date end = timePeriod.getEnding().getPosition().getDate();
        LOGGER.debug("time period lowerBound = {}", start);
        LOGGER.debug("time period upperBound = {}", end);
        LOGGER.debug("EXITING: (temporal) filter");
        returnPredicate = new TemporalPredicate(start, end, DateType.valueOf(temporalType));
    // CREATE RELATIVE
    } else if (literal instanceof PeriodDuration) {
        DefaultPeriodDuration duration = (DefaultPeriodDuration) literal;
        long offset = duration.getTimeInMillis();
        LOGGER.debug("EXITING: (temporal) filter");
        returnPredicate = new TemporalPredicate(offset, DateType.valueOf(temporalType));
    }
    LOGGER.debug("temporalType: " + temporalType);
    LOGGER.debug("Temporal Predicate: " + returnPredicate);
    LOGGER.debug("EXITING: During filter");
    return returnPredicate;
}
Also used : TemporalPredicate(ddf.catalog.pubsub.predicate.TemporalPredicate) AttributeExpressionImpl(org.geotools.filter.AttributeExpressionImpl) LiteralExpressionImpl(org.geotools.filter.LiteralExpressionImpl) Period(org.opengis.temporal.Period) Date(java.util.Date) PeriodDuration(org.opengis.temporal.PeriodDuration) DefaultPeriodDuration(org.geotools.temporal.object.DefaultPeriodDuration) DefaultPeriodDuration(org.geotools.temporal.object.DefaultPeriodDuration) GeospatialPredicate(ddf.catalog.pubsub.predicate.GeospatialPredicate) ContentTypePredicate(ddf.catalog.pubsub.predicate.ContentTypePredicate) EntryPredicate(ddf.catalog.pubsub.predicate.EntryPredicate) TemporalPredicate(ddf.catalog.pubsub.predicate.TemporalPredicate) ContextualPredicate(ddf.catalog.pubsub.predicate.ContextualPredicate) Predicate(ddf.catalog.pubsub.predicate.Predicate)

Aggregations

Predicate (ddf.catalog.pubsub.predicate.Predicate)27 ContentTypePredicate (ddf.catalog.pubsub.predicate.ContentTypePredicate)26 GeospatialPredicate (ddf.catalog.pubsub.predicate.GeospatialPredicate)26 Test (org.junit.Test)17 Event (org.osgi.service.event.Event)16 SubscriptionFilterVisitor (ddf.catalog.pubsub.internal.SubscriptionFilterVisitor)13 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)10 HashMap (java.util.HashMap)10 Filter (org.opengis.filter.Filter)9 ContextualPredicate (ddf.catalog.pubsub.predicate.ContextualPredicate)8 EntryPredicate (ddf.catalog.pubsub.predicate.EntryPredicate)8 TemporalPredicate (ddf.catalog.pubsub.predicate.TemporalPredicate)8 Date (java.util.Date)6 FilterTransformer (org.geotools.filter.FilterTransformer)6 ArrayList (java.util.ArrayList)5 DatatypeFactory (javax.xml.datatype.DatatypeFactory)5 XMLGregorianCalendar (javax.xml.datatype.XMLGregorianCalendar)5 IncludeFilter (org.opengis.filter.IncludeFilter)3 AttributeExpressionImpl (org.geotools.filter.AttributeExpressionImpl)2 LiteralExpressionImpl (org.geotools.filter.LiteralExpressionImpl)2