Search in sources :

Example 26 with VisitException

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);
}
Also used : Interpreter(bsh.Interpreter) IEntity(org.whole.lang.model.IEntity) VisitException(org.whole.lang.visitors.VisitException) IBindingManager(org.whole.lang.bindings.IBindingManager) EvalError(bsh.EvalError)

Example 27 with VisitException

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));
}
Also used : SubstituteException(org.whole.lang.matchers.SubstituteException) IEntity(org.whole.lang.model.IEntity) VisitException(org.whole.lang.visitors.VisitException) MissingVariableException(org.whole.lang.visitors.MissingVariableException)

Example 28 with VisitException

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();
        }
    }));
}
Also used : IEntity(org.whole.lang.model.IEntity) VisitException(org.whole.lang.visitors.VisitException) GenericIdentityVisitor(org.whole.lang.visitors.GenericIdentityVisitor)

Example 29 with 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;
    }
}
Also used : VisitException(org.whole.lang.visitors.VisitException) FreshNameGenerator(org.whole.lang.util.FreshNameGenerator) HashSet(java.util.HashSet)

Example 30 with VisitException

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();
}
Also used : IEntity(org.whole.lang.model.IEntity) VisitException(org.whole.lang.visitors.VisitException) GenericTraversalFactory(org.whole.lang.visitors.GenericTraversalFactory) HashSet(java.util.HashSet)

Aggregations

VisitException (org.whole.lang.visitors.VisitException)35 IEntity (org.whole.lang.model.IEntity)25 IBindingManager (org.whole.lang.bindings.IBindingManager)6 File (java.io.File)4 AbstractVisitor (org.whole.lang.visitors.AbstractVisitor)4 HashSet (java.util.HashSet)3 GenericIdentityVisitor (org.whole.lang.visitors.GenericIdentityVisitor)3 IOException (java.io.IOException)2 PrintWriter (java.io.PrintWriter)2 PrettyPrinterOperation.toPrettyPrintString (org.whole.lang.operations.PrettyPrinterOperation.toPrettyPrintString)2 GenericTraversalFactory (org.whole.lang.visitors.GenericTraversalFactory)2 IVisitor (org.whole.lang.visitors.IVisitor)2 EvalError (bsh.EvalError)1 Interpreter (bsh.Interpreter)1 BufferedReader (java.io.BufferedReader)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 Writer (java.io.Writer)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1