Skip to content
Closed
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
12 changes: 6 additions & 6 deletions jena-arq/src/main/java/org/apache/jena/riot/lang/LangEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@

import static org.apache.jena.riot.tokens.TokenType.EOF ;
import static org.apache.jena.riot.tokens.TokenType.NODE ;

import org.apache.jena.atlas.AtlasException ;
import org.apache.jena.atlas.iterator.PeekIterator ;
import org.apache.jena.graph.Node ;
import org.apache.jena.riot.RiotParseException ;
import org.apache.jena.riot.system.ErrorHandler ;
import org.apache.jena.riot.system.ParserProfile ;
Expand Down Expand Up @@ -117,11 +117,11 @@ protected final Token nextToken()
}
}

protected final Node scopedBNode(Node scopeNode, String label)
{
return profile.getLabelToNode().get(scopeNode, label) ;
}

// protected final Node scopedBNode(Node scopeNode, String label)
// {
// return profile.getLabelToNode().get(scopeNode, label) ;
// }
//
protected final void expectOrEOF(String msg, TokenType tokenType)
{
// DOT or EOF
Expand Down
56 changes: 56 additions & 0 deletions jena-arq/src/main/java/org/apache/jena/riot/system/FactoryRDF.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.jena.riot.system;

import org.apache.jena.datatypes.RDFDatatype ;
import org.apache.jena.graph.Graph ;
import org.apache.jena.graph.Node ;
import org.apache.jena.graph.Triple ;
import org.apache.jena.sparql.core.DatasetGraph ;
import org.apache.jena.sparql.core.Quad ;

/**
* Create core RDF objects: {@link Node}s, {@link Triple}s, {@link Quad}s,
* {@link Graph}, {@link DatasetGraph}s.
* <p>
*/
public interface FactoryRDF {
// ?? Are these too varied?
public Graph createGraph() ;
// ?? Are these too varied?
public DatasetGraph createDatasetGraph() ;
public Triple createTriple(Node subject, Node predicate, Node object) ;
public Quad createQuad(Node graph, Node subject, Node predicate, Node object) ;
public Node createURI(String uriStr) ;
public Node createTypedLiteral(String lexical, RDFDatatype datatype) ;
public Node createLangLiteral(String lexical, String langTag) ;
public Node createStringLiteral(String lexical) ;
/** Create a blank node */
public Node createBlankNode() ;
/** Create a blank node with the given string as internal system id */
public Node createBlankNode(String label) ;
/** Create a blank with the internal system id taken from 128 bit number provided.
*/
public Node createBlankNode(long mostSigBits, long leastSigBits) ;

// // Object for scope better?
// public Node createBlankNode(Node scope, String label) ;
// // Object for scope better?
// public Node createBlankNode(Node scope) ;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.jena.riot.system;

import java.util.concurrent.ExecutionException ;

import org.apache.jena.ext.com.google.common.cache.Cache ;
import org.apache.jena.atlas.lib.cache.CacheInfo ;
import org.apache.jena.datatypes.RDFDatatype ;
import org.apache.jena.datatypes.xsd.XSDDatatype ;
import org.apache.jena.ext.com.google.common.cache.CacheBuilder ;
import org.apache.jena.ext.com.google.common.cache.CacheStats ;
import org.apache.jena.graph.Node ;
import org.apache.jena.riot.RiotException ;
import org.apache.jena.riot.lang.LabelToNode ;
import org.apache.jena.sparql.graph.NodeConst ;

/** Adds some caching of created nodes - the caching is tuned to RIOT parser usage */
public class FactoryRDFCaching extends FactoryRDFStd {
public static final int DftNodeCacheSize = 5000 ;

// Control the setup - for one thread; start size = 50% of full size, no stats
private final Cache<String, Node> cache ;

public FactoryRDFCaching() {
this(DftNodeCacheSize) ;
}

public FactoryRDFCaching(int cacheSize) {
super() ;
cache = setCache(cacheSize) ;
}

private Cache<String, Node> setCache(int cacheSize) {
return CacheBuilder.newBuilder()
.maximumSize(cacheSize)
.initialCapacity(cacheSize/2)
//.recordStats()
.concurrencyLevel(1)
.build() ;
}

public FactoryRDFCaching(int cacheSize, LabelToNode labelMapping) {
super(labelMapping) ;
cache = setCache(cacheSize) ;
}

@Override
public Node createURI(String uriStr) {
try {
return cache.get(uriStr, ()->RiotLib.createIRIorBNode(uriStr)) ;
}
catch (ExecutionException e) {
throw new RiotException("Execution exception filling cache <"+uriStr+">", e) ;
}
}

// A few constants

@Override
public Node createTypedLiteral(String lexical, RDFDatatype datatype) {
if ( XSDDatatype.XSDinteger.equals(datatype) ) {
switch(lexical) {
case "0" : return NodeConst.nodeZero ;
case "1" : return NodeConst.nodeOne ;
case "2" : return NodeConst.nodeTwo ;
case "-1" : return NodeConst.nodeMinusOne ;
}
// fallthrough.
} else if ( XSDDatatype.XSDboolean.equals(datatype) ) {
switch(lexical) {
case "true" : return NodeConst.nodeTrue ;
case "false" : return NodeConst.nodeFalse ;
}
// fallthrough.
}
return super.createTypedLiteral(lexical, datatype) ;
}

@Override
public Node createStringLiteral(String lexical) {
if ( lexical.isEmpty() )
return NodeConst.emptyString ;
return super.createStringLiteral(lexical) ;
}

public CacheInfo stats() {
CacheStats stats = cache.stats() ;
if ( stats.missCount() == 0 && stats.hitCount() == 0 )
// Stats not enabled - all counts zero.
return null ;
return new CacheInfo(DftNodeCacheSize, stats) ;
}
}
108 changes: 108 additions & 0 deletions jena-arq/src/main/java/org/apache/jena/riot/system/FactoryRDFStd.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.jena.riot.system;

import org.apache.jena.datatypes.RDFDatatype ;
import org.apache.jena.graph.Graph ;
import org.apache.jena.graph.Node ;
import org.apache.jena.graph.NodeFactory ;
import org.apache.jena.graph.Triple ;
import org.apache.jena.riot.lang.LabelToNode ;
import org.apache.jena.sparql.core.DatasetGraph ;
import org.apache.jena.sparql.core.DatasetGraphFactory ;
import org.apache.jena.sparql.core.Quad ;
import org.apache.jena.sparql.graph.GraphFactory ;

public class FactoryRDFStd implements FactoryRDF {
// Needs reset?
private final LabelToNode labelMapping ;

public FactoryRDFStd() {
this(SyntaxLabels.createLabelToNode()) ;
}

public FactoryRDFStd(LabelToNode labelMapping) {
this.labelMapping = labelMapping ;
}

@Override
public Graph createGraph() {
return GraphFactory.createDefaultGraph() ;
}

@Override
public DatasetGraph createDatasetGraph() {
return DatasetGraphFactory.create(); // createTxnMem() ;
}

@Override
public Triple createTriple(Node subject, Node predicate, Node object) {
return Triple.create(subject, predicate, object);
}

@Override
public Quad createQuad(Node graph, Node subject, Node predicate, Node object) {
return Quad.create(graph, subject, predicate, object) ;
}

@Override
public Node createURI(String uriStr) {
return RiotLib.createIRIorBNode(uriStr) ;
//return NodeFactory.createURI(uriStr) ;
}

@Override
public Node createTypedLiteral(String lexical, RDFDatatype datatype) {
return NodeFactory.createLiteral(lexical, datatype) ;
}

@Override
public Node createLangLiteral(String lexical, String langTag) {
return NodeFactory.createLiteral(lexical, langTag) ;
}

@Override
public Node createStringLiteral(String lexical) {
return NodeFactory.createLiteral(lexical) ;
}

@Override
public Node createBlankNode(long mostSigBits, long leastSigBits) {
// XXX Style: Do this fast. Guava? Apache commons? Special case for char[32]
// (Eventually, blank node Nodes will have two longs normally.)
return createBlankNode(String.format("%08X%08X", mostSigBits, leastSigBits)) ;
}

// Fixed scope.
private static Node scope = NodeFactory.createURI("urn:jena:global_scope") ;
// Scope
@Override
public Node createBlankNode(String label) {
//return NodeFactory.createBlankNode(label) ;
return labelMapping.get(scope, label) ;
}

// Scope
@Override
public Node createBlankNode() {
//return NodeFactory.createBlankNode() ;
return labelMapping.create() ;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@

package org.apache.jena.riot.system;


import org.apache.jena.datatypes.RDFDatatype ;
import org.apache.jena.graph.Node ;
import org.apache.jena.graph.Triple ;
import org.apache.jena.iri.IRI ;
import org.apache.jena.riot.lang.LabelToNode ;
import org.apache.jena.riot.tokens.Token ;
import org.apache.jena.sparql.core.Quad ;

Expand Down Expand Up @@ -55,21 +53,23 @@ public interface ParserProfile
/** Create a fresh blank node */
public Node createBlankNode(Node scope, long line, long col) ;

/** Make a node from a token - called after all else has been tried - return null for no such node */
/** Make a node from a token - called after all else has been tried to handle special cases
* Return null for "no special node recoginzed"
*/
public Node createNodeFromToken(Node scope, Token token, long line, long col) ;

/** Make any node from a token as appropriate */
public Node create(Node currentGraph, Token token) ;

public LabelToNode getLabelToNode() ;
public void setLabelToNode(LabelToNode labelToNode) ;


public ErrorHandler getHandler() ;
public void setHandler(ErrorHandler handler) ;

public Prologue getPrologue() ;
public void setPrologue(Prologue prologue) ;

public FactoryRDF getFactoryRDF() ;
public void setFactoryRDF(FactoryRDF factory) ;

public boolean isStrictMode() ;
public void setStrictMode(boolean mode) ;
}
Loading