Giter VIP home page Giter VIP logo

the-sequence-is-key's Introduction

The Sequence Is Key

A DSL for a limited subset of Linear Temporal Logic

Ideally you have idempotent data, and will never need this library. But hey, the real world is messy.

Whom this library is for

You have a sequence of data entering from somewhere. But there is something special about this data; the ordering of content matters in your application, and there is a certain ordering that either must be preserved, or can never happen.

In other words, you can say stuff like: "if there is a :header, then there MUST be a :trailer before we can see a :header again." Like this:

(require '[sequence.engine.machine :as m])
(m/defrules my-rules 
  {:header  {::m/not-eventually :header}
   :trailer {::m/relax [:header]}}

This can be extended by saying ":trailer must follow a :header, and immediately after :trailer we must see a :header":

(m/defrules my-rules 
  {:header  {::m/not-eventually :header}
   :trailer {::m/is-after :header
             ::m/relax [:header]
             ::m/next :header}})

This is hard to describe in any programming language, even in math. I've been inspired mostly by Linear Temporal Logic. For integrated circuits there is the property specification language, But I don't find very much about it online.

The basic structure of the data input must be a sequence (coll) of data that can be tagged by a tag function. The tag function is the information that the rule machine works on.

The user must also write the rules that are to be checked. The rules must be a hashmap that provides a hasmap of new rules to apply when the tag is hit

The rule machine does the job of transforming the user-provided rules and the tagged sequence, into a reduction which keeps track of what the legal states are for each successive position in the sequence.

What this library is not for

You will not use this library for type-checking you data. That is more easily done with clojure/spec.alpha, prismatic/schema, or com.taoensso/truss.

Usage

;; valid rules are: #{::not-eventually ::is-after ::relax ::next ::free}
(require '[sequence.engine.machine :as m])
(m/defrules demorules-t
  {:header {::m/not-eventually :header} ; :header cannot follow :header
   :beta {::m/is-after :header} ; :header must have arrived before :beta
   :trailer {::m/is-after :header 
             ::m/relax [:header] ; :header rule relaxed
             ::m/next :header}}) ; :header must follow immediately after :trailer

(def my-data
[:header :beta :trailer :header])

(m/validate {:rules demorules-t
             :tag-fn identity
             :user-sequence my-data})
;=> {:ok true}
(m/validate {:rules demorules-t
             :tag-fn identity
             :user-sequence [:header :beta :header]})
;; => #:sequence.engine.machine{:problem
;;                              [{:rule [:sequence.engine.machine/not-eventually :header],
;;                                :broken-by :header,
;;                                :sequence.engine.machine/position 2}]}

(m/is-ok? (reduce (m/rule-parser demorules-t)
        m/rule-parsing-default
        my-data))
;=> true
(m/is-ok? (reduce (m/rule-parser demorules-t)
        m/rule-parsing-default
        [:header :beta :header]))
;=> false

Features

  • Automatic rule checking: defrules runs a small set of checks to see if rules are feasible

  • Rules:

    • ::not-eventually x: x is no longer a valid sequence item
    • ::eventually x : x must appear somewhere in the sequence. will not cause an issue until sequence is fully iterated over.
    • ::is-after y: y must come before
    • ::relax [x y z]: the active rules concerning x, y, and z are released.
    • ::next x: x must be the following element in the collection
    • ::free y: do nothing operator

the-sequence-is-key's People

Contributors

maersdal avatar

Watchers

 avatar  avatar

the-sequence-is-key's Issues

Custom interceptor support

The error catching mechanism is conj'ing onto a list of errors

What about custom interceptor support, so that any user can inject whatever mechanism is nessecary?

machine reducing functions are overly complex

(defn rule-reduce
([active-rules clause k]
(rule-reduce active-rules clause k false))
([active-rules clause k immediate]
(reduce (fn [arules kvp]
(let [[rule value] kvp
;_ (println arules kvp)
]
(case rule
::not-eventually (if (and (= value k) (not immediate))
(update arules ::problem conj {:rule [::not-eventually value]
:broken-by k
::position (active-rules ::position)})
arules)
::is-after (if-not (contains? active-rules value)
(update arules ::problem conj {:rule [::is-after value]
:broken-by k
::position (active-rules ::position)})
arules)
::relax (apply dissoc arules value)
::next (assoc arules ::next value)
::free arules
:default (update arules ::problem conj
{:rule [::not-implemented rule value]
::position (active-rules ::position)}))))
active-rules clause)))
(defn rule-reduce-immediate [rules clause k]
(rule-reduce rules clause k true))
(defn inc-or-zero [x]
(if-not (nil? x)
(inc x)
0))
(def rule-parsing-default {::position 0})
(defn rule-parsing [all-rules active-rules k]
(when debugprint
(println active-rules k))
;; next is priority rule!
(if-let [target (active-rules ::next)]
(if-not (= k (active-rules ::next))
(update active-rules ::problem conj {:rule [::next target]
:broken-by k
::position (active-rules ::position)})
(rule-parsing all-rules (dissoc active-rules ::next) k))
;;
(if (contains? active-rules k)
(let [clause (active-rules k)]
(update (rule-reduce active-rules clause k)
::position inc-or-zero))
(let [clause (all-rules k)]
(-> (rule-reduce-immediate active-rules clause k)
(assoc k clause)
(update ::position inc-or-zero))))))

Infinite streams

For now, the error catching mechanism is conj'ing onto a list of errors, then this list of errors is displayed in the final output.

To be able to use the verification with infinite streams, it should probably subscribe to a stream somehow, and have an atom of errors

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.