我应该更改为 Scala 以创建具有重写规则的系统吗? [英] Should I change to scala to create a system with rewrite rules?

查看:18
本文介绍了我应该更改为 Scala 以创建具有重写规则的系统吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一些在 Android 应用程序中使用的类.我在该核心中有大约 6-7 个类,其中一些是具有抽象方法的抽象类.创建这些类是为了提供一个 API 来扩展我的 Android 应用程序.

I have some classes that I developed that I am using in a Android application. I have about 6-7 classes in that core, some of them are abstract classes with abstract methods. Those classes were created to provide a API to extend my Android Application.

现在我想创建一个接受重写规则的可扩展系统.这些规则对于在运行时替换某些组件很有用.想象一个有数学运算的系统,你可以看到所有的和、乘法等.现在你可以缩小,我想根据缩放级别简化一些运算.

Now I want to create an extensible system that accepts rewrite rules. Those rules are useful to replace some components at runtime. Imagine a system with mathematical operations where you see all the sums, multiplications, etc. Now you can zoom out and I want to simplify some operations dependending on the zoom level.

我的系统是用 Java 构建的,但我相信具有模式匹配的 Scala 会简化我的问题.然而,每次我看 Scala 时,我都会看到我必须花费很多时间以及配置 IDE 的很多麻烦......

My system was built in java, but I belive that scala, with pattern matching, will simplify my problem. However, everytime I look at scala I see a lot of time I have to spend and a lot of headches configuring IDEs...

我的类是为创建这样的结构而构建的:

My classes are built to create structures like this one:

我希望能够编写规则来创建包含其他块的块.类似的东西:

I want to be able to write rules that create a block that contains other blocks. Something like:

Integer Provider + Integer Provider -> Sum Provider
Sum Provider + Sum -> Sum Provider

规则可以由程序员创建.我的结构中的任何元素也可以由程序员构建.我不知道 Scala 是否简化了这个规则引擎系统,但我知道这个引擎,在 java 中,构建起来可能很无聊(可能会产生很多错误,我会忘记一些情况等).

Rules can be created by programmers. Any element of my structure can also be built by programmers. I don't know if scala simplifies this rule engine system, but I know that this engine, in java, can be boring to build (probly a lot of bugs will be created, I will forget some cases, etc).

我应该将所有系统更改为 Scala 吗?或者只能使用scala的这个功能?值得吗?

Should I change all my system to scala? Or there is away to use only this feature of scala? Does it worth it?

PS:有关结构的更多信息,请参阅用户体验.

PS: For more information on the structure please see this post at User Experience.

推荐答案

是的,在 Scala 中编写这样的规则很容易,实际上 Stack Overflow 上也有一些与 Scala 中的规则重写系统相关的问题.另外,有一些库可以帮助你解决这个问题,与战略编程和 nlp 相关,但我没有使用它们,所以我不能评论.

Yes, it is easy to write such rules in Scala, and, in fact, there have been some questions on Stack Overflow related to rule rewriting systems in Scala. Also, there are some libraries that may help you with this, related to strategic programming and nlp, but I haven't used them, so I can't comment much.

现在,我不知道这些类的确切来源.如果您正在解析和构建它们,解析器组合器库可以轻松处理它:

Now, I don't know exactly where these classes are coming from. If you are parsing and building them, the parser combinator library can trivially handle it:

sealed trait Expr { def value: Int }
case class Number(value: Int) extends Expr
case class Sum(e1: Expr, e2: Expr) extends Expr { def value = e1.value + e2.value }

object Example extends scala.util.parsing.combinator.RegexParsers {
  def number: Parser[Expr] = """\d+""" ^^ (n => Number(n.toInt))
  def sum: Parser[Expr] = number ~ "+" ~ expr ^^ {
    case n ~ "+" ~ exp => Sum(n, exp)
  }
  def expr: Parser[Expr] = sum | number
}

如果您以其他方式拥有这些类并且正在应用简化,您可以这样做:

If you have these classes in some other way and are applying simplifications, you could do it like this:

def simplify(expr: List[Expr]): Expr = expr match {
  case expr :: Nil => 
    List(expr) // no further simplification
  case (n1: NumberProvider) :: Plus :: (n2: NumberProvider) :: rest => 
    simplify(SumProvider(n1, n2) :: rest)
  case (n: NumberProvider) :: Plus :: (s: SumProvider) :: rest => 
    simplify(SumProvider(n, s) :: rest)
  case (s: SumProvider) :: Plus :: (n: NumberProvider) :: rest => 
    simplify(SumProvider(s, n) :: rest)
  case other => other // no further simplification possible
}

这里的重要元素是case classesextractors模式匹配.

The important elements here are case classes, extractors and pattern matching.

这篇关于我应该更改为 Scala 以创建具有重写规则的系统吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆