wotlib

package module
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 8, 2021 License: Apache-2.0 Imports: 4 Imported by: 0

README

WoT Lib

This lib was created to simplify working with WoT Thing Descriptions. It is coupled to W3C's recommendation from 9 April 2020 (https://www.w3.org/TR/2020/REC-wot-thing-description-20200409/) which means it assumes a certain structure. So far only a subset of the fields are implemented.

Working principle

Read Thing Description -> Expand -> Perform operations

Capabilities

  • Determine if a thing and its sub elements do match with certain criteria
  • Search for property affordances with specific constraints
  • Search for action affordances with specific constraints

Example

The following example reads a td from bytes and retrieves all property affordances that match a given criteria

// Define additionally used schema
var iotSchema = wotlib.SchemaMapping{
    Prefix: wotlib.SchemaPrefix("iot"),
    IRI:    "http://iotschema.org/",
}

// Append it so its used during compaction
wotlib.AppendSchema(iotSchema)

expandedTD, err := wotlib.FromBytes(input)
if err != nil {
    panic(err)
}

// Retrieve all property affordances that match given criteria
props := expandedTD.GetPropertyAffordances(wotlib.PropertyConstraint{
    Type: &[]string{iotSchema.IRIPrefix("SwitchStatus")},
})

// Prints the href of the first match
fmt.Printf("Result: %s", props[0].Form.Value().Href.Value())

In case multiple Thing Descriptions have to be processed append them to a set and apply the operation

set := wotlib.NewExpandedThingDescriptionSet(expandedTD)
set.GetActionAffordances(thingConstraint)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	SchemaWoT = SchemaMapping{
		Prefix: SchemaPrefix("wot"),
		IRI:    "https://www.w3.org/2019/wot/td#",
	}

	SchemaHypermedia = SchemaMapping{
		Prefix: SchemaPrefix("hypermedia"),
		IRI:    "https://www.w3.org/2019/wot/hypermedia#",
	}

	SchemaRdfType = SchemaMapping{
		Prefix: SchemaPrefix("rdftype"),
		IRI:    "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
	}

	SchemaJSON = SchemaMapping{
		Prefix: SchemaPrefix("jsonschema"),
		IRI:    "https://www.w3.org/2019/wot/json-schema#",
	}
)

well know schemata

View Source
var DefaultContext = map[string]interface{}{
	SchemaWoT.Prefix.String():        SchemaWoT.IRI,
	SchemaHypermedia.Prefix.String(): SchemaHypermedia.IRI,
	SchemaRdfType.Prefix.String():    SchemaRdfType.IRI,
	SchemaJSON.Prefix.String():       SchemaJSON.IRI,
}

DefaultContext defines the default context Modifications to existing prefixes will corrupt parsing process

View Source
var (
	DefaultJSONDLDOptions = ld.NewJsonLdOptions("")
)

Default json-ld options

Functions

func AppendSchema

func AppendSchema(m SchemaMapping)

AppendSchema appends a schema to the default context

Types

type ActionConstraint

type ActionConstraint struct {
	Name            *string
	Type            *[]string
	InputConstraint *InputConstraint
	IsIdempotent    *bool
	IsSafe          *bool
}

ActionConstraint defines an action constraint

type BooleanNode

type BooleanNode []BooleanValue

BooleanNode defines an array of boolean values

func (BooleanNode) Value

func (b BooleanNode) Value() bool

Value returns the first element inside the boolean node or false if no such element exists

type BooleanValue

type BooleanValue struct {
	Value bool `json:"@value"`
}

BooleanValue describes a boolean value

type DataPropertyConstraint

type DataPropertyConstraint struct {
	Name     *string
	Type     *[]string
	DataType *string
}

DataPropertyConstraint defines a data property constraint

type ExpandedActionAffordance

type ExpandedActionAffordance struct {
	Name         StringNode             `json:"https://www.w3.org/2019/wot/td#name"`
	Type         []string               `json:"@type,omitempty"`
	Form         ExpandedFormNode       `json:"https://www.w3.org/2019/wot/td#hasForm"`
	Input        ExpandedDataSchemaNode `json:"https://www.w3.org/2019/wot/td#hasInputSchema"`
	IsIdempotent BooleanNode            `json:"https://www.w3.org/2019/wot/td#isIdempotent"`
	IsSafe       BooleanNode            `json:"https://www.w3.org/2019/wot/td#isSafe"`
}

ExpandedActionAffordance defines an expanded action affordance within a td

func (*ExpandedActionAffordance) Compact

Compact compacts an expanded action affordance

func (ExpandedActionAffordance) Fulfills

Fulfills checks if ActionConstraint is fulfilled by given ExpandedActionAffordance

type ExpandedDataProperty

type ExpandedDataProperty struct {
	Name     StringNode `json:"https://www.w3.org/2019/wot/json-schema#propertyName"`
	Type     []string   `json:"@type,omitempty"`
	DataType IDNode     `json:"http://www.w3.org/1999/02/22-rdf-syntax-ns#type"`
}

ExpandedDataProperty is part of a data schema

func (ExpandedDataProperty) Fulfills

Fulfills checks if DataPropertyConstraint is fulfilled by given ExpandedDataProperty

type ExpandedDataSchema

type ExpandedDataSchema struct {
	DataType   IDNode                 `json:"http://www.w3.org/1999/02/22-rdf-syntax-ns#type"`
	Properties []ExpandedDataProperty `json:"https://www.w3.org/2019/wot/json-schema#properties"`
}

ExpandedDataSchema can be inside an input param of an action affordance or inside a property

type ExpandedDataSchemaNode

type ExpandedDataSchemaNode []ExpandedDataSchema

ExpandedDataSchemaNode is an array of expanded data schema

func (ExpandedDataSchemaNode) Fulfills

Fulfills checks if ExpandedInputConstraint matches with given ExpandedDataProperty

func (ExpandedDataSchemaNode) Value

Value returns the first element inside the schema node or an empty schema if no such element exists

type ExpandedForm

type ExpandedForm struct {
	ContentType StringNode `json:"https://www.w3.org/2019/wot/hypermedia#forContentType"`
	Op          IDNode     `json:"https://www.w3.org/2019/wot/hypermedia#hasOperationType"`
	Href        IDNode     `json:"https://www.w3.org/2019/wot/hypermedia#hasTarget"`
}

ExpandedForm is part of actions and properties and describes how to resolve an entity

type ExpandedFormNode

type ExpandedFormNode []ExpandedForm

ExpandedFormNode is an array of expanded forms

func (ExpandedFormNode) Value

func (s ExpandedFormNode) Value() ExpandedForm

Value returns the first element inside the form node or an empty form if no such element exists

type ExpandedPropertyAffordance

type ExpandedPropertyAffordance struct {
	Name         StringNode             `json:"https://www.w3.org/2019/wot/td#name"`
	Type         []string               `json:"@type,omitempty"`
	DataType     IDNode                 `json:"http://www.w3.org/1999/02/22-rdf-syntax-ns#type"`
	Properties   []ExpandedDataProperty `json:"https://www.w3.org/2019/wot/json-schema#properties"`
	Form         ExpandedFormNode       `json:"https://www.w3.org/2019/wot/td#hasForm"`
	IsObservable BooleanNode            `json:"https://www.w3.org/2019/wot/td#isObservable"`
}

ExpandedPropertyAffordance defines an expanded property affordance within a td

func (*ExpandedPropertyAffordance) Compact

Compact compacts an expanded property affordance

func (ExpandedPropertyAffordance) Fulfills

Fulfills checks if PropertyConstraint is fulfilled by given ExpandedPropertyAffordance

type ExpandedThingDescription

type ExpandedThingDescription struct {
	ID         string                       `json:"@id"`
	Type       []string                     `json:"@type,omitempty"`
	Name       StringNode                   `json:"https://www.w3.org/2019/wot/td#name"`
	Actions    []ExpandedActionAffordance   `json:"https://www.w3.org/2019/wot/td#hasActionAffordance"`
	Properties []ExpandedPropertyAffordance `json:"https://www.w3.org/2019/wot/td#hasPropertyAffordance"`
}

ExpandedThingDescription reflects a thing description in its expanded format Note: currently this lib only supports a small sub set of fields

func FromBytes

func FromBytes(b []byte) (ExpandedThingDescription, error)

FromBytes expands input bytes and converts it to ExpandedThingDescription

func FromResponse

func FromResponse(resp *http.Response) (ExpandedThingDescription, error)

FromResponse tries to extract an expanded wot td from a response object

func (*ExpandedThingDescription) Compact

Compact compacts the thing description

func (ExpandedThingDescription) Fulfills

Fulfills checks if constraint matches with given element A thing description matches if ID, type and name match (if given) and if at least one of the PropertyConstraints and one of the ActionConstraints matches (if given)

func (*ExpandedThingDescription) GetActionAffordances

func (t *ExpandedThingDescription) GetActionAffordances(constraint ActionConstraint) []ExpandedActionAffordance

GetActionAffordances searches for an action affordance with specific criteria

func (*ExpandedThingDescription) GetPropertyAffordances

func (t *ExpandedThingDescription) GetPropertyAffordances(constraint PropertyConstraint) []ExpandedPropertyAffordance

GetPropertyAffordances searches for a property affordance with specific criteria

type ExpandedThingDescriptionSet

type ExpandedThingDescriptionSet map[string]ExpandedThingDescription

ExpandedThingDescriptionSet set of thing descriptions with some convenience functions

func NewExpandedThingDescriptionSet

func NewExpandedThingDescriptionSet(tds ...ExpandedThingDescription) ExpandedThingDescriptionSet

NewExpandedThingDescriptionSet creates a new set

func (*ExpandedThingDescriptionSet) Append

Append appends an expanded td to the list

func (*ExpandedThingDescriptionSet) Get

Get retrieves a td by id

func (*ExpandedThingDescriptionSet) GetActionAffordances

func (s *ExpandedThingDescriptionSet) GetActionAffordances(constraint ThingConstraint) []ExpandedActionAffordance

GetActionAffordances searches within a set for all actions affordances where constraints match

func (*ExpandedThingDescriptionSet) GetPropertyAffordances

func (s *ExpandedThingDescriptionSet) GetPropertyAffordances(constraint ThingConstraint) []ExpandedPropertyAffordance

GetPropertyAffordances searches within a set for all property affordances where constraints match

func (*ExpandedThingDescriptionSet) Remove

func (s *ExpandedThingDescriptionSet) Remove(id string)

Remove removes a td from the list

type IDNode

type IDNode []IDValue

IDNode defines an array of id values

func (IDNode) Value

func (s IDNode) Value() string

Value returns the first element inside the string node or an empty string if no such element exists

type IDValue

type IDValue struct {
	ID string `json:"@id"`
}

IDValue describes an id value

type InputConstraint

type InputConstraint struct {
	DataType               *string
	DataPropertyConstraint *DataPropertyConstraint
}

InputConstraint defines an input constraint

type PropertyConstraint

type PropertyConstraint struct {
	Name                   *string
	Type                   *[]string
	DataType               *string
	DataPropertyConstraint *DataPropertyConstraint
	IsObservable           *bool
}

PropertyConstraint defines a property constraint

type SchemaMapping

type SchemaMapping struct {
	Prefix SchemaPrefix
	IRI    string
}

SchemaMapping defines a prefix iri mapping

func (SchemaMapping) IRIPrefix

func (s SchemaMapping) IRIPrefix(nodeID string) string

IRIPrefix builds a prefixed node name for referencing elements in a thing description. Eg. SchemaWoT.IRIPrefix(PropertyAffordance) would return "https://...#PropertyAffordance"

type SchemaPrefix

type SchemaPrefix string

SchemaPrefix like wot, schema...

func (SchemaPrefix) String

func (p SchemaPrefix) String() string

type StringNode

type StringNode []StringValue

StringNode defines an array of string values

func (StringNode) Value

func (s StringNode) Value() string

Value returns the first element inside the string node or an empty string if no such element exists

type StringValue

type StringValue struct {
	Value string `json:"@value"`
}

StringValue describes a string value

type ThingConstraint

type ThingConstraint struct {
	ID                 *string
	Type               *[]string
	Name               *string
	PropertyConstraint *PropertyConstraint
	ActionConstraint   *ActionConstraint
}

ThingConstraint defines a thing constraint

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL