1 5 package org.h2.bnf; 6 7 import java.util.HashMap ; 8 9 public class RuleOptional implements Rule { 10 private Rule rule; 11 private boolean mapSet; 12 13 RuleOptional(Rule rule, boolean repeat) { 14 this.rule = rule; 15 } 16 17 public String name() { 18 return null; 19 } 20 21 public String random(Bnf config, int level) { 22 if(level > 10 ? config.getRandom().nextInt(level) == 1 : config.getRandom().nextBoolean()) { 23 return rule.random(config, level+1); 24 } else { 25 return ""; 26 } 27 } 28 29 public Rule last() { 30 return this; 31 } 32 33 public void setLinks(HashMap ruleMap) { 34 if(!mapSet) { 35 rule.setLinks(ruleMap); 36 mapSet = true; 37 } 38 } 39 40 public String matchRemove(String query, Sentence sentence) { 41 if(sentence.stop()) { 42 return null; 43 } 44 if(query.length()==0) { 45 return query; 46 } 47 String s = rule.matchRemove(query, sentence); 48 if(s == null) { 49 return query; 50 } 51 return s; 52 } 53 54 55 public void addNextTokenList(String query, Sentence sentence) { 56 if(sentence.stop()) { 57 return; 58 } 59 rule.addNextTokenList(query, sentence); 60 } 61 62 } 63 | Popular Tags |