19、使用 Groovy 实现规则领域特定语言(DSL)

使用 Groovy 实现规则领域特定语言(DSL)

1. 引言

在当今的软件开发中,领域特定语言(DSL)发挥着重要作用,它能够让业务人员和开发人员更高效地沟通和实现业务规则。本文将介绍如何使用 Groovy 构建一个能够实现应用程序业务规则的 DSL,以在线宽带媒体提供商的促销系统为例,讲解如何实现各类奖励和奖金规则。

2. Groovy 绑定基础

每个 Groovy 脚本都有一个关联的绑定对象,它是存储脚本中引用变量实例的地方。绑定对象是 groovy.lang.Binding 类的实例,我们可以在任何脚本中通过引用内置变量 binding 来访问它。

当在脚本中引用一个先前未声明的变量时,Groovy 会在绑定中创建该变量的实例。而在脚本中定义的变量被视为局部变量,不会存储在绑定中。这为 DSL 提供了一个机会,通过在绑定中显示变量,我们可以使用预定义的值来操作脚本;通过向绑定中添加闭包,我们可以为 DSL 提供内置方法。

以下是一个示例,展示了变量在绑定中的存储情况:

count = 1
assert count == 1
assert binding.getVariable("count") == 1
binding.setVariable("count" , 2)
assert count == 2
assert binding.getVariable("count") == 2 
def local = count 
assert local == 2
try {
    bindi
Groovy开发语言系列(第三篇):DSL的强大能力 DSL是一种专注于特定领域的编程语言。与通用编程语言(如C、C++、Java、Python、Ruby和Scala)相比,DSL更加关注某个具体领域的问题解决。Groovy通过其动态性和灵活性提供了强大的DSL支持。 阅读详情

相关推荐

1、探索Groovy与领域特定语言(DSL)的融合之美

本文深入探讨了Groovy如何与领域特定语言(DSL)融合,展示了Groovy作为JVM上的动态语言在简化代码表达、提升开发效率方面的强大能力。文章涵盖了Groovy的核心语言特性、DSL的基本概念与实现方式,并通过示例代码演示了Groovy在构建DSL中的灵活应用。此外,还介绍了Groovy的快速入门方法、语言基础、闭包的高级用法以及构建DSL的高级特性,为开发者提供了一条从入门到精通Groovy与DSL开发的清晰路径。

ruby5的博客 103

3、领域特定语言(DSL)与 Groovy 语言入门

本文介绍了领域特定语言(DSL)的基本概念及其在实际应用中的优势,并重点探讨了 Groovy 语言作为构建 DSL 的理想工具的原因。文章详细分析了 DSL 语法设计的注意事项、Groovy 的关键语言特性(如运算符重载、闭包、可选类型、原生集合支持等),并通过代码示例展示了如何使用 Groovy 实现简单的 DSL。此外,还比较了 Groovy 与 Java 的主要区别,并给出了使用 Groovy 开发 DSL 的操作步骤与最佳实践。无论是希望了解 DSL 的开发者,还是想探索 Groovy 强大功能的用

ruby5的博客 126

25、Groovy 领域特定语言(DSL)的创建与应用

本文深入探讨了 Groovy 在领域特定语言(DSL)创建与应用中的强大能力。通过流畅的语法设计和多种实现方式,如命令链、闭包、方法拦截、ExpandoMetaClass 等,Groovy 提供了灵活的工具来构建内部 DSL。文章还分析了 DSL 的类型区别、设计技巧、应用场景及未来发展趋势,并结合自动化测试和微服务配置等实际案例展示了 Groovy DSL 的实用价值。无论是初学者还是有经验的开发者,都能从中获得对 Groovy DSL 的全面理解与实践指导。

fff88的博客 159

Unity开源游戏源码精选:17个亲测可运行教学项目

Unity游戏开发中,掌握真实项目结构与工程实践远比熟记API更重要。本文围绕‘Unity开源项目’和‘游戏源码’两大核心概念,解析高质量开源资源的本质特征——结构清晰、逻辑可溯、修改即时反馈。从基础的MonoBehaviour生命周期组织,到进阶的ScriptableObject数据驱动、事件总线解耦,再到架构级的DOTS ECS设计与网络同步模式,技术价值在于将抽象原理落地为可调试、可验证、可迁移的代码范式。适用于新手建立开发直觉、中级开发者突破系统设计瓶颈,以及团队构建可维护框架。所有项目均基于Uni

weixin_31528001的博客 746

从大疆智图到Cesium:3D Tiles模型加载与Web三维场景构建实战

本文详细介绍了如何利用大疆智图生成3D Tiles数据,并通过Cesium在Web端实现高效加载与三维场景构建。从大疆智图参数设置到Cesium环境配置,再到3D Tiles性能调优与高级功能实现,提供了一套完整的实战指南,帮助开发者快速掌握3D Tiles模型加载与Web三维场景构建技术。

weixin_30520015的博客 1300

RTKLIB PPP与RTK_POST精密定位实战:从实时到事后处理的完整流程解析

本文详细解析了如何使用RTKLIB进行精密单点定位(PPP)的完整流程,涵盖从实时处理到事后处理(RTK_POST)的关键步骤。文章深入探讨了数据准备、软件配置、核心参数设置(如电离层、对流层、潮汐改正)以及常见问题排查,旨在帮助用户掌握高精度GNSS定位技术,实现从入门到精通的实战应用。

weixin_29047919的博客 291

【OpenCV3】cv::compare()使用详解

cv::compare()主要用于两个图像之间进行逐像素的比较,并输出比较的结果。具体用法如下: cv::compare() bool cv::compare( cv::InputArray src1, // 输入数组1 cv::InputArray src2, // 输入数组2 cv::OutputArray dst, // 输出数组 int cmpop // 比较操作子,见下表 ...

guduruyu的专栏 9771

24届-Python面经(华为OD)

【摘要】24届非科班生通过栗栗姐指导成功转行华为OD。从4月4日零基础准备,经历机考(4/18)、测评(4/19)、资格面(5/7)、技术面(5/9-11)到5月17日获offer。全程栗栗姐提供题库、面经指导和实时跟进。虽遇压力面和技术短板,但在专业辅导下10天走完全流程。案例证明转行需自身努力+优质HR支持,栗栗姐团队提供多岗位(Java/Python等)、多地域(北上深等)OD机会,含笔试资料和全流程攻略。

2401_86909848的博客 1200

Ubuntu 24.04 部署 EMQX 5.8 开源版MQTT服务器:从零搭建到实战应用

本文详细指导如何在Ubuntu 24.04系统上部署EMQX 5.8开源版MQTT服务器。内容涵盖从环境准备、安装Erlang、下载安装包到启动服务的完整步骤,并提供了验证服务、配置网络防火墙以及通过WebSocket工具进行基础消息收发的实战方法,帮助用户快速搭建稳定、免费的物联网消息中间件。

weixin_29209825的博客 300

华为OD机试真题2026双机位C卷 C语言 实现【主次关联成环警告】

在ICT运维领域,现网运维工程师面向对设备上报的众多告警,往往需要筛选出最主要的告警优先处理,次等级的告警或许为同一个根因导致的告警,处理优先级会放后或者不处理,这样就诞生出主次关联告警的概念。给定一系列告警的主次关联关系,判断是否存在如下情况:

MISAYAONE的博客 176

ABAP Channels 之 WebSocket 通信实践:用 ABAP Push Channel 打造实时应用

摘要:本文介绍了ABAP Push Channel(APC)技术,它通过WebSocket协议实现ABAP系统与外部客户端的实时双向通信。文章对比了传统HTTP轮询与WebSocket的差异,解释了APC的事件模型及其接口演进(从IF_APC_WS_EXTENSION到IF_APC_WSP_EXTENSION)。通过仓库拣货等实际案例,展示了APC在企业级应用中的价值,如实时监控、协作场景等。文中还详细阐述了ABAP引擎中WebSocket的交互模型,说明其资源优化设计原理,并提供了基础代码示例。该技术解决

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 306

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

上一篇: 3、文章推荐系统与基于推特推文的人格预测
下一篇: 50、Java编程语言:选择与开发指南
ruby5
博客等级 码龄15年 29粉丝 893原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址