factgraph

Knowledge graph for modeling facts, rules, and calculations in Kit

Files

FileDescription
kit.tomlPackage manifest with metadata and dependencies
src/main.kitMain module - exports all public functions and types
tests/factgraph.test.kitTests for dictionaries, paths, and validation
examples/basic.kitPerson schema with get/set and paths
examples/fact-graph.kitTax form modeling with nested paths
LICENSEMIT license file

Dependencies

No Kit package dependencies.

Installation

kit add gitlab.com/kit-lang/packages/kit-factgraph.git

Usage

import Kit.Factgraph

License

MIT License - see LICENSE for details.

Exported Functions & Types

empty-dictionary

Create an empty fact dictionary with metadata

a -> {meta: a, types: Map, facts: Map, calcs: Map}

dict = FactGraph.empty-dictionary {name: "Tax Form", version: "1.0"}

empty

Create an empty fact graph with a dictionary

a -> {dict: a, data: Map, errors: List}

graph = FactGraph.empty dict

make-type

Create a type definition

String -> String -> TypeDef

int-type = FactGraph.make-type "PositiveInt" "Int"

add-type

Add a type definition to a dictionary

{meta: a, types: Map, facts: Map, calcs: Map} -> String -> TypeDef -> {meta: a, types: Map, facts: Map, calcs: Map}

dict = dict |> FactGraph.add-type "Money" (FactGraph.make-type "Money" "Float")

make-fact

Create a required fact definition

String -> FactDef

age-fact = FactGraph.make-fact "Int"

make-optional-fact

Create an optional fact

String -> FactDef

status-fact = FactGraph.make-optional-fact "String"

add-fact

Add a fact definition to a dictionary

{meta: a, types: Map, facts: Map, calcs: Map} -> String -> FactDef -> {meta: a, types: Map, facts: Map, calcs: Map}

dict = dict |> FactGraph.add-fact "/person/age" age-fact

add-calculation

Add a calculation (derived fact) to a dictionary

{meta: a, types: Map, facts: Map, calcs: Map} -> String -> (b -> c) -> {meta: a, types: Map, facts: Map, calcs: Map}

dict = dict |> FactGraph.add-calculation "/total" (fn(graph) => ...)

is-calculated?

Check if a path is a calculated fact

{calcs: Map, ..} -> String -> Bool

is-calc = FactGraph.is-calculated? dict "/total"

split-path

Split a path like "/person/age" into parts ["person", "age"]

String -> List String

parts = FactGraph.split-path "/person/age"
# parts = ["person", "age"]

get

Get a value from the fact graph at the given path

{data: Map, ..} -> String -> a

age = FactGraph.get graph "/person/age"

set

Set a value in the fact graph at the given path

{dict: a, data: Map, errors: List} -> String -> b -> {dict: a, data: Map, errors: List}

graph = FactGraph.set graph "/person/age" 30

get-at-path

Get a value at a nested path in a map

Map String a -> String -> a

set-at-path

Set a value at a nested path in a map

Map String a -> String -> a -> Map String a

paths

Get all paths in the fact graph data

{data: Map, ..} -> List String

all-paths = FactGraph.paths graph

dictionary

Get the dictionary from a fact graph

{dict: a, ..} -> a

dict = FactGraph.dictionary graph

data

Get the data map from a fact graph

{data: a, ..} -> a

data = FactGraph.data graph

has-errors?

Check if the fact graph has validation errors

{errors: List, ..} -> Bool

has-errors = FactGraph.has-errors? graph

get-errors

Get all validation errors from the fact graph

{errors: a, ..} -> a

errors = FactGraph.get-errors graph

to-map

Convert fact graph data to a plain map (for serialization)

{data: a, ..} -> a

map = FactGraph.to-map graph

from-map

Create a fact graph from a dictionary and a plain map

a -> Map -> {dict: a, data: Map, errors: List}

graph = FactGraph.from-map dict saved-data

is-valid-type?

Check if a value matches a type definition

String -> a -> Bool

valid = FactGraph.is-valid-type? "Int" 42

is-valid-fact?

Check if a fact value is valid against its definition in the graph

{dict: {facts: Map, ..}, ..} -> String -> a -> Bool

result = FactGraph.is-valid-fact? graph "/person/age" 30