Code Monkey home page Code Monkey logo

g4s's Introduction

FP Generic Graph Library

Highly experimental FP directed graph library built over GraphBLAS (C BLAS for graphs over sparse matrices) All resources are managed and safely released at the end.

Able to solve non cyclical graph isomorphisms (not optimized)

Examples

  1. Create a simple graph
import fix._

def graph: Resource[IO, ConcurrentDirectedGraph[IO, Vertex, Relation]] = ConcurrentDirectedGraph[IO, Vertex, Relation] 
  1. Insert 1 edge and 2 vertices
    graph.use { g =>
      for {
        src <- g.insertVertex(new A)
        dst <- g.insertVertex(new B)
        _ <- g.insertEdge(src, dst, new X)
      } yield assertEquals((src, dst), (0L, 1L))
    }
  1. insert node and get it back
    graph.use { g =>
      val av = new A
      for {
        a <- g.insertVertex(av)
        aOut <- g.getV(a)
      } yield assertEquals(aOut, Some(av))
    }
  1. single edge path matching in a diamond graph
    val query = for {
      a <- node[A]
      b <- node[B]
      _ <- edge[X](a, b)
    } yield Ret(b)

    graph.use { g =>
      for {
        a <- g.insertVertex(new A)
        b1 <- g.insertVertex(b1Node)
        b2 <- g.insertVertex(b2Node)
        c <- g.insertVertex(new C)
        _ <- g.insertEdge(a, b1, new X) // first edge matched by the query
        _ <- g.insertEdge(b1, c, new Y)
        _ <- g.insertEdge(a, b2, new X) // second edge matched by the query
        _ <- g.insertEdge(b2, c, new Y)
        results <- g.resolveTraverser(query).compile.toList
      } yield {
        assertEquals(results.toSet, Set[Vector[Vertex]](
            Vector(b1Node), 
            Vector(b2Node)))
      }
    }
  1. 2 edge patch matching in a diamond graph
    val query = for {
      a <- node[A]
      b <- node[B]
      c <- node[C]
      _ <- edge[X](a, b)
      _ <- edge[Y](b, c)
    } yield Ret(c)

    val cNode = new C

    graph.use { g =>
      for {
        a <- g.insertVertex(new A)
        b1 <- g.insertVertex(new B)
        b2 <- g.insertVertex(new B)
        // node returned at the end of the path (a)-[X]->(b)-[Y]->(c)
        c <- g.insertVertex(cNode) 
        _ <- g.insertEdge(a, b1, new X)
        _ <- g.insertEdge(b1, c, new Y)
        _ <- g.insertEdge(a, b2, new X)
        _ <- g.insertEdge(b2, c, new Y)
        results <- g.resolveTraverser(query).compile.toList
      } yield {
        assertEquals(results.toSet, Set[Vector[Vertex]](Vector(cNode)))
      }
    }
  1. find the top path in a diamond graph
    val queryTop = for {
      a <- node[A]
      b <- node[B]
      c <- node[C]
      _ <- edge[X](a, b)
      _ <- edge[Y](b, c)
    } yield Ret(a, b, c)

    val aNode = new A
    val b1Node = new B
    val b2Node = new B
    val cNode = new C

    graph.use { g =>
      for {
        a <- g.insertVertex(aNode)
        b1 <- g.insertVertex(b1Node)
        b2 <- g.insertVertex(b2Node)
        c <- g.insertVertex(cNode)
        _ <- g.insertEdge(a, b1, new X) // first part (a)-[X]->(b)
        _ <- g.insertEdge(b1, c, new Y) // second part (b)-[Y]->(c)
        _ <- g.insertEdge(a, b2, new Z)
        _ <- g.insertEdge(b2, c, new W)
        results <- g.resolveTraverser(query).compile.toList
      } yield {
        assertEquals(results.toSet,
                     Set[Vector[Vertex]](
                       Vector(aNode, b1Node, cNode)
                     ))
      }
    }

g4s's People

Contributors

fabianmurariu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

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.