Giter VIP home page Giter VIP logo

rdf2go's Introduction

rdf2go

Build Status Coverage Status

Native golang parser/serializer from/to Turtle and JSON-LD.

Installation

Just go get it!

go get -u github.com/deiu/rdf2go

Example usage

Working with graphs

// Set a base URI
baseUri := "https://example.org/foo"

// Create a new graph
g := (baseUri)

// Add a few triples to the graph
triple1 := NewTriple(NewResource("a"), NewResource("b"), NewResource("c"))
g.Add(triple1)
triple2 := NewTriple(NewResource("a"), NewResource("d"), NewResource("e"))
g.Add(triple2)

// Get length of Graph (nr of triples)
g.Len() // -> 2

// Dump graph contents to NTriples
out := g.String()
// <a> <b> <c> .
// <a> <d> <e> .

// Delete a triple
g.Remove(triple2)

Looking up triples from the graph

Returning a single match

The g.One() method returns the first triple that matches against any (or all) of Subject, Predicate, Object patterns.

// Create a new graph
g := NewGraph("https://example.org")

// Add a few triples
g.Add(NewTriple(NewResource("a"), NewResource("b"), NewResource("c")))

// Look up one triple matching the given subject
triple := g.One(NewResource("a"), nil, nil) // -> <a> <b> <c> .

// Look up one triple matching the given predicate
triple = g.One(nil, NewResource("b"), nil) // -> <a> <b> <c> .

// Look up one triple matching the given object
triple = g.One(nil, nil, NewResource("c")) // -> <a> <b> <c> .

// Look up one triple matching the given subject and predicate
triple = g.One(NewResource("a"), NewResource("b"), nil) // -> <a> <b> <c> .

// Look up one triple matching the a bad predicate
triple = g.One(nil, NewResource("z"), nil) // -> nil

Returning a list of matches

Similar to g.One(), g.All() returns all triples that match the given pattern.

// Create a new graph
g := NewGraph("https://example.org")

// Add a few triples
g.Add(NewTriple(NewResource("a"), NewResource("b"), NewResource("c")))
g.Add(NewTriple(NewResource("a"), NewResource("b"), NewResource("d")))

// Look up one triple matching the given subject
triples := g.All(nil, nil, NewResource("c")) //
for _, triple := range triples {
	triple.String()
}
// Returns a single triple that matches object <c>:
// <a> <b> <c> .

triples = g.All(nil, NewResource("b"), nil)
for _, triple := range triples {
	triple.String()
}
// Returns all triples that match subject <b>: 
// <a> <b> <c> .
// <a> <b> <d> .

Different types of terms (resources)

IRIs

// Create a new IRI
iri := NewResource("https://example.org")
iri.String() // -> <https://example.org>

Literals

// Create a new simple Literal
lit := NewLiteral("hello world")
lit.String() // -> "hello word"

// Create a new Literal with language tag
lit := NewLiteralWithLanguage("hello world", "en")
lit.String() // -> "hello word"@en

// Create a new Literal with a data type
lit := NewLiteralWithDatatype("newTypeVal", NewResource("https://datatype.com"))
lit.String() // -> "newTypeVal"^^<https://datatype.com>

Blank Nodes

// Create a new Blank Node with a given ID
bn := NewBlankNode(9)
bn.String() // -> "_:n9"

// Create an anonymous Blank Node with a random ID
abn := NewAnonNode()
abn.String() // -> "_:n192853"

Parsing data

The parser takes an io.Reader as first parameter, and the string containing the mime type as the second parameter.

Currently, the supported parsing formats are Turtle (with mime type text/turtle) and JSON-LD (with mime type application/ld+json).

Parsing Turtle from an io.Reader

// Set a base URI
baseUri := "https://example.org/foo"

// Create a new graph
g := NewGraph(baseUri)

// r is of type io.Reader
g.Parse(r, "text/turtle")

Parsing JSON-LD from an io.Reader

// Set a base URI
baseUri := "https://example.org/foo"

// Create a new graph
g := NewGraph(baseUri)

// r is an io.Reader
g.Parse(r, "application/ld+json")

Parsing either Turtle or JSON-LD from a URI on the Web

In this case you don't have to specify the mime type, as the internal http client will try to content negotiate to either Turtle or JSON-LD. An error will be returned if it fails.

Note: The NewGraph() function accepts an optional parameter called skipVerify that is used to tell the internal http client whether or not to ignore bad/self-signed server side certificates. By default, it will not check if you omit this parameter, or if you set it to true.

// Set a base URI
uri := "https://example.org/foo"

// Check remote server certificate to see if it's valid 
// (don't skip verification)
skipVerify := false

// Create a new graph. You can also omit the skipVerify parameter
// and accept invalid certificates (e.g. self-signed)
g := NewGraph(uri, skipVerify)

err := g.LoadURI(uri)
if err != nil {
	// deal with the error
}

Serializing data

The serializer takes an io.Writer as first parameter, and the string containing the mime type as the second parameter.

Currently, the supported serialization formats are Turtle (with mime type text/turtle) and JSON-LD (with mime type application/ld+json).

Serializing to Turtle

// Set a base URI
baseUri := "https://example.org/foo"

// Create a new graph
g := NewGraph(baseUri)

triple := NewTriple(NewResource("a"), NewResource("b"), NewResource("c"))
g.Add(triple)

// w is of type io.Writer
g.Serialize(w, "text/turtle")

Serializing to JSON-LD

// Set a base URI
baseUri := "https://example.org/foo"

// Create a new graph
g := NewGraph(baseUri)

triple := NewTriple(NewResource("a"), NewResource("b"), NewResource("c"))
g.Add(triple)

// w is of type io.Writer
g.Serialize(w, "application/ld+json")

rdf2go's People

Contributors

afduarte avatar cem-okulmus avatar davidaf3 avatar deiu avatar drewp avatar kiivihal avatar philharveyonline avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

rdf2go's Issues

IterTriples causes concurrent map error

I'm a happy user of this library but we get occasional errors from the Go runtime's 'concurrent map misuse detector' which makes the program crash: https://go.dev/doc/go1.6#runtime

I believe IterTriples() needs to be changed to avoid this error, e.g. by adding a lock:

rdf2go/graph.go

Line 107 in c39eb29

func (g *Graph) IterTriples() (ch chan *Triple) {

You may also need to use this same lock inside the reader methods.

Alternatively you could avoid creating a goroutine inside IterTriples().

Escaping characters throws an exception

Hello,

There's a problem (bug) in the current library code regarding the escpaction of special characters.
I'm trying to parse a RDF literal (string) which has dobule slash: "aaa\\bbb".
As a result of that, an exception is being thrown: panic: strconv.ParseInt: parsing "ser": invalid syntax

Program's code:

package main

import (
	"github.com/deiu/rdf2go"
	"log"
	"os"
)

func main() {
	if err := convertTtlToJsonLd(); err != nil {
		log.Fatalln(err)
	}
}

func convertTtlToJsonLd() error {
	r, _ := os.OpenFile("test.ttl", os.O_CREATE|os.O_RDONLY, 0664)
	w, _ := os.OpenFile("test.json", os.O_CREATE|os.O_WRONLY, 0644)
	g := rdf2go.NewGraph("https://oxfordsemantic.tech/RDFox/getting-started/")
	if err := g.Parse(r, "text/turtle"); err != nil {
		return err
	}
	return g.Serialize(w, "application/ld+json")
}

RDF (Turtle) file - test.ttl:

@prefix : <https://oxfordsemantic.tech/RDFox/getting-started/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfox: <http://oxfordsemantic.tech/RDFox#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix swrl: <http://www.w3.org/2003/11/swrl#> .
@prefix swrlb: <http://www.w3.org/2003/11/swrlb#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

:Username :Asset_account :Account_159 ;
    :Str_username "w10apps01\\user" ;
    rdf:type :Username .

Exception being thrown:

panic: strconv.ParseInt: parsing "ser": invalid syntax

goroutine 1 [running]:
github.com/deiu/gon3.unescapeUChar({0xc0001c41f0?, 0x12a0120?})
        /Users/bora/go/pkg/mod/github.com/deiu/[email protected]/rdf.go:208 +0x2be
github.com/deiu/gon3.lexicalForm({0xc0001c41ef, 0x11})
        /Users/bora/go/pkg/mod/github.com/deiu/[email protected]/rdf.go:155 +0xd2
github.com/deiu/gon3.(*Parser).parseRDFLiteral(0xc000129e68?)
        /Users/bora/go/pkg/mod/github.com/deiu/[email protected]/parser.go:560 +0x7e
github.com/deiu/gon3.(*Parser).parseLiteral(0xc000129e68?)
        /Users/bora/go/pkg/mod/github.com/deiu/[email protected]/parser.go:508 +0x5e
github.com/deiu/gon3.(*Parser).parseObject(0xc000129e68)
        /Users/bora/go/pkg/mod/github.com/deiu/[email protected]/parser.go:469 +0x9c5
github.com/deiu/gon3.(*Parser).parseObjectList(0xc000129e68?)
        /Users/bora/go/pkg/mod/github.com/deiu/[email protected]/parser.go:381 +0x25
github.com/deiu/gon3.(*Parser).parsePredicateObjectList(0xc000129e68?)
        /Users/bora/go/pkg/mod/github.com/deiu/[email protected]/parser.go:344 +0xa9
github.com/deiu/gon3.(*Parser).parseTriples(0xc000129e68)
        /Users/bora/go/pkg/mod/github.com/deiu/[email protected]/parser.go:273 +0xb8
github.com/deiu/gon3.(*Parser).parseStatement(0x1343660?)
        /Users/bora/go/pkg/mod/github.com/deiu/[email protected]/parser.go:168 +0x105
github.com/deiu/gon3.(*Parser).Parse(0xc000129e68, {0x1343660?, 0xc000010230?})
        /Users/bora/go/pkg/mod/github.com/deiu/[email protected]/parser.go:51 +0x11b
github.com/deiu/rdf2go.(*Graph).Parse(0xc00007dc50, {0x1343660, 0xc000010230}, {0x12c8083?, 0x300000002?})
        /Users/bora/go/pkg/mod/github.com/deiu/[email protected]/graph.go:201 +0x2f7
main.convertTtlToJsonLd()
        /Users/bora/converter/main.go:19 +0x1c7
main.main()
        /Users/bora/converter/main.go:10 +0x19

Process finished with the exit code 2

Convert TTL or JSON-LD to GO struct

I'm able to convert TTL file to a JSON-LD format. Note, the conversion is not perfect because it lacks of @Context metadata. I compared the output using this repo vs online converters.
But how can I convert from JSON-LD to an a Golang structs? Or even better, can I convert straight from RDF TTL to Golang struct?

Working with # IRI

So I am seeing if I can read a VoID doc in turtle. All is going very well till I try to look for triples based on a # IRI.

When I try and do this line (full code ref below)

vds.DownloadURL = getObject(g, triples[triple].Subject, rdf2go.NewResource("http://www.w3.org/ns/dcat#downloadURL"))

It doesn't work since of course everything after # is effectively ignored which is of course correct in many cases. Still, in SPARQL I can set up a query and use a prefix that has a #'s base.

Is there a way to conduct a search in rdf2go with IRIs that have the # structure like in https://github.com/OpenCoreData/ocdGarden/blob/master/GraphUtils/VOIDReader/void.ttl

Code Ref: https://github.com/OpenCoreData/ocdGarden/blob/master/GraphUtils/VOIDReader/main.go

rdf2go usage with SMW?

Hello,

I have read this Paper and it mentioned the usage of rdf2go with Semantic Mediawiki (SMW). I came to your repository, hoping for some example or some demo that can show how one can make it work with SMW.

I am looking forward to getting any help from you regarding the rdf2go integration with SMW!

Thanks.

Error parsing text/turtle

Hi, thanks for your great library. Recently I encountered an error parsing turtle with booleans in it. Here is an example to reproduce:

package main

import (
  "fmt"
  "strings"
  "github.com/deiu/rdf2go"
)

func main() {
  g := rdf2go.NewGraph("")
  if err := g.Parse(strings.NewReader(`<s> <p> [ <o> true; <other> <triple> ] .`), "text/turtle"); err != nil {
    panic(err)
  }
  fmt.Println(g)
}

This results in:

panic: Expected object, got Expected ':' while lexing pname%!(EXTRA []interface {}=[]) (type %!s(easylex.TokenType=-2))

Blank node creation error in deserializing JSON-LD

Reference:
https://github.com/OpenCoreData/ocdGarden/blob/master/JSON-goLD/rdf2goTesting/main.go

Taking a JSON-LD document that contains blank nodes results in an error where all blank nodes get the same ID.

Expect something like

_:b0 <http://schema.org/description> "The Empire State Building is a 102-story landmark in New York City." .
_:b0 <http://schema.org/geo> _:b1 .
_:b0 <http://schema.org/image> <http://www.civil.usherbrooke.ca/cours/gci215a/empire-state-building.jpg> .
_:b0 <http://schema.org/name> "The Empire State Building" .
_:b1 <http://schema.org/latitude> "40.75"^^<http://www.w3.org/2001/XMLSchema#float> .
_:b1 <http://schema.org/longitude> "73.98"^^<http://www.w3.org/2001/XMLSchema#float> .

Get

_:n0 <http://schema.org/description> "The Empire State Building is a 102-story landmark in New York City."^^<http://www.w3.org/2001/XMLSchema#string> .
_:n0 <http://schema.org/geo> _:n0 .
_:n0 <http://schema.org/image> <http://www.civil.usherbrooke.ca/cours/gci215a/empire-state-building.jpg> .
_:n0 <http://schema.org/name> "The Empire State Building"^^<http://www.w3.org/2001/XMLSchema#string> .
_:n0 <http://schema.org/latitude> "40.75"^^<http://www.w3.org/2001/XMLSchema#float> .
_:n0 <http://schema.org/longitude> "73.98"^^<http://www.w3.org/2001/XMLSchema#float> .

Note the geo line at row 2 in both outputs.
The rightmost blank node can not be _:n0 again.

Also, curious if I load multiple JSON-LD documents into a single graph if the blank nodes will be unique so that subsequent JSON-LD documents don't deserialize with conflicting blank node IDs.

How can a prefix be used?

For example:
rdf is the prefix for <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
Therefore, in the .ttl file we should write:

@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
...
ont:TACTIC rdf:type owl:Class ;
    neo4voc:name "TACTIC" .

ont:CustomId rdf:type owl:DatatypeProperty ;
    neo4voc:name "CustomId" .

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.