org.snapscript.studio.agent.debug.ScopeExtractor 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.Collections;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import org.snapscript.core.Context;
import org.snapscript.core.ResourceManager;
import org.snapscript.core.function.Function;
import org.snapscript.core.scope.Scope;
public class ScopeExtractor implements ScopeBrowser {
private final AtomicReference evaluate;
private final ScopeNodeEvaluator evaluator;
private final ScopeNodeTraverser traverser;
private final AtomicBoolean execute;
private final AtomicInteger counter;
private final Set watch;
private final Set local;
private final Context context;
private final String path;
public ScopeExtractor(Context context, Scope scope, Function function, String path) {
this.traverser = new ScopeNodeTraverser(context, scope);
this.evaluator = new ScopeNodeEvaluator(context, scope, function);
this.evaluate = new AtomicReference();
this.watch = new CopyOnWriteArraySet();
this.local = new CopyOnWriteArraySet();
this.execute = new AtomicBoolean();
this.counter = new AtomicInteger();
this.context = context;
this.path = path;
}
public ScopeContext build(boolean grabSource, boolean expandVariables) {
int change = counter.get();
boolean refresh = execute.getAndSet(false);
String expression = evaluate.get();
ResourceManager manager = context.getManager();
String source = null;
if(grabSource) {
source = manager.getString(path);
}
if(expandVariables){
Map> variables = traverser.expand(local);
Map> evaluation = evaluator.expand(watch, expression, refresh);
ScopeVariableTree tree = new ScopeVariableTree.Builder(change)
.withLocal(variables)
.withEvaluation(evaluation)
.build();
return new ScopeContext(tree, source);
}
ScopeVariableTree tree = new ScopeVariableTree.Builder(change)
.withLocal(Collections.EMPTY_MAP)
.withEvaluation(Collections.EMPTY_MAP)
.build();
return new ScopeContext(tree, source);
}
@Override
public void browse(Set expand) {
local.clear();
local.addAll(expand);
counter.getAndIncrement();
}
@Override
public void evaluate(Set expand, String expression) {
evaluate(expand, expression, false);
}
@Override
public void evaluate(Set expand, String expression, boolean refresh) {
watch.clear();
watch.addAll(expand);
evaluate.set(expression);
execute.set(refresh); // should we execute same expression again
counter.getAndIncrement();
}
}