Dart 省道当量

Dart 省道当量,dart,Dart,我刚刚开始探索Dart语言,我想测试我用Java编写的现有代码,如下所示: public interface Condition { Condition FALSE = facts->false; Boolean evaluate(Fact<?> fact); default Condition and(Condition other) { return fact-> this.evaluate(fact) &

我刚刚开始探索Dart语言,我想测试我用Java编写的现有代码,如下所示:

public interface Condition {

    Condition FALSE = facts->false;

    Boolean evaluate(Fact<?> fact);
    
    default Condition and(Condition other) {
        return fact-> this.evaluate(fact) && other.evaluate(fact);
    }

    default Condition or(Condition other) {
        return fact-> this.evaluate(fact) || other.evaluate(fact);
    }
}
利用此功能的完整测试类是:

public class AnonymousLoopTest {
    @Test
    public void test() {
        RulesEngine rulesEngine = new InferenceRuleEngine();
        List<Name> names = NamesFactory.fetchNames();
        Rules rules = new Rules();
        Facts facts = new Facts();
        AtomicReference<Integer> countRef = new AtomicReference<>(1);
        names.forEach(personName -> {
            facts.put("name-" + countRef.get(), personName);
            countRef.set(countRef.get()+1);
            Condition condition = fact -> !personName.name().isEmpty();
            //Hack the comparator logic of DefaultRule/BasicRule in order to override its internal logic as below.
            //This is needed to register our Rule with Rules which uses a Set<Rule> to register new Rules
            //with the comparator logic written in BasicRule.
            Rule nameRule = new RuleBuilder((o1, o2) -> personName.name().compareTo(o1.getName()))
                    .when(condition).then(action -> System.out.println("In Action:" + personName)).build();
            rules.register(nameRule);
        });
        rulesEngine.fire(rules, facts);
    }

}

record Name(Integer id, String name){}

class NamesFactory{
    static List<Name> fetchNames(){
        return List.of(new Name(10, "Sara"), new Name(20, "Zara"), new Name(30, ""),new Name(40, "Lara"));
    }
}
公共类匿名测试{
@试验
公开无效测试(){
RulesEngine RulesEngine=新推理RuleEngine();
List name=NamesFactory.fetchNames();
规则=新规则();
事实=新的事实();
AtomicReference countRef=新的AtomicReference(1);
name.forEach(人名->{
facts.put(“name-”+countRef.get(),personName);
countRef.set(countRef.get()+1);
条件条件=事实->!personName.name().isEmpty();
//破解DefaultRule/BasicRule的比较器逻辑,以覆盖其内部逻辑,如下所示。
//这是用使用集合注册新规则的规则注册规则所必需的
//用BasicRule编写的比较器逻辑。
规则名称Rule=new RuleBuilder((o1,o2)->personName.name().compareTo(o1.getName())
.when(condition).then(action->System.out.println(“In-action:+personName”).build();
规则。注册(名称规则);
});
规则引擎火灾(规则、事实);
}
}
记录名称(整数id,字符串名称){}
类名称工厂{
静态列表获取名称(){
返回列表(新名称(10,“莎拉”)、新名称(20,“萨拉”)、新名称(30“)、新名称(40,“劳拉”));
}
}
该条件由
when()
方法使用。 在给定的示例中,空白名称将被过滤掉。其他三个名字将被打印出来


我试着用Dart写作,但我被卡住了。用Dart编写这段代码的方法是什么?

这看起来像是我要做的:

typedef条件=bool函数(事实);
bool-falseCondition(事实)=>false;
扩展条件组合条件{
条件运算符和(条件其他)=>(事实事实)=>此(事实)和其他(事实);
条件运算符|(条件其他)=>(事实事实)=>此(事实)|其他(事实);
条件运算符~()=>(事实)=>!此(事实);
}
如果您坚持要为函数对象使用包装器类,我会按照以下方式执行:

类条件{
静态常量条件falseCondition=条件(_kFalse);
最终布尔函数(事实)\ u测试;
常量条件(布尔测试(事实)):_测试=测试;
bool评估(事实)=>\u测试(事实);
条件运算符&(条件其他)=>条件((事实)=>
本.评估(事实)和其他.评估(事实));
条件运算符|(条件其他)=>条件((事实)=>
这个。评估(事实)|其他。评估(事实));
静态bool kFalse()=>false;
}
但是,对于一个真正只是简单函数的类来说,这似乎有些过分了。Dart具有一流的功能

您可以将前一版本用作:

测试(“测试条件”),(){
var str=“字符串”;
条件a=(事实)=>str.isNotEmpty();
条件b=(事实)=>str.contains(“A”);
var两者=a&b;
expect(两者都是(someDefaultFact),true);
}

这看起来像是我应该做的事情:

typedef条件=bool函数(事实);
bool-falseCondition(事实)=>false;
扩展条件组合条件{
条件运算符和(条件其他)=>(事实事实)=>此(事实)和其他(事实);
条件运算符|(条件其他)=>(事实事实)=>此(事实)|其他(事实);
条件运算符~()=>(事实)=>!此(事实);
}
如果您坚持要为函数对象使用包装器类,我会按照以下方式执行:

类条件{
静态常量条件falseCondition=条件(_kFalse);
最终布尔函数(事实)\ u测试;
常量条件(布尔测试(事实)):_测试=测试;
bool评估(事实)=>\u测试(事实);
条件运算符&(条件其他)=>条件((事实)=>
本.评估(事实)和其他.评估(事实));
条件运算符|(条件其他)=>条件((事实)=>
这个。评估(事实)|其他。评估(事实));
静态bool kFalse()=>false;
}
但是一个类对于一个简单的函数来说似乎有些过分了

您可以将前一版本用作:

测试(“测试条件”),(){
var str=“字符串”;
条件a=(事实)=>str.isNotEmpty();
条件b=(事实)=>str.contains(“A”);
var两者=a&b;
expect(两者都是(someDefaultFact),true);
}

你能不能通过一个例子说明这个接口是如何实现的?你能不能通过一个例子说明这个类的用法?你能不能通过一个例子说明这个接口是如何实现的?你能不能通过一个例子说明这个类的用法?第一种方法很简单,看起来很有前途。我会尝试实现第一种方法。第一种方法很简洁,看起来很有前途。我将尝试实现第一种方法。
public class AnonymousLoopTest {
    @Test
    public void test() {
        RulesEngine rulesEngine = new InferenceRuleEngine();
        List<Name> names = NamesFactory.fetchNames();
        Rules rules = new Rules();
        Facts facts = new Facts();
        AtomicReference<Integer> countRef = new AtomicReference<>(1);
        names.forEach(personName -> {
            facts.put("name-" + countRef.get(), personName);
            countRef.set(countRef.get()+1);
            Condition condition = fact -> !personName.name().isEmpty();
            //Hack the comparator logic of DefaultRule/BasicRule in order to override its internal logic as below.
            //This is needed to register our Rule with Rules which uses a Set<Rule> to register new Rules
            //with the comparator logic written in BasicRule.
            Rule nameRule = new RuleBuilder((o1, o2) -> personName.name().compareTo(o1.getName()))
                    .when(condition).then(action -> System.out.println("In Action:" + personName)).build();
            rules.register(nameRule);
        });
        rulesEngine.fire(rules, facts);
    }

}

record Name(Integer id, String name){}

class NamesFactory{
    static List<Name> fetchNames(){
        return List.of(new Name(10, "Sara"), new Name(20, "Zara"), new Name(30, ""),new Name(40, "Lara"));
    }
}