Giter Site home page Giter Site logo

blueprints-scala's Introduction

Tinkerpop Blueprints Scala Build Status

Scala wrapper for tinkerpop blueprints, this library provide more scalastic code when working with graph database supported by blueprints.

Usage

For installation see install section.

For Scala 2.9 see https://github.com/anvie/blueprints-scala/tree/scala-2.9

More working and complete examples can be found in specs test.

Creating new vertex:

case class Person(name:String, kind:String)

val hercules = Person("hercules", "demigod")

db.save(hercules)

If your class subclassing from DbObject you will have more sweet code like using save() directly:

case class Person(name:String, kind:String) extends DbObject

val hercules = Person("hercules", "demigod").save()

The save() method return a Vertex object, if you want to get class back just:

val herculesObject = hercules.toCC[Person].get

Creating edges:

hercules --> "father" --> jupiter
hercules --> "mother" --> alcmene

Or you can also add multiple edges in one line:

hercules --> "father" --> jupiter --> "father" --> saturn

Creating mutual (both) edges:

jupiter <--> "brother" <--> neptune

Or more complex mutual edges:

jupiter <--> "brother" <--> neptune <--> "brother" <--> pluto

Shorthand property getter and setter:

jupiter.set("kind", "god")

val kind = jupiter.get[String]("kind").get

Getter get return Option instance, so you can handle unexpected return data efficiently using map:

jupiter.get[String]("kind") map { kind =>
	// do with kind here
}

Or getting value by using default value if empty:

jupiter.getOrElse[String]("status", "live")

Easy getting mutual connection, for example jupiter and pluto is brother both has IN and OUT edges, is very easy to get mutual connection using mutual method:

val jupitersBrothers = jupiter.mutual("brother")

Inspect helpers to print vertex list:

jupitersBrothers.printDump("jupiter brothers:", "name")

// will produce output:

jupiter brothers:
 + neptune
 + pluto

Using Gremlin Pipeline like a boss:

val battled = hercules.pipe.out("battled")
battled.printDump("hercules battled:", "name")

// output:

hercules battled:
 + nemean
 + hydra
 + cerberus

Syntactic sugar filtering on Gremlin Pipeline:

// get monster battled with hercules more than 5 times
val monsters = hercules.pipe.outE("battled").wrap.filter { edge =>
   edge.getOrElse[Int]("time", 0).toString.toInt > 5
}

Returning edges from chain by adding < on the last line:

var edge = hercules --> "battled" --> hydra <

edge.set("time", 2)

Using transaction:

transact {
	hercules --> "father" --> jupiter
	hercules --> "mother" --> alcmene
	val edge = hercules --> "battled" --> hydra <

	edge.set("time", 15)
}

Want more attributes that not fit for case class constructor parameter?

Use @Persistent annotation:

case class City(name:String){

    @Persistent var province = "Jakarta" // this attribute will be saved
    @Persistent var country = "Indonesia" // this attribute will be saved

    val code = 123 // this not

}

Support inheritance as well:

abstract class City(name:String) {
    @Persistent var province = ""
}

trait Street {
    @Persistent var street = ""
}

trait Code {
    @Persistent var postalCode = 0
}


case class Jakarta extends City("Jakarta") with Street with Code with DbObject

// usage

val address = Jakarta()
address.province = "Jakarta Barat"  // this will be saved
address.street = "KS. Tubun"        // this will be saved
address.postalCode = 12345          // this will be saved
address.save()

// getting back

db.getVertex(address.getVertex.getId).toCC[Jakarta] map { adr =>
    println(adr.province) // will printed `Jakarta Barat`
    println(adr.postalCode) // will printed `12345`
}

// instantiate to parent class
// this is valid

db.getVertex(address.getVertex.getId).toCC[City] map { adr =>
    println(adr.province) // will printed `Jakarta Barat`
}

// this also valid

db.getVertex(address.getVertex.getId).toCC[Street] map { adr =>
    println(adr.street) // will printed `KS. Tubun`
}

How it work? All you needs is to adding dependency (see install), import it, and summon Graph db instance with implicit:

import com.ansvia.graph.BlueprintsWrapper._

// you can use any graph database that support tinkerpop blueprints
// here is i'm using simple in-memory Tinkergraph db for example.
implicit val db = TinkerGraphFactory.createTinkerGraph()

//... work here ....

Done, now you can using Scalastic sweet syntactic sugar code.

Install

Add dependency to your build.sbt:

"com.ansvia.graph" %% "blueprints-scala" % "0.1.17"

If you get an error when you compile, similar to sbt.ResolveException: unresolved dependency: com.ansvia.graph#blueprints-scala_2.11;0.1.6: not found then your Scala version differs from that of the latest version, this will fix the issue:

"com.ansvia.graph" % "blueprints-scala_2.10" % "0.1.17"

License

Apache 2

[] Robin Sy.

Donation

Support me via Gittip

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.