Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefix events #22

Closed
pietercolpaert opened this issue Jun 12, 2020 · 4 comments
Closed

Prefix events #22

pietercolpaert opened this issue Jun 12, 2020 · 4 comments
Labels
enhancement New feature or request good first issue Good for newcomers
Projects

Comments

@pietercolpaert
Copy link

A feature request for developers:

When working with data in JavaScript, and in order to pretty-print it later on, it’s useful to know what prefixes were used in the original file. Could we emit prefix events for when a parser encounters a new prefix? Of course only to be implemented for these serializations that support prefixes.

@rubensworks
Copy link
Owner

Good idea, should be easy to implement.
In theory the only thing that we'd need to do is forward the prefix events from Comunica's quad stream and delegate it to our readable.

@rubensworks rubensworks added enhancement New feature or request good first issue Good for newcomers labels Jun 15, 2020
@rubensworks rubensworks added this to To do in Work via automation Jun 15, 2020
@fvitali
Copy link

fvitali commented May 12, 2021

I am not sure what is going on: if it is my implementation error, or there is still something missing: I can already get working callbacks for the "prefix" and the "context" events (json+ld of course), but they seem to have no parameters specified:

rdfParser.parse(textStream, { contentType: syntax }) 
		.on('prefix', (a) => { console.log(a) })
		.on('context', (b) => {  console.log(b) })
		.on('data', (quad) => { data.push(quad) })
		.on('error', (e) => { reject({ error: e} ) })
		.on('end', () => { resolve({quads: data}) 

both functions get executed, and both print "undefined" on the console.

Can you help?

@rubensworks
Copy link
Owner

@fvitali I'm surprised that prints anything to be honest.
In any case, this feature is not implemented yet, so it's normal that it's not working as intended.

You're welcome to submit a PR though! :-)

@fvitali
Copy link

fvitali commented May 12, 2021

I submitted two separate Pull requests, one here and another on N3.js, to fully enable support for prefix and context callbacks.

I am not really sure about the N3.js, as I was not able to fully follow the complex chain of required module. Anyway, this now works for me.

Working code:

	async function parseRDF(text, syntax) {			
			const textStream = streamify(text+"");

			try {
				var d = await new Promise( (resolve, reject) => {
					var data = []
					rdfParser.parse(textStream, { 
						contentType: syntax })
							.on('prefix', (prefix,iri) => {
								console.log(prefix)
							})
							.on('context', (c) => { 
								console.log(c)
							 })
							.on('data', (quad) => { data.push(quad) })
							.on('error', (e) => { 
								reject( e ) 
							})
							.on('end', () => { 
								resolve({quads: data}) 
							} );

				})
				return d
			} catch (e) {
				return { error: e}
			}
	}

Work automation moved this from To do to Done Jul 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
Work
  
Done
Development

No branches or pull requests

3 participants