use of org.whole.lang.visitors.VisitException in project whole by wholeplatform.
the class JavaInterpreterVisitor method visit.
@Override
public void visit(IJavaEntity entity) {
final IBindingManager bm = getBindings();
/*
* TODO
* for each nested lower level fragment:
* evaluate and replace the fragment with the result
* for each nested upper level fragment:
* build the template and bind it to a fresh name
* replace the fragment with the name
*/
IEntity sourceEntity = entity;
String sourceString = PrettyPrinterOperation.toPrettyPrintString(sourceEntity);
Interpreter javaInterpreter = new Interpreter();
javaInterpreter.setClassLoader(ReflectionFactory.getClassLoader(bm));
javaInterpreter.setConsole(this);
boolean useAutoboxing = true;
do {
try {
for (String name : bm.wNames()) {
IEntity valueEntity = bm.wGet(name);
if (useAutoboxing && valueEntity != null && EntityUtils.isData(valueEntity))
javaInterpreter.set(name, valueEntity.wGetValue());
else
javaInterpreter.set(name, valueEntity);
}
Object result = javaInterpreter.eval(sourceString);
if (result instanceof IEntity)
bm.setResult((IEntity) result);
else if (result != null)
bm.setResult(BindingManagerFactory.instance.createSpecificValue(result));
for (String name : javaInterpreter.getNameSpace().getVariableNames()) {
if ("bsh".equals(name))
continue;
Object value = javaInterpreter.get(name);
if (bm.wIsSet(name)) {
IEntity valueEntity = bm.wGet(name);
if (useAutoboxing && valueEntity != null && EntityUtils.isData(valueEntity))
valueEntity.wSetValue(value);
else if (value != valueEntity)
bm.wSet(name, (IEntity) value);
} else if (value instanceof IEntity)
bm.wDef(name, (IEntity) value);
else
bm.wDef(name, BindingManagerFactory.instance.createSpecificValue(value));
}
return;
} catch (EvalError e) {
if (!useAutoboxing)
throw new VisitException("Java Interpreter failure.", e);
useAutoboxing = false;
}
} while (true);
}
use of org.whole.lang.visitors.VisitException in project whole by wholeplatform.
the class CommonsInterpreterVisitor method evaluate.
public static final IEntity evaluate(Variable variable, IBindingManager bm) {
String varName = variable.getVarName().getValue();
IEntity value = BindingUtils.wGet(bm, varName);
if (value != null) {
if (Matcher.match(CommonsEntityDescriptorEnum.InlineVariable, variable)) {
bm.setResultIterator(IteratorFactory.constantChildIterator(value));
value = null;
} else {
EntityDescriptor<?> varType = variable.getVarType().getValue();
try {
bm.setResult(value = EntityUtils.convertCloneIfParented(value, varType));
} catch (IllegalArgumentException e) {
throw new SubstituteException(variable, value.wGetEntityDescriptor());
}
}
return value;
} else
throw new VisitException(new MissingVariableException(varName).withSourceEntity(variable).withBindings(bm));
}
use of org.whole.lang.visitors.VisitException in project whole by wholeplatform.
the class ActionsUIEntityFactory method createAllVariablesGroupAction.
public GroupAction createAllVariablesGroupAction(ActionKindEnum.Value kind, Set<String> excludeSet, EntityDescriptor<?> resultEd, IEntity model) {
MatcherIterator<IEntity> i = IteratorFactory.<IEntity>descendantOrSelfMatcherIterator();
i.reset(EntityUtils.safeGetRootEntity(model));
return createVariablesGroupAction(kind, excludeSet, resultEd, i.withPattern(new GenericIdentityVisitor() {
public void visit(IEntity entity) {
if (!isVariable(entity.wGetAdaptee(false).wGetEntityDescriptor()))
throw new VisitException();
}
}));
}
use of org.whole.lang.visitors.VisitException in project whole by wholeplatform.
the class Matcher method forceMatchUsingVariables.
public static boolean forceMatchUsingVariables(IEntity pattern, IEntity model) {
try {
GenericMatcher gm = new GenericMatcher();
final Set<String> boundNames = new HashSet<String>();
gm.withMatchStrategy(IMatchStrategy.CollectVariableNames(boundNames), CommonsEntityDescriptorEnum.Variable, CommonsEntityDescriptorEnum.InlineVariable).withMismatchStrategy(IMatchStrategy.IgnoreSubtree).match(model, pattern);
final FreshNameGenerator fng = new FreshNameGenerator(boundNames);
gm.withAsIsMatching().withMismatchStrategy(IMatchStrategy.ReplaceWithVariable(fng)).match(pattern, model);
return true;
} catch (MatchException | VisitException e) {
return false;
}
}
use of org.whole.lang.visitors.VisitException in project whole by wholeplatform.
the class Matcher method find.
public static IEntity find(IVisitor matcherVisitor, IEntity model, boolean includeAdjacents) {
GenericTraversalFactory tf = GenericTraversalFactory.instance;
Set<IEntity> c = new HashSet<IEntity>();
try {
tf.onceTopDown(tf.collect(matcherVisitor, c), includeAdjacents).visit(model);
} catch (VisitException e) {
}
if (c.isEmpty())
return null;
else
return c.iterator().next();
}
Aggregations