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
Modifier and TypeClassDescriptionprotected static interface
protected static interface
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
protected int
protected String
protected boolean
Fields inherited from class org.eclipse.epsilon.erl.ErlModule
declaredPost, declaredPre, post, pre
Fields inherited from class org.eclipse.epsilon.eol.EolModule
context, declaredModelDeclarations, declaredOperations, imports, main, modelDeclarations, operations, postOperationStatements
Fields inherited from class org.eclipse.epsilon.eol.AbstractModule
parseProblems, parser, sourceFile, sourceUri
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionadapt
(AST cst, ModuleElement parentAst) void
protected org.antlr.runtime.Lexer
createLexer
(org.antlr.runtime.ANTLRInputStream inputStream) protected PatternMatchModel
PatternMatchModel factory method.createParser
(org.antlr.runtime.TokenStream tokenStream) protected PatternMatch
createPatternMatch
(Pattern pattern, Collection<? extends Iterable<?>> combination) Converts all roles of the pattern into a PatternMatch with the specified bindings.protected Object
executeDoBlock
(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 boolean
getMatchResult
(Pattern pattern) Gets the result of the match block for the specified pattern.int
protected 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.boolean
protected boolean
isValidCombination
(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 PatternMatchModel
protected 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 boolean
negativeGuard
(ExecutableBlock<Boolean> guard, String roleName, Collection<?> currentInstances) Executes the guard block when called bygetNegativeRoleInstances(Role, String, Collection)
protected final void
postProcessMatches
(int level, Collection<PatternMatch> matches) Executes the do block for all matched patterns at the specified level.protected void
preMatch
(PatternMatchModel model) Pre-processes the model.protected Object
Main rule processing logic.protected static final int
putRoleBindingsIntoFrame
(Collection<Role> roles, Collection<? extends Iterable<?>> combination, Frame frame) Puts the result of#flatMapRoleBindings(Pattern, Collection)
into the frame.void
setMaxLoops
(int maxLoops) void
setRepeatWhileMatches
(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, prepareExecution
Methods inherited from class org.eclipse.epsilon.eol.EolModule
checkImports, clearCache, createDebugger, execute, getDeclaredModelDeclarations, getDeclaredOperations, getImportManager, getImports, getImportsByExtension, getMain, getModelDeclarations, getOperations, getParentModule, getPostOperationStatements, prepareContext, setContext, setImportManager, setMain, setParentModule, toString
Methods inherited from class org.eclipse.epsilon.eol.AbstractModule
assignAnnotations, assignComment, assignComments, createAst, extractComments, getParseProblems, getSourceFile, getSourceUri, invokeMainRule, parse, parse
Methods inherited from class org.eclipse.epsilon.common.module.AbstractModuleElement
equals, getChildren, getComments, getData, getDebugInfo, getFile, getModule, getParent, getRegion, getUri, hashCode, setModule, setParent, setRegion, setUri
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods 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, setParentModule
Methods inherited from interface org.eclipse.epsilon.epl.IEplModule
getMaximumLevel
Methods inherited from interface org.eclipse.epsilon.erl.IErlModule
getDeclaredPost, getDeclaredPre, getPost, getPre
Methods inherited from interface org.eclipse.epsilon.common.module.IModule
createAst, getSourceUri, parse, parse, parse, parse, parse
Methods 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:
getContext
in interfaceIEolModule
- Specified by:
getContext
in interfaceIErlModule
- Overrides:
getContext
in classErlModule
-
createLexer
protected org.antlr.runtime.Lexer createLexer(org.antlr.runtime.ANTLRInputStream inputStream) - Overrides:
createLexer
in classErlModule
-
createParser
- Overrides:
createParser
in classErlModule
-
getMainRule
- Overrides:
getMainRule
in classErlModule
-
getImportConfiguration
- Overrides:
getImportConfiguration
in classErlModule
-
adapt
-
build
- Specified by:
build
in interfaceModuleElement
- Overrides:
build
in classErlModule
-
processRules
Description copied from class:ErlModule
Main rule processing logic. Non-abstract for compatibility.- Overrides:
processRules
in classErlModule
- Returns:
- The result of executing this module.
- Throws:
EolRuntimeException
-
getDeclaredPatterns
- Specified by:
getDeclaredPatterns
in interfaceIEplModule
-
getPatterns
- Specified by:
getPatterns
in interfaceIEplModule
-
getMaxLoops
public int getMaxLoops()- Specified by:
getMaxLoops
in interfaceIEplModule
-
setMaxLoops
public void setMaxLoops(int maxLoops) - Specified by:
setMaxLoops
in interfaceIEplModule
-
isRepeatWhileMatches
public boolean isRepeatWhileMatches()- Specified by:
isRepeatWhileMatches
in interfaceIEplModule
-
setRepeatWhileMatches
public void setRepeatWhileMatches(boolean repeatWhileMatches) - Specified by:
setRepeatWhileMatches
in interfaceIEplModule
-
matchPatterns
- Specified by:
matchPatterns
in 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:
match
in 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
true
if 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()
andcombination
collections 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
-