org.snapscript.studio.agent.debug.ScopeNodeEvaluator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of snap-all Show documentation
Show all versions of snap-all Show documentation
Dynamic scripting for the JVM
package org.snapscript.studio.agent.debug;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.snapscript.core.Context;
import org.snapscript.core.scope.Scope;
import org.snapscript.studio.agent.debug.ExpressionProcessor;
import org.snapscript.studio.agent.debug.ExpressionScopeNode;
import org.snapscript.studio.agent.debug.ScopeNode;
import org.snapscript.studio.agent.debug.ScopeNodeBuilder;
import org.snapscript.studio.agent.debug.VariableNameEncoder;
public class ScopeNodeEvaluator {
private final ExpressionProcessor processor;
private final VariableNameEncoder encoder;
private final Context context;
public ScopeNodeEvaluator(Context context, Scope scope) {
this.processor = new ExpressionProcessor(context, scope); // this keeps expression cache
this.encoder = new VariableNameEncoder();
this.context = context;
}
public Map> expand(Set expand, String expression, boolean refresh) {
Map> variables = new HashMap>();
ScopeNodeBuilder builder = new ScopeNodeBuilder(variables, context);
ScopeNode node = new ExpressionScopeNode(builder, processor, encoder, expression, refresh);
if(!expand.isEmpty()) {
for(String path : expand) {
String[] parts = path.split("\\.");
expand(node, parts, 0);
}
} else {
node.getNodes(); // expand root
}
return variables;
}
private void expand(ScopeNode node, String[] parts, int index) {
List children = node.getNodes();
String match = encoder.decode(parts[index]);
for(ScopeNode child : children) {
String name = child.getName();
if(name.equals(match)) {
expand(child, parts, index+1);
break;
}
}
}
}