/** * Creates a {@link RuleSpecification} with the given query specification and jobs, * using the {@link DefaultActivationLifeCycle#DEFAULT} life-cycle. * * @param querySpecification * @param jobs */ public static <Match extends IPatternMatch> RuleSpecification<Match> newMatcherRuleSpecification(IQuerySpecification<? extends ViatraQueryMatcher<Match>> querySpecification, Set<Job<Match>> jobs){ return newMatcherRuleSpecification(querySpecification, Lifecycles.getDefault(true, true), jobs); }
/** * Sets the given collection of (partial) matches as an event filter with the given semantics * used by the built observable collection. */ public ObservablePatternMatchCollectionBuilder<M> setFilter(Collection<M> multifilters, ViatraQueryFilterSemantics semantics){ this.filter = Rules.newMultiMatchFilter(multifilters, semantics); return this; }
/** * Sets the given (partial) match as an event filter used by the built observable collection. */ public ObservablePatternMatchCollectionBuilder<M> setFilter(M filter){ this.filter = Rules.newSingleMatchFilter(filter); return this; }
/** * Creates the rule used for updating the results including changes in feature values. * * @param observableCollectionUpdate * the observable collection to handle * @param querySpecification * the {@link IQuerySpecification} used to create the rule */ protected static <Match extends IPatternMatch, Matcher extends ViatraQueryMatcher<Match>> RuleSpecification<Match> createUpdatingRuleSpecification( IObservablePatternMatchCollectionUpdate<Match> observableCollectionUpdate, IQuerySpecification<Matcher> querySpecification) { Set<Job<Match>> jobs = getObservableCollectionJobs(observableCollectionUpdate); Job<Match> updateJob = Jobs.newErrorLoggingJob(new SequentialProcessorsJob<Match>( CRUDActivationStateEnum.UPDATED, Arrays.asList( new ObservableCollectionProcessor<Match>(Direction.DELETE, observableCollectionUpdate), new ObservableCollectionProcessor<Match>(Direction.INSERT, observableCollectionUpdate) ))); jobs.add(updateJob); return Rules.newMatcherRuleSpecification(querySpecification, Lifecycles.getDefault(true, true), jobs); }
/** * Constructor that creates a new ChangeMonitor instance based on the specified ViatraQuery engine. Note that to * monitor changes of a specific model instance, a VIATRA Query should be initialized on said model instance. * * @param engine * The ViatraQueryEngine the monitor is based on. */ public ChangeMonitor(ViatraQueryEngine engine) { super(engine); this.changesBetweenCheckpoints = new HashMap<>(); this.accumulator = new HashMap<>(); allJobs = new HashSet<Job<?>>(); rules = new HashSet<RuleSpecification<IPatternMatch>>(); specs = new HashMap<IQuerySpecification<?>, RuleSpecification<IPatternMatch>>(); started = false; UpdateCompleteBasedSchedulerFactory schedulerFactory = Schedulers.getQueryEngineSchedulerFactory(engine); executionSchema = ExecutionSchemas.createViatraQueryExecutionSchema(engine, schedulerFactory); }
/** * @since 2.0 */ public EventDrivenTransformationRule(String name, IQuerySpecification<Matcher> precondition, Map<CRUDActivationStateEnum, ? extends Consumer<Match>> stateActions, ActivationLifeCycle lifeCycle, EventFilter<? super Match> filter) { this.name = name; Set<Job<Match>> jobs = new HashSet<>(); boolean createdJobAdded = false; for (Entry<CRUDActivationStateEnum, ? extends Consumer<Match>> stateAction : stateActions.entrySet()) { CRUDActivationStateEnum state = stateAction.getKey(); Consumer<Match> action = stateAction.getValue(); jobs.add(Jobs.newStatelessJob(state, action)); if (state == CRUDActivationStateEnum.CREATED) { createdJobAdded = true; } } if (!createdJobAdded) { jobs.add(Jobs.newNopJob(CRUDActivationStateEnum.CREATED)); } this.precondition = precondition; ruleSpecification = Rules.newMatcherRuleSpecification(precondition, lifeCycle, jobs, name); this.filter = filter; }
/** * Returns a RuleSpecification that can be added to a rule engine. */ @Override public RuleSpecification<MATCH> getRuleSpecification() { if (ruleSpec == null) { final Job<MATCH> job = Jobs.newStatelessJob(CRUDActivationStateEnum.CREATED, action); ruleSpec = Rules.newMatcherRuleSpecification(precondition, lifecycle, Collections.singleton(job), getName()); } return ruleSpec; }
/** * Creates a new execution schema that is initialized over the given ViatraQueryEngine, creates an executor and * agenda without rules and prepares a scheduler using the provided factory. * * @param engine * @param schedulerFactory * @return the prepared execution schema */ public static ExecutionSchema createViatraQueryExecutionSchema(final ViatraQueryEngine engine, final ISchedulerFactory schedulerFactory) { return createViatraQueryExecutionSchema(engine, schedulerFactory, Collections.<RuleSpecification<?>>emptySet()); }
/** * Creates a new rule engine that is initialized over the given * ViatraQueryEngine and an agenda with the given (unfiltered) rule specifications. * @param engine * @param specifications * @return the prepared rule engine */ public static RuleEngine createViatraQueryRuleEngine(final ViatraQueryEngine engine, final Set<RuleSpecification<?>> specifications) { Objects.requireNonNull(specifications, "Cannot create rule engine with null rule specification set"); RuleEngine ruleEngine = createViatraQueryRuleEngine(engine); for (RuleSpecification<?> ruleSpecification : specifications) { ruleEngine.addRule(ruleSpecification); } return ruleEngine; }
private static <Match extends IPatternMatch> Set<Job<Match>> getObservableCollectionJobs( IObservablePatternMatchCollectionUpdate<Match> observableCollectionUpdate) { Set<Job<Match>> jobs = new HashSet<>(); Job<Match> insertJob = Jobs.newErrorLoggingJob(Jobs.newStatelessJob(CRUDActivationStateEnum.CREATED, new ObservableCollectionProcessor<Match>(Direction.INSERT, observableCollectionUpdate))); jobs.add(insertJob); Job<Match> deleteJob = Jobs.newErrorLoggingJob(Jobs.newStatelessJob(CRUDActivationStateEnum.DELETED, new ObservableCollectionProcessor<Match>(Direction.DELETE, observableCollectionUpdate))); jobs.add(deleteJob); return jobs; }
/** * Creates the default EVM Jobs which are executed as a new match appears, disappears or is updated. Can be * overridden to specify domain specific functionality * * @return */ protected Set<Job<IPatternMatch>> createDefaultProcessorJobs() { // Define default MatchProcessors Consumer<IPatternMatch> appearProcessor = this::registerAppear; Consumer<IPatternMatch> disappearProcessor = this::registerDisappear; Consumer<IPatternMatch> updateProcessor = this::registerUpdate; // Create Jobs Set<Job<IPatternMatch>> jobs = new HashSet<>(); Job<IPatternMatch> appear = new StatelessJob<IPatternMatch>(CRUDActivationStateEnum.CREATED, appearProcessor); Job<IPatternMatch> disappear = new StatelessJob<IPatternMatch>(CRUDActivationStateEnum.DELETED, disappearProcessor); Job<IPatternMatch> update = new StatelessJob<IPatternMatch>(CRUDActivationStateEnum.UPDATED, updateProcessor); jobs.add(Jobs.newEnableJob(appear)); jobs.add(Jobs.newEnableJob(disappear)); jobs.add(Jobs.newEnableJob(update)); allJobs.addAll(jobs); return jobs; }
public EventDrivenTransformationRule<Match, Matcher> build() { Preconditions.checkState(!stateActions.isEmpty(), "The rule has no actions added."); if (lifeCycle == null) { lifeCycle = Lifecycles.getDefault(isUpdateJobAdded, isDeleteJobAdded); } return createRule(name, precondition, stateActions, lifeCycle, filter); } }
public static <Match extends IPatternMatch> RuleSpecification<Match> newMatcherRuleSpecification(ViatraQueryMatcher<Match> matcher, ActivationLifeCycle lifecycle, Set<Job<Match>> jobs){ FavouredMatcherSourceSpecification<Match> sourceSpecification = new FavouredMatcherSourceSpecification<Match>(matcher); return new RuleSpecification<Match>(sourceSpecification, lifecycle, jobs); }
/** * Creates the rule used for updating the results. * * @param observableCollectionUpdate * the observable collection to handle * @param matcher * the {@link ViatraQueryMatcher} used to create the rule */ protected static <Match extends IPatternMatch, Matcher extends ViatraQueryMatcher<Match>> RuleSpecification<Match> createRuleSpecification( IObservablePatternMatchCollectionUpdate<Match> observableCollectionUpdate, Matcher matcher) { Set<Job<Match>> jobs = getObservableCollectionJobs(observableCollectionUpdate); return Rules.newMatcherRuleSpecification(matcher, Lifecycles.getDefault(false, true), jobs); }
/** * Reuse rule engine if it was already created for this query engine to handle observable collections. * */ private static RuleEngine getRuleEngine(ViatraQueryEngine engine) { Map<ViatraQueryEngine, WeakReference<RuleEngine>> ruleEngineMap = getInstance().getQueryToRuleEngineMap(); if(ruleEngineMap.containsKey(engine)){ WeakReference<RuleEngine> ruleEngineRef = ruleEngineMap.get(engine); RuleEngine ruleEngine = ruleEngineRef.get(); if(ruleEngine != null){ return ruleEngine; } } RuleEngine ruleEngine = ExecutionSchemas.createViatraQueryExecutionSchema(engine, Schedulers.getQueryEngineSchedulerFactory(engine)); ruleEngineMap.put(engine, new WeakReference<RuleEngine>(ruleEngine)); return ruleEngine; }
/** * Creates a new execution schema that is initialized over the given {@link ViatraQueryEngine}, * {@link ISchedulerFactory} and {@link ConflictResolver} creates an executor and agenda without rules, prepares a * scheduler using the provided factory and sets its scheduler factory accordingly. * * @param engine * @param schedulerFactory * @return the prepared execution schema */ public static ExecutionSchema createViatraQueryExecutionSchema(final ViatraQueryEngine engine, final ISchedulerFactory schedulerFactory, final ConflictResolver conflictResolver) { ExecutionSchema schema = createViatraQueryExecutionSchema(engine, schedulerFactory); schema.setConflictResolver(conflictResolver); return schema; }
private BatchTransformation doBuild() { final IExecutor executor = new Executor(); RuleEngine ruleEngine = RuleEngines.createViatraQueryRuleEngine(engine); return new BatchTransformation(ruleEngine, engine, executor); }
public static <Match extends IPatternMatch> RuleSpecification<Match> newMatcherRuleSpecification(ViatraQueryMatcher<Match> matcher, ActivationLifeCycle lifecycle, Set<Job<Match>> jobs, String name){ FavouredMatcherSourceSpecification<Match> sourceSpecification = new FavouredMatcherSourceSpecification<Match>(matcher); return new RuleSpecification<Match>(sourceSpecification, lifecycle, jobs, name); }
/** * Creates the rule used for updating the results. * * @param observableCollectionUpdate * the observable collection to handle * @param querySpecification * the {@link IQuerySpecification} used to create the rule */ protected static <Match extends IPatternMatch, Matcher extends ViatraQueryMatcher<Match>> RuleSpecification<Match> createRuleSpecification( IObservablePatternMatchCollectionUpdate<Match> observableCollectionUpdate, IQuerySpecification<Matcher> querySpecification) { Set<Job<Match>> jobs = getObservableCollectionJobs(observableCollectionUpdate); return Rules.newMatcherRuleSpecification(querySpecification, Lifecycles.getDefault(false, true), jobs); }
/** * Public method used for adding new rules to the monitor. Based on the QuerySpecification provided here, a new Rule * will be added to the monitor. This method can be used both before and after monitoring has been started. * * @param spec * QuerySpecification to be added to the Monitor */ public void addRule(IQuerySpecification<?> spec) { RuleSpecification<IPatternMatch> rule = Rules.newMatcherRuleSpecification( (IQuerySpecification<? extends ViatraQueryMatcher<IPatternMatch>>) spec, Lifecycles.getDefault(true, true), createDefaultProcessorJobs()); specs.put(spec, rule); addRule(rule); }