Skip to content

Commit

Permalink
Fix #190
Browse files Browse the repository at this point in the history
  • Loading branch information
enridaga committed Dec 26, 2021
1 parent c321c11 commit f2f9b59
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 92 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright (c) 2021 SPARQL Anything Contributors @ http://github.com/sparql-anything
*
* Licensed 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 com.github.sparqlanything.model;

import org.apache.jena.graph.Node;
import org.apache.jena.graph.NodeFactory;
import org.apache.jena.vocabulary.RDF;

import java.net.URI;
import java.util.Properties;

public abstract class BaseFacadeXBuilder implements FacadeXNodeBuilder, FacadeXQuadHandler, FacadeXComponentHandler {
protected final Properties properties;
protected final Node mainGraphName;
//
protected final boolean p_blank_nodes;
protected final String p_namespace;
protected final String p_root;
protected final boolean p_trim_strings;
protected final String p_null_string;

public BaseFacadeXBuilder(String resourceId, Properties properties) {
this.properties = properties;
this.mainGraphName = NodeFactory.createURI(resourceId);
this.p_blank_nodes = Triplifier.getBlankNodeArgument(properties);
this.p_namespace = Triplifier.getNamespaceArgument(properties);
this.p_root = Triplifier.getRootArgument(properties, resourceId);
this.p_trim_strings = Triplifier.getTrimStringsArgument(properties);
this.p_null_string = Triplifier.getNullStringArgument(properties);
}

public boolean addContainer(String dataSourceId, String containerId, String slotKey, String childContainerId) {
return add(NodeFactory.createURI(dataSourceId), container2node(containerId), key2predicate(slotKey),
container2node(childContainerId));
}

public boolean addContainer(String dataSourceId, String containerId, URI customKey, String childContainerId) {
return add(NodeFactory.createURI(dataSourceId), container2node(containerId),
NodeFactory.createURI(customKey.toString()), container2node(childContainerId));
}

public boolean addContainer(String dataSourceId, String containerId, Integer slotKey, String childContainerId) {
return add(NodeFactory.createURI(dataSourceId), container2node(containerId), RDF.li(slotKey).asNode(),
container2node(childContainerId));
}

public boolean addType(String dataSourceId, String containerId, String typeId) {
return add(NodeFactory.createURI(dataSourceId), container2node(containerId), RDF.type.asNode(),
NodeFactory.createURI(typeId));
}

public boolean addType(String dataSourceId, String containerId, URI type) {
return add(NodeFactory.createURI(dataSourceId), container2node(containerId), RDF.type.asNode(),
NodeFactory.createURI(type.toString()));
}

public boolean addValue(String dataSourceId, String containerId, String slotKey, Object value) {
return add(NodeFactory.createURI(dataSourceId), container2node(containerId), key2predicate(slotKey),
value2node(value));
}

public boolean addValue(String dataSourceId, String containerId, Integer slotKey, Object value) {
return add(NodeFactory.createURI(dataSourceId), container2node(containerId), RDF.li(slotKey).asNode(),
value2node(value));
}

public boolean addValue(String dataSourceId, String containerId, URI customKey, Object value) {
return add(NodeFactory.createURI(dataSourceId), container2node(containerId),
NodeFactory.createURI(customKey.toString()), value2node(value));
}

public boolean addRoot(String dataSourceId, String rootId) {
return add(NodeFactory.createURI(dataSourceId), container2node(rootId), RDF.type.asNode(),
NodeFactory.createURI(Triplifier.FACADE_X_TYPE_ROOT));
}

public Node container2node(String container) {
if (p_blank_nodes) {
return container2BlankNode(container);
//return NodeFactory.createBlankNode(container);
} else {
// return NodeFactory.createURI(container);
return container2URI(container);
}
}

public Node key2predicate(String key) {
return key2predicate(this.p_namespace, key);
}

public Node value2node(Object value) {
// trims_strings == true and if object is string, trim it
if (p_trim_strings && value instanceof String) {
value = ((String) value).trim();
}
return FacadeXNodeBuilder.super.value2node(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,14 @@
import java.nio.file.Files;
import java.util.Properties;

public class BaseFacadeXGraphBuilder implements FacadeXGraphBuilder {
public class BaseFacadeXGraphBuilder extends BaseFacadeXBuilder implements FacadeXGraphBuilder {
private static final String PROPERTY_ONDISK_REUSE = "ondisk.reuse";
private static final String PROPERTY_ONDISK = "ondisk";
protected static final Logger log = LoggerFactory.getLogger(TripleFilteringFacadeXGraphBuilder.class);
protected final Properties properties;
protected final Node mainGraphName;

// when using a TDB2 this is defined
protected static Dataset dataset = null; // TODO making this static is a kludge maybe
protected final DatasetGraph datasetGraph;
//
protected final boolean p_blank_nodes;
protected final String p_namespace;
protected final String p_root;
protected final boolean p_trim_strings;
protected final String p_null_string;
protected static String previousTDB2Path = "";

public BaseFacadeXGraphBuilder(String resourceId, Properties properties) {
Expand Down Expand Up @@ -106,19 +98,13 @@ public static DatasetGraph getDatasetGraph(Properties properties){
}

protected BaseFacadeXGraphBuilder(String resourceId, DatasetGraph ds, Properties properties) {
this.properties = properties;
this.mainGraphName = NodeFactory.createURI(resourceId);
super(resourceId, properties);
this.datasetGraph = BaseFacadeXGraphBuilder.getDatasetGraph(properties);

// the single place to begin write txns
log.debug("begin write txn");
this.datasetGraph.begin(TxnType.WRITE);

this.p_blank_nodes = Triplifier.getBlankNodeArgument(properties);
this.p_trim_strings = Triplifier.getTrimStringsArgument(properties);
this.p_null_string = Triplifier.getNullStringArgument(properties);
this.p_namespace = Triplifier.getNamespaceArgument(properties);
this.p_root = Triplifier.getRootArgument(properties, resourceId);
}

// @Deprecated
Expand All @@ -145,82 +131,6 @@ public boolean add(Node graph, Node subject, Node predicate, Node object) {
return true;
}

@Override
public boolean addContainer(String dataSourceId, String containerId, String slotKey, String childContainerId) {
return add(NodeFactory.createURI(dataSourceId), container2node(containerId), key2predicate(slotKey),
container2node(childContainerId));
}

@Override
public boolean addContainer(String dataSourceId, String containerId, URI customKey, String childContainerId) {
return add(NodeFactory.createURI(dataSourceId), container2node(containerId),
NodeFactory.createURI(customKey.toString()), container2node(childContainerId));
}

@Override
public boolean addContainer(String dataSourceId, String containerId, Integer slotKey, String childContainerId) {
return add(NodeFactory.createURI(dataSourceId), container2node(containerId), RDF.li(slotKey).asNode(),
container2node(childContainerId));
}

@Override
public boolean addType(String dataSourceId, String containerId, String typeId) {
return add(NodeFactory.createURI(dataSourceId), container2node(containerId), RDF.type.asNode(),
NodeFactory.createURI(typeId));
}

@Override
public boolean addType(String dataSourceId, String containerId, URI type) {
return add(NodeFactory.createURI(dataSourceId), container2node(containerId), RDF.type.asNode(),
NodeFactory.createURI(type.toString()));
}

@Override
public boolean addValue(String dataSourceId, String containerId, String slotKey, Object value) {
return add(NodeFactory.createURI(dataSourceId), container2node(containerId), key2predicate(slotKey),
value2node(value));
}

@Override
public boolean addValue(String dataSourceId, String containerId, Integer slotKey, Object value) {
return add(NodeFactory.createURI(dataSourceId), container2node(containerId), RDF.li(slotKey).asNode(),
value2node(value));
}

@Override
public boolean addValue(String dataSourceId, String containerId, URI customKey, Object value) {
return add(NodeFactory.createURI(dataSourceId), container2node(containerId),
NodeFactory.createURI(customKey.toString()), value2node(value));
}

@Override
public boolean addRoot(String dataSourceId, String rootId) {
return add(NodeFactory.createURI(dataSourceId), container2node(rootId), RDF.type.asNode(),
NodeFactory.createURI(Triplifier.FACADE_X_TYPE_ROOT));
}

public Node container2node(String container) {
if (p_blank_nodes) {
return FacadeXGraphBuilder.super.container2BlankNode(container);
//return NodeFactory.createBlankNode(container);
} else {
// return NodeFactory.createURI(container);
return FacadeXGraphBuilder.super.container2URI(container);
}
}

public Node key2predicate(String key) {
return FacadeXGraphBuilder.super.key2predicate(this.p_namespace, key);
}

public Node value2node(Object value) {
// trims_strings == true and if object is string, trim it
if(p_trim_strings && value instanceof String){
value = ((String)value).trim();
}
return FacadeXGraphBuilder.super.value2node(value);
}

/**
* This includes triples from the default graph / union of all graphs.
*
Expand Down

0 comments on commit f2f9b59

Please sign in to comment.