Package org.eclipse.epsilon.epl
Class AbstractEplModule
java.lang.Object
org.eclipse.epsilon.common.module.AbstractModuleElement
org.eclipse.epsilon.eol.AbstractModule
org.eclipse.epsilon.eol.EolModule
org.eclipse.epsilon.erl.ErlModule
org.eclipse.epsilon.epl.AbstractEplModule
- All Implemented Interfaces:
IModule,ModuleElement,IEolModule,IEplModule,IErlModule
- Direct Known Subclasses:
EplModule
Base implementation of IEplModule allowing for alternative
extensions / implementations.
- Since:
- 1.6
- Author:
- Sina Madani
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static interfaceprotected static interface -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intprotected intprotected Stringprotected booleanFields inherited from class org.eclipse.epsilon.erl.ErlModule
declaredPost, declaredPre, post, preFields inherited from class org.eclipse.epsilon.eol.EolModule
context, declaredModelDeclarations, declaredOperations, imports, main, modelDeclarations, operations, postOperationStatementsFields inherited from class org.eclipse.epsilon.eol.AbstractModule
parseProblems, parser, sourceFile, sourceUri -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionadapt(AST cst, ModuleElement parentAst) voidprotected org.antlr.runtime.LexercreateLexer(org.antlr.runtime.ANTLRInputStream inputStream) protected PatternMatchModelPatternMatchModel factory method.createParser(org.antlr.runtime.TokenStream tokenStream) protected PatternMatchcreatePatternMatch(Pattern pattern, Collection<? extends Iterable<?>> combination) Converts all roles of the pattern into a PatternMatch with the specified bindings.protected ObjectexecuteDoBlock(ExecutableBlock<?> doBlock, Map<String, Object> roleBindings) Executes the do block with the specified variables.protected Collection<?>filterElements(ExecutableBlock<Boolean> guard, String roleName, Collection<?> currentInstances) Executes the guard block when called bygetAllRoleInstances(Role, String, Collection)protected static final Collection<Variable>flatMapRoleBindings(Collection<Role> roles, Collection<? extends Iterable<?>> combination) This method simply flatmaps the results of calling getVariables(Iterable, Role) for each role in the pattern.protected abstract Iterator<? extends Collection<? extends Iterable<?>>>getCandidates(Pattern pattern) The dimensions of the returned nested Iterables are as follows:
Outer: The number of roles in the pattern (i.e.protected final booleangetMatchResult(Pattern pattern) Gets the result of the match block for the specified pattern.intprotected final Collection<?>getRoleInstances(Role role, String roleName) Executes the role, returning applicable model elements which satisfy the conditions specified in the role.protected static Collection<Variable>getVariables(Iterable<?> bindings, Role role) Binds role names to the objects returned by the bindings iterator.booleanprotected booleanisValidCombination(Pattern pattern, List<? extends Iterable<?>> combination) Validates whether the given combination matches the constraints imposed by the pattern.This method provides the main high-level execution logic for EPL.protected final Optional<PatternMatch>matchCombination(Collection<? extends Iterable<?>> combination, Pattern pattern) Executes the match, onmatch and/or nomatch blocks.final PatternMatchModelprotected Set<PatternMatch>matchPatterns(int level, PatternMatchModel model) Adds all matches returned byIEplModule#match()to the match model for all patterns at the specified level.protected booleannegativeGuard(ExecutableBlock<Boolean> guard, String roleName, Collection<?> currentInstances) Executes the guard block when called bygetNegativeRoleInstances(Role, String, Collection)protected final voidpostProcessMatches(int level, Collection<PatternMatch> matches) Executes the do block for all matched patterns at the specified level.protected voidpreMatch(PatternMatchModel model) Pre-processes the model.protected ObjectMain rule processing logic.protected static final intputRoleBindingsIntoFrame(Collection<Role> roles, Collection<? extends Iterable<?>> combination, Frame frame) Puts the result of#flatMapRoleBindings(Pattern, Collection)into the frame.voidsetMaxLoops(int maxLoops) voidsetRepeatWhileMatches(boolean repeatWhileMatches) protected abstract Collection<?>wrapAdvancedRoleInstances(Role role, String roleName, Collection<?> currentInstances, AbstractEplModule.LazyAdvancedRoleInstancesInitializer initializer) protected abstract Collection<?>wrapBasicRoleInstances(Role role, String roleName, AbstractEplModule.LazyBasicRoleInstancesInitializer initializer) Wraps the result ofpreprocessRoleInstances(Role, String)into the specified collection.Methods inherited from class org.eclipse.epsilon.erl.ErlModule
calculateSuperRules, execute, execute, executeImpl, getDeclaredPost, getDeclaredPre, getPost, getPre, postExecution, prepareExecutionMethods inherited from class org.eclipse.epsilon.eol.EolModule
clearCache, createDebugger, execute, getDeclaredModelDeclarations, getDeclaredOperations, getImportManager, getImports, getMain, getModelDeclarations, getOperations, getParentModule, getPostOperationStatements, loadImports, prepareContext, setContext, setImportManager, setMain, setParentModule, toStringMethods inherited from class org.eclipse.epsilon.eol.AbstractModule
assignAnnotations, assignComment, assignComments, createAst, extractComments, getParseProblems, getSourceFile, getSourceUri, invokeMainRule, parse, parseMethods inherited from class org.eclipse.epsilon.common.module.AbstractModuleElement
equals, getChildren, getComments, getData, getDebugInfo, getFile, getModule, getParent, getRegion, getUri, hashCode, setModule, setParent, setRegion, setUriMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface org.eclipse.epsilon.eol.IEolModule
configure, createDebugger, execute, getConfigurationProperties, getDeclaredModelDeclarations, getDeclaredOperations, getImportManager, getImports, getMain, getModelDeclarations, getOperations, getParentModule, getParseProblems, getPostOperationStatements, parse, setContext, setImportManager, setParentModuleMethods inherited from interface org.eclipse.epsilon.epl.IEplModule
getMaximumLevelMethods inherited from interface org.eclipse.epsilon.erl.IErlModule
getDeclaredPost, getDeclaredPre, getPost, getPreMethods inherited from interface org.eclipse.epsilon.common.module.IModule
createAst, getSourceUri, parse, parse, parse, parse, parseMethods inherited from interface org.eclipse.epsilon.common.module.ModuleElement
getChildren, getComments, getData, getFile, getModule, getParent, getRegion, getUri, setModule, setParent, setRegion, setUri
-
Field Details
-
INFINITE
public static final int INFINITE- See Also:
-
patterns
-
declaredPatterns
-
repeatWhileMatchesFound
protected boolean repeatWhileMatchesFound -
maxLoops
protected int maxLoops -
patternMatchModelName
-
-
Constructor Details
-
AbstractEplModule
public AbstractEplModule() -
AbstractEplModule
-
-
Method Details
-
getContext
- Specified by:
getContextin interfaceIEolModule- Specified by:
getContextin interfaceIErlModule- Overrides:
getContextin classErlModule
-
createLexer
protected org.antlr.runtime.Lexer createLexer(org.antlr.runtime.ANTLRInputStream inputStream) - Overrides:
createLexerin classErlModule
-
createParser
- Overrides:
createParserin classErlModule
-
getMainRule
- Overrides:
getMainRulein classErlModule
-
getImportConfiguration
- Overrides:
getImportConfigurationin classErlModule
-
adapt
-
build
- Specified by:
buildin interfaceModuleElement- Overrides:
buildin classErlModule
-
processRules
Description copied from class:ErlModuleMain rule processing logic. Non-abstract for compatibility.- Overrides:
processRulesin classErlModule- Returns:
- The result of executing this module.
- Throws:
EolRuntimeException
-
getDeclaredPatterns
- Specified by:
getDeclaredPatternsin interfaceIEplModule
-
getPatterns
- Specified by:
getPatternsin interfaceIEplModule
-
getMaxLoops
public int getMaxLoops()- Specified by:
getMaxLoopsin interfaceIEplModule
-
setMaxLoops
public void setMaxLoops(int maxLoops) - Specified by:
setMaxLoopsin interfaceIEplModule
-
isRepeatWhileMatches
public boolean isRepeatWhileMatches()- Specified by:
isRepeatWhileMatchesin interfaceIEplModule
-
setRepeatWhileMatches
public void setRepeatWhileMatches(boolean repeatWhileMatches) - Specified by:
setRepeatWhileMatchesin interfaceIEplModule
-
matchPatterns
- Specified by:
matchPatternsin interfaceIEplModule- Throws:
EolRuntimeException
-
match
This method provides the main high-level execution logic for EPL. The idea is that for each role in the pattern, appropriate bindings are made to the role and executed. Then the match block (and subsequently onMatch) is executed for the role and a PatternMatch is created where applicable.
In essence, this method can be thought of as the executor of all roles in the pattern, and therefore acts as a bridge between the high-level methods (e.g.matchPatterns(int, PatternMatchModel)) and the low-level ones (e.g.getRoleInstances(Role, String)).
Implementation-wise, this method delegates the main execution logic tomatchCombination(Collection, Pattern), and so the sole responsibility of this method is to loop through the combinations returned bygetCandidates(Pattern)and collect all the results. Subclasses may override this method to alter the type of collection returned and/or alter the looping mechanism.- Specified by:
matchin interfaceIEplModule- Throws:
EolRuntimeException
-
matchCombination
protected final Optional<PatternMatch> matchCombination(Collection<? extends Iterable<?>> combination, Pattern pattern) throws EolRuntimeException Executes the match, onmatch and/or nomatch blocks.- Parameters:
combination- The values to use for role bindings.pattern-- Returns:
- A PatternMatch if the criteria was met, empty otherwise.
- Throws:
EolRuntimeException
-
createModel
PatternMatchModel factory method.- Throws:
EolRuntimeException
-
preMatch
Pre-processes the model.- Throws:
EolRuntimeException
-
matchPatterns
protected Set<PatternMatch> matchPatterns(int level, PatternMatchModel model) throws EolRuntimeException Adds all matches returned byIEplModule#match()to the match model for all patterns at the specified level.- Returns:
- The set of pattern matches added to the model.
- Throws:
EolRuntimeException
-
postProcessMatches
protected final void postProcessMatches(int level, Collection<PatternMatch> matches) throws EolRuntimeException Executes the do block for all matched patterns at the specified level. To control the execution of the block itself, subclasses can override theexecuteDoBlock(ExecutableBlock, Map)method.- Throws:
EolRuntimeException
-
executeDoBlock
protected Object executeDoBlock(ExecutableBlock<?> doBlock, Map<String, Object> roleBindings) throws EolRuntimeExceptionExecutes the do block with the specified variables.- Parameters:
doBlock- The block to execute.roleBindings- The effective collection of variables.- Returns:
- The result of the block, usually
null. - Throws:
EolRuntimeException
-
getMatchResult
Gets the result of the match block for the specified pattern.- Parameters:
pattern- the pattern being executedcontext- the context- Returns:
- the result of executing the match block or
trueif the pattern does not define a match block. - Throws:
EolRuntimeException- if the result is not a boolean.
-
createPatternMatch
protected PatternMatch createPatternMatch(Pattern pattern, Collection<? extends Iterable<?>> combination) Converts all roles of the pattern into a PatternMatch with the specified bindings. Note that the size of combinations must be equal to the number of roles in the pattern. The inner (nested) iterable of combinations are the source bindings for each role name.- See Also:
-
#flatMapRoleBindings(Pattern, Iterable)
-
flatMapRoleBindings
protected static final Collection<Variable> flatMapRoleBindings(Collection<Role> roles, Collection<? extends Iterable<?>> combination) This method simply flatmaps the results of calling getVariables(Iterable, Role) for each role in the pattern. It will iterate over the larger ofpattern.getRoles()andcombinationcollections first.- Parameters:
roles- The roles of a pattern.combination- The instances for each binding. Note that the inner iterable's size must be equal to the number of names in the role. However the outer size (that is,combination.size()) need not necessarily be equal to the number of roles in the pattern.- Returns:
- A flattened view of the Collection of Collection of Variables.
-
getVariables
Binds role names to the objects returned by the bindings iterator.- Parameters:
bindings- The elements to map to each role name. Note that the number of objects returned by the iterator must be equal to the number of role names.role-- Returns:
- The variables, effectively a Collection<Map.Entry<String, ?>> where the entry is the role name and value is the model element bound to it.
-
putRoleBindingsIntoFrame
protected static final int putRoleBindingsIntoFrame(Collection<Role> roles, Collection<? extends Iterable<?>> combination, Frame frame) Puts the result of#flatMapRoleBindings(Pattern, Collection)into the frame.- Parameters:
roles-combinations-frame-- Returns:
- The number of variables inserted into the frame.
-
isValidCombination
protected boolean isValidCombination(Pattern pattern, List<? extends Iterable<?>> combination) throws EolRuntimeException Validates whether the given combination matches the constraints imposed by the pattern.- Parameters:
pattern-combination-- Returns:
- Throws:
EolRuntimeException
-
getCandidates
protected abstract Iterator<? extends Collection<? extends Iterable<?>>> getCandidates(Pattern pattern) throws EolRuntimeException The dimensions of the returned nested Iterables are as follows:
Outer: The number of roles in the pattern (i.e.pattern.getRoles().size())
Mid: The number of instances as returned byRoleExecutor#getRoleInstances(Role, String)multiplied by the number of names for that role (i.e.getRoleInstances(role).size()*role.getNames().size()).
Inner: The number of elements which satisfy the guard and domain of the role, holding the value of previous role bindings and variables constant. As such, the dimensions of this inner-most Iterable can vary throughout execution if the role has dependencies on values computed in prior roles.- Parameters:
pattern-- Returns:
- All combinations of model element instances conforming to the constraints imposed by the pattern's roles. Note that in most cases this will not be a Collection, but a generator (lazy Iterator) wrapped into an Iterable for convenience.
- Throws:
EolRuntimeException
-
getRoleInstances
protected final Collection<?> getRoleInstances(Role role, String roleName) throws EolRuntimeException Executes the role, returning applicable model elements which satisfy the conditions specified in the role. Since roles may depend on other roles, which in turn depend on the combination of elements currently bound to those roles, the implementation of this method is non-trivial.
This method structures the execution of a role and for consistency, it cannot be overriden. Subclasses should override the#negativeGuard(ExecutableBlock, Collection)and/or#filterElements(ExecutableBlock, Collection)methods as these involve executing guard blocks for each element. If even greater control is desired (e.g. to change the return type of the collection), subclasses can override the intermediate methods, which are:-
getRoleInstances(Role, String)
-
getNegativeRoleInstances(Role, String, Collection)
-
getAllRoleInstances(Role, String, Collection)
- Parameters:
role-roleName- The name to which instances will be bound when executing the guard block. This will always come fromrole.getNames()- Returns:
- All objects satisfying the constraints of the role.
- Throws:
EolRuntimeException
-
-
wrapBasicRoleInstances
protected abstract Collection<?> wrapBasicRoleInstances(Role role, String roleName, AbstractEplModule.LazyBasicRoleInstancesInitializer initializer) throws EolRuntimeException Wraps the result ofpreprocessRoleInstances(Role, String)into the specified collection.- Parameters:
role-roleName-initializer- Method reference topreprocessRoleInstances(Role, String).- Returns:
- The result of AbstractEplModule.LazyBasicRoleInstancesInitializer.get(Role, String); either directly or wrapped into a custom collection.
- Throws:
EolRuntimeException
-
wrapAdvancedRoleInstances
protected abstract Collection<?> wrapAdvancedRoleInstances(Role role, String roleName, Collection<?> currentInstances, AbstractEplModule.LazyAdvancedRoleInstancesInitializer initializer) throws EolRuntimeException - Parameters:
role-roleName-currentInstances- The values returned fromwrapBasicRoleInstances(Role, String, LazyBasicRoleInstancesInitializer).initializer- Method reference togetNegativeRoleInstances(Role, String, Collection)orgetAllRoleInstances(Role, String, Collection).- Returns:
- The result of LazyAdvancedRoleInstancesInitializer#initialize(Role, String); either directly or wrapped into a custom collection.
- Throws:
EolRuntimeException
-
negativeGuard
protected boolean negativeGuard(ExecutableBlock<Boolean> guard, String roleName, Collection<?> currentInstances) throws EolRuntimeException Executes the guard block when called bygetNegativeRoleInstances(Role, String, Collection)- Parameters:
guard-currentInstances- The model elements of the negative role.- Returns:
- Whether any of the elements in currentInstances satisfy the guard.
- Throws:
EolRuntimeException
-
filterElements
protected Collection<?> filterElements(ExecutableBlock<Boolean> guard, String roleName, Collection<?> currentInstances) throws EolRuntimeException Executes the guard block when called bygetAllRoleInstances(Role, String, Collection)- Parameters:
guard-currentInstances- The model elements of the role.- Returns:
- The subset of model elements satisfying the guard.
- Throws:
EolRuntimeException
-