Giter VIP home page Giter VIP logo

Comments (7)

Grokzen avatar Grokzen commented on July 21, 2024

Hi.

It would make a little bit easier if you could provide an example data structure that you want to validate. It should be rather easy to do something with partial-schemas and the include directives. Also it would help some with what you really want to validate in the data structure. Are you after validating all file names is valid or directory names?

from pykwalify.

AlexandreDecan avatar AlexandreDecan commented on July 21, 2024

I'm currently working on a tool to interpret and check statecharts (see sismic). I use YAML to define statecharts. In a nutshell, a statechart is a set of states, and each state can also have nested states and so one.

Here is a simplified example:

statechart:
  name: Elevator
  initial: active

  # Skipped part
  # ...

  states:
    - name: active
      parallel states:
        - name: movingElevator
          initial: doorsOpen
          states:
            - name: doorsOpen
              transitions:
                - target: doorsClosed
                  guard: destination != current
                  action: doors.close()
                - target: doorsClosed
                  guard: after(10) and current > 0
                  action: |
                    destination = 0
                    doors.close()
            - name: doorsClosed
              transitions:
                - target: movingUp
                  guard: destination > current
                - target: movingDown
                  guard: destination < current and destination >= 0
            - name: moving
              transitions:
                - target: doorsOpen
                  guard: destination == current
                  action: doors.open()
              states:
                - name: movingUp
                  on entry: current = current + 1
                  transitions:
                    - target: movingUp
                      guard: destination > current
                - name: movingDown
                  on entry: current = current - 1
                  transitions:
                    - target: movingDown
                      guard: destination < current
        - name: floorListener
          initial: floorSelecting
          states:
            - name: floorSelecting
              transitions:
                - target: floorSelecting
                  event: floorSelected
                  action: destination = event.floor

The root element, namely statechart defines a states sequence. Each item in this sequence is a state (defined by a name and that can have many other attributes). A state can be basic (meaning it has no nested state), composite (meaning it has nested states, defined using states:) or parallel (meaning it has nested parallel states, defined using parallel states:).

I would like to validate this kind of YAML definition of a statechart. "Classical attributes" are easy to define, but I don't see how to say "a state can have nested states, which in turn can have nested states, which in turn can have nested states, which...". I need some kind of recursive definition:

[type state]
 - name: str
 - (optional) type: [final | shallow memory | deep memory]
 - (optional) transitions: sequence of [type transition]
 - (optional) states: sequence of [type state]
 - (optional) parallel states: sequence of [type state]
 - (optional) on entry: str
 - (optional) on exit: str

[type transition]
 - (optional) event: str
 - (optional) guard: str
 - (optional) action: str
 - (optional) target: str     (ideally, a foreign key to a [type state].name)

Thank you!

from pykwalify.

Grokzen avatar Grokzen commented on July 21, 2024

Oh yeah, that is supported right now. You should be looking at the following feature https://github.com/Grokzen/pykwalify/blob/unstable/docs/Validation%20Rules.md#partial-schemas

Basically you define the top level statechard level and then you implement a partial schema that defined the following data level

- name: foo
  initial: bar
  states:
    type: seq
    include: foobar

Just to get you started on a schema that you can finish up is the following

schema;foo:
  type: map
  mapping:
    name:
      type: str
    states:
      type: seq
      sequence:
        - type: map
          include: foo
    initial:
      type: str
    parallel-states:
      type: map
      include: foo


type: map
mapping:
  statechart:
    type: map
    include: foo

This schema validates for me with your test data you posted (with one exception that i changed to paralell-states but that is just semantics)

I leave it up to you to finish the rest of the schema :]

One thing i can agree on is that the docs could be lacking in describing that nested schema is supported with the schema; and include tag. I might make some new examples and update the docs to be abit more clear on this feature in the future.

If you have other questions or want to chat some more, i suggest joining in the gitter chatroom and ping me in there and we can continue in there.

from pykwalify.

AlexandreDecan avatar AlexandreDecan commented on July 21, 2024

I'll have a look at partial-schemas.
Thanks for your answer!

I'll probably get back with new questions ;-)

from pykwalify.

AlexandreDecan avatar AlexandreDecan commented on July 21, 2024

I still have to do some modifications, but here's the resulting schema. Thank you again!

schema;contract:
  type: seq
  sequence:
    - type: map
      mapping:
        "before":
          type: str
    - type: map
      mapping:
        "after":
          type: str
    - type: map
      mapping:
        "always":
          type: str

schema;transition:
  type: map
  mapping:
    "target":
      type: str
    "event":
      type: str
    "guard":
      type: str
    "action":
      type: str
    "contract":
      include: contract

schema;state:
  type: map
  mapping:
    "name":
      type: str
      required: yes
    "initial":
      type: str
    "on entry":
      type: str
    "on exit":
      type: str
    "type":
      type: str
      enum: [final, shallow history, deep history]
    "states":
      type: seq
      sequence:
        - include: state
    "parallel states":
      type: seq
      sequence:
        - include: state
    "transitions":
      type: seq
      sequence:
        - include: transition
    "contract":
      include: contract

type: map
mapping:
  "statechart":
    type: map
    required: yes
    mapping:
      "name":
        type: str
        required: yes
      "description":
        type: str
      "on entry":
        type: str
      "initial":
        type: str
        required: yes
      "states":
        type: seq
        required: yes
        sequence:
          - include: state
      "contract":
        include: contract

from pykwalify.

Grokzen avatar Grokzen commented on July 21, 2024

Awesome that you got it to work :]

I hope you do not mind if i use a subset of the final answer in a example file for Advance Recursive Schema Example

from pykwalify.

AlexandreDecan avatar AlexandreDecan commented on July 21, 2024

Yeah, feel free to use it ;)

from pykwalify.

Related Issues (20)

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.