Traversing Java JDT AST and Building java tree - java

The default implementation of ASTVisitor traverses in a depth first manner. So one can get the source file in order of occurrence. My requirement is to build a java tree out of the ASTNode
I am motivated by the following
API to compare AST?
http://www.programcreek.com/2011/11/use-jdt-astparser-to-parse-java-file/
Eclipse Abstract Syntax Tree Diff
public class AST_Json_Bulid_tree
{
static private List sourcecodeStatObj = new LinkedList();
public static void parse(String str, final String path)
{
ASTParser parser = ASTParser.newParser(AST.JLS8 );
parser.setSource(str.toCharArray());
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setResolveBindings(true); // we need bindings later on
final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
try{
cu.accept(new ASTVisitor()
{
public boolean visit(ForStatement node)
{
List<String> initializer = node.initializers();
Expression exp = node.getExpression();
String expression = "";
List updater = node.updaters();
int length = node.getBody().getLength();
int startposition = node.getStartPosition();
int startLineNumber = cu.getLineNumber(node.getStartPosition());
int nodeLength = node.getLength();
int endLineNumber = cu.getLineNumber(node.getStartPosition() + nodeLength);
boolean isexpNull = false ;
forcount += 1;
if(exp == null)
{
isexpNull = true;
}
if(!isexpNull)
{
this.names.add(exp.toString());
expression = exp.toString();
}
Map forobj=new LinkedHashMap();
forobj.put("Node_Type", "FOR_STATEMENT");
forobj.put("keyword", "for");
forobj.put("Initializers", initializer.toString());
forobj.put("Expression", expression);
forobj.put("Updater", updater.toString());
forobj.put("CF.BeginLine, CF.EndLine", "(" + startLineNumber + ", " + endLineNumber + ")");
String astjsonText = JSONValue.toJSONString(forobj);
try
{
sourcecodeStatObj.add(forobj);
}
catch (Exception e)
{
e.printStackTrace();
}
//Accept the visit of the other parts
//super.visit(node);
if(!isexpNull)
{
exp.accept(this);
}
for (Iterator it = node.initializers().iterator(); it.hasNext(); )
{
Expression e = (Expression) it.next();
e.accept(this);
}
for (Iterator it = node.updaters().iterator(); it.hasNext(); )
{
Expression e = (Expression) it.next();
e.accept(this);
}
node.getBody().accept(this);
return false; // continue
}
public boolean visit(WhileStatement node)
{
// Similar code goes here
return false; // don't continue to visit
}
public boolean visit(DoStatement node)
{
// Similar code goes here
return false; // don't continue to visit
}
public boolean visit (IfStatement node)
{
// Similar code goes here
return false; // don't continue to visit
}
public boolean visit(MethodDeclaration node)
{
// Similar code goes here
return false;
}
public boolean visit(TypeDeclaration node)
{
// Similar code goes here
return true;
}
public boolean visit(AnnotationTypeDeclaration node)
{
// Similar code goes here
return true;
}
public boolean visit(PackageDeclaration node)
{
// Similar code goes here
return false;
}
public boolean visit(EnumDeclaration node)
{
// Similar code goes here
return true;
}
#SuppressWarnings("static-access")
public boolean visit(Block node)
{
// Similar code goes here
return true;
}
public boolean visit(ImportDeclaration node)
{
// Similar code goes here
return false;
}
//Other STATEMENTs goes here
});
}
finally
{
writeJsonFile(path);
}
}
}
So here the sourcecodeStatObj contains all the items in order of visited i.e depth first search.
other class's and methods' constructs are omitted here.
I would appreciate if you give me the direction and the twist!
Thank you!

Related

Algorithm or code to print the binary tree of agents

I try to write code in jade (Java Agent DEvelopment Framework) to print the binary tree of agents in sma. I want to print tree inorder traversal with origin algorithm:
Traverse the left subtree, i.e., call Inorder(left-subtree)
Visit the root.
Traverse the right subtree, i.e., call Inorder(right-subtree)
And my node agent is:
class Noeud {
private Integer value = null;
private AID gauche = null;
private AID droit = null;
private AID pere = null;
}
In my agent to print, I use CyclicBehaviour as extensions. With my code:
public class ReceiveAffichageBehaviour extends CyclicBehaviour{
#Override
public void action() {
MessValueGauche();
MessValue();
MessValueDroit();
}
private void MessValueGauche() {
MessageTemplate messageTemplate = MessageTemplate.and(
MessageTemplate.MatchOntology(Constants.AFFICHAGE_VALUE_GAUCHE),
MessageTemplate.MatchPerformative(ACLMessage.REQUEST)
);
ACLMessage receiveMsg = myAgent.receive(messageTemplate);
if (receiveMsg != null) {
String contenu = receiveMsg.getContent() + "(";
ACLMessage message = new ACLMessage(ACLMessage.REQUEST);
if (noeud.getGauche() == null) {
message.setOntology(Constants.AFFICHAGE_VALUE);
message.addReceiver(myAgent.getAID());
} else {
message.setOntology(Constants.AFFICHAGE_VALUE_GAUCHE);
message.addReceiver(noeud.getGauche());
}
message.setContent(contenu);
myAgent.send(message);
} else {
block();
}
}
private void MessValue() {
MessageTemplate messageTemplate = MessageTemplate.and(
MessageTemplate.MatchOntology(Constants.AFFICHAGE_VALUE),
MessageTemplate.MatchPerformative(ACLMessage.REQUEST)
);
ACLMessage receiveMsg = myAgent.receive(messageTemplate);
if (receiveMsg != null) {
String contenu = receiveMsg.getContent() + (noeud.getValue() == null ? "" : noeud.getValue().toString());
ACLMessage message = new ACLMessage(ACLMessage.REQUEST);
if (noeud.getDroit() == null){
message.setOntology(Constants.AFFICHAGE_VALUE_DROIT);
message.addReceiver(myAgent.getAID());
} else if (noeud.getDroit().equals(receiveMsg.getSender())){
message.setOntology(Constants.AFFICHAGE_VALUE_DROIT);
message.addReceiver(noeud.getPere());
} else {
message.setOntology(Constants.AFFICHAGE_VALUE_GAUCHE);
message.addReceiver(noeud.getDroit());
}
message.setContent(contenu);
myAgent.send(message);
} else {
block();
}
}
private void MessValueDroit() {
MessageTemplate messageTemplate = MessageTemplate.and(
MessageTemplate.MatchOntology(Constants.AFFICHAGE_VALUE_DROIT),
MessageTemplate.MatchPerformative(ACLMessage.REQUEST)
);
ACLMessage receiveMsg = myAgent.receive(messageTemplate);
if (receiveMsg != null) {
String contenu = receiveMsg.getContent() + ")";
ACLMessage message = new ACLMessage(ACLMessage.REQUEST);
message.setContent(contenu);
if (noeud.getDroit() == null && noeud.getGauche() == null ){
if (noeud.getPere().equals(Constants.AID_RACINE)){
message.setPerformative(ACLMessage.CONFIRM);
} else {
message.setOntology(Constants.AFFICHAGE_VALUE_GAUCHE);
}
message.addReceiver(noeud.getPere());
}
myAgent.send(message);
} else {
block();
}
}
I don't know to fix the infini loop in my code. Who can help me the detailed algorithm instead the general? Or could fix my bug in code? Thank you!
A cyclicBehaviour is infinite by nature. Do not use a cyclic if you want the behaviour to stop.
Just use a simpleBehaviour and set the finished boolean at true when you want to remove it from the agent.

Unable to push data read from file onto generic stack

I am trying to make a generic class called reverser that takes the contents of a text file and reverses the order of the contents. I am required to do this by using the generic stack class provided. I have to make sure that it works for two types, strings, and floats as they have been instantiated in main.
I am aware I can't push int values (as is returned by BufferedInputStream) onto the stack. My question is what may be a solution to this problem, should I instantiate two different versions of the generic stack class, one for strings and one for floats, or is there another solution?
public class project_5
{
public static void main(String[] args) throws Exception
{
Reverser<String> reversePoem = new Reverser<String>();
Reverser<Float> majiGame = new Reverser<Float>();
}
}
class Reverser<E>
{
private Stack<E> tempStack;
int k = 0;
int val = 0;
public Reverser()
{
tempStack = new Stack<E>();
}
void FileToStack(String fileIn) throws Exception
{
E item = null;
try
{
BufferedInputStream readFile = new BufferedInputStream(new FileInputStream(fileIn));
for(k = 0; (val = readFile.read()) != -1; k++)
{
tempStack.push(val);
}
readFile.close();
}
catch(FileNotFoundException e)
{
System.out.print("File: " + fileIn + " Not Found");
}
catch(IOException e)
{
System.out.print("Reached end of file.");
}
}
void StackToFile(String fileIn)
{
try
{
PrintWriter fileOut = new PrintWriter(fileIn);
fileOut.println();
fileOut.close();
}
catch(FileNotFoundException e)
{
}
}
}
//Class Stack ---------------------------------------
class Stack<E>
{
// pointer to first node in stack
private Node<E> top;
// constructor
public Stack()
{
top = null;
}
public boolean isEmpty()
{
return top == null;
}
public void push(E data)
{
if (data == null)
return;
// build a node and place it on the stack
Node<E> newNode = new Node<E>(data);
newNode.next = top;
top = newNode;
}
public E pop()
{
Node<E> temp;
temp = top;
if (isEmpty())
return null;
top = top.next;
return temp.getData();
}
// console display
public String toString()
{
Node<E> p;
String showAll = "";
// Display all the nodes in the stack
for( p = top; p != null; p = p.next )
showAll += p.toString() + "\n";
return showAll;
}
}
Edit: Removed pictures, added code in text format.
I have made a change to your logic where you are reading a file and then putting into the stack,
You have not put your Node class so I have added that accordingly
You need to typecast your value when reading from file
Note: in case of the file contains improper data you will get a casting error, that you need to handle
source.txt
1 2 3 4 5
target.txt
5
4
3
2
1
Code
import java.io.*;
import java.util.Arrays;
public class Test
{
public static void main(String[] args) throws Exception
{
Reverser<String> reversePoem = new Reverser<String>(String.class);
Reverser<Float> majiGame = new Reverser<Float>(Float.class);
majiGame.FileToStack("source.txt");
majiGame.StackToFile("target.txt");
}
}
class Reverser<E>
{
private Stack<E> tempStack;
private Class<E> tClass;
public Reverser(Class<E> tClass)
{
tempStack = new Stack<E>();
this.tClass = tClass;
}
public static <E> E convertInstanceOfObject(String value, Class<E> clazz) {
try {
// add custom casting, casting could be varies as par usage
if (clazz.getName().equals("java.lang.Integer")) {
return (E)Integer.valueOf(value);
} else if (clazz.getName().equals("java.lang.Float")) {
return (E)Float.valueOf(value);
} else if (clazz.getName().equals("java.lang.String")) {
return (E)value;
}
return clazz.cast(value);
} catch(ClassCastException e) {
return null;
}
}
void FileToStack(String fileIn) throws Exception
{
BufferedReader br;
try
{
br = new BufferedReader(new FileReader(fileIn));
String line = null;
while((line = br.readLine()) != null)
{
String[] valueArray= line.split(" ");
for (String v : valueArray) {
E item = convertInstanceOfObject(v, tClass);
tempStack.push(item);
}
}
br.close();
}
catch(FileNotFoundException e)
{
System.out.print("File: " + fileIn + " Not Found");
}
catch(IOException e)
{
System.out.print("Reached end of file.");
}
}
void StackToFile(String fileIn)
{
try
{
PrintWriter fileOut = new PrintWriter(new FileWriter(fileIn));
E d = null;
while (!tempStack.isEmpty()) {
d = tempStack.pop();
fileOut.println(d);
}
fileOut.close();
}
catch(IOException e)
{
}
}
}
class Node<E> {
Node<E> next;
private E data;
public Node(E data) {
this.data = data;
}
public Node<E> getNext() {
return next;
}
public void setNext(Node<E> next) {
this.next = next;
}
public E getData() {
return data;
}
public void setData(E data) {
this.data = data;
}
}
//Class Stack ---------------------------------------
class Stack<E>
{
// pointer to first node in stack
private Node<E> top;
// constructor
public Stack()
{
top = null;
}
public boolean isEmpty()
{
return top == null;
}
public void push(E data)
{
if (data == null)
return;
// build a node and place it on the stack
Node<E> newNode = new Node<E>(data);
newNode.next = top;
top = newNode;
}
public E pop()
{
Node<E> temp;
temp = top;
if (isEmpty())
return null;
top = top.next;
return temp.getData();
}
// console display
public String toString()
{
Node<E> p;
String showAll = "";
// Display all the nodes in the stack
for( p = top; p != null; p = p.next )
showAll += p.toString() + "\n";
return showAll;
}
}

How to fix Stack Overflow from recursive getHeight method

When I run my code for a method calculating the height of a binary search tree, it results in a Stack Overflow error, but only for trees with more than one node (BSTElements in my program). I have read that this is due to a faulty recursive call, but cannot identify the problem in my code.
public int getHeight() {
return getHeight(this.getRoot());
}
private int getHeight(BSTElement<String,MorseCharacter> element) {
int height=0;
if (element == null) {
return -1;
}
int leftHeight = getHeight(element.getLeft());
int rightHeight = getHeight(element.getRight());
if (leftHeight > rightHeight) {
height = leftHeight;
} else {
height = rightHeight;
}
return height +1;
}
Here is full code:
public class MorseCodeTree {
private static BSTElement<String, MorseCharacter> rootElement;
public BSTElement<String, MorseCharacter> getRoot() {
return rootElement;
}
public static void setRoot(BSTElement<String, MorseCharacter> newRoot) {
rootElement = newRoot;
}
public MorseCodeTree(BSTElement<String,MorseCharacter> element) {
rootElement = element;
}
public MorseCodeTree() {
rootElement = new BSTElement("Root", "", new MorseCharacter('\0', null));
}
public int getHeight() {
return getHeight(this.getRoot());
}
private int getHeight(BSTElement<String,MorseCharacter> element) {
if (element == null) {
return -1;
} else {
int leftHeight = getHeight(element.getLeft());
int rightHeight = getHeight(element.getRight());
if (leftHeight < rightHeight) {
return rightHeight + 1;
} else {
return leftHeight + 1;
}
}
}
public static boolean isEmpty() {
return (rootElement == null);
}
public void clear() {
rootElement = null;
}
public static void add(BSTElement<String,MorseCharacter> newElement) {
BSTElement<String, MorseCharacter> target = rootElement;
String path = "";
String code = newElement.getKey();
for (int i=0; i<code.length(); i++) {
if (code.charAt(i)== '.') {
if (target.getLeft()!=null) {
target=target.getLeft();
} else {
target.setLeft(newElement);
target=target.getLeft();
}
} else {
if (target.getRight()!=null) {
target=target.getRight();
} else {
target.setRight(newElement);
target=target.getRight();
}
}
}
MorseCharacter newMorseChar = newElement.getValue();
newElement.setLabel(Character.toString(newMorseChar.getLetter()));
newElement.setKey(Character.toString(newMorseChar.getLetter()));
newElement.setValue(newMorseChar);
}
public static void main(String[] args) {
MorseCodeTree tree = new MorseCodeTree();
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(file));
String line = reader.readLine();
while (line != null) {
String[] output = line.split(" ");
String letter = output[0];
MorseCharacter morseCharacter = new MorseCharacter(letter.charAt(0), output[1]);
BSTElement<String, MorseCharacter> bstElement = new BSTElement(letter, output[1], morseCharacter);
tree.add(bstElement);
line = reader.readLine();
System.out.println(tree.getHeight());
}
reader.close();
} catch (IOException e) {
System.out.println("Exception" + e);
}
There doesn't appear to be anything significantly wrong1 with the code that you have shown us.
If this code is giving a StackOverflowException for a small tree, that most likely means that your tree has been created incorrectly and has a cycle (loop) in it. If your recursive algorithm encounters a cycle in the "tree" it will loop until the stack overflows2.
To be sure of this diagnosis, we need to see an MVCE which includes all code needed to construct an example tree that exhibits thie behavior.
1 - There is possibly an "off by one" error in the height calculation, but that won't cause a stack overflow.
2 - Current Java implementations do not do tail-call optimization.

Function based expression message renderer

I'm doing a simple MessageRenderer.
It's specification:
Render message based on an Context (it's a map that's contains all key/value pair parameters)
Supports simple render such as: Your username is << username >>. Assume username in the context is barcelona and the result will be Your username is Barcelona.
Supported function-like object. Example: Current time is << now() >>, now(): is an object that will returns a string of current date time. And result will be: Current time is 2011-05-30
Each parameter of function can also be templated: Current time is << now( << date_format >> ) >> . This template returns a string of current date time with format is the value of key 'date_format' retrieved from the Context. Assume date_format in Context is dd/MM/yyyy and the result will be: Current time is 30/05/2011
Each parameter of function can also be templated with a different method call: Time is << now_locale ( << getLocale() >> ). Assume that getLocale() is an function object that will be return a locale is en_US and the result will be: Time is 2011/05/30 11:20:34 PM
Template can be nested. Example: Your user name is << << username >> >>. It means, Key username has value param1, Key param1 has value is barcelona so the final result will be: Your user name is Barcelona.
My classes and interfaces:
RenderContext.java
public interface RenderContext {
public String getParameter(String key);
}
MessageRenderer.java
public interface MessageRenderer {
public String render(String s, RenderContext... context);
}
MethodExpressionEvaluator.java
// Using this class to implements the method evaluation, such as now(), now_locale()
public interface MethodExpressionEvaluator {
public String evaluate(String[] methodParams, RenderContext... context);
}
AbstractMessageRenderer.java
public abstract class AbstractMessageRenderer implements MessageRenderer {
public static final String DEFAULT_NULL = "###";
public static final String PLACEHOLDER_START_TOKEN = "<<";
public static final String PLACEHOLDER_END_TOKEN = ">>";
protected int lenPlaceholderStartToken = 0;
protected int lenPlaceholderEndToken = 0;
protected String nullToken;
protected String placeholderStartToken;
protected String placeholderEndToken;
protected boolean escape = true;
public AbstractMessageRenderer() {
placeholderStartToken = PLACEHOLDER_START_TOKEN;
placeholderEndToken = PLACEHOLDER_END_TOKEN;
lenPlaceholderStartToken = placeholderStartToken.length();
lenPlaceholderEndToken = placeholderEndToken.length();
nullToken = DEFAULT_NULL;
}
public String getNullToken() {
return nullToken;
}
public void setNullToken(String defaultNull) {
this.nullToken = defaultNull;
}
public String getPlaceholderStartToken() {
return placeholderStartToken;
}
public void setPlaceholderStartToken(String placeholderStartToken) {
this.placeholderStartToken = placeholderStartToken;
lenPlaceholderStartToken = placeholderStartToken.length();
}
public String getPlaceholderEndToken() {
return placeholderEndToken;
}
public void setPlaceholderEndToken(String placeholderEndToken) {
this.placeholderEndToken = placeholderEndToken;
lenPlaceholderEndToken = placeholderEndToken.length();
}
public boolean isEscape() {
return escape;
}
public boolean getEscape() {
return escape;
}
public void setEscape(boolean escape) {
this.escape = escape;
}
public String getParam(String key, RenderContext... context) {
if(context != null)
{
for(RenderContext param:context)
{
if(param != null)
{
String value = param.getParameter(key);
if(!StringUtil.isEmpty(value))
{
return value;
}
}
}
}
return nullToken;
}
public String render(String s, RenderContext... context) {
// handle trivial cases of empty template or no placeholders
if (s == null)
{
Log4j.app.debug("Message is null in template. Cannot render null message.");
return nullToken;
}
if (context == null)
{
Log4j.app.debug("RenderContext is null. Cannot render message with null RenderContext.");
return nullToken;
}
if (s.indexOf(placeholderStartToken) < 0)
{
return s;
}
String msg = nullToken;
try
{
// private int renderTemplate(Renderable r, String src, StringBuffer dst, String nil, int i, String[] marks, StringBuffer end,boolean escapes)
msg = doRender(s, context);
}
catch (Exception e)
{
Log4j.app.error("Exception in rendering template: " + e.getMessage(), e);
return nullToken;
}
return msg;
}
protected abstract String doRender(String s, RenderContext... context);
}
MethodExpressionRenderer.java
public class MethodExpressionRenderer extends AbstractMessageRenderer {
private boolean inSingleQuote = false;
private boolean inDoubleQuote=false;
private int placeholders;
private Stack<String> methodStack;
private String[] endTokens;
private String marker;
private List<String> methodParams;
private String prefix = "&";
public MethodExpressionRenderer() {
super();
methodStack = new Stack<String>();
marker = ",";
endTokens = new String[] { placeholderEndToken, marker, "(", ")" };
methodParams = new ArrayList<String>();
}
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getMarker() {
return marker;
}
public void setMarker(String marker) {
this.marker = marker;
endTokens = new String[] { placeholderEndToken, marker };
}
#Override
public void setPlaceholderEndToken(String placeholderEndToken) {
super.setPlaceholderEndToken(placeholderEndToken);
endTokens = new String[] { placeholderEndToken, marker };
}
protected String doRender(String s, RenderContext... context) {
StringBuffer sb = new StringBuffer();
try
{
renderTemplate(s, sb, nullToken, 0, endTokens, null, context);
}
catch (Exception e)
{
Log4j.app.error("Exception in rendering method expression message emplate: " + e.getMessage(), e);
return nullToken;
}
return sb.toString();
}
private int renderTemplate(String src, StringBuffer dst, String nil, int i, String[] marks, StringBuffer end, RenderContext... context) {
int len = src.length();
while (i < len)
{
char c = src.charAt(i);
if (escape)
{
if (c=='\\')
{
i++;
char ch = src.charAt(i);
if(inSingleQuote)
{
if(ch=='\'')
{
inSingleQuote=false;
}
}
else if(inDoubleQuote)
{
if(ch=='"')
{
inDoubleQuote=false;
}
}
else
{
if(ch=='\'')
{
inSingleQuote=true;
}
else if(ch=='"')
{
inDoubleQuote=true;
}
}
dst.append(ch);
i++;
continue;
}
}
if(inSingleQuote)
{
if(c=='\'')
{
inSingleQuote=false;
}
}
else if(inDoubleQuote)
{
if(c=='"')
{
inDoubleQuote=false;
}
}
else
{
if(c=='\'')
{
inSingleQuote=true;
}
else if(c=='"')
{
inDoubleQuote=true;
}
}
// check for end marker
if (marks != null && !inSingleQuote && !inDoubleQuote)
{
for (int m = 0; m < marks.length; m++)
{
// If one of markers found
if (src.regionMatches(i, marks[m], 0, marks[m].length()))
{
// return marker if required
if (end != null)
{
end.append(marks[m]);
}
return i+marks[m].length();
}
}
}
// check for start of placeholder
if (src.regionMatches(i, placeholderStartToken, i, lenPlaceholderStartToken))
{
synchronized(this)
{
++placeholders;
}
i = renderPlaceholder(src, dst, nil, i, new ArrayList<String>(), context);
continue;
}
// just add plain character
if(c != '\'' && c!= '"')
{
dst.append(c);
}
i++;
}
return i;
}
private int renderPlaceholder(String src, StringBuffer dst, String nil, int i, List<String> params, RenderContext... context){
StringBuffer token = new StringBuffer(); // placeholder token
StringBuffer end = new StringBuffer(); // placeholder end marker
String value;
i = renderTemplate(src, token, nil, i+lenPlaceholderStartToken, endTokens, end);
String sToken = token.toString().trim();
String sEnd = end.toString().trim();
boolean isFunction = sEnd.equals("(");
// This is method name
if(isFunction && placeholders > methodStack.size())
{ // Method
synchronized(this)
{
methodStack.push(sToken); // put method into stack
}
}
else if(!isFunction && (methodStack.size()==0) && sEnd.equals(placeholderEndToken)) // Single template param such as <<param>>
{
value = getParam(sToken, context);
if(value != null)
{
if(value.trim().startsWith(placeholderStartToken))
{
value = render(src, context);
}
dst.append(value);
return i;
}
}
// TODO: Process method parameters to invoke
//.... ?????????
// Found end method token ')'
// Pop method out of stack to invoke
if ( (methodStack.size() >0) && (sEnd.length() == 0 || sEnd.equals(")")))
{
String method = null;
synchronized(this)
{
// Pop method out of stack to invoke
method = methodStack.pop();
--placeholders;
dst.append(invokeMethodEvaluator(method, methodParams.toArray(new String[0]), context));
methodParams.clear();
}
}
return i;
}
// Currently this method just implement to test so it just printout the method name
// and its parameter
// We can register MethodExpressionEvaluator to process
protected String invokeMethodEvaluator(String method, String[] params, RenderContext... context){
StringBuffer result = new StringBuffer();
result.append("[ ")
.append(method)
.append(" ( ");
if(params != null)
{
for(int i=0; i<params.length; i++)
{
result.append(params[i]);
if(i != params.length-1)
{
result.append(" , ");
}
}
}
result.append(" ) ")
.append(" ] ");
return result.toString();
}
}
We can easily register more method to the renderer to invoke. Each method will be an object and can be reused. But I'm in trouble how to resolve the nested method parameter. Can anyone give me an advice how we can process nested template of method parameter to invoke??? The line has TODO. Will my code in on the right way???
When you evaluate something like << count( << getTransId() >> ) >> you can either:
perform direct-evaluation as you parse, and push each function onto a stack, so that once you've evaluated getTransId() you pop the stack and use the return value (from the stack) as an argument for count(), or
you can build a parse tree to represent all the function calls that will be made, and then evaluate your parse tree after building it. (Building a tree probably doesn't buy you anything; since you're writing a template engine there is probably no high-level tree operation 'optimizations' that you could perform.)
An excellent little book I really enjoyed was Language Implementation Patterns by Parr. He walks through building simple to complex languages, and covers decisions like this in some depth. (Yes, he uses the ANTLR parser generator throughout, but your code looks like you're familiar enough with hand-generated parsers that different tools won't be a distraction for you.)
I found the bug and fixed it.
This is my new source:
// AbstractMethodExpressionRenderer.java
public class AbstractMethodExpressionRenderer extends AbstractMessageRenderer {
private boolean inSingleQuote = false;
private boolean inDoubleQuote=false;
private Stack<MethodExpressionDescriptor> functionStack;
private String[] endTokens;
private String marker;
private String prefix = "~";
public AbstractMethodExpressionRenderer() {
super();
functionStack = new Stack<MethodExpressionDescriptor>();
marker = ",";
endTokens = new String[] { placeholderEndToken, "(", ")", };
}
private class MethodExpressionDescriptor {
public List<String> params;
public String function;
public MethodExpressionDescriptor() {
params = new ArrayList<String>();
}
public MethodExpressionDescriptor(String name) {
this();
this.function = name;
}
}
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getMarker() {
return marker;
}
public void setMarker(String marker) {
this.marker = marker;
endTokens = new String[] { placeholderEndToken, marker };
}
#Override
public void setPlaceholderEndToken(String placeholderEndToken) {
super.setPlaceholderEndToken(placeholderEndToken);
endTokens = new String[] { placeholderEndToken, marker };
}
protected String doRender(String s, RenderContext... context) {
StringBuffer sb = new StringBuffer();
try
{
renderTemplate(s, sb, nullToken, 0, endTokens, null, context);
}
catch (Exception e)
{
Log4j.app.error("Exception in rendering method expression message emplate: " + e.getMessage(), e);
return nullToken;
}
return sb.toString();
}
private int renderTemplate(String src, StringBuffer dst, String nil, int i, String[] marks, StringBuffer end, RenderContext... context) {
int len = src.length();
while (i < len)
{
char c = src.charAt(i);
if (escape)
{
if (c=='\\')
{
i++;
char ch = src.charAt(i);
if(inSingleQuote)
{
if(ch=='\'')
{
inSingleQuote=false;
}
}
else if(inDoubleQuote)
{
if(ch=='"')
{
inDoubleQuote=false;
}
}
else
{
if(ch=='\'')
{
inSingleQuote=true;
}
else if(ch=='"')
{
inDoubleQuote=true;
}
}
dst.append(ch);
i++;
continue;
}
}
if(inSingleQuote)
{
if(c=='\'')
{
inSingleQuote=false;
}
}
else if(inDoubleQuote)
{
if(c=='"')
{
inDoubleQuote=false;
}
}
else
{
if(c=='\'')
{
inSingleQuote=true;
}
else if(c=='"')
{
inDoubleQuote=true;
}
}
// check for end marker
if (marks != null && !inSingleQuote && !inDoubleQuote)
{
for (int m = 0; m < marks.length; m++)
{
// If one of markers found
if (src.regionMatches(i, marks[m], 0, marks[m].length()))
{
// return marker if required
if (end != null)
{
end.append(marks[m]);
}
return i+marks[m].length();
}
}
}
// check for start of placeholder
if (src.regionMatches(i, placeholderStartToken, 0, lenPlaceholderStartToken))
{
i = renderPlaceholder(src, dst, nil, i, new ArrayList<String>(), context);
continue;
}
// just add plain character
if(c != '\'' && c!= '"')
{
dst.append(c);
}
i++;
}
return i;
}
/**
* Render a placeholder as follows:
*
* <<key>>: Simple render, key value map
* <<function(<<param1>>, <<param2>>)>> : Function object render
*
* #param src
* #param dst
* #param nil
* #param i
* #param params
* #param context
* #return
*/
private int renderPlaceholder(String src, StringBuffer dst, String nil, int i, List<String> params, RenderContext... context){
StringBuffer token = new StringBuffer(); // placeholder token
StringBuffer end = new StringBuffer(); // placeholder end marker
String value = null;
// Simple key
i = renderTemplate(src, token, nil, i+lenPlaceholderStartToken, endTokens, end, context);
String sToken = token.toString().trim();
String sEnd = end.toString().trim();
// This is method name
if(sEnd.equals("("))
{ // Method
functionStack.add(new MethodExpressionDescriptor(sToken));
}
else // Try to resolve value
{
if(sToken.startsWith(placeholderStartToken))
{
value = render(sToken, context);
}
else if(sToken.startsWith(prefix))
{
if(functionStack.size() > 0)
{
functionStack.peek().params.add(sToken.substring(1));
}
return i;
}
else
{
value = getParam(sToken, context);
}
}
if (sEnd.length() == 0 || sEnd.equals(placeholderEndToken))
{
// No method found but found the end of placeholder token
if(functionStack.size() == 0)
{
if(value != null)
{
dst.append(value);
}
else
{
dst.append(nil);
}
}
else
{
functionStack.peek().params.add(value);
}
}
else
{
if(value != null)
{
value = value.trim();
}
if(end.substring(0, 1).equals("(") ||
end.substring(0, 1).equals(marker))
{
// right hand side is remainder of placeholder
StringBuffer tmp = new StringBuffer();
end = new StringBuffer();
i = renderTemplate(src, tmp, nil, i, endTokens, end, context);
}
if(end.substring(0, 1).equals(")"))
{
if ( functionStack.size() > 0 )
{
// Pop method out of stack to invoke
MethodExpressionDescriptor descriptor = functionStack.pop();
if(functionStack.size() > 0 )
{
functionStack.peek().params.add(invokeMethodEvaluator(descriptor.function, descriptor.params.toArray(new String[0]), context));
}
else
{
dst.append(invokeMethodEvaluator(descriptor.function, descriptor.params.toArray(new String[0]), context));
}
end = new StringBuffer();
StringBuffer tmp = new StringBuffer();
i = renderTemplate(src, tmp, nil, i, endTokens, end, context);
}
}
}
return i;
}
protected String invokeMethodEvaluator(String method, String[] params, RenderContext... context){
StringBuffer result = new StringBuffer();
result.append("[ ")
.append(method)
.append(" ( ");
if(params != null)
{
for(int i=0; i<params.length; i++)
{
result.append(params[i]);
if(i != params.length-1)
{
result.append(" , ");
}
}
}
result.append(" ) ")
.append(" ] ");
return result.toString();
}
}

stack trace in code

I was wondering how I would add a trace to the stack of this code that converts infix to postix for expressions.
class Node {
public Object data;
public Node next;
public Node () {
data =' '; next = null; }
public Node (Object val) {
data = val; next = null; }
}
public class LinkStack {
private Node top;
public LinkStack() {
top = null; }
public boolean empty(){
return top == null; }
public boolean full(){
return false;
}
public void push(Object e){
Node tmp = new Node(e);
tmp.next = top;
top = tmp;
}
public Object pop(){
Object e = top.data;
top = top.next;
return e;
}
public Object peek(){
Object e = top.data;
return e;
}
public void matching(String x)
{
LinkStack S=new LinkStack();
for(int i=0;i<x.length();i++)
{
char c=x.charAt(i);
if(c=='(')
S.push(c);
else
{
if(c==')')
if(S.empty())
System.out.println("NOT MATCHING !!!");
else
S.pop();
}
}
if(!S.empty())
System.out.println("NOT MATCHING !!!");
else
System.out.println("MATCHING !!!");
}
public void Evaluation(String x)
{
LinkStack S=new LinkStack();
for(int i=0;i<x.length();i++)
{
char c=x.charAt(i);
String s="0"+c;
if(c=='+')
{
int z=Integer.parseInt((String)S.pop())+Integer.parseInt((String)S.pop());
S.push(Integer.toString(z));
}
else if(c=='*')
{
int z=Integer.parseInt((String)S.pop())*Integer.parseInt((String)S.pop());
S.push(Integer.toString(z));
}
else if(c=='/')
{ int u=Integer.parseInt((String)S.pop());
int z=Integer.parseInt((String)S.pop())/u;
S.push(Integer.toString(z));
}
else if(c=='-')
{ int u=Integer.parseInt((String)S.pop());
int z=Integer.parseInt((String)S.pop())-u;
S.push(Integer.toString(z));
}
else
S.push(s);
}
System.out.println("THE POSTFIX = "+x);
System.out.println("THE RESULT = "+S.pop());
}
public void postfix(String x)
{
String output="";
LinkStack S=new LinkStack();
for(int i=0;i<x.length();i++)
{
char c=x.charAt(i);
if(c==('+')||c==('*')||c==('-')||c==('/'))
{while(!S.empty() && priority(S.peek())>= priority(c))
output+=S.pop();
S.push(c);
System.out.println(output);
}
else if(c=='(')
{
S.push(c);
}
else if(c==')')
{
while(!S.peek().equals('('))
output+=S.pop();
S.pop();
System.out.println(output);
}
else
{
output+=c;
System.out.println(output);
}
}
while(!S.empty())
output+=S.pop();
System.out.println("THE INFIX = "+x);
System.out.println("THE POSTFIX = "+output);
}
public int priority(Object x)
{
if(x.equals('+')||x.equals('-'))
return 1;
else if(x.equals('*')||x.equals('/'))
return 2;
else
return 0;
}
public static void main(String args[])
{
LinkStack s=new LinkStack();
s.postfix("x*y–z+(a–c/d)");
System.out.println("------------------------------------------");
s.matching("x*y–z+(a–c/d)");
System.out.println("------------------------------------------");
}
}
I am pretty much wanting to follow the contents of the stack throughout the conversion
There's no rocket science solution to this.
Just add System.err.println(...) calls at the relevant places. Or if you were doing this in production code (heaven forbid!) you might use a Logger instead of System.err.
(For the record, the term "stack trace" normally means a trace of a program's call stack, not a trace of what happens to some application-specific stack data structure. You might want to choose your terminology a little more carefully next time.)

Categories

Resources