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);
}
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;
}
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)));
}
}
}
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);
}
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;
}
Aggregations