Skip to content

Commit

Permalink
Merge pull request #195 from ate47/dev_quadparser
Browse files Browse the repository at this point in the history
Add simple parser extension to parse NQuad and QuadString
  • Loading branch information
D063520 authored May 17, 2023
2 parents a0d4dbf + 5d5d938 commit c325269
Show file tree
Hide file tree
Showing 9 changed files with 605 additions and 216 deletions.
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

0 comments on commit c325269

Please sign in to comment.