Skip to content
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
1 change: 1 addition & 0 deletions jena-arq/Grammar/arq.jj
Original file line number Diff line number Diff line change
Expand Up @@ -2480,6 +2480,7 @@ TOKEN:
["\u200C"-"\u200D"] | ["\u2070"-"\u218F"] | ["\u2C00"-"\u2FEF"] |
["\u3001"-"\uD7FF"] | ["\uF900"-"\uFFFD"]
// [#x10000-#xEFFFF]
// \-U not legal here so as an extension allow surrogate pairs.
// Put in surrogate pairs because by the time javacc sees codepoints,
// they are in UTF-16.
| <SURROGATE_PAIR>
Expand Down
60 changes: 34 additions & 26 deletions jena-arq/Grammar/jj2bnf
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ if ( $#ARGV != 1 )
exit 1 ;
}

$WithNumbers = 1 ;

$/ = undef ;
# Just table or full page.

Expand Down Expand Up @@ -122,9 +124,6 @@ for $t (@t) {
$tokenbody =~ s/"/'/g ; # '" -- But not literal " -- how?
$tokenbody =~ s/\<\>\'\{\}/\<\>\"\{\}/ ; # '" IRI fixup

## IRIREF - special case of inline but with nested terminal name.
$tokenbody =~ s/<UCHAR>/UCHAR/ ; #

$inline{$tokenname} = $tokenbody ;
#print "INLINE: ",$tokenname," => ",$tokenbody,"\n" ;
} else {
Expand Down Expand Up @@ -159,11 +158,11 @@ for $r (@rules) {

# Inlines
for $k (keys %inline) {
## $s = esc($inline{$k}) ;
## $k = esc($k) ;
## # Assumes escaped <> round tokens.
## $k = quotemeta $k ;
## $ruleBodyStr =~ s/$k/$s/g ;
## $s = esc($inline{$k}) ;
## $k = esc($k) ;
## # Assumes escaped <> round tokens.
## $k = quotemeta $k ;
## $ruleBodyStr =~ s/$k/$s/g ;

$s = $inline{$k};
$ruleBodyStr =~ s/$k/$s/g ;
Expand Down Expand Up @@ -196,12 +195,14 @@ for $r (@rules) {
## Third part of the output.
$ruleBodyStr = fixupRule($rulename, $ruleBodyStr) ;

##Why no NL needed?
## printf "%-5s %-25s ::= %s\n", $rlabel, $rn, $ruleBodyStr ;
printf "%-25s ::= %s\n", $rn, $ruleBodyStr ;

# $rule{$rulename, $rulebody) ;
# print $rulename , "\n" ;
if ( $WithNumbers ) {
## 5 spaces for the line number text, 2 gap = 7
$ruleNumStr = sprintf("%-5s", '[' . $ruleNum . ']');
printf "%s %-25s ::= %s\n", $ruleNumStr, $rn, $ruleBodyStr ;
} else {
## No number label
printf "%-25s ::= %s\n", $rn, $ruleBodyStr ;
}
}

sub readFile {
Expand Down Expand Up @@ -237,7 +238,7 @@ sub fixupRule {
$body =~ s/^\(\s+([^\(]*)\s+\)$/$1/ ;

# There aren't any of these
## $body =~ s!\(\s+(\S*)\s+\)!$1!g ;
## $body =~ s!\(\s+(\S*)\s+\)!$1!g ;

# Remove <> around tokens.
$body =~ s/&lt;(\w+)&gt;/$1/g ;
Expand All @@ -252,12 +253,12 @@ sub fixupRule {
$head eq "Aggregate" )
{
## Except BNODE, STRLEN etc
## [138] RegexExpression ::= 'REGEX' '(' Expression ',' Expression ( ',' Expression )? ')'
## [139] SubstringExpression ::= 'SUBSTR' '(' Expression ',' Expression ( ',' Expression )? ')'
## [140] StrReplaceExpression ::= 'REPLACE' '(' Expression ',' Expression ',' Expression ( ',' Expression )? ')'
## [141] ExistsFunc ::= 'EXISTS' GroupGraphPattern
## [142] NotExistsFunc ::= 'NOT' 'EXISTS' GroupGraphPattern
## | 'BNODE' ( '(' Expression ')' | <NIL> )
## [138] RegexExpression ::= 'REGEX' '(' Expression ',' Expression ( ',' Expression )? ')'
## [139] SubstringExpression ::= 'SUBSTR' '(' Expression ',' Expression ( ',' Expression )? ')'
## [140] StrReplaceExpression ::= 'REPLACE' '(' Expression ',' Expression ',' Expression ( ',' Expression )? ')'
## [141] ExistsFunc ::= 'EXISTS' GroupGraphPattern
## [142] NotExistsFunc ::= 'NOT' 'EXISTS' GroupGraphPattern
## | 'BNODE' ( '(' Expression ')' | <NIL> )

print STDERR "A:",$body,"\n" if $p ;

Expand All @@ -266,8 +267,15 @@ sub fixupRule {

## Alternative more general removal: NB space-) - matched the BNF parens
##$body =~ s%'BNODE'.*? \)%XXX-BNODE-XXX%;

$body =~ s%\|\s*%\n | %g ;

if ( $WithNumbers ) {
## Align spaces with the number label
## With number label
$body =~ s%\|\s*%\n | %g ;
} else {
## No number label
$body =~ s%\|\s*%\n | %g ;
}

## Replace marker
$body =~ s%XXX-BNODE-XXX%'BNODE' \( '(' Expression ')' \| NIL \)%;
Expand All @@ -289,9 +297,9 @@ sub fixupRule {
}

## Split?
## if ( $head eq "Query" )
## {
## }
## if ( $head eq "Query" )
## {
## }

#Rules where an outer () is unnecessary.
if ( $head eq "GroupCondition" ||
Expand Down
1 change: 1 addition & 0 deletions jena-arq/Grammar/main.jj
Original file line number Diff line number Diff line change
Expand Up @@ -3066,6 +3066,7 @@ TOKEN:
["\u3001"-"\uD7FF"] | ["\uF900"-"\uFFFD"]
// [#x10000-#xEFFFF]
#ifdef ARQ
// \-U not legal here so as an extension allow surrogate pairs.
// Put in surrogate pairs because by the time javacc sees codepoints,
// they are in UTF-16.
| <SURROGATE_PAIR>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ public class HttpNames
// XXX Rename as mtCharset?
public static final String charset = "charset" ;
// RDF .1.2+ SPARQL 1.2+ HTTP header parameters
public static final String mtParamVersion = "version" ;
public static final String mtParamVersion = "version" ;
// RFC 6906 -- https://www.rfc-editor.org/rfc/rfc6906
public static final String mtParamProfile = "profile" ;
public static final String mtParamProfile = "profile" ;

// GSP parameter names
public static final String paramGraph = "graph" ;
Expand Down Expand Up @@ -99,7 +99,7 @@ public class HttpNames
public static final String paramForceAccept = "force-accept" ; // Force the accept header at the last moment
public static final String paramTimeout = "timeout" ;

// Replace by enum HttpMethods
// Replace by enum HttpMethod
public static final String METHOD_DELETE = "DELETE";
public static final String METHOD_HEAD = "HEAD";
public static final String METHOD_GET = "GET";
Expand Down
27 changes: 12 additions & 15 deletions jena-arq/src/main/java/org/apache/jena/sparql/core/Substitute.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,9 @@ public static Triple substitute(Triple triple, Binding binding) {
Node p1 = substitute(p, binding);
Node o1 = substitute(o, binding);

Triple t = triple;
if ( s1 != s || p1 != p || o1 != o )
t = Triple.create(s1, p1, o1);
return t;
if ( s1 == s && p1 == p && o1 == o )
return triple;
return Triple.create(s1, p1, o1);
}

public static TriplePath substitute(TriplePath triplePath, Binding binding) {
Expand All @@ -136,10 +135,9 @@ public static TriplePath substitute(TriplePath triplePath, Binding binding) {
Node s1 = substitute(s, binding);
Node o1 = substitute(o, binding);

TriplePath tp = triplePath;
if ( s1 != s || o1 != o )
tp = new TriplePath(s1, triplePath.getPath(), o1);
return tp;
if ( s1 == s && o1 == o )
return triplePath;
return new TriplePath(s1, triplePath.getPath(), o1);
}

public static Quad substitute(Quad quad, Binding binding) {
Expand All @@ -158,10 +156,9 @@ public static Quad substitute(Quad quad, Binding binding) {
Node p1 = substitute(p, binding);
Node o1 = substitute(o, binding);

Quad q = quad;
if ( s1 != s || p1 != p || o1 != o || g1 != g )
q = Quad.create(g1, s1, p1, o1);
return q;
if ( s1 == s && p1 == p && o1 == o && g1 == g )
return quad;
return Quad.create(g1, s1, p1, o1);
}

public static Node substitute(Node n, Binding binding) {
Expand All @@ -187,9 +184,10 @@ public static Node substitute(Node n, Binding binding) {
// No change - return original
if ( s1 == s && o1 == o && p1 == p )
return n;
// Change happened. Create new.
Node n2 = NodeFactory.createTripleTerm(s1, p1, o1);
return n2;

// Change. Create new.
return NodeFactory.createTripleTerm(s1, p1, o1);
}

/** Substitute for a node that makes up a triple in a Node_Triple. Recursively. */
Expand All @@ -198,7 +196,6 @@ private static Node subTripleTermNode(Node n, Binding binding) {
if ( ! n.isConcrete() )
n = substitute(n, binding);
} else if ( Var.isVar(n) ) {
Var var = Var.alloc(n);
n = Var.lookup(binding::get, n);
}
return n;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,62 +124,55 @@ public static Quad subst(Quad quad, Binding b, Map<Node, Node> bNodeMap) {
Node p = quad.getPredicate();
Node o = quad.getObject();

Node g1 = g;
Node s1 = s;
Node p1 = p;
Node o1 = o;

// replace blank nodes.
if ( g1.isBlank() || Var.isBlankNodeVar(g1) )
g1 = newBlank(g1, bNodeMap);

if ( s1.isBlank() || Var.isBlankNodeVar(s1) )
s1 = newBlank(s1, bNodeMap);

if ( p1.isBlank() || Var.isBlankNodeVar(p1) )
p1 = newBlank(p1, bNodeMap);

if ( o1.isBlank() || Var.isBlankNodeVar(o1) )
o1 = newBlank(o1, bNodeMap);

Quad q = quad;
if ( s1 != s || p1 != p || o1 != o || g1 != g )
q = Quad.create(g1, s1, p1, o1);
Quad q2 = Substitute.substitute(q, b);
return q2;
Node g1 = subst(g, b, bNodeMap, false);
Node s1 = subst(s, b, bNodeMap, true);
Node p1 = subst(p, b, bNodeMap, false);
Node o1 = subst(o, b, bNodeMap, true);

if ( s1 == s && p1 == p && o1 == o && g1 == g )
return quad;
return Quad.create(g1, s1, p1, o1);
}

/** Substitute into a triple, with rewriting of bNodes */
public static Triple subst(Triple triple, Binding b, Map<Node, Node> bNodeMap) {
// Transfer to quads.
Node s = triple.getSubject();
Node p = triple.getPredicate();
Node o = triple.getObject();

Node s1 = s;
Node p1 = p;
Node o1 = o;
Node s1 = subst(s, b, bNodeMap, true);
Node p1 = subst(p, b, bNodeMap, false);
Node o1 = subst(o, b, bNodeMap, true);

if ( s1.isBlank() || Var.isBlankNodeVar(s1) )
s1 = newBlank(s1, bNodeMap);

if ( p1.isBlank() || Var.isBlankNodeVar(p1) )
p1 = newBlank(p1, bNodeMap);

if ( o1.isBlank() || Var.isBlankNodeVar(o1) )
o1 = newBlank(o1, bNodeMap);
if ( s1 == s && p1 == p && o1 == o )
return triple;
return Triple.create(s1, p1, o1);
}

Triple t = triple;
if ( s1 != s || p1 != p || o1 != o )
t = Triple.create(s1, p1, o1);
Triple t2 = Substitute.substitute(t, b);
return t2;
// Deep node processing, with blank node rewrite and variable substitution.
public static Node subst(Node n, Binding b, Map<Node, Node> bNodeMap, boolean allowTripleTerms) {
if ( n.isURI() || n.isLiteral() )
return n;
if ( Var.isVar(n))
return Substitute.substitute(Var.alloc(n), b);
if ( n.isBlank() || Var.isBlankNodeVar(n) )
return newBlank(n, bNodeMap);
if ( allowTripleTerms && n.isTripleTerm() ) {
Triple t1 = subst(n.getTriple(), b, bNodeMap);
return NodeFactory.createTripleTerm(t1);
}
return n;
}

/** generate a blank node consistently */
/** Generate a blank node consistently */
private static Node newBlank(Node n, Map<Node, Node> bNodeMap) {
if ( !bNodeMap.containsKey(n) )
bNodeMap.put(n, NodeFactory.createBlankNode());
return bNodeMap.get(n);
Node n2 = bNodeMap.get(n);
if ( n2 != null )
return n2;
Node bNew = NodeFactory.createBlankNode();
bNodeMap.put(n, bNew);
return bNew;
}

// ---- Template to query pattern
Expand Down
25 changes: 23 additions & 2 deletions jena-arq/src/main/java/org/apache/jena/system/G.java
Original file line number Diff line number Diff line change
Expand Up @@ -505,16 +505,23 @@ public static Set<Node> typesOfNodeAsSet(Graph graph, Node node) {
}

// ---- RDF list.
// Most ways to call GList.xxx

/** Return a java list for an RDF list of data. */
/** @deprecated use {@link #listMembers(Graph, Node)} */
@Deprecated(forRemoval = true)
public static List<Node> rdfList(Graph graph, Node node) {
return listMembers(graph, node);
}

/** Return a java list of an RDF list of data. */
public static List<Node> listMembers(Graph graph, Node node) {
Objects.requireNonNull(graph, "graph");
Objects.requireNonNull(node, "node");
List<Node> nodes = GList.members(graph, node);
return nodes;
}

/** Return a the length of an RDF list. */
/** Return a the length of an RDF list. Return -1 if it is not a list. */
public static int listLength(Graph graph, Node node) {
Objects.requireNonNull(graph, "graph");
Objects.requireNonNull(node, "node");
Expand All @@ -523,6 +530,20 @@ public static int listLength(Graph graph, Node node) {
return (int)GList.listLength(graph, node);
}

/**
* Test whether node looks like a list (RDF Collection).
* The test is whether node is {@code rdf:nil}
* or has exactly one of each of {@code rdf:first}
* and {@code rdf:next}.
* Multiple occurrences of {@code rdf:first}
* or {@code rdf:next} are considered errors.
*/
public static boolean isList(Graph graph, Node node) {
Objects.requireNonNull(graph, "graph");
Objects.requireNonNull(node, "node");
return GList.isListNode(graph, node);
}

/**
* Return a java list where the {@code node} is an RDF list of nodes,
* of if the node is not a list, return the node as a list of one.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private void parseEntries() {
Node entriesNode = G.getZeroOrOneSP(manifestGraph, manifest, TestManifest.entries.asNode());
if ( entriesNode == null )
return;
List<Node> items = G.rdfList(manifestGraph, entriesNode);
List<Node> items = G.listMembers(manifestGraph, entriesNode);

List<Node> missingEntries = items.stream().filter(entry -> ! G.contains(manifestGraph, entry, null, null)).toList();
if ( ! missingEntries.isEmpty() ) {
Expand Down Expand Up @@ -167,7 +167,7 @@ private void parseOneIncludesList(Node r)
if ( ! r.isBlank() )
return;
// Blank node - assumed to be a list.
List<Node> includes = G.rdfList(manifestGraph, r);
List<Node> includes = G.listMembers(manifestGraph, r);
for ( Node inc : includes ) {
if ( inc.isBlank() || inc.isURI() ) {
parseOneIncludesList(inc);
Expand Down
Loading