Graphviz subgraphs looking like record shaped nodes - gstreamer

I'm trying to generate graph for GStreamer applications where are many nested bins. Each bin have some elements but bin for parent bin looks like any other common element that have pads (sinks and sources). I want to visualize it but in Graphviz I can't generate nested nodes so I have to do it by subgraphs. Unfortunately subgraphs don't have node options like record shape. In my graph each node have record shape but subgraphs can't. Example:
digraph G
{
graph [rankdir = TD]
node [shape = record]
subgraph cluster_player
{
label = "player"
subgraph cluster_bin1
{
label = "bin1"
bin1_sink1 [label = "sink1"]
bin1_sink2 [label = "sink2"]
obj1 [
label = "{ { <sink1> sink1 | <sink2> sink2 } | obj1 | { <src1> src1 | <src2> src2 } }"
]
bin1_src1 [label = "src1"]
bin1_src2 [label = "src2"]
bin1_sink1:s -> obj1:sink1:n []
bin1_sink2:s -> obj1:sink2:n []
obj1:src1:s -> bin1_src1:n []
obj1:src2:s -> bin1_src2:n []
}
bin1_src1:s -> bin2_sink1:n []
bin1_src2:s -> bin2_sink2:n []
subgraph cluster_bin2
{
label = "bin2"
bin2_sink1 [label = "sink1"]
bin2_sink2 [label = "sink2"]
bin2_obj1 [
label = "{ { <sink1> sink1 | <sink2> sink2 } | obj1 | { <src1> src1 } }"
]
bin2_obj2 [
label = "{ { <sink1> sink1 } | obj2 | { <src1> src1 } }"
]
bin2_src1 [label = "src1"]
bin2_sink1:s -> bin2_obj1:sink1:n []
bin2_sink2:s -> bin2_obj1:sink2:n []
bin2_obj1:src1:s -> bin2_obj2:sink1:n []
bin2_obj2:src1:s -> bin2_src1:n []
}
}
}
As You can see now sources and sinks in bins are like normal elements, but i want them to look like records in "obj1". How to do it? If it's impossible maybe there are others languages that will have that function?

Graphviz subgraphs are containers. They do not support the record notation or the shape attributes you are looking for. shape is restricted to nodes, and subgraphs are not.

Related

Static casting between types for Generic nested records

Nested F# Record with generic type parameter, how do I statically cast between types in nested structure equivalent to traversing and performing 'T |> 'K, e.g. float |> int?
Currently I am Naively traversing the nested records and explicitly converting the type with from:float |> to:int or equivalently int(from). However, this is not very beautiful.
type Person<'T> = {Id : int; Value : 'T}
type Family<'T> = {Id : 'T; People : seq<Person<'T>>}
let fam1 = {Id = 1.0; People = [{Id = 1.1; Value = 2.9}; {Id = 1.2; Value = 4.4}]} : Family<float>
let fam2 = {Id = 2.0; People = [{Id = 2.1; Value = 3.9}; {Id = 2.2; Value = 5.4}]} : Family<float>
let partyFloat = seq{ yield fam1; yield fam2}
// In general, how to do this from a type T to a type K where conversion using T |> K will work
let partyInt : seq<Family<int>> = partyFloat
How to statically and/or
lazily convert to seq<Family<int>>?
In my real world case I have a DiffSharp D type that can be converted to a float with D |> float or float(D).
There is no magic way to cast the insides of types, you have to write your own.
It is idiomatic for F# and functional programming in general (and I personally recommend it, too) to write small functions for simple data transformations, and then assemble them together:
let mapPerson f p = { Id = p.Id; Value = f p.Value }
let mapFamily f fm = { Id = f fm.Id; People = Seq.map (mapPerson f) fm.People }
let mapParty f = Seq.map (mapFamily f)
let partyInt = mapParty int partyFloat
But of course you can do it in one big messy go:
let partyInt =
partyFloat
|> Seq.map (fun fm ->
{ Id = int fm.Id
People =
fm.People
|> Seq.map (fun p ->
{ Id = p.Id; Value = int p.Value }
)
}
)
It seems like what you are asking for are covariance ie that this should compile
let vs : obj list = ["1"; "2"]
F# doesn't support covariance (or contravariance) and probably never will. C# does however so you could write something like this
using System.Collections.Generic;
interface IPerson<out T>
{
int Id { get; }
T Value { get; }
}
interface IFamily<out T>
{
int Id { get; }
IEnumerable<IPerson<T>> Members { get; }
}
static class Program
{
static IFamily<string> CreateFamily()
{
return null;
}
static void Main(string[] args)
{
IFamily<string> familyOfString = CreateFamily();
IFamily<object> familyOfObject = familyOfString;
}
}
However, there's a functional pattern that could help us called polymorphic lenses.
(Picture from reddit thread: https://www.reddit.com/r/haskell/comments/2qjnho/learning_curves_for_different_programming/)
I used to think that polymorphic lenses isn't possible in F# due to the lack of higher-rank types. However, there's a hidden gem out there: http://www.fssnip.net/7Pk
Vesa Karvonen (IIRC he is also behind hopac so he's pretty cool) implements polymorphic lenses in F# using some pretty interesting tricks.
We can then map the inner values of an immutable structure reasonably easy.
let input : Family<int> =
{
Id = 1
Members = [{ Id = 10; Value = 123}; { Id = 11; Value = 456}]
}
printfn "%A" input
let output : Family<string> =
input
|> over Family.membersL (overAll Person.valueL ((+) 1 >> string))
printfn "%A" output
Full source code
// ----------------------------------------------------------------------------
// The code below taken from: http://www.fssnip.net/7Pk
// by Vesa+Karvonen - http://www.fssnip.net/authors/Vesa+Karvonen
// ----------------------------------------------------------------------------
type LensFunctor<'a> =
| Over of 'a
| View
member t.map a2b =
match t with
| Over a -> Over (a2b a)
| View -> View
type Lens<'s,'t,'a,'b> = ('a -> LensFunctor<'b>) -> 's -> LensFunctor<'t>
module Lens =
let view l s =
let r = ref Unchecked.defaultof<_>
s |> l (fun a -> r := a; View) |> ignore
!r
let over l f =
l (f >> Over) >> function Over t -> t | _ -> failwith "Impossible"
let set l b = over l <| fun _ -> b
let (>->) a b = a << b
let lens get set = fun f s ->
(get s |> f : LensFunctor<_>).map (fun f -> set f s)
let fstL f = lens fst (fun x (_, y) -> (x, y)) f
let sndL f = lens snd (fun y (x, _) -> (x, y)) f
// ----------------------------------------------------------------------------
// The code above taken from: http://www.fssnip.net/7Pk
// by Vesa+Karvonen - http://www.fssnip.net/authors/Vesa+Karvonen
// ----------------------------------------------------------------------------
let overAll l f = List.map (over l f)
open Lens
type Person<'T> = { Id : int; Value : 'T }
module Person =
let idS i p = { p with Id = i }
let valueS v { Id = i } = { Id = i; Value = v }
let idL f = lens (fun {Id = i } -> i) idS f
let valueL f = lens (fun {Value = v } -> v) valueS f
type Family<'T> = { Id : int; Members : Person<'T> list }
module Family =
let idS i f = { f with Id = i }
let membersS m { Id = i } = { Id = i; Members = m }
let idL f = lens (fun {Id = i } -> i) idS f
let membersL f = lens (fun {Members = m } -> m) membersS f
[<EntryPoint>]
let main argv =
let input =
{
Id = 1
Members = [{ Id = 10; Value = 123}; { Id = 11; Value = 456}]
}
printfn "%A" input
let output =
input
|> over Family.membersL (overAll Person.valueL ((+) 1 >> string))
printfn "%A" output
0

Access the record fields in Ocaml

The problem is , I can't write n.key >= n.left.key && n.key < n.right.key in the compare_children function;
I would like to write it like in OOP node.left.right.left...
I would really like to read more about lenses but I haven't found any material on the web.
type avl_tree =
Node of avl_tree_node
| Leaf
and avl_tree_node = {
key : int;
balance : int;
left : avl_tree;
right : avl_tree;
}
type subtree_lens = {
get : avl_tree_node -> avl_tree;
set : avl_tree_node -> avl_tree -> avl_tree_node
}
let lens_right = {
get = (fun node -> node.right);
set = fun node t -> {node with right = t}
}
let lens_left = {
get = (fun node -> node.left);
set = fun node t -> {node with left = t}
}
let compare_children nd =
match nd with
| Leaf -> true
| Node n -> n.key >= n.left.key && n.key < n.right.key
One way to look at this is that you can't write n.left.key because n.left might be a Leaf.
If you want to keep your type definition, you have to handle Leaf and Node as separate cases:
let compare_children nd =
match nd with
| Leaf -> true
| Node { left = Leaf } -> (* Leaf case, fill in... *) false
| Node { right = Leaf } -> (* Other leaf case, fill in... *) false
| Node { left = Node ln; right = Node rn; key = k } ->
k >= ln.key && k < rn.key
Update
An OCaml expression can look like this: { x with f = v }. The type of the expression x must be a record type that includes a field named f. The expression evaluates to a record whose fields are the same as the fields of x, except that the f field has the value v. In fact, you can have any number of fields after with.
To access fields of nd you can make the patterns more explicit:
let compare_children nd =
match nd with
| Leaf -> true
| Node { left = Leaf; right = Leaf } -> true
| Node { left = Leaf; right = Node rn; key = k } -> k < rn.key
| Node { left = Node ln; right = Leaf; key = k } -> k >= ln.key
| Node { left = Node ln; right = Node rn; key = k } ->
k >= ln.key && k < rn.key
Note that I'm just guessing at what the function is supposed to return. It's just an example of what patterns you might want to use.

How to update JSON object using Yojson?

After reading some examples, it's easy to construct a JSON object by Yojson.Basic.from_string or from_channel.
On the other side, we could also easily to convert a JSON object to string by pretty_to_string.
However, update an JSON object is tricky, for example the input parameter is as follows:
{
"content": "test content",
"base" : {
"version": 1,
"id" : "a001"
}
}
I want to update the "id" in it and return a new JSON object:
{
"content": "test content",
"base" : {
"version": 1,
"id" : "a002"
}
}
I tried to write a function to update a JSON object:
let update_json (js: json) (key: string) (new_value: string) : json =
let rec aux (json_ele:json):json =
match json_ele with
| `Bool b -> `Bool b
| `Float f -> `Float f
| `Int i -> `Int i
| `List jl -> `List (List.map jl ~f:aux)
| `Null -> `Null
| `String st -> `String st
| `Assoc kvlist ->
`Assoc (
List.map
kvlist
~f:(fun (okey, ovalue) ->
match ovalue with
| `String v when okey = key -> (okey, `String new_value)
| _ -> (okey, aux ovalue)))
in
aux js
I was wondering if writing a function as above is the best way to update a JSON object in Yojson?
I'm not clear on what you mean by "bugs", but `Assoc doesn't take a string * json, it takes a (string * json) list. This means that in your `Assoc cases, you should do a List.map:
| `Assoc pairs ->
List.map pairs ~f:(fun (okey, ovalue) ->
if okey = key then
key, new_value
else
okey, aux ovalue)
You may want to factor this function out and give it a name.

Filtering OCaml list to one variant

So I have a list of stmt (algebraic type) that contain a number of VarDecl within the list.
I'd like to reduce the list from stmt list to VarDecl list.
When I use List.filter I can eliminate all other types but I'm still left with a stmt list.
I found that I was able to do the filtering as well as the type change by folding, but I can't figure out how to generalize it (I need this pattern many places in the project).
let decls = List.fold_left
(fun lst st -> match st with
| VarDecl(vd) -> vd :: lst
| _ -> lst
) [] stmts in
Is there a better way to perform a filter and cast to a variant of the list type?
Assuming you have a type like
type stmt = VarDecl of int | Foo of int | Bar | Fie of string
and a stmt list, Batteries lets you do
let vardecl_ints l =
List.filter_map (function Vardecl i -> Some i | _ -> None) l
let foo_ints l =
List.filter_map (function Foo i -> Some i | _ -> None) l
which I think is about as concise as you're going to get. I don't
think you can make general "list-getters" for ADT's, because e.g.
let bars l =
List.filter_map (function Bar -> Some Bar | _ -> None) l
https://github.com/ocaml-batteries-team/batteries-included/blob/d471e24/src/batList.mlv#L544
has the Batteries implementation of filter_map, if you don't want the
dependency. A functional version with [] instead of dst would be quite similar, only doing
(x::dst) and a |>List.rev at the end.
You could use GADTs or polymorphic variants, but both tend to drive up complexity.
Here's a rough sketch of how you might approach this problem with polymorphic variants:
type constant = [ `Int of int | `String of string ]
type var = [ `Var of string ]
type term = [ constant | var | `Add of term * term ]
let rec select_vars (list : term list) : var list =
match list with
| [] -> []
| (#var as v)::list -> v::select_vars list
| _::list -> select_vars list
let rec select_constants (list : term list) : constant list =
match list with
| [] -> []
| (#constant as k)::list -> k::select_constants list
| _::list -> select_constants list
Another possibility is to pull the bits of a var out into an explicit type of which you can have a list:
type var = {
...
}
type term =
| Int of int
| Var of var
This has some overhead over having the bits just be constructor args, and a var is not a term, so you will likely need to do some wrapping and unwrapping.
It's hard to answer without seeing your type definition (or a simplified version of it).
Note, though, that if you have this definition:
type xyz = X | Y | Z
The values X, Y, and Z aren't types. They're values. Possibly Vardecl is a value also. So you can't have a list of that type (in OCaml).
Update
One thing I have done for cases like this is to use the type projected from the one variant you want:
type xyz = X | Y of int * int | Z
let extract_proj v l =
match v with
| X | Z -> l
| Y (a, b) -> (a, b) :: l
let filter_to_y l =
List.fold_right extract_proj l []
Here's a toplevel session:
type xyz = X | Y of int * int | Z
val extract_proj : xyz -> (int * int) list -> (int * int) list = <fun>
val filter_to_y : xyz list -> (int * int) list = <fun>
# filter_to_y [X; Z; Y(3,4); Z; Y(4,5)];;
- : (int * int) list = [(3, 4); (4, 5)]

automata in OCaml

I am a bit new to OCaml. I want to implement product construction algorithm for automata in OCaml. I am confused how to represent automata in OCaml. Can someone help me?
A clean representation for a finite deterministic automaton would be:
type ('state,'letter) automaton = {
initial : 'state ;
final : 'state -> bool ;
transition : 'letter -> 'state -> 'state ;
}
For instance, an automaton which determines whether a word contains an odd number of 'a' could be represented as such:
let odd = {
initial = `even ;
final = (function `odd -> true | _ -> false) ;
transition = (function
| 'a' -> (function `even -> `odd | `odd -> `even)
| _ -> (fun state -> state))
}
Another example is an automation which accepts onlythe string "bbb" (yes, these are taken from this online handout) :
let bbb = {
initial = `b0 ;
final = (function `b3 -> true | _ -> false) ;
transition = (function
| 'b' -> (function `b0 -> `b1 | `b1 -> `b2 | `b2 -> `b3 | _ -> `fail)
| _ -> (fun _ -> `fail))
}
Automaton product is described mathematically as using the cartesian product of the state sets as the new sets, and the natural extensions of the final and transition functions over that set:
let product a b = {
initial = (a.initial, b.initial) ;
final = (fun (x,y) -> a.final x && b.final y) ;
transition = (fun c (x,y) -> (a.transition c x, b.transition c y)
}
This product automaton computes the intersection of two languages. You can also use || in lieu of && to implement the union of two languages.