Search in sources :

Example 1 with GenericTraversalFactory

use of org.whole.lang.visitors.GenericTraversalFactory in project whole by wholeplatform.

the class Matcher method findAll.

public static <E extends IEntity> Collection<E> findAll(IVisitor matcherVisitor, IEntity model, Collection<E> result, boolean includeAdjacents) {
    GenericTraversalFactory tf = GenericTraversalFactory.instance;
    tf.topDownUntil(tf.collect(matcherVisitor, result), includeAdjacents).visit(model);
    return result;
}
Also used : GenericTraversalFactory(org.whole.lang.visitors.GenericTraversalFactory)

Example 2 with GenericTraversalFactory

use of org.whole.lang.visitors.GenericTraversalFactory in project whole by wholeplatform.

the class RewriteTest method testRewrite.

@Test
public void testRewrite() {
    IEntity program = buildImpProgram();
    PrettyPrinterOperation.prettyPrint(program);
    GenericMatcherFactory mf = GenericMatcherFactory.instance;
    GenericTraversalFactory tf = GenericTraversalFactory.instance;
    IVisitor v = tf.innermost(tf.concurrentSome(mf.rewrite(IfElseEmptyPattern(), IfElseEmptyRewrite(), false), mf.rewrite(IfTruePattern(), IfTrueRewrite(), false), mf.rewrite(IfTrueElsePattern(), IfTrueRewrite(), false), mf.rewrite(IfNotPattern(), IfNotRewrite(), false)));
    v.visit(program);
    PrettyPrinterOperation.prettyPrint(program);
}
Also used : IEntity(org.whole.lang.model.IEntity) IVisitor(org.whole.lang.visitors.IVisitor) GenericMatcherFactory(org.whole.lang.matchers.GenericMatcherFactory) GenericTraversalFactory(org.whole.lang.visitors.GenericTraversalFactory) Test(org.junit.Test)

Example 3 with GenericTraversalFactory

use of org.whole.lang.visitors.GenericTraversalFactory in project whole by wholeplatform.

the class CommandFactory method create.

public Command create(PartRequest request) {
    IPartRequestHandler handler;
    // tries to handle operation with custom handlers
    List<IPartRequestHandler> handlerList = handlersMap.get(request.getRequestType());
    if (handlerList != null && !handlerList.isEmpty()) {
        Iterator<IPartRequestHandler> handlerIterator = handlerList.iterator();
        while (handlerIterator.hasNext()) {
            handler = (IPartRequestHandler) handlerIterator.next();
            if (handler.canHandlePartRequest(request))
                return handler.handlePartRequest(request);
        }
    }
    // handle basic requests
    if (EntityUtils.isComposite(request.getModelEntity()) && request.getRequestType() == PartRequest.MOVE_CHILD)
        return moveIndexedFeature.create(request);
    if (request.getRequestType() == PartRequest.MOVE_ORPHAN_CHILD) {
        if (EntityUtils.isComposite(request.getModelEntity()))
            return orphanIndexedFeature.create(request);
        else
            return orphanFeature.create(request);
    }
    if (request.getRequestType() == PartRequest.DELETE) {
        if (EntityUtils.isComposite(request.getParentModelEntity()))
            return removeIndexedFeature.create(request);
        else
            // was else if (EntityUtils.isSimple(request.getParentModelEntity()))
            return removeFeature.create(request);
    }
    // handle composite and simple requests
    if (isDnDRequest(request)) {
        boolean isCompositeAddCommand, isReplaceCommand;
        isCompositeAddCommand = isReplaceCommand = request.size() > 0;
        IEntity targetEntity = request.getModelEntity();
        boolean isCloneRequest = request.getRequestType() == PartRequest.CLONE_CHILD;
        HashSet<IEntity> targetEntityAncestors = new HashSet<IEntity>();
        GenericTraversalFactory tf = GenericTraversalFactory.instance;
        IBinaryVisitor ancestorsVisitor = tf.ancestors(tf.collect(tf.identity(), targetEntityAncestors));
        ancestorsVisitor.visit(targetEntity);
        for (IEntityPart dndPart : request) {
            IEntity dndEntity = dndPart.getModelEntity();
            if (targetEntityAncestors.contains(dndEntity) && !isCloneRequest)
                return UnexecutableCommand.INSTANCE;
            EntityDescriptor<?> dndEntityDescriptor = dndEntity.wGetEntityDescriptor();
            isCompositeAddCommand &= EntityUtils.isAddable(targetEntity, dndEntityDescriptor) || EntityUtils.isComposite(targetEntity);
            isReplaceCommand &= EntityUtils.isReplaceable(targetEntity, dndEntityDescriptor);
        }
        if (isCompositeAddCommand)
            return createCompositeAddCommand(request);
        else if (isReplaceCommand)
            return createReplaceCommand(request);
        else
            return UnexecutableCommand.INSTANCE;
    }
    return null;
}
Also used : IEntity(org.whole.lang.model.IEntity) GenericTraversalFactory(org.whole.lang.visitors.GenericTraversalFactory) IBinaryVisitor(org.whole.lang.visitors.IBinaryVisitor) IEntityPart(org.whole.lang.ui.editparts.IEntityPart) HashSet(java.util.HashSet)

Example 4 with GenericTraversalFactory

use of org.whole.lang.visitors.GenericTraversalFactory in project whole by wholeplatform.

the class Matcher method indexOf.

public static int indexOf(IVisitor matcherVisitor, IEntity model) {
    GenericTraversalFactory tf = GenericTraversalFactory.instance;
    try {
        Set<IEntity> c = new HashSet<IEntity>();
        tf.traverseOne(tf.collect(matcherVisitor, c), true).visit(model);
        return model.wIndexOf(c.iterator().next());
    } catch (VisitException e) {
        return -1;
    }
}
Also used : IEntity(org.whole.lang.model.IEntity) VisitException(org.whole.lang.visitors.VisitException) GenericTraversalFactory(org.whole.lang.visitors.GenericTraversalFactory) HashSet(java.util.HashSet)

Example 5 with GenericTraversalFactory

use of org.whole.lang.visitors.GenericTraversalFactory 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

GenericTraversalFactory (org.whole.lang.visitors.GenericTraversalFactory)5 IEntity (org.whole.lang.model.IEntity)4 HashSet (java.util.HashSet)3 VisitException (org.whole.lang.visitors.VisitException)2 Test (org.junit.Test)1 GenericMatcherFactory (org.whole.lang.matchers.GenericMatcherFactory)1 IEntityPart (org.whole.lang.ui.editparts.IEntityPart)1 IBinaryVisitor (org.whole.lang.visitors.IBinaryVisitor)1 IVisitor (org.whole.lang.visitors.IVisitor)1