creating own scripting language in Java desktop app? - java

I have a simple web application testing desktop application. I am wondering how I can create my own scripting language for it (Domain Specific Language).
The purpose of this is to offer a very intuitive scripting even for non programmers, and able to define user scenario specific details.
I hope this is clear, basically the question is, how do I create a simple-to-use scripting language that can be translated into Java code.
Perhaps, they can use netbean's IDE to write the scripts.
For ex)
my script:
load "http://www.google.com";
click "Search" button;
java code:
browser.navigate("http://www.google.com";
browser.wait
browser.element("Search").click();

The best tool/framework for languages which extend beyond very simple grammars is to use ANTLR. Its a full featured lexer+parser combination, great Java integration, and tons of examples with good documentation.
Do realize that this isn't a trivial project to get right. You should use an existing language if your time is limited

The best solution would be to reuse one the existing scripting languages for Java; for example BeanShell. Implementing your own scripting language would be whole a lot of work. And if you've never done any language design and implementation work before the results are likely to be "less than stellar".
Perhaps, they can use netbean's IDE to write the scripts.
Expecting users of your application (especially non-programmers) to install and use a particular IDE sounds like a really bad idea.
... that can be translated into Java code.
Translating to Java code complicates things as well. It is probably better to interpret a scripting language and access Java APIs using a combination of staticly typed method calls and (only where necessary) the Java reflection APIs.

Related

Making an API or scripting language for game engine?

I am currently writing a very simple 2.5D game engine in java and game. This is basically just a fun project I am working on so the amount of time it will take to implement features is no problem, as I can always put it aside and work on it whenever. With that said, I was planning on implementing a custom sort of scripting system. Basically the engine would be a shell of a 2.5D game, which can be modified and extended through simple scripts. So far I have the following ideas of how to go about this but I am unsure of what is practical or not.
Write a very simplistic scripting language and a simplistic parser that checks a folder for the scripts and runs them.
Make a java library/API that allows you to modify parts of the game on client and server via just plain old java
Use an already created language that is very simple such as lua for extendable parts of the game
I really thought about option 1 as being what I wanted to do, however I have never really done such a thing, I was wondering if someone could recommend what I should do here. If I do go with writing a simplistic language, I am just looking for some material or documentation on such topics, would I just look at how people wrote basic compilers/interpreters?
The short answer would be 2, go with the API. It's the easiest way to add or change features to your game engine.
Creating your own scripting language and parser is tempting for every developer. It's a great learning experience, but it's also a downside. When you start to write the scripts and the result isn't what you expected, you don't know if the bug is in your script or in the parser. You will need some debugging info from the parser maybe some stacktrace, even a very simple scripting language may start to take over your project. When you start to write your scripts you will probably feel limited by the features missing, the ones you are used to from writing java code and maybe start to expand the scripting language.
Some of the downsides with creating your own language is also true for allready existing languages. If you really need a very free way to extend your game engine you can choose that road, but I would not recommend it. You would still risk shifting your focus from your game engine to the scripting implementation.
One way to think about it is also from the writer of the mods point of view. Would they want to write in a new script language, in a existing script language or in java? The answer will most likely be java and probably not a new language.

Java game engine, writing own scripting language?

I'm planning to create a simple Visual Novel game engine out of Java. I've developed for Java before but I have experience with other languages. What I would like to do, is to create my own scripting language that people can use with the engine to make the complete game out of a script, since Visual Novels are simple enough to do it, I think.
What I don't know is, is Java suitable for making such a scripting language? The way I imagine it is, the engine reads a script file in the game folder, and takes in the input and executes each command linking up to a certain function or something like that. I'm not sure how difficult theoretically that would be to pull off.
As you can probably tell I'm a newbie when it comes to this.
Thanks.
It sounds like you need a domain-specific-language (DSL). Here's an article on DSLs within Java
DSLs are small, expressive programming languages custom designed for
specific tasks. In this four-part series, Venkat Subramaniam
introduces the concept of DSLs and eventually shows you how to build
them using Java
and there's a related question on SO.
You can use Scala to write DSLs and run them on the JVM. However I would warn you that without Scala knowledge it's a (IMO) non-trivial task to do this.
Alternatively you can use a Java implementation of a scripting language together with the Java Scripting API. This will allow you to use an existing language and have it compiled into bytecode. The advantag eis that you're using an existing language. The disadvantage is that it's not specific to your particular needs.
Don't do it. Writing a new language is surprisingly complex. Designing grammar, parsing, error reporting, performance.
It's already there: Creating meta language with Java
Consider groovy, lua or jruby first. People will love you for using familiar syntax.
While it's technically possible to do this, I don't know if Java would be your best bet. While I don't know the scope or purpose of your project, I'd recommend PyGame as a game/scripting language. (You said that you have experience with other languages, so I assume that switching to Python is ok)
I think the above answers give nice directions.
I would like to offer some more ideas:
A. Provide a java "GAME API", which can be exposed via REST/SOAP web services and can have client classes generated from (for example, take a WSDL, and generate client classes from it).
B. Provide the API as a jar file, and have you game implement a File class loader that will load external jars, and use the classes developers will develop using the API.
Here is an example of how to implement such a class loader.
C. Use the ScriptEngine provided by java, to invoke scripts.
See here an example of how to invoke a JavaScript script from within java.
This question is old and i am a bit late for the party (though this question is not answered yet), but it is possible. From what i understand, this sounds like a job for Just Another Framework. Full disclosure: I am the developer of the Framework i am describing. I had a similar problem and wrote this framework to reuse this system, which worked for this kind of issue.
TL;DR
Do not use JAF, if you want your own, fully functional, perfect language.
Use JAF, if you just want to create a simple scripting part, that decouples parts of your code or if you want to evaluate "user input" scripts at Runtime (like in the topic of this question).
What I don't know is, is Java suitable for making such a scripting language?
You can do it with nearly every programming language. You should consider to use Javascript or Lua or [...] and put those into a file instead of creating a custom scripting language. Java has a place for those languages. You should also consider to maybe read .java or .class files from a folder and take those into your "Game-Engine". If you however want to provide your own language and no one can change your mind about it (that was my issue), JAF can help you with that.
JAF is a framework, which allows you to define custom scripting languages within Java, that may be evaluated and executed at Runtime. You give the Parser some Functions, Rules as well as the raw script and the result is a fixed set of instructions (in the form of consumers), that can be executed as many times as you like.
This is by no means another language build on top of java! It is meant to be a foundation for your language. It is only meant for creating small parts though. An example would be some sort of logic that should be interchangeable without stopping the application. This logic should be executed via small scripts and provided via a Database or (like in your case) from files located in a folder.
You can use a Parser, to parse "raw-scripts" that are created by the users. This parser takes Functions and Rules in the form of a Strategy-/Command-Pattern, which basically define your language. After parsing, a "Script"-Object is returned, that can be executed as many times as you want. You can also set values of the Script and therefor connect this Script to your real and running java-code.
The real thing i loved about this however is, that you can provide custom script elements through Rules and Functions, which basically directly connect to java. Just an example would be the printline function. This calls System.out.println();. As i said, just an example, but you can imagine that you can call any existing code from your Functions and Rules.
A (very) simple example of how to use this would look like this:
String rawScript = ... //your users script here
Parser parser = Parser.create(); // used to parse the script
parser.add(IOModule.getPackage()); // add packages of Functions and Rules
Script script;
try {
script = parser.parse(rawScript); // Create the Script
} catch (ParsingFailedException e) {
// Handle an error while parsing the script
// Do not continue, there is no script!
}
script.setValue("yourKey", /* Whatever should be available in your script */); // Inject outside variables for your Rules/Functions
try {
script.run(); // Execute the script
} catch (ExecutionFailedException e) {
// Handle an error at the scripts execution
}
I do not want to write a book about how to use this framework. Take a look at the wiki, it is better explained there.
The advantage of this is, that you can simply define your own language, by providing simple commands that parse specific pieces of the raw script. They are just "applied"; All the magic is hidden within the framework. It's explained a bit within the frameworks-wiki, but i am bad at explaining. So you can create your own little scripting language by providing the building-blocks of your language without requiring deeper knowledge of theoretical computer science.
It has some issues though. It is currently not compatibly with the JSR 223. Also, it is (of course) slower than playn java.

Best dynamic language to pair with Java on a Java project

What is the best dynamic language to pair with Java on a large Java project?
We are considering using a dynamic language for tests, controllers, services. Some options are Groovy, JRuby or Jython. What are the pros and cons of each for this? Ideally we'd be able to call Java from the dynamic language as well as call the dynamic language from Java.
EDIT: If it helps, we're using Hibernate with PicoContainer and Webwork.
Thanks,
Alex
There are really three dynamic languages that offer a very seamless interop with Java - scala, groovy and clojure. From there, I'd ask your team which language they would rather work in or have them try a prototype in each language and see what they think.
If the team efficiency isn't important in the beginning, look to what problem each language attempts to solve:
Groovy is going to be very loose but natural to experienced Java developers and allows fast prototype development due to it's duck typing.
Scala is going to enable you to write DSLs making it a good for frameworks and tools where you want to solve the problem in a language more akin to how you would describe the problem.
Clojure is going to impose lisp's functional programming and immutable state concepts and could be a very natural fit for problems in AI, natural language processing, etc.
Finally, I've gone down the path of looking for the perfect language to base projects on and have found there is no perfect language. All of the languages I've mentioned above compile to native JVM byte code and are quite solid. Sometimes you just need to pick a language that might not be as cool as the others but gets you on the way to solving your problem.
I recommend Groovy, principally because it interoperates seamlessly with Java, and is almost a superset of Java, so very easy for a Java developer to learn. I have absolutely no evidence to support this, but based on hearsay, guesswork, and personal experience, I suspect the Groovy community is much larger than that of either JRuby or Jython.
Incidentally, Groovy++ is way too immature to consider for production use, in my opinion.
The answer is, of course, going to depend somewhat on matters of taste and flexibility. If there are folk who don't have experience with Ruby or Python then Groovy is going to have a syntax much closer to Java (in fact it is a superset of Java), and consequently be a much easier sell.
I can't really speak to JRuby as I haven't used it.
Groovy gives you probably the easiest interop with Java of the three you listed. It also has a very nice BDD library in EasyB which I like a lot. On the negative side I don't think the features or syntax of Groovy really hang together very well. It can kind of feel like a whole bunch of separate extensions to Java.
Jython is of course Python so the syntax is different, but also has all the consistency of Python. Interop is very good at the script level but at least used to be a bit awkward if you wanted to write pre-compiled classes in Jython that you call from Java. The other main pro over Groovy for me is that is that you have a real REPL to interact with the Java project.
I would also mention Clojure, the syntax is even more different but the Java interop is excellent, probably the best of all, and again you have a REPL. On the down-side if folk have trouble adjusting to Ruby or Python syntax then a Lisp is probably right out.
Clojure is probably the best dynamic language for controllers and services. (depending on what you mean with "services".
Scala and Groovy++ has the best java interop, but those are not dynamic (well in Groovy++ you decide for yourself what is typed.). Scala has the look and feel of a dynamic language. Scala has good testing frameworks http://www.scalatest.org/ and Specs and AKKA is very mature for services and also has Java APIs
I'd suggest going with Jython. The syntax is clean, and you get whatever additional power/conveniences that Python gives you.
For example, if you were to go with Groovy, you are basically limited to only what Java will give you. Jython would add the powers of Python to that as well.
If it helps any, I've used Jython with Hibernate, SOAP, Corba, and EJBs and it is much easier than doing the same with just plain Java.

Java + Scripting languages (JSR 223)

I'm designing a Framework which I want to attach to the scripting language API to make it more versatile and easier to use (for some things I really prefer the scripting way myself ;). With languages like JRuby, Jython or Rhino (JavaScript) there are interpreters for many popular scripting languages available and as far as I read, all of them implement the Java Scripting language API to embed them in your Java application.
Do you have experience with running it that way? I'm especially interested in handling e.g. associative arrays (or Java Beans).
How is the performance (e.g. compared to a CGI like approach or the native Java way)? Would it be easy to switch between the different interpreters (of course it is an API specification but I still don't know on how to handle language specific issues)?
I have run Rhino, Jython, JRuby, and Groovy. There is the obvious language difference between them all and performance is pretty slow across the board. I've found that Groovy was the easiest to create a domain specific language (DSL) for my application. Groovy was also the easiest language to control in terms of package accessibility and runtime variables but required using Groovy API to do it instead of JSR-223.
I feel like the Groovy tooling/documenation/api meshes better with the JVM, but certainly ruby/python have quite a following and the syntax may be more comfortable for some. Ultimately I'd try them all out in your framework and choose one. Multiple scripting languages sounds nice but might be a headache to debug/support/transition.
after though: You could check into BeanShell

Scripting in Java [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
Me and some friends are writing a MORPG in Java, and we would like to use a scripting language to, eg. to create quests.
We have non experience with scripting in Java. We have used Python, but we are very inexperienced with it. One of us also have used Javascript.
What scripting language should we use?
What scripting language should we not use?
I'm responsible for a fairly large hybrid Java/Jython system. We use java for core API development, then wire Java objects together using Jython. This is in a scientific computing environment where we need to be able to put together ad-hoc data analysis scripts quickly.
If I were starting this system from scratch today, I would not choose Jython as the scripting language. I like Python fine, but I frequently encounter awkward mismatches between the Python type system and the Java type system. For example, if you just want a hashtable, should you use a Python dictionary or a Java HashMap? The decision might be different depending on whether you are just using the data structure locally in Python code or passing it across the Java boundary. Jython does a certain amount of type coercion for you, but it's not perfect. It's annoying to even have to think about issues like this when the purpose of using a scripting language in the first place is to enhance your productivity.
I assume JavaScript or JRuby would have similar issues. Today I would choose a scripting language that is specifically targeted to the JVM and leverages the Java type system. The obvious candidates are Groovy and Beanshell; Groovy seems to have been picking up momentum lately so I'd look most closely at it.
I agree with Viktor's Jython suggestion. Other than that and JavaScript (which you've mentioned, and is built into Java 6+ via the javax.script package), Groovy and JRuby are also worth looking at too.
By the way, you should look at Wyvern, also an MMORPG written in Java and using Jython for scripting. Steve Yegge, its author, has much to say about it from time to time. :-)
Java supports a variety of (scripting) languages, some are listed in Wikipedia here and here. You probably should choose language with powerful DSL and metaprogramming capabilities, such as Clojure.
But if you need something simpler, JavaScript might be a viable alternative.
How about Jython?
http://www.jython.org/Project/
what about creating your own specialized scripting language? If your app is written with java, you can use ANTLR (http://www.antlr.org/) to create your language parsing code.
The reason I say this is because a general purpose scripting language may provide too much power (because the script it to be used for quests only i assume).
But if making your own language is too hard then any of the above suggestions works - you just have to figure out how to bind the game's runtime into the script. I also suggest Lua (http://www.lua.org/) as another choice that lots of games use.
Short version
Don’t use a scripting language! Instead focus on configurability (which is something that a non-programmer can do well).
Longer version
One oft-used argument in favour of having a scripting language is that it allows for lesser programmers to more trivial tasks. Don't belive this, it will not save you any time, since trivial tasks are already accomplished by real programmers in no time. Aim for configurability instead of scripting, and you will have a much lower risk of bleeding over complex algorithms and concepts into the incapable hands of game designers. :)
Lack of hotswapping (edit-and-continue) would have been a reason to implement a scripting language in an MMOG (you don’t want to reload the whole game for a minor code change), but using Java, with built-in hotswap, you really have no reason for adding a scripting language on top.
I have spent years pondering these questions; in the day I implemented a complete scripting language, IDE, VM, debugger, etc for an MMOG myself. Since, I have grown wiser.
If you still choose to go down the infinitely crappy path of no return, keep the following in mind.
Pick a mature language which has been around for a while.
Auto testing, debugging and editing will suck bigtime until you make your own tools/plugins/start hacking around in the VM.
To date, I have never seen a DSL that improved the situation (getting a more maintainable product). Myself, I integrated Python into my indie game engine, but eventually came to my senses and ripped it out. "Stackless Python" is just a way of saying "unmaintainable but fast". Please, anyone correct me if I'm wrong?
See Java: Scripting language (macro) to embed into a Java desktop application
You have quite a few options:
Groovy - http://groovy.codehaus.org/
Jython - http://www.jython.org/Project/
JRuby - http://jruby.codehaus.org/
Possibly even BeanShell (http://www.beanshell.org/)
I'm a fan of Python myself so I'd recommend Jython, but they're probably all reasonable options.
I would have to recommend Javascript for this purpose. Mozilla Rhino http://www.mozilla.org/rhino/ is an excellent implementation that would fit your needs perfectly.
I recommend Javascript over Jython or JRuby because of familiarity. Trivial Javascript follows a very familiar syntax that anybody can use. However if someone wants to do something more intense, Javascript is a very powerful functional programming language.
I regularly use Groovy and Ruby professionally and believe that their purpose is best for writing parts of applications with particularly complex logic where Java is cumbersome to write. Javascript is a much better choice as an embedded, general scripting language to use in a game. I haven't used Python, but it's syntactically similar to Ruby and I would believe it's purpose would also be similar.
LuaJ seems to be a nice way to embed Lua into Java:
http://sourceforge.net/projects/luaj/
I am a big fan of Python/Jython due to the clean syntax - which may suit you if you have some python experience.
Otherwise Groovy which is based on Java syntax and may be an easier learning curve if most of your developers are Java guys. It also has the advantage of closer ties with the Java language and libraries.
Beanshell is good if you have simple scripting in mind - it doesn't support classes. However I don't think it has had any support over the last few years (the JSR seemed to kill it off...) so is perhaps a bad choice if support is important to you.

Categories