DSL реализован с помощью AST трансформаций на этапе компиляции. Все выражения конвертируются и заменяются на соответствующие вызовы методов библиотеки. После этого на выходе получаем обычный Groovy скрипт. К примеру, файл из статьи HelloGrules.groovy будет выглядеть следующим образом:
public class test.HelloGrules extends groovy.lang.Script {
public test.HelloGrules() {
}
public test.HelloGrules(groovy.lang.Binding context) {
super.setBinding(context)
}
public static void main(java.lang.String[] args) {
org.codehaus.groovy.runtime.InvokerHelper.runScript(test.HelloGrules, args)
}
public java.lang.Object run() {
this.applyRuleToRequiredParameter('email', {
org.grules.script.expressions.SubrulesSeqWrapper.wrap(new org.grules.script.expressions.FunctionTerm({
this.isEmail(it)
}, 'isEmail') [ 'Invalid email'])
})
this.applyRuleToRequiredParameter('age', {
org.grules.script.expressions.SubrulesSeqWrapper.wrap(new org.grules.script.expressions.FunctionTerm({
this.toPositiveInt(it)
}, 'toPositiveInt') [ 'Invalid age']) >> {
it > 18
} [ 'You must be and adult']
})
}
}
Затем, на этапе выполнения, скармливаем скрипту набор входящих параметров и, анализируя результат каждого метода, собираем все воедино в виде отчета.
Добавил HelloWorld пример для наглядности. Также есть небольшое видео с туториалом по использованию библиотеки в проекте под управлением Grails (English).
Тут можно найти пример Groovy DSL для валидации данных. Так как тема довольно новая, хорошей литературы особо не найдешь, при написании библиотеки я в основном использовал открытую докумнтацию по Groovy и статьи других разработчиков DSL. Сорсы проекта лежат на гитхабе.
На Scala можно довольно быстро написать собственный компилятор, но вы теряете преимущества встроенных DSL.
This is an automatically generated Delivery Status Notification
Delivery to the following recipient failed permanently:
admin@mydomain.ru
Technical details of permanent failure:
PERM_FAILURE: Gmail tried to deliver your message, but it was rejected by the recipient domain. The error that the other server returned was: 550 550 Requested action not taken: mailbox unavailable. We recommend contacting the other email provider for further information about the cause of this error. Thanks for your continued support. (state 14)
----- Original message -----
Received: by 10.86.71.1 with SMTP id t1mr24345345fga.7.43453453453453;
Sun, 06 Apr 2008 03:08:11 -0700 (PDT)
Received: by 10.86.81.4 with HTTP; Sun, 6 Apr 2008 03:08:11 -0700 (PDT)
Date: Sun, 6 Apr 2008 13:08:11 +0300
From: "Somebody"
To: admin@ .ru
Subject: test
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
Затем, на этапе выполнения, скармливаем скрипту набор входящих параметров и, анализируя результат каждого метода, собираем все воедино в виде отчета.
Добавил HelloWorld пример для наглядности. Также есть небольшое видео с туториалом по использованию библиотеки в проекте под управлением Grails (English).
На Scala можно довольно быстро написать собственный компилятор, но вы теряете преимущества встроенных DSL.
www.google.com/trends?q=vkontakte%2C+facebook&ctab=0&geo=ru&geor=all&date=2010&sort=1
(99 * 1 + 1 * 10 000 ) / 100 = (98 * 1 + 2 * 10 000) / x
отсюда x = 199.02 сервера
Pages 69-85
объясните зачем в конце нужен "return false"?
немогу настроить почту :(
приходит:
This is an automatically generated Delivery Status Notification
Delivery to the following recipient failed permanently:
admin@mydomain.ru
Technical details of permanent failure:
PERM_FAILURE: Gmail tried to deliver your message, but it was rejected by the recipient domain. The error that the other server returned was: 550 550 Requested action not taken: mailbox unavailable. We recommend contacting the other email provider for further information about the cause of this error. Thanks for your continued support. (state 14)
----- Original message -----
Received: by 10.86.71.1 with SMTP id t1mr24345345fga.7.43453453453453;
Sun, 06 Apr 2008 03:08:11 -0700 (PDT)
Received: by 10.86.81.4 with HTTP; Sun, 6 Apr 2008 03:08:11 -0700 (PDT)
Date: Sun, 6 Apr 2008 13:08:11 +0300
From: "Somebody"
To: admin@ .ru
Subject: test
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline