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

Add simple parser extension to parse NQuad and QuadString #195

Merged
merged 1 commit into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Create QuadString and add simple parser extension to parse NQuad
  • Loading branch information
ate47 committed May 16, 2023
commit 5d5d9385123d14a9a9bf0bda0c9024326d851927
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ public class ParserException extends Exception {

private static final long serialVersionUID = -5159336711525269027L;

private static String createMessage(String message, String line, int location) {
int start = Math.max(0, location - 10);
int end = Math.min(line.length(), location + 10);
return message + " near " + line.substring(start, end);
}

public ParserException() {
super();
}
Expand All @@ -43,6 +49,10 @@ public ParserException(String message) {
super(message);
}

public ParserException(String message, String line, int location) {
this(createMessage(message, line, location));
}

public ParserException(Throwable e) {
super(e);
}
Expand Down
119 changes: 119 additions & 0 deletions hdt-api/src/main/java/org/rdfhdt/hdt/quad/QuadString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package org.rdfhdt.hdt.quad;

import org.rdfhdt.hdt.exceptions.ParserException;
import org.rdfhdt.hdt.triples.TripleString;

public class QuadString extends TripleString {
protected CharSequence context;

public QuadString() {
super();
context = "";
}

public QuadString(CharSequence subject, CharSequence predicate, CharSequence object, CharSequence context) {
super(subject, predicate, object);
this.context = context;
}

public QuadString(TripleString other) {
super(other);
this.context = other.getObject();
}

@Override
public void clear() {
super.clear();
context = "";
}

@Override
public boolean equals(Object other) {
if (context.length() == 0) {
return super.equals(other);
}
if (!(other instanceof QuadString)) {
return false;
}
QuadString qs = (QuadString) other;
return equalsCharSequence(subject, qs.subject)
&& equalsCharSequence(predicate, qs.predicate)
&& equalsCharSequence(object, qs.object)
&& equalsCharSequence(context, qs.context);
}

@Override
public CharSequence getGraph() {
return context;
}

@Override
public void setGraph(CharSequence context) {
this.context = context;
}

@Override
public void setAll(CharSequence subject, CharSequence predicate, CharSequence object) {
setAll(subject, predicate, object, "");
}

/**
* Sets all components at once. Useful to reuse existing object instead of
* creating new ones for performance.
*
* @param subject subject
* @param predicate predicate
* @param object object
* @param context context
*/
public void setAll(CharSequence subject, CharSequence predicate, CharSequence object, CharSequence context) {
super.setAll(subject, predicate, object);
this.context = context;
}

@Override
public boolean match(TripleString pattern) {
if (context.length() != 0
&& !(pattern instanceof QuadString && equalsCharSequence(((QuadString) pattern).context, context))) {
// if a context is defined, we don't match
return false;
}
if (pattern.getSubject().length() == 0 || equalsCharSequence(pattern.getSubject(), this.subject)) {
if (pattern.getPredicate().length() == 0 || equalsCharSequence(pattern.getPredicate(), this.predicate)) {
return pattern.getObject().length() == 0 || equalsCharSequence(pattern.getObject(), this.object);
}
}
return false;
}

@Override
public boolean isEmpty() {
return super.isEmpty() && context.length() == 0;
}

@Override
public void read(String line) throws ParserException {
super.read(line, true);
}

@Override
public void read(String line, int start, int end) throws ParserException {
super.read(line, start, end, true);
}

@Override
public int hashCode() {
// Same as Objects.hashCode(subject, predicate, object), with fewer
// calls
int s = subject == null ? 0 : subject.hashCode();
int p = predicate == null ? 0 : predicate.hashCode();
int o = object == null ? 0 : object.hashCode();
int c = context == null ? 0 : context.hashCode();
return 31 * (31 * (31 * (31 * s) + p) + o) + c;
}

@Override
public QuadString tripleToString() {
return new QuadString(subject.toString(), predicate.toString(), object.toString(), context.toString());
}
}
Loading