Arrays. Using Gremlin through Groovy - GitHub. <dependency> <groupId> com.tinkerpop.gremlin </groupId> <artifactId> gremlin-groovy </artifactId> <version> ?? </version> </dependency> Gremlin Groovy works by using the metaprogramming facilities provided by Groovy . Groovy is used to compile Gremlin syntax down to raw Java Pipes . Groovy Classes with Gremlin If you are using Groovy classes, its trivial to access Gremlin. In this way, your Java code and your Gremlin/Groovy code seamless interact through standard class mechanisms (i.e. method invocations).
There is a Groovy class called Gremlin.groovy . Class SimpleExample { static { Gremlin . load () } public List exampleMethod () { Graph g = TinkerGraphFactory . createTinkerGraph () def results = [] g . v ( 1 ). out ( ' knows ' ). fill ( results ) return results } } Java Classes talking to Groovy Classes with Gremlin Here is a typical used pattern when mixing Gremlin/Groovy and Java classes. Finally, if you build with Maven2 , here are some useful snippets that you can add to your pom.xml. GroovyCollections (Groovy 1.7.8) Shed the weight with Groovlets | The Disco Blog. Groovlets as Lightweight Servlets. Groovlets are Groovy scripts that are executed by a servlet.
With Groovlets a user can request a Groovy script that is executed on the server and the results are displayed in a web browser. We only have to define a servlet in the web.xml of a Java web application, place the Groovy libraries in the web application's lib folder and we can execute Groovy scripts. The Groovy script, or we can call it a Groovlet, has a couple of implicit variables we can use. For example reqeust, response to access the HttpServletRequest and HttpServletResponse objects. Let's create a Groovy script serverinfo.groovy: 00.def method = request.method 02.if (!
03. session = request.getSession(true) 06.if (! 07. session.groovlet = 'Groovlets rock! ' 10.html.html { 11. head { 12. title 'Groovlet info' 14. body { 15. h1 'General info' 16. ul { 17. li "Method: ${method}" 18. li "RequestURI: ${request.requestURI}" 19. li "session.groovlet: ${session.groovlet}" 20. li "application.version: ${context.version}" 23. h1 'Headers' 24. ul { Working with Dates. Thanks to Groovy's extensions to the JDK Date classes we can work with dates more easily.
For example we can now add and subtract days with the plus() and minus() methods. And because this methods are mapped to the operators + and - we can write dense code. Also the next() and previous() methods are implemented so we can use the ++ and -- operators to get to a next or previous day. We can even use the subscript operator ([]) to get date fields, because of the getAt() method Groovy adds to the Date class.
The Date class also contains parse() and format() methods to convert string to a date and to format a date into a string. 01.date = new Date().parse('yyyy/MM/dd', '1973/07/09') 04.assert 1973 == date[Calendar.YEAR] 05.assert 6 == date[Calendar.MONTH] 06.assert 9 == date.getAt(Calendar.DATE) 08.dateNext = date.clone() 09.datePrevious = date.clone() 13.nextDay = date + 1 14.previousDay = date - 1 17.dateNext++ 18.assert dateNext == nextDay 21.datePrevious-- 22.assert datePrevious == previousDay. Implementing Interfaces with closures in Groovy - what method was called.
Groovy programming: an introduction ... Groovy - Should I define interfaces in Duck Typed languages. Practically Groovy: Metaprogramming with closures, ExpandoMetaClass, and categories. You've heard people say for years that Groovy is a dynamic programming language for the JVM. But what does that really mean? In this Practically Groovy installment, you'll learn about metaprogramming — Groovy's ability to add new methods to classes dynamically at run time. This flexibility goes far beyond what the standard Java language can offer.
Through a series of code examples (all available for download) you'll see that metaprogramming is one of Groovy's most powerful and practical features. Modeling the world Our job as programmers is to model the real world in software. Listing 1. Class ScalyOrFeatheryAnimal{ ScalyOrFeatheryAnimal layEgg(){ return new ScalyOrFeatheryAnimal() } } class FurryAnimal{ FurryAnimal giveBirth(){ return new FurryAnimal() } } Unfortunately, the real world is rife with exceptions and edge cases — duck-billed platypuses are furry and lay eggs. Listing 2. Platypus.metaClass.layEgg = {-> return new FurryAnimal() } def baby = new Platypus().layEgg() Back to top. Putting Proxy-o-Matic to work. It all started a couple of weeks ago when Alex Tkachman submitted the following code to the Groovy dev list 01. as (MouseListener) { 02. mouseClicked{ 03. println "Clicked: $it" 06. mousePressed { 07. println "Pressed: $it" This little piece of code should create an instance of (an unknown class) that implements the MouseListener interface, in other words, it creates a proxy of MouseListener.
You must use the keyword when coercing a Closure, the target interface should define one method only when coercing a Map, the target may be an interface, an abstract or concrete class Those rules allow you to create a proxy easily and though maps let you proxy 3 different targets they come with two drawbacks maps must hold a unique key per method name, you can't proxy overloaded method definitions you can't call a proxied method within another proxied method Fortunately Groovy includes a very handy alternative to dynamic beans: Expandos. 1. def bean = new Expando() 2. bean.foo = {-> "foo" } 4. 7.
GetProperty. Reflection - Dynamically implement interface in Groovy using invokeMethod. Groovy - Evgeny Goldin. From Evgeny Goldin What's New in Groovy 1.6 def (a, b) = [a, b]def (int a, String b) = [3, "Opa"] def a, b (a, b) = {[ 'aaa', 'bbb' ]}() (a, b) = [b, a] for (i in 0..9) println i for (int i = 0; i < 10; i++) println i "Practically Groovy: Smooth operators" def builder = new groovy.xml.MarkupBuilder() def bobsFriend = { person { name "fred" age 40 }} builder.person { name "bob" age 30 friend bobsFriend } Groovy JDK Dynamic Property access / Method Invocation /** * Dynamic property access */ obj.
" MOP Classes groovy.lang.GroovyObject groovy.lang.MetaObjectProtocol groovy.lang.MetaClass groovy.runtime.HandleMetaClass groovy.lang.MetaClassImpl : Allows methods to be dynamically added to existing classes at runtime groovy.lang.ExpandoMetaClass : Allows to dynamically add or replace methods, constructors, and properties using a closure syntax. MetaProgramming Hooks Inspecting MetaClass groovy.util.Expando groovy.lang.ExpandoMetaClass Intercept + Cache + Invoke Runtime mixins @Immutable @Delegate @Singleton. 2010 May | Groovy Examples.
Open Source Web Frameworks in Groovy.