A few questions, mostly about parameterization
wstafford Aug 23, 2016 11:54 AMHello,
Currently I have code that looks something like this:
public class MyCustomProvider extends AbstractRuleProvider{
static ArrayList<String> list = new ArrayList<String>();
static int counter = 0;
public MyCustomProvider() {
super(MetadataBuilder.forProvider(MyCustomProvider.class));
}
@Override
public Configuration getConfiguration(GraphContext context)
{
return ConfigurationBuilder.begin()
.addRule()
.when(XmlFile.matchesXpath("//rf:Transition/@Source").namespace(...).as("test"))
.perform(myMethod("{test}")
.where("test").matches(".*")
.withId("CustomProvider_1");
}
public Operation myMethod(String source) {
if (list.contains(source)) {
counter++;
} else {
list.add(source);
}
return Classification.as("Title: ").withDescription("Count: " + count);
}
}
Now I have a few questions.
First off, the syntax for parameterization is a bit confusing. From what I gather here:
https://github.com/windup/windup/wiki/Rules-Java-based-Rule-Structure
and from this example here:
The name of the variable doesn't need curly braces when:
- it is defined in .as(...)
- when it is customized in .where(...)
- when it is iterated over in Iteration.over(...)
Anywhere else, it seems that it needs curly braces to extract the value. In both the quickstart example and in the rewrite documentation (rewrite/index.asciidoc at master · ocpsoft/rewrite · GitHub) it seems even if you are defining the variable, if it is not within the .as(..) method, it needs curly braces. Please correct me if I am wrong about any of that.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Second, I would like to take the value of the parameter and pass it into a custom method, as shown in the code above:
myMethod("{test}");
Obviously the syntax above does not work. I'd like to know what the best way to go about this is.
What I would like to do, is to take the text from each Source attribute of my Transition tags in my XML file and then do the following:
- Check the text against a list
- If the text is already in the list, increment a counter
- If not, add the text to the list
I'm essentially testing for duplicate text in those tags. I promise that this does have relevance to an application migration.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Lastly, (this might be several discussions worth of questions, sorry about that) my counter is a static field that starts at 0. I've attempted to just increment the counter every time myMethod is called. There are 8 Source attributes in my file. In theory, the counter should then also increment to 8. In reality, the counter only increments twice. To be clear, the test looked like this:
...
static int counter = 0;
...
.when(...)
.perform(myMethod())
...
public Operation myMethod() {
counter++;
return Classification.as("Title: ").withDescription("Count: " + counter);
}
The report that generates then looks like this:
Optimally, I'd want that to only print once, but that's another issue for another time. Right now I'm interested in why the counter is perpetually stuck at two? And also why it doesn't start at 0. I'm having trouble wrapping my head around it.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
In summary, three questions:
- Is my understanding of parameterization correct?
- How can I pass the parameter or the value of the parameter to my method in .perform()?
- What is going on with my counter?
Thanks!