Skip to content

Commit

Permalink
Issue #58
Browse files Browse the repository at this point in the history
  • Loading branch information
tviegut committed Jan 3, 2023
1 parent 1239b76 commit 47e5427
Show file tree
Hide file tree
Showing 5 changed files with 532 additions and 0 deletions.
33 changes: 33 additions & 0 deletions Kena/src/au/com/langdale/kena/rdf/model/impl/SortedModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package au.com.langdale.kena.rdf.model.impl;

import com.hp.hpl.jena.enhanced.BuiltinPersonalities;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.rdf.model.impl.ModelCom;

/**
* Common methods for model implementations.
*
* <P>
* This class implements common methods, mainly convenience methods, for model
* implementations. It is intended use is as a base class from which model
* implemenations can be derived.
* </P>
*
* @author bwm hacked by Jeremy, tweaked by Chris (May 2002 - October 2002)
*/

public class SortedModel extends ModelCom {

public SortedModel(Model model) {
super(model.getGraph(), BuiltinPersonalities.model);
}

public StmtIterator listStatements(Resource s, Property p, RDFNode o) {
return new SortedStmtIteratorImpl(super.listStatements(s, p, o));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package au.com.langdale.kena.rdf.model.impl;

import java.util.Comparator;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.impl.StmtIteratorImpl;

/**
* An implementation of StmtIterator.
*
* @author bwm
* @version Release='$Name: $' Revision='$Revision: 1.1 $' Date='$Date:
* 2009/06/29 08:55:32 $'
*/

public class SortedStmtIteratorImpl extends StmtIteratorImpl {

public SortedStmtIteratorImpl(Iterator<Statement> iterator) {
super(initialize(iterator));
}

protected static Iterator<Statement> initialize(Iterator<Statement> iterator) {
Comparator<Statement> comparator = new Comparator<Statement>() {
@Override
public int compare(Statement statement1, Statement statement2) {
return statement1.toString().compareTo(statement2.toString());
}
};

Set<Statement> sortedSet = new TreeSet<Statement>(comparator);
while (iterator.hasNext()) {
sortedSet.add(iterator.next());
}

return sortedSet.iterator();
}
}
150 changes: 150 additions & 0 deletions Kena/src/com/hp/hpl/jena/graph/query/SimpleQueryHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
(c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Hewlett-Packard Development Company, LP
[See end of file]
$Id: SimpleQueryHandler.java,v 1.1 2009/06/29 08:55:45 castagna Exp $
*/

package com.hp.hpl.jena.graph.query;

import java.util.Comparator;
import java.util.Set;

import com.hp.hpl.jena.graph.Graph;
import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.util.CollectionFactory;
import com.hp.hpl.jena.util.iterator.ClosableIterator;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
import com.hp.hpl.jena.util.iterator.WrappedIterator;

/**
* A SimpleQueryHandler is a more-or-less straightforward implementation of
* QueryHandler suitable for use on graphs with no special query engines.
*
* @author kers
*/

public class SimpleQueryHandler implements QueryHandler {
/** the Graph this handler is working for */
protected Graph graph;

/** make an instance, remember the graph */
public SimpleQueryHandler(Graph graph) {
this.graph = graph;
}

public Stage patternStage(Mapping map, ExpressionSet constraints, Triple[] t) {
return new PatternStage(graph, map, constraints, t);
}

public BindingQueryPlan prepareBindings(Query q, Node[] variables) {
return new SimpleQueryPlan(graph, q, variables);
}

public TreeQueryPlan prepareTree(Graph pattern) {
return new SimpleTreeQueryPlan(graph, pattern);
}

public ExtendedIterator<Node> objectsFor(Node s, Node p) {
return objectsFor(graph, s, p);
}

public ExtendedIterator<Node> subjectsFor(Node p, Node o) {
return subjectsFor(graph, p, o);
}

public ExtendedIterator<Node> predicatesFor(Node s, Node o) {
return predicatesFor(graph, s, o);
}

public static ExtendedIterator<Node> objectsFor(Graph g, Node s, Node p) {
Comparator<Node> comparator = new Comparator<Node>() {
@Override
public int compare(Node node1, Node node2) {
return node1.toString().compareTo(node2.toString());
}
};
Set<Node> objects = CollectionFactory.createSortedSet(comparator);

ClosableIterator<Triple> it = g.find(s, p, Node.ANY);

while (it.hasNext())
objects.add(it.next().getObject());

return WrappedIterator.createNoRemove(objects.iterator());
}

public static ExtendedIterator<Node> subjectsFor(Graph g, Node p, Node o) {
Comparator<Node> comparator = new Comparator<Node>() {
@Override
public int compare(Node node1, Node node2) {
return node1.toString().compareTo(node2.toString());
}
};
Set<Node> objects = CollectionFactory.createSortedSet(comparator);

ClosableIterator<Triple> it = g.find(Node.ANY, p, o);

while (it.hasNext())
objects.add(it.next().getSubject());

return WrappedIterator.createNoRemove(objects.iterator());
}

public static ExtendedIterator<Node> predicatesFor(Graph g, Node s, Node o) {
Comparator<Node> comparator = new Comparator<Node>() {
@Override
public int compare(Node node1, Node node2) {
return node1.toString().compareTo(node2.toString());
}
};
Set<Node> predicates = CollectionFactory.createSortedSet(comparator);

ClosableIterator<Triple> it = g.find(s, Node.ANY, o);

while (it.hasNext())
predicates.add(it.next().getPredicate());

return WrappedIterator.createNoRemove(predicates.iterator());
}

/**
* this is a simple-minded implementation of containsNode that uses find up
* to three times to locate the node. Almost certainly particular graphs
* will be able to offer better query-handlers ...
*/
public boolean containsNode(Node n) {
return graph.contains(n, Node.ANY, Node.ANY)
|| graph.contains(Node.ANY, n, Node.ANY)
|| graph.contains(Node.ANY, Node.ANY, n);
}
}

/*
* (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Hewlett-Packard
* Development Company, LP All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
107 changes: 107 additions & 0 deletions Kena/src/com/hp/hpl/jena/util/CollectionFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
(c) Copyright 2004, 2005, 2006, 2007, 2008, 2009 Hewlett-Packard Development Company, LP, all rights reserved.
[See end of file]
$Id: CollectionFactory.java,v 1.1 2009/06/29 08:55:47 castagna Exp $
*/
package com.hp.hpl.jena.util;

import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

/**
* CollectionFactory - a central place for allocating sets and maps, mostly so
* that it's easy to plug in new implementations (eg trove).
*
* @author kers
*/
public class CollectionFactory {
/**
* Answer a new Map which uses hashing for lookup.
*/
public static <K, V> Map<K, V> createHashedMap() {
return new HashMap<K, V>();
}

/**
* Answer a new Map which uses hashing for lookup and has initial size
* <code>size</code>.
*/
public static <K, V> Map<K, V> createHashedMap(int size) {
return new HashMap<K, V>(size);
}

/**
* Answer a new Map which uses hashing for lookup and is initialised to be a
* copy of <code>toCopy</code>.
*/
public static <K, V> Map<K, V> createHashedMap(Map<K, V> toCopy) {
return new HashMap<K, V>(toCopy);
}

/**
* Answer a new Set which uses haashing for lookup.
*/
public static <T> Set<T> createHashedSet() {
return new HashSet<T>();
}

/**
* Answer a new Set which uses hashing for lookup and is initialised as a
* copy of <code>toCopy</code>.
*/
public static <T> Set<T> createHashedSet(Collection<T> toCopy) {
return new HashSet<T>(toCopy);
}

/**
* Answer a new Set which sorts for lookup.
*/
public static <T> Set<T> createSortedSet(Comparator<T> comparator) {
return new TreeSet<T>(comparator);
}

/**
* Answer a new Set which uses hashing for lookup and is initialised as a
* copy of <code>toCopy</code>.
*/
public static <T> Set<T> createSortedSet(Comparator<T> comparator,
Collection<T> toCopy) {
Set<T> sortedSet = new TreeSet<T>(comparator);
sortedSet.addAll(toCopy);
return sortedSet;
}
}

/*
* (c) Copyright 2004, 2005, 2006, 2007, 2008, 2009 Hewlett-Packard Development
* Company, LP All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
Loading

0 comments on commit 47e5427

Please sign in to comment.