将简单的表达式语言放入java [英] Putting a simple expression language into java

查看:105
本文介绍了将简单的表达式语言放入java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Java应用程序,用户可以指定如何从一系列元数据字段中命名他们的文件。即

 %artist% - %album% - %disctotal%

我的代码解析这些字段,并重新命名文件。但是我希望用户能够使用表达式,以便他们可以这样说:

  $ if(% disctotal%> = 01,%discno%)

使用 ifelse ,比较长度和大小写等等。



我不想从头开始写这个,有没有什么可以提供给我这个我可以只是插件对我的代码?



编辑:我认为Ive有一些很好的回复,但我的知识是让我失望。让我们简化问题我希望用户能写



$ if(%disctotal%> = 01,%discno%)



到gui中的一个字段。



然后在我的Java代码中,我希望能够将这个表达式应用于一组文件,所以对于每个文件,我有disctotal的价值,并且discno但我想要能够将表​​达式转换成像



if(discTotal> = 1)
{
return discNo
}
else
{
return
}



我不想做的是必须编写识别字符串的代码(
$ if(%disctotal%> = 01,%discno%)是一个if语句,因为这是要做的。



然后在此扩展,我希望表达式允许诸如大写字段之间的字段,检查字段的长度等。



另外:也许应该以这种方式工作,用户输入表达式,然后在稍后的日期为每个文件Java代码复制aces每个变量的实际值



ie
$ if(%disctotal%> = 01,%discno%) - > $ if(2> = 01 ,1)



然后将其传递给外部压缩语言进行解析并给出结果



这是Javascript的想法?

解决方案

如何使用与Java 1.6捆绑在一起的JavaScript引擎



您可以看到将传入所有参数:

  ScriptEngineManager manager = new ScriptEngineManager(); 
ScriptEngine engine = manager.getEngineByName(JavaScript);
engine.put(艺术家,艺术家);
...

您将使用

$ b $读回该值b

  ScriptEngine.get(...)

胶水的最后一点是用函数声明来包围用户的表达式,并写一个调用函数的表达式,并将结果分配给一个众所周知的变量。



所以要开始实验,让我们有一个函数来测试表达式:

  private static void printResult(final ScriptEngine jsEngine ,String name,String expr)throws ScriptException {
Object result = jsEngine.eval(expr);
System.out.println(name +result:+ result +; expr:+ expr);
}

现在我们来说:

  public static void main(String [] args)throws Exception {
ScriptEngineManager sem = new ScriptEngineManager();
ScriptEngine jsEngine = sem.getEngineByName(JavaScript);
printResult(jsEngine,Hello World,Hello World);
printResult(jsEngine,Simple Math,123 + 456);
}

这产生:

  Hello World结果:Hello World; expr:'Hello World'
简单数学结果:579.0; expr:123 + 456

现在让我们尝试一下你的usecase:

  public static void main(String [] args)throws Exception {
ScriptEngineManager sem = new ScriptEngineManager();
ScriptEngine jsEngine = sem.getEngineByName(JavaScript);

String expr =artist +' - '+ album +(disktotal> 1?('-D'+ diskno):'');

jsEngine.put(艺术家,U2);
jsEngine.put(专辑,约书亚树);
jsEngine.put(disktotal,1);
jsEngine.put(diskno,1);
printResult(jsEngine,Single Disk,expr);

jsEngine.put(艺术家,Tori Amos);
jsEngine.put(album,To Venus and Back);
jsEngine.put(disktotal,2);
jsEngine.put(diskno,2);
printResult(jsEngine,Muti-Disk,expr);
}

产生结果:

 单磁盘结果:U2-Joshua Tree; expr:... 
多磁盘结果:Tori Amos-To Venus和Back-D2; expr:...

注意Tori的最后有D2。


Im my Java application, users can specify how to name their files from a series of metadata fields. i.e

%artist% - %album% - %disctotal%

My code then parses these fields and renames file accoringly. But I want the user to be able to use an 'expression languge' so they can say things like:

$if(%disctotal% >= 01,%discno%) 

Use ifelse, compare length and case and so on.

I dont want to write this from scratch, is there something that offers me this that I can just plugin to my code?

EDIT:I think Ive had some good replies, but my knowledge is letting me down. Lets simplify the issue I want the user to be able to write

$if(%disctotal% >= 01,%discno%)

into a field in a gui.

Then later on in my Java code I want to be able to apply this expression to a set of files, so for each file I have the value of disctotal, and discno but I want to be able to convert the expression into something like

if(discTotal >=1) { return discNo } else { return "" }

What I dont want to do is have to write the code that recognises the string (" $if(%disctotal% >= 01,%discno%)" is an if statement because this is akward to do.

Then expanding on this I would like the expression to allow things such as capitalizing oof fields, checking lengths of fields and so on.

Alternatively: Perhaps it should work this way, the user enters the expression, then at a later date for each file the Java code replaces each variable with the real value

i.e $if(%disctotal% >= 01,%discno%) -> $if(2 >= 01,1)

then this is passed to the exopression language to parse and give a result,

Is this the Javascript idea ?

解决方案

How about using the JavaScript engine that is bundled with Java 1.6?

You can see how you would pass in all the parameters:

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
engine.put("artist", artist);
...

You would read back the value using

ScriptEngine.get(...)

The final bit of glue would be to surround the user's expression with a function declaration, and write an expression that calls the function and assigns the result to a well known variable.

So to start experimenting, lets have a function to test expressions out:

private static void printResult(final ScriptEngine jsEngine, String name, String expr) throws ScriptException {
    Object result = jsEngine.eval(expr);
    System.out.println(name + " result: " + result + "; expr: " + expr);
}

Now let's call it:

public static void main(String[] args) throws Exception {
    ScriptEngineManager sem = new ScriptEngineManager();
    ScriptEngine jsEngine = sem.getEngineByName("JavaScript");
    printResult(jsEngine, "Hello World", "'Hello World'");
    printResult(jsEngine, "Simple Math", "123 + 456");
}

This produces:

Hello World result: Hello World; expr: 'Hello World'
Simple Math result: 579.0; expr: 123 + 456

Now lets try with your usecase:

public static void main(String[] args) throws Exception {
    ScriptEngineManager sem = new ScriptEngineManager();
    ScriptEngine jsEngine = sem.getEngineByName("JavaScript");

    String expr = "artist + '-' + album + (disktotal > 1 ? ('-D' + diskno) : '')";

    jsEngine.put("artist", "U2");
    jsEngine.put("album", "The Joshua Tree");
    jsEngine.put("disktotal", 1);
    jsEngine.put("diskno", 1);
    printResult(jsEngine, "Single Disk", expr);

    jsEngine.put("artist", "Tori Amos");
    jsEngine.put("album", "To Venus and Back");
    jsEngine.put("disktotal", 2);
    jsEngine.put("diskno", 2);
    printResult(jsEngine, "Muti-Disk", expr);
}

Produces result:

Single Disk result: U2-The Joshua Tree; expr: ...
Muti-Disk result: Tori Amos-To Venus and Back-D2; expr: ...

Notice how Tori's has 'D2' at the end.

这篇关于将简单的表达式语言放入java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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