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;
}
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);
}
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;
}
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;
}
}
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();
}
Aggregations