protected <Match extends IPatternMatch> ViatraQueryEventSource<Match> createSource( EventSourceSpecification<Match> sourceSpecification) { Preconditions.checkArgument(sourceSpecification instanceof ViatraQueryEventSourceSpecification, "Source definition must be ViatraQueryEventSourceSpecification!"); ViatraQueryEventSource<Match> eventSource = new ViatraQueryEventSource<Match>(this, (ViatraQueryEventSourceSpecification<Match>) sourceSpecification); eventSource.prepareSource(); return eventSource; }
/** * Initializes an attribute monitor listener * @return the prepared attribute monitor listener; must not be null */ protected IAttributeMonitorListener<Match> prepareAttributeMonitorListener() { return atom -> notifyHandlers(new ViatraQueryEvent<Match>(CRUDEventTypeEnum.UPDATED, atom)); }
@Override protected void prepareSource() { this.attributeMonitorListener = prepareAttributeMonitorListener(); this.matchUpdateListener = prepareMatchUpdateListener(); }
@Override protected void prepareEventHandler() { super.prepareEventHandler(); attributeMonitor = Objects.requireNonNull(prepareAttributeMonitor(), "Prepared attribute monitor is null!"); ViatraQueryEventSource<Match> eventSource = (ViatraQueryEventSource<Match>) getSource(); eventSource.addHandler(this); attributeMonitor.addAttributeMonitorListener(eventSource.getAttributeMonitorListener()); unregisterListener = Objects.requireNonNull(prepareActivationNotificationListener(), "Prepared activation notification listener is null!"); getInstance().addActivationNotificationListener(unregisterListener, false); }
@Override protected void beforeHandlerAdded(EventHandler<Match> handler, boolean handlersEmpty) { resendEventsForExistingMatches(handler); if(handlersEmpty) { ((AdvancedViatraQueryEngine)this.matcher.getEngine()).addMatchUpdateListener(this.matcher, matchUpdateListener, false); } }
protected AttributeMonitor<Match> prepareAttributeMonitor(){ //return new DefaultAttributeMonitor<Match>(); LightweightAttributeMonitor<Match> monitor = null; ViatraQueryEventSource<Match> eventSource = (ViatraQueryEventSource<Match>) getSource(); try { monitor = new LightweightAttributeMonitor<Match>(eventSource.getMatcher().getEngine().getBaseIndex()); } catch (ViatraQueryException e) { ViatraQueryLoggingUtil.getLogger(getClass()).error("Error happened while accessing base index", e); } return monitor; }
@Override public void dispose() { getInstance().removeActivationNotificationListener(unregisterListener); ((ViatraQueryEventSource<Match>) getSource()).removeHandler(this); attributeMonitor.dispose(); }
/** * Initializes the corresponding match update listener * @return the prepared update listener; must not be null */ protected IMatchUpdateListener<Match> prepareMatchUpdateListener(){ Consumer<Match> matchAppearProcessor = match -> notifyHandlers(new ViatraQueryEvent<Match>(CRUDEventTypeEnum.CREATED, match)); Consumer<Match> matchDisppearProcessor = match -> notifyHandlers(new ViatraQueryEvent<Match>(CRUDEventTypeEnum.DELETED, match)); return new MatchUpdateAdapter<Match>(matchAppearProcessor, matchDisppearProcessor); }