diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ac67aaf --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +/_build +/cover +/deps +erl_crash.dump +*.ez +*.beam diff --git a/README.md b/README.md new file mode 100644 index 0000000..de50586 --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +# Exstreme + +**TODO: Add description** + +## Installation + +If [available in Hex](https://hex.pm/docs/publish), the package can be installed as: + + 1. Add exstreme to your list of dependencies in `mix.exs`: + + def deps do + [{:exstreme, "~> 0.0.1"}] + end + + 2. Ensure exstreme is started before your application: + + def application do + [applications: [:exstreme]] + end + diff --git a/config/config.exs b/config/config.exs new file mode 100644 index 0000000..77e40c1 --- /dev/null +++ b/config/config.exs @@ -0,0 +1,30 @@ +# This file is responsible for configuring your application +# and its dependencies with the aid of the Mix.Config module. +use Mix.Config + +# This configuration is loaded before any dependency and is restricted +# to this project. If another project depends on this project, this +# file won't be loaded nor affect the parent project. For this reason, +# if you want to provide default values for your application for +# 3rd-party users, it should be done in your "mix.exs" file. + +# You can configure for your application as: +# +# config :exstreme, key: :value +# +# And access this configuration in your application as: +# +# Application.get_env(:exstreme, :key) +# +# Or configure a 3rd-party app: +# +# config :logger, level: :info +# + +# It is also possible to import configuration files, relative to this +# directory. For example, you can emulate configuration per environment +# by uncommenting the line below and defining dev.exs, test.exs and such. +# Configuration from the imported file will override the ones defined +# here (which is why it is important to import them last). +# +# import_config "#{Mix.env}.exs" diff --git a/lib/exstreme.ex b/lib/exstreme.ex new file mode 100644 index 0000000..8b39fc4 --- /dev/null +++ b/lib/exstreme.ex @@ -0,0 +1,2 @@ +defmodule Exstreme do +end diff --git a/lib/exstreme/stream_graph.ex b/lib/exstreme/stream_graph.ex new file mode 100644 index 0000000..b35b158 --- /dev/null +++ b/lib/exstreme/stream_graph.ex @@ -0,0 +1,96 @@ +defmodule Exstreme.StreamGraph do + @moduledoc """ + """ + defmodule Graph do + defstruct params: [], nodes: %{}, connections: [] + end + + @doc """ + """ + def create_graph(params \\ []), do: %Graph{params: params} + + @doc """ + """ + def create_node(graph = %Graph{nodes: nodes}, params \\ []) do + key = next_node_key(nodes) + new_graph = update_in(graph.nodes, &(Map.put(&1, key, params))) + {new_graph, key} + end + + @doc """ + """ + def add_connection(graph = %Graph{nodes: nodes}, start, finish) when start != finish do + if Map.has_key?(nodes, start) and Map.has_key?(nodes, finish) do + update_in(graph.connections, store_connection_fn(start, finish)) + else + raise ArgumentError, message: "nodes not found" + end + end + + #private + + defp store_connection_fn(start, finish) do + fn(connections) -> + connections + |> validate_repeated(start, finish) + |> validate_repeated(finish, start) + |> validate_position(start, :start) + |> validate_position(finish, :end) + |> Keyword.put(start, finish) + end + end + + def validate_repeated(connections, start, finish) do + if connections |> Enum.any?(&(&1 == {start, finish})) do + raise ArgumentError, message: "there is already a connection like that" + else + connections + end + end + + defp validate_position(connections, node, position) do + case node|> Atom.to_string |> String.first do + "n" -> validate_position_node(connections, node, position) + # "b" -> + # "f" -> + _ -> raise ArgumentError, message: "invalid node" + end + end + + defp validate_position_node(connections, node, :start) do + exist = + connections + |> Keyword.has_key?(node) + if exist do + raise ArgumentError, message: "the node can't be twice at start position" + else + connections + end + end + + defp validate_position_node(connections, node, :end) do + exist = + connections + |> Keyword.values + |> Enum.member?(node) + if exist do + raise ArgumentError, message: "the node can't be twice at end position" + else + connections + end + end + + defp next_node_key(nodes) do + next_key(nodes, "n") + end + + defp next_key(map, letter) do + count = + map + |> Map.keys + |> Enum.map(&Atom.to_string/1) + |> Enum.filter(fn(str) -> String.starts_with?(str, letter) end) + |> Enum.count + String.to_atom("#{letter}#{count + 1}") + end +end diff --git a/mix.exs b/mix.exs new file mode 100644 index 0000000..53da30e --- /dev/null +++ b/mix.exs @@ -0,0 +1,32 @@ +defmodule Exstreme.Mixfile do + use Mix.Project + + def project do + [app: :exstreme, + version: "0.0.1", + elixir: "~> 1.2", + build_embedded: Mix.env == :prod, + start_permanent: Mix.env == :prod, + deps: deps] + end + + # Configuration for the OTP application + # + # Type "mix help compile.app" for more information + def application do + [applications: [:logger]] + end + + # Dependencies can be Hex packages: + # + # {:mydep, "~> 0.3.0"} + # + # Or git/path repositories: + # + # {:mydep, git: "https://github.com/elixir-lang/mydep.git", tag: "0.1.0"} + # + # Type "mix help deps" for more examples and options + defp deps do + [] + end +end diff --git a/test/exstreme/stream_graph_test.exs b/test/exstreme/stream_graph_test.exs new file mode 100644 index 0000000..369f3cb --- /dev/null +++ b/test/exstreme/stream_graph_test.exs @@ -0,0 +1,59 @@ +defmodule Exstreme.StreamGraphTest do + use ExUnit.Case + alias Exstreme.StreamGraph + alias Exstreme.StreamGraph.Graph + doctest Exstreme.StreamGraph + + def params, do: [] + + def create_graph do + graph = StreamGraph.create_graph(params) + + {graph, n1} = graph |> StreamGraph.create_node(params) + {graph, n2} = graph |> StreamGraph.create_node(params) + + graph + |> StreamGraph.add_connection(n1, n2) + end + + test "creates a valid graph struct" do + graph = %Graph{ + nodes: %{n1: [], n2: []}, + connections: [{:n1, :n2}] + } + assert create_graph == graph + end + + test "throws an error when adding again the relation" do + assert_raise ArgumentError, fn -> + create_graph + |> StreamGraph.add_connection(:n1, :n2) + end + end + + test "throws an error when adding again a self relation" do + assert_raise FunctionClauseError, fn -> + create_graph + |> StreamGraph.add_connection(:n1, :n1) + end + end + + test "throws an error when adding a cicle relation" do + assert_raise ArgumentError, "there is already a connection like that", fn -> + create_graph + |> StreamGraph.add_connection(:n2, :n1) + end + end + + test "can create n3 and add a relatio between n2 and n3" do + graph = %Graph{ + nodes: %{n1: [], n2: []}, + connections: [{:n1, :n2}] + } + + {new_graph, _} = create_graph |> StreamGraph.create_node(params) + new_graph = + new_graph + |> StreamGraph.add_connection(:n2, :n3) + end +end diff --git a/test/exstreme_test.exs b/test/exstreme_test.exs new file mode 100644 index 0000000..6115594 --- /dev/null +++ b/test/exstreme_test.exs @@ -0,0 +1,8 @@ +defmodule ExstremeTest do + use ExUnit.Case + doctest Exstreme + + test "the truth" do + assert 1 + 1 == 2 + end +end diff --git a/test/test_helper.exs b/test/test_helper.exs new file mode 100644 index 0000000..869559e --- /dev/null +++ b/test/test_helper.exs @@ -0,0 +1 @@ +ExUnit.start()