Example #1
0
 @Override
 public CyclicStackItem<P> enter(Pda<S, P> pda, S state, CyclicStackItem<P> previous) {
   P item;
   if ((item = pda.getPush(state)) != null) return previous.push(item);
   if ((item = pda.getPop(state)) != null) return previous.pop(item);
   if (previous == null) return new CyclicStackItem<P>();
   return previous;
 }
Example #2
0
 public <S, P, R, D extends Pda<S, P>> D filterEdges(
     Pda<S, P> pda, Traverser<? super Pda<S, P>, S, R> traverser, PdaFactory<D, S, P, S> factory) {
   HashStack<TraversalItem<S, R>> trace = new HashStack<TraversalItem<S, R>>();
   R previous = traverser.enter(pda, pda.getStart(), null);
   if (previous == null)
     return factory == null ? null : factory.create(pda.getStart(), pda.getStop());
   Map<S, Integer> distances = new NfaUtil().distanceToFinalStateMap(pda);
   MappedComparator<S, Integer> distanceComp = new MappedComparator<S, Integer>(distances);
   trace.push(newItem(pda, distanceComp, distances, pda.getStart(), previous));
   Multimap<S, S> edges = LinkedHashMultimap.create();
   HashSet<S> states = Sets.newLinkedHashSet();
   HashSet<Pair<S, R>> success = Sets.newLinkedHashSet();
   states.add(pda.getStart());
   states.add(pda.getStop());
   ROOT:
   while (!trace.isEmpty()) {
     TraversalItem<S, R> current = trace.peek();
     while (current.followers.hasNext()) {
       S next = current.followers.next();
       R item = traverser.enter(pda, next, current.data);
       if (item != null) {
         if ((next == pda.getStop() && traverser.isSolution(item))
             || success.contains(Tuples.create(next, item))) {
           S s = null;
           for (TraversalItem<S, R> i : trace) {
             if (s != null) edges.put(s, i.state);
             states.add(i.state);
             success.add(Tuples.create(i.state, i.data));
             s = i.state;
           }
           edges.put(s, next);
         } else {
           if (trace.push(newItem(pda, distanceComp, distances, next, item))) continue ROOT;
         }
       }
     }
     trace.pop();
   }
   if (factory == null) return null;
   D result = factory.create(pda.getStart(), pda.getStop());
   Map<S, S> old2new = Maps.newLinkedHashMap();
   old2new.put(pda.getStart(), result.getStart());
   old2new.put(pda.getStop(), result.getStop());
   for (S old : states) {
     if (old == pda.getStart() || old == pda.getStop()) continue;
     else if (pda.getPop(old) != null) old2new.put(old, factory.createPop(result, old));
     else if (pda.getPush(old) != null) old2new.put(old, factory.createPush(result, old));
     else old2new.put(old, factory.createState(result, old));
   }
   for (S old : states) {
     List<S> followers = Lists.newArrayList();
     for (S f : edges.get(old)) followers.add(old2new.get(f));
     factory.setFollowers(result, old2new.get(old), followers);
   }
   return result;
 }
Example #3
0
 protected <S, P> TraceItem<S, P> traceToWithPruningStack(
     Pda<S, P> pda,
     Iterable<S> starts,
     Iterator<P> stack,
     Predicate<S> matches,
     Predicate<S> canPass) {
   StackItem<P> stackItem = createStack(stack);
   List<TraceItem<S, P>> current = Lists.newArrayList();
   Set<S> visited = Sets.newLinkedHashSet(starts);
   TraceItem<S, P> result = null;
   for (S start : starts) {
     TraceItem<S, P> item = new TraceItem<S, P>(null, start, stackItem);
     //			if (matches.apply(start))
     //				result = item;
     current.add(item);
   }
   int counter = stackItem.size() * -1;
   while (current.size() > 0 && counter < visited.size()) {
     List<TraceItem<S, P>> newCurrent = Lists.newArrayList();
     for (TraceItem<S, P> trace : current)
       for (S follower : pda.getFollowers(trace.state)) {
         if (matches.apply(follower)) {
           TraceItem<S, P> found = new TraceItem<S, P>(trace, follower, trace.stackitem);
           if (found.stackitem == null) return found;
           if (result == null || result.stackitem.size() > found.stackitem.size()) {
             result = found;
             counter = result.stackitem.size() * -1;
           } else if (result.stackitem.size() == found.stackitem.size()
               && result.size() > found.size()) {
             result = found;
             counter = result.stackitem.size() * -1;
           }
         }
         if (canPass.apply(follower)) {
           P push = pda.getPush(follower);
           visited.add(follower);
           if (push != null) {
             StackItem<P> pushed = trace.stackitem.push(push);
             newCurrent.add(new TraceItem<S, P>(trace, follower, pushed));
           } else {
             P pop = pda.getPop(follower);
             if (pop != null) {
               if (trace.stackitem != null && pop == trace.stackitem.peek()) {
                 StackItem<P> popped = trace.stackitem.pop();
                 newCurrent.add(new TraceItem<S, P>(trace, follower, popped));
               }
             } else newCurrent.add(new TraceItem<S, P>(trace, follower, trace.stackitem));
           }
         }
       }
     current = newCurrent;
     counter++;
   }
   return result;
 }
Example #4
0
 protected <S, P, T, D extends Pda<S, P>> S clone(
     S state,
     Pda<S, P> src,
     D target,
     Function<S, T> tokens,
     PdaFactory<D, S, P, T> fact,
     Identity<S> identity) {
   if (state == src.getStart()) return target.getStart();
   if (state == src.getStop()) return target.getStop();
   P push = src.getPush(state);
   if (push != null) return identity.get(fact.createPush(target, tokens.apply(state)));
   P pop = src.getPop(state);
   if (pop != null) return identity.get(fact.createPop(target, tokens.apply(state)));
   return identity.get(fact.createState(target, tokens.apply(state)));
 }
Example #5
0
 public <S, P, D extends Pda<S, P>> Map<P, Pair<S, S>> collectPopsAndPushs(Pda<S, P> pda) {
   Map<P, Pair<S, S>> result = Maps.newLinkedHashMap();
   for (S s : nfaUtil.collect(pda)) {
     P push = pda.getPush(s);
     if (push != null) {
       Pair<S, S> o = result.get(push);
       Pair<S, S> n = Tuples.create(s, o == null ? null : o.getSecond());
       result.put(push, n);
     }
     P pop = pda.getPop(s);
     if (pop != null) {
       Pair<S, S> o = result.get(pop);
       Pair<S, S> n = Tuples.create(o == null ? null : o.getFirst(), s);
       result.put(pop, n);
     }
   }
   return result;
 }