2018-11-29 05:40:11 +09:00
|
|
|
package rdf
|
|
|
|
|
2018-12-02 00:36:33 +09:00
|
|
|
import (
|
|
|
|
"fmt"
|
2018-12-10 05:23:32 +09:00
|
|
|
"github.com/cjslep/activity/tools/exp/codegen"
|
|
|
|
"github.com/dave/jennifer/jen"
|
|
|
|
"net/url"
|
2018-12-03 07:48:54 +09:00
|
|
|
"strings"
|
2018-12-02 00:36:33 +09:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
rdfSpec = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
|
2018-12-09 02:57:40 +09:00
|
|
|
langstringSpec = "langString"
|
2018-12-02 00:36:33 +09:00
|
|
|
propertySpec = "Property"
|
|
|
|
)
|
|
|
|
|
2018-12-10 05:23:32 +09:00
|
|
|
func SerializeValueFunction(pkg, valueName string,
|
|
|
|
concreteType jen.Code,
|
|
|
|
impl []jen.Code) *codegen.Function {
|
|
|
|
name := fmt.Sprintf("Serialize%s", strings.Title(valueName))
|
|
|
|
return codegen.NewCommentedFunction(
|
|
|
|
pkg,
|
|
|
|
name,
|
|
|
|
[]jen.Code{jen.Id(codegen.This()).Add(concreteType)},
|
|
|
|
[]jen.Code{jen.Interface(), jen.Error()},
|
|
|
|
impl,
|
|
|
|
jen.Commentf("%s converts a %s value to an interface representation suitable for marshalling into a text or binary format.", name, valueName))
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeserializeValueFunction(pkg, valueName string,
|
|
|
|
concreteType jen.Code,
|
|
|
|
impl []jen.Code) *codegen.Function {
|
|
|
|
name := fmt.Sprintf("Deserialize%s", strings.Title(valueName))
|
|
|
|
return codegen.NewCommentedFunction(
|
|
|
|
pkg,
|
|
|
|
name,
|
|
|
|
[]jen.Code{jen.Id(codegen.This()).Interface()},
|
|
|
|
[]jen.Code{concreteType, jen.Error()},
|
|
|
|
impl,
|
|
|
|
jen.Commentf("%s creates %s value from an interface representation that has been unmarshalled from a text or binary format.", name, valueName))
|
|
|
|
}
|
|
|
|
|
|
|
|
func LessFunction(pkg, valueName string,
|
|
|
|
concreteType jen.Code,
|
|
|
|
impl []jen.Code) *codegen.Function {
|
|
|
|
name := fmt.Sprintf("Less%s", strings.Title(valueName))
|
|
|
|
return codegen.NewCommentedFunction(
|
|
|
|
pkg,
|
|
|
|
name,
|
|
|
|
[]jen.Code{jen.List(jen.Id("lhs"), jen.Id("rhs")).Add(concreteType)},
|
|
|
|
[]jen.Code{jen.Bool()},
|
|
|
|
impl,
|
|
|
|
jen.Commentf("%s returns true if the left %s value is less than the right value.", name, valueName))
|
|
|
|
}
|
|
|
|
|
2018-12-09 02:57:40 +09:00
|
|
|
type RDFOntology struct {
|
2018-12-10 05:23:32 +09:00
|
|
|
Package string
|
|
|
|
alias string
|
2018-12-09 02:57:40 +09:00
|
|
|
}
|
2018-11-29 05:40:11 +09:00
|
|
|
|
|
|
|
func (o *RDFOntology) SpecURI() string {
|
2018-12-02 00:36:33 +09:00
|
|
|
return rdfSpec
|
2018-11-29 05:40:11 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o *RDFOntology) Load() ([]RDFNode, error) {
|
2018-12-02 00:36:33 +09:00
|
|
|
return o.LoadAsAlias("")
|
2018-11-29 05:40:11 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o *RDFOntology) LoadAsAlias(s string) ([]RDFNode, error) {
|
2018-12-09 02:57:40 +09:00
|
|
|
o.alias = s
|
2018-12-02 00:36:33 +09:00
|
|
|
return []RDFNode{
|
|
|
|
&AliasedDelegate{
|
|
|
|
Spec: rdfSpec,
|
|
|
|
Alias: s,
|
|
|
|
Name: langstringSpec,
|
2018-12-10 05:23:32 +09:00
|
|
|
Delegate: &langstring{pkg: o.Package, alias: o.alias},
|
2018-12-02 00:36:33 +09:00
|
|
|
},
|
|
|
|
&AliasedDelegate{
|
|
|
|
Spec: rdfSpec,
|
|
|
|
Alias: s,
|
|
|
|
Name: propertySpec,
|
|
|
|
Delegate: &property{},
|
|
|
|
},
|
|
|
|
}, nil
|
2018-11-29 05:40:11 +09:00
|
|
|
}
|
|
|
|
|
2018-12-03 04:04:56 +09:00
|
|
|
func (o *RDFOntology) LoadSpecificAsAlias(alias, name string) ([]RDFNode, error) {
|
|
|
|
switch name {
|
|
|
|
case langstringSpec:
|
|
|
|
return []RDFNode{
|
|
|
|
&AliasedDelegate{
|
|
|
|
Spec: "",
|
|
|
|
Alias: "",
|
|
|
|
Name: alias,
|
2018-12-10 05:23:32 +09:00
|
|
|
Delegate: &langstring{pkg: o.Package, alias: o.alias},
|
2018-12-03 04:04:56 +09:00
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
case propertySpec:
|
|
|
|
return []RDFNode{
|
|
|
|
&AliasedDelegate{
|
|
|
|
Spec: "",
|
|
|
|
Alias: "",
|
|
|
|
Name: alias,
|
|
|
|
Delegate: &property{},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("rdf ontology cannot find %q to make alias %q", name, alias)
|
|
|
|
}
|
|
|
|
|
2018-11-29 05:40:11 +09:00
|
|
|
func (o *RDFOntology) LoadElement(name string, payload map[string]interface{}) ([]RDFNode, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2018-12-02 00:36:33 +09:00
|
|
|
|
2018-12-03 07:48:54 +09:00
|
|
|
func (o *RDFOntology) GetByName(name string) (RDFNode, error) {
|
|
|
|
name = strings.TrimPrefix(name, o.SpecURI())
|
|
|
|
switch name {
|
2018-12-06 06:53:26 +09:00
|
|
|
case langstringSpec:
|
2018-12-10 05:23:32 +09:00
|
|
|
return &langstring{pkg: o.Package, alias: o.alias}, nil
|
2018-12-06 06:53:26 +09:00
|
|
|
case propertySpec:
|
|
|
|
return &property{}, nil
|
2018-12-03 07:48:54 +09:00
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("rdf ontology could not find node for name %s", name)
|
|
|
|
}
|
|
|
|
|
2018-12-02 00:36:33 +09:00
|
|
|
var _ RDFNode = &langstring{}
|
|
|
|
|
2018-12-09 02:57:40 +09:00
|
|
|
type langstring struct {
|
|
|
|
alias string
|
2018-12-10 05:23:32 +09:00
|
|
|
pkg string
|
2018-12-09 02:57:40 +09:00
|
|
|
}
|
2018-12-02 00:36:33 +09:00
|
|
|
|
|
|
|
func (l *langstring) Enter(key string, ctx *ParsingContext) (bool, error) {
|
|
|
|
return true, fmt.Errorf("rdf langstring cannot be entered")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *langstring) Exit(key string, ctx *ParsingContext) (bool, error) {
|
|
|
|
return true, fmt.Errorf("rdf langstring cannot be exited")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *langstring) Apply(key string, value interface{}, ctx *ParsingContext) (bool, error) {
|
2018-12-09 02:57:40 +09:00
|
|
|
for k, p := range ctx.Result.Vocab.Properties {
|
|
|
|
for _, ref := range p.Range {
|
|
|
|
if ref.Name == langstringSpec && ref.Vocab == l.alias {
|
|
|
|
p.NaturalLanguageMap = true
|
|
|
|
ctx.Result.Vocab.Properties[k] = p
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-10 05:23:32 +09:00
|
|
|
u, e := url.Parse(rdfSpec + langstringSpec)
|
|
|
|
if e != nil {
|
|
|
|
return true, e
|
|
|
|
}
|
|
|
|
e = ctx.Result.GetReference(rdfSpec).SetValue(langstringSpec, &VocabularyValue{
|
|
|
|
Name: langstringSpec,
|
|
|
|
URI: u,
|
|
|
|
DefinitionType: "map[string]string",
|
|
|
|
Zero: "nil",
|
|
|
|
SerializeFn: SerializeValueFunction(
|
|
|
|
l.pkg,
|
|
|
|
langstringSpec,
|
|
|
|
jen.Map(jen.String()).String(),
|
|
|
|
[]jen.Code{
|
|
|
|
// TODO
|
|
|
|
}),
|
|
|
|
DeserializeFn: DeserializeValueFunction(
|
|
|
|
l.pkg,
|
|
|
|
langstringSpec,
|
|
|
|
jen.Map(jen.String()).String(),
|
|
|
|
[]jen.Code{
|
|
|
|
// TODO
|
|
|
|
}),
|
|
|
|
LessFn: LessFunction(
|
|
|
|
l.pkg,
|
|
|
|
langstringSpec,
|
|
|
|
jen.Map(jen.String()).String(),
|
|
|
|
[]jen.Code{
|
|
|
|
// TODO
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
return true, e
|
2018-12-02 00:36:33 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ RDFNode = &property{}
|
|
|
|
|
|
|
|
type property struct{}
|
|
|
|
|
|
|
|
func (p *property) Enter(key string, ctx *ParsingContext) (bool, error) {
|
|
|
|
return true, fmt.Errorf("rdf property cannot be entered")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *property) Exit(key string, ctx *ParsingContext) (bool, error) {
|
|
|
|
return true, fmt.Errorf("rdf property cannot be exited")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *property) Apply(key string, value interface{}, ctx *ParsingContext) (bool, error) {
|
2018-12-06 06:53:26 +09:00
|
|
|
// Prepare a new VocabularyProperty in the context. If one already
|
|
|
|
// exists, skip.
|
|
|
|
if _, ok := ctx.Current.(*VocabularyProperty); ok {
|
|
|
|
return true, nil
|
|
|
|
} else if !ctx.IsReset() {
|
|
|
|
return true, fmt.Errorf("rdf property applied with non-reset ParsingContext")
|
|
|
|
}
|
|
|
|
ctx.Current = &VocabularyProperty{}
|
|
|
|
return true, nil
|
2018-12-02 00:36:33 +09:00
|
|
|
}
|