@Test public void testSimplify() { Symbol c = Symbol.get("c"); Epsilon epsilon = Epsilon.get(); assertEquals(aOrb, Or.get(new ArrayList<ERE>(Arrays.asList(a, b, a, b, a, b, a, b, a)))); assertEquals(Or.get(new ArrayList<ERE>(Arrays.asList(a, b, c))), Or.get(new ArrayList<ERE>(Arrays.asList(aOrb, c)))); }
public ERE simplify() { ArrayList<ERE> flattened = new ArrayList<ERE>(children.size() >> 1); ArrayList<ERE> previous = new ArrayList<ERE>(children); boolean changed = true; while(changed) { changed = false; flattened = new ArrayList<ERE>(children.size()); for(ERE child : previous) { if(child.getEREType() == EREType.OR) { flattened.addAll(child.getChildren()); changed = true; } else { flattened.add(child); } } previous = flattened; } children = flattened; Collections.sort(children); for(int i = 0; i < children.size(); ++i) { if(children.get(i) == Empty.get()) { children.remove(i); } } for(int i = 0; i < children.size() - 1; ++i) { if(children.get(i).equals(children.get(i + 1))) { children.remove(i); i--; } } if(children.size() == 0) { return Empty.get(); } else if(children.size() == 1) { return children.get(0); } return this; }
Or extends ERE { public ERE simplify() { ArrayList<ERE> flattened = new ArrayList<ERE>(children.size() >> 1); ArrayList<ERE> previous = new ArrayList<ERE>(children); boolean changed = true; while(changed) { changed = false; flattened = new ArrayList<ERE>(children.size()); for(ERE child : previous) { if(child.getEREType() == EREType.OR) { flattened.addAll(child.getChildren()); changed = true; } else { flattened.add(child); } } previous = flattened; } children = flattened; Collections.sort(children); for(int i = 0; i < children.size(); ++i) { if(children.get(i) == Empty.get()) { children.remove(i); } } for(int i = 0; i < children.size() - 1; ++i) { if(children.get(i).equals(children.get(i + 1))) { children.remove(i); i--; } } if(children.size() == 0) { return Empty.get(); } else if(children.size() == 1) { return children.get(0); } return this; } }
Or extends ERE { public ERE simplify() { ArrayList<ERE> flattened = new ArrayList<ERE>(children.size() >> 1); ArrayList<ERE> previous = new ArrayList<ERE>(children); boolean changed = true; while(changed) { changed = false; flattened = new ArrayList<ERE>(children.size()); for(ERE child : previous) { if(child.getEREType() == EREType.OR) { flattened.addAll(child.getChildren()); changed = true; } else { flattened.add(child); } } previous = flattened; } children = flattened; Collections.sort(children); for(int i = 0; i < children.size(); ++i) { if(children.get(i) == Empty.get()) { children.remove(i); } } for(int i = 0; i < children.size() - 1; ++i) { if(children.get(i).equals(children.get(i + 1))) { children.remove(i); i--; } } if(children.size() == 0) { return Empty.get(); } else if(children.size() == 1) { return children.get(0); } return this; } private  Or(List<ERE> children); }
Or extends ERE { public ERE simplify() { ArrayList<ERE> flattened = new ArrayList<ERE>(children.size() >> 1); ArrayList<ERE> previous = new ArrayList<ERE>(children); boolean changed = true; while(changed) { changed = false; flattened = new ArrayList<ERE>(children.size()); for(ERE child : previous) { if(child.getEREType() == EREType.OR) { flattened.addAll(child.getChildren()); changed = true; } else { flattened.add(child); } } previous = flattened; } children = flattened; Collections.sort(children); for(int i = 0; i < children.size(); ++i) { if(children.get(i) == Empty.get()) { children.remove(i); } } for(int i = 0; i < children.size() - 1; ++i) { if(children.get(i).equals(children.get(i + 1))) { children.remove(i); i--; } } if(children.size() == 0) { return Empty.get(); } else if(children.size() == 1) { return children.get(0); } return this; } private  Or(List<ERE> children); static ERE get(List<ERE> children); ERE simplify(); @Override EREType getEREType(); @Override String toString(); @Override ERE copy(); @Override boolean containsEpsilon(); @Override ERE derive(Symbol s); }
Or extends ERE { public ERE simplify() { ArrayList<ERE> flattened = new ArrayList<ERE>(children.size() >> 1); ArrayList<ERE> previous = new ArrayList<ERE>(children); boolean changed = true; while(changed) { changed = false; flattened = new ArrayList<ERE>(children.size()); for(ERE child : previous) { if(child.getEREType() == EREType.OR) { flattened.addAll(child.getChildren()); changed = true; } else { flattened.add(child); } } previous = flattened; } children = flattened; Collections.sort(children); for(int i = 0; i < children.size(); ++i) { if(children.get(i) == Empty.get()) { children.remove(i); } } for(int i = 0; i < children.size() - 1; ++i) { if(children.get(i).equals(children.get(i + 1))) { children.remove(i); i--; } } if(children.size() == 0) { return Empty.get(); } else if(children.size() == 1) { return children.get(0); } return this; } private  Or(List<ERE> children); static ERE get(List<ERE> children); ERE simplify(); @Override EREType getEREType(); @Override String toString(); @Override ERE copy(); @Override boolean containsEpsilon(); @Override ERE derive(Symbol s);  }