Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added simple groovy rule discovery (still needs boilerplate DSL to en…
…able rule list creation)
- Loading branch information
1 parent
9386584
commit e1a87ca
Showing
8 changed files
with
435 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 63 additions & 36 deletions
99
engine/groovy/src/main/groovy/org/jboss/windup/addon/groovy/ExampleRuleFile.wrl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,67 @@ | ||
/* | ||
* Copyright 2014 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package org.jboss.windup.addon.groovy | ||
|
||
import org.jboss.windup.addon.config.RulePhase | ||
import org.jboss.windup.addon.config.WindupConfigurationProvider | ||
import org.jboss.windup.addon.config.graphsearch.GraphSearchConditionBuilder | ||
import org.jboss.windup.addon.config.operation.Log | ||
import org.jboss.windup.graph.GraphContext | ||
import org.jboss.windup.graph.model.meta.javaclass.JavaClassMetaModel | ||
import org.ocpsoft.logging.Logger.Level | ||
import org.ocpsoft.rewrite.config.Configuration | ||
import org.ocpsoft.rewrite.config.ConfigurationBuilder | ||
|
||
/** | ||
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a> | ||
* | ||
*/ | ||
class ExampleRuleFile extends WindupConfigurationProvider { | ||
@Override | ||
public RulePhase getPhase() { | ||
return RulePhase.MIGRATION_RULES; | ||
|
||
// ---- A bit of script initialization ---------------------------- | ||
|
||
// use the script binding for silent sentence words like "to", "the" | ||
binding = new CustomBinding() | ||
|
||
|
||
rule "XYZ371" when { | ||
} perform { | ||
} | ||
|
||
|
||
|
||
def r = rule "ruleid1234" when { | ||
println "evaluating"; return true | ||
} perform { println "performing" }; | ||
|
||
r.ex(); | ||
|
||
class RuleBuilder { | ||
String id; | ||
Closure c; | ||
Closure p; | ||
|
||
RuleBuilder(String id) { | ||
this.id = id; | ||
} | ||
|
||
def when (Closure c) { | ||
this.c = c; | ||
return this; | ||
} | ||
|
||
def perform (Closure p) { | ||
this.p = p; | ||
return this; | ||
} | ||
|
||
def ex() | ||
{ | ||
if( c() ) | ||
p(); | ||
} | ||
|
||
def rule (Closure c) { | ||
c() | ||
return this | ||
} | ||
|
||
String toString() { | ||
"ID: $id, Closure: $c" | ||
} | ||
} | ||
|
||
class CustomBinding extends Binding { | ||
def getVariable(String word) { | ||
// return System.out when the script requests to write to 'out' | ||
if (word == "out") System.out | ||
|
||
@Override | ||
public Configuration getConfiguration(GraphContext context) { | ||
return ConfigurationBuilder.begin() | ||
.addRule() | ||
.when( | ||
GraphSearchConditionBuilder.create("javaClasses").ofType(JavaClassMetaModel.class) | ||
) | ||
.perform( | ||
Log.message(Level.INFO, "Sample groovy rule") | ||
) | ||
// don't thrown an exception and return null | ||
// when a silent sentence word is used, | ||
// like "to" and "the" in our DSL | ||
null | ||
} | ||
} | ||
|
||
def rule(ruleID) { return new RuleBuilder(ruleID) } |
79 changes: 65 additions & 14 deletions
79
engine/groovy/src/main/java/org/jboss/windup/addon/groovy/ExampleRuleFile.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,67 @@ | ||
/* | ||
* Copyright 2014 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package org.jboss.windup.addon.groovy | ||
|
||
/** | ||
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a> | ||
* | ||
*/ | ||
class ExampleRuleFile | ||
{ | ||
|
||
// ---- A bit of script initialization ---------------------------- | ||
|
||
// use the script binding for silent sentence words like "to", "the" | ||
binding = new CustomBinding() | ||
|
||
|
||
rule "XYZ371" when { | ||
} perform { | ||
} | ||
|
||
|
||
|
||
def r = rule "ruleid1234" when { | ||
println "evaluating"; return true | ||
} perform { println "performing" }; | ||
|
||
r.ex(); | ||
|
||
class RuleBuilder { | ||
String id; | ||
Closure c; | ||
Closure p; | ||
|
||
RuleBuilder(String id) { | ||
this.id = id; | ||
} | ||
|
||
def when (Closure c) { | ||
this.c = c; | ||
return this; | ||
} | ||
|
||
def perform (Closure p) { | ||
this.p = p; | ||
return this; | ||
} | ||
|
||
def ex() | ||
{ | ||
if( c() ) | ||
p(); | ||
} | ||
|
||
def rule (Closure c) { | ||
c() | ||
return this | ||
} | ||
|
||
String toString() { | ||
"ID: $id, Closure: $c" | ||
} | ||
} | ||
|
||
class CustomBinding extends Binding { | ||
def getVariable(String word) { | ||
// return System.out when the script requests to write to 'out' | ||
if (word == "out") System.out | ||
|
||
// don't thrown an exception and return null | ||
// when a silent sentence word is used, | ||
// like "to" and "the" in our DSL | ||
null | ||
} | ||
} | ||
|
||
def rule(ruleID) { return new RuleBuilder(ruleID) } |
Oops, something went wrong.