template

package
v0.0.0-...-7a6127a Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2020 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Apply

func Apply(w io.Writer, tmpl *template.Template, name, nodeSubject string, g *graph.Graph) error

Apply a template "name" to the node identified by "nodeSubject" in the graph

Example
const templ = `
{{- define "http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/1999/02/22-rdf-syntax-ns#Property" -}}
property {{ .Node.Subject.RawValue }} {{ index (.Objects "http://www.w3.org/2000/01/rdf-schema#label") 0 }}
{{- end }}

{{- define "http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class" -}}
Class: {{ .Node.Subject.RawValue }}
{{- range .To }}
{{- if .HasPredicate "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" "http://www.w3.org/1999/02/22-rdf-syntax-ns#Property"}}
{{ template "http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/1999/02/22-rdf-syntax-ns#Property" . -}}
{{ end -}}
{{ end -}}
{{ end -}}

{{ define "main" }}
start
{{ template "http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class" . }}
end
{{ end }}
	`
const ontology = `
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix example: <http://example.com/> .

# a is a shortcut for http://www.w3.org/1999/02/22-rdf-syntax-ns#type
example:PostalAddress a rdfs:Class ;
    rdfs:label "PostalAddress" ;
    rdfs:comment "The mailing address." .
	
example:addressCountry a rdf:Property ;
    rdfs:label "addressCountry" ;
    rdfs:domain example:PostalAddress ;
	rdfs:comment "A comment" .
	
example:address a rdf:Property ;
    rdfs:label "address" ;
	rdfs:domain example:PostalAddress ;
	rdfs:comment "Physical address of the item." .
	`

baseURI := "https://example.org/foo"
parser := rdf.NewParser(baseURI)
gr, _ := parser.Parse(strings.NewReader(ontology))
g := graph.NewGraph(gr)

tmpl, _ := template.New("global").Parse(templ)
Apply(os.Stdout, tmpl, "main", "http://example.com/PostalAddress", &g)
Output:

start
Class: http://example.com/PostalAddress
property http://example.com/addressCountry addressCountry
property http://example.com/address address
end

Types

type Current

type Current struct {
	Graph *graph.Graph
	Node  *graph.Node
}

Current object to apply to the template

func (Current) From

func (g Current) From(links ...string) []Current

From the node with edge holding a value fro m links

func (Current) HasPredicate

func (g Current) HasPredicate(predicate, object string) bool

HasPredicate returns true if the couple predicate/object exists in the tuple

ex:

{{- if .HasPredicate "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" "http://www.w3.org/1999/02/22-rdf-syntax-ns#Property"}}{{end}}

func (Current) Objects

func (g Current) Objects(predicate string) []string

Objects return all the objects for the predicate in the current node

to return the label of an object

{{ index (.Objects "http://www.w3.org/2000/01/rdf-schema#label") 0 }}

func (Current) To

func (g Current) To(links ...string) []Current

To the node with edge holding a value fro m links

Jump to

Keyboard shortcuts

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