/** * Returns the singular form of the word in the string. * * <p>Examples: * * <pre> * inflector.singularize("posts") #=> "post" * inflector.singularize("octopi") #=> "octopus" * inflector.singularize("sheep") #=> "sheep" * inflector.singularize("words") #=> "word" * inflector.singularize("the blue mailmen") #=> "the blue mailman" * inflector.singularize("CamelOctopi") #=> "CamelOctopus" * </pre> * * Note that if the {@link Object#toString()} is called on the supplied object, so this method * works for non-strings, too. * * @param word the word that is to be pluralized. * @return the pluralized form of the word, or the word itself if it could not be pluralized * @see #pluralize(Object) */ public String singularize(Object word) { if (word == null) return null; String wordStr = word.toString().trim(); if (wordStr.length() == 0) return wordStr; if (isUncountable(wordStr)) return wordStr; for (Rule rule : this.singulars) { String result = rule.apply(wordStr); if (result != null) return result; } return wordStr; }
private void applyRules(String name) { if (applyingRulesFor.contains(name)) { return; } applyingRulesFor.add(name); try { for (Rule rule : rules) { rule.apply(name); } } finally { applyingRulesFor.remove(name); } }
/** * applyRules. * * @param op a {@link lupos.engine.operators.BasicOperator} object. * @param mapStartNodes a {@link java.util.Map} object. * @param rules an array of {@link lupos.optimizations.logical.rules.Rule} objects. * @param untilRule a {@link lupos.optimizations.logical.rules.Rule} object. */ public void applyRules( final BasicOperator op, final Map<Class<? extends BasicOperator>, Set<BasicOperator>> mapStartNodes, final Rule[] rules, final Rule untilRule) { for (final Rule r : rules) { if (untilRule != null && r.equals(untilRule)) break; while (r.apply(op, mapStartNodes)) { Rule[] rulesToApply = r.getRulesToApply(this); if (rulesToApply == null) rulesToApply = this.rules; applyRules(op, mapStartNodes, rulesToApply, r); } } }
private static String applyRules(List<Rule> rules, String word) { String result = word; if (!uncountables.contains(word.toLowerCase())) { for (int i = rules.size() - 1; i >= 0; i--) { Rule rule = rules.get(i); if ((result = rule.apply(word)) != null) { break; } } } return result; }
/** * 执行规则验证 * * @param astCompilationUnit - 编译单元 * @param rule - 对应的规则 * @return */ public static RuleContext executeRuleValidator(ASTCompilationUnit astCompilationUnit, Rule rule) { RuleContext ruleContext = new RuleContext(); if (astCompilationUnit.jjtGetNumChildren() > 0) { // 忽略编译单元为空,即类中无任何信息的类文件 VisitorStarter visitorStarter = new VisitorStarter() { public void start(Node rootNode) { new SymbolFacade().initializeWith((ASTCompilationUnit) rootNode); } }; visitorStarter.start(astCompilationUnit); ArrayList<Node> nodeList = new ArrayList<Node>(); nodeList.add(astCompilationUnit); rule.apply(nodeList, ruleContext); } return ruleContext; }
/** * Take a single step of the simulation. This is done by walking through each cell and calculating * its new state by applying the rules in their order until either one applies and yields a new * state, or not apply (hence, the original state is retained). */ public void step() { int width = board.getWidth(); int height = board.getHeight(); // nCells holds the updated state for each cell on the board. int[] nCells = new int[width * height]; /* * Now visit each cell and compute it's updated cell. Note that we don't update the board * yet. This is to prevent one cell from seeing the updated state of another cell in the * same step. */ for (int x = 0; x < width; ++x) { for (int y = 0; y < height; ++y) { nCells[(y * width) + x] = board.getCellState(x, y); Point p = new Point(x, y); for (Rule r : rules) { // Apply the rule Integer result = r.apply(new Pair<Point, Board>(p, board)); if (result != null && result >= 0 && result <= 9) { /* * Yes, the rule was successfully applied. Therefore, update the cell's * state and move onto the next cell. */ nCells[(y * width) + x] = result; break; } } } } // Finally, go through and update each cell on the board with its new state. for (int x = 0; x < width; ++x) { for (int y = 0; y < height; ++y) { int nState = nCells[(y * width) + x]; board.setCellState(x, y, nState); } } }
/** Test of apply method, of class Rule. */ @Test public void testApply() { Rule simple = new Rule("test"); assertEquals("test", simple.apply("test")); Rule simpleReplace = new Rule("s/test/foo/"); assertEquals("foo", simpleReplace.apply("test")); Rule simpleReplace2 = new Rule("s/test/foo"); assertEquals("foo", simpleReplace2.apply("test")); Rule erase = new Rule("s/test//"); assertEquals("", erase.apply("test")); Rule complexReplace = new Rule("s/test(.*)/foo$1/"); assertEquals("foo", complexReplace.apply("test")); assertEquals("foo2", complexReplace.apply("test2")); Rule complexReplace2 = new Rule("s/.*/debian/"); assertEquals("debian", complexReplace2.apply("xxx")); assertEquals("debian", complexReplace2.apply("test")); assertEquals("debian", complexReplace2.apply("test2")); Rule generic = new Rule("*"); assertEquals("xxx", generic.apply("xxx")); assertEquals("test", generic.apply("test")); assertEquals("test2", generic.apply("test2")); Rule generic2 = new Rule("test*"); assertEquals("test", generic2.apply("test")); assertEquals("test2", generic2.apply("test2")); }
@Override public boolean apply(T input) { return left.apply(input) || right.apply(input); }