我的类中有一个方法TextPlan
,它调用另一个类中RSTRule
的所有规则并将所有规则添加到数组列表中。对于这种方法,我在每次调用 RST 方法时都使用了import java.lang.reflect.InvocationTargetException and
import java.lang.reflect.Method .
But it adds to the array list incorrectly. For example, the result of
add(RSTRule)`,如下所示:
call 1: RSTRules.add(RSTRule)=R1
call 2: RSTRules.add(RSTRule)=R2,R2
call 3: RSTRules.add(RSTRule)=R3,R3,R3
call 4: RSTRules.add(RSTRule)=R4,R4,R4
这意味着每次我添加一个新元素时,它都会从数组列表中删除以前的元素,但会重复新元素。
这是我的代码:
public class TextPlan extends ArrayList<Object> {
RSTRules rstRules=new RSTRules();
public RSTRules produceAllRSTRules(RSTRules rstRules) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
RSTRule rstRule=new RSTRule();
Method[] methods =rstRule.getClass().getMethods();
for (Method method : methods) {
if (method.getName().startsWith("generate")) {
rstRule=(RSTRule)method.invoke(rstRule);
rstRules.add(rstRule) ;
}
}
return rstRules;
}
}
这是我在“RSTRule”中的一种方法,我在“TextPlan”中将它们全部调用以生成所有规则的实例;
public class RSTRule{
protected String ruleName;
protected DiscourseRelation discourseRelation;
protected String ruleNucleus;
protected String ruleSatellite;
protected String condition;
int heuristic;
public RSTRule generateBothAppearBothCosts_Join(){
this.discourseRelation=DiscourseRelation.Join;
this.ruleNucleus="BothProductAppear_Elaboration";
this.ruleSatellite="BothProductCost_Elaboration";
this.ruleName="BothProductAppearAndBothProductCost_Join";
this.condition=null;
this.heuristic=9;
return this;
}
}