Skip to content

Commit

Permalink
Added simple groovy rule discovery (still needs boilerplate DSL to en…
Browse files Browse the repository at this point in the history
…able rule list creation)
  • Loading branch information
lincolnthree committed May 30, 2014
1 parent 9386584 commit e1a87ca
Show file tree
Hide file tree
Showing 8 changed files with 435 additions and 85 deletions.
Expand Up @@ -7,34 +7,41 @@ public enum RulePhase
*/
DISCOVERY(100),
/**
* Called to perform basic analysis (extract all method names from class files, extract metadata from xml files, etc)
* Called to perform basic analysis (extract all method names from class files, extract metadata from xml files,
* etc)
*/
INITIAL_ANALYSIS(200),
/**
* Perform high-level composition operations on the graph.
*
* Eg, these may attach metadata from XML files to related Java classes, or perform other high-level graph operations, now that
* all metadata has been extracted
* Eg, these may attach metadata from XML files to related Java classes, or perform other high-level graph
* operations, now that all metadata has been extracted
*/
COMPOSITION(300),
/**
* Migration rules will attach data to the graph associated with migration. This could include:
*
* - Hints to migrators for manual migration
* - Automated migration of schemas or source segments
* - Blacklists to indicate vendor specific APIs
* - Hints to migrators for manual migration - Automated migration of schemas or source segments - Blacklists to
* indicate vendor specific APIs
*/
MIGRATION_RULES(400),
/**
* Reporting visitors produce reports based upon the information contained within the graph
*/
REPORTING(500);


REPORTING(500),

/**
* Clean up resources and close streams.
*/
FINALIZE(600);

private int priority;
RulePhase(int priority) {

RulePhase(int priority)
{
this.priority = priority;
}

public int getPriority()
{
return priority;
Expand Down
59 changes: 37 additions & 22 deletions engine/groovy/pom.xml
Expand Up @@ -7,33 +7,35 @@
<artifactId>windup-engine-parent</artifactId>
<version>2.0.0-SNAPSHOT</version>
</parent>

<groupId>org.jboss.windup.addon</groupId>
<artifactId>groovy</artifactId>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jboss.forge.furnace.container</groupId>
<artifactId>cdi</artifactId>
<version>2.6.0.Final</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- Project deps -->
<dependency>
<groupId>org.jboss.windup.addon</groupId>
<artifactId>config</artifactId>
<classifier>forge-addon</classifier>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.windup.addon</groupId>
<artifactId>config-api</artifactId>
<version>${project.version}</version>
<classifier>forge-addon</classifier>
<scope>provided</scope>
</dependency>

<!-- Forge -->
<dependency>
<groupId>org.jboss.forge.furnace.container</groupId>
<artifactId>cdi</artifactId>
<classifier>forge-addon</classifier>
<scope>provided</scope>
</dependency>



<dependency>
<groupId>org.jboss.forge.furnace.test</groupId>
<artifactId>furnace-test-harness</artifactId>
Expand All @@ -44,24 +46,29 @@
<artifactId>arquillian-furnace-classpath</artifactId>
<scope>test</scope>
</dependency>

<!-- Groovy -->
<dependency>
<artifactId>groovy-all</artifactId>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.jboss.windup.addon</groupId>
<artifactId>graph</artifactId>
<version>${project.version}</version>
<classifier>forge-addon</classifier>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<resources>
<resource>
<directory>src/main/groovy</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>

<plugins>
<!-- Forge -->
<plugin>
<groupId>org.jboss.forge.furnace</groupId>
<artifactId>furnace-maven-plugin</artifactId>
Expand All @@ -70,8 +77,12 @@
<execution>
<id>generate-dot</id>
<phase>prepare-package</phase>
<goals> <goal>generate-dot</goal> </goals>
<configuration> <attach>true</attach> </configuration>
<goals>
<goal>generate-dot</goal>
</goals>
<configuration>
<attach>true</attach>
</configuration>
</execution>
</executions>
</plugin>
Expand All @@ -81,8 +92,12 @@
<execution>
<id>create-forge-addon</id>
<phase>package</phase>
<goals> <goal>jar</goal> </goals>
<configuration> <classifier>forge-addon</classifier> </configuration>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>forge-addon</classifier>
</configuration>
</execution>
</executions>
</plugin>
Expand Down
@@ -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) }
@@ -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) }

0 comments on commit e1a87ca

Please sign in to comment.