/*
* (c) Copyright 2006 Hewlett-Packard Development Company, LP
* All rights reserved.
* [See end of file]
*/
package com.hp.hpl.squirrelrdf.querymap;
import java.util.Iterator;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.query.core.Binding;
import com.hp.hpl.jena.query.engine.QueryIterator;
import com.hp.hpl.jena.query.engine.QueryIteratorBase;
import com.hp.hpl.jena.query.engine1.ExecutionContext;
/**
*
* Continues an existing query iterator with the contents of a list of maps.
*
* @author pldms
*
*/
public class QueryMapIterator extends QueryIteratorBase
{
final static Log log = LogFactory.getLog(QueryMapIterator.class);
private QueryIterator existingBindings;
private Binding existingBinding;
private Iterator<Map<String, Node>> newBindings;
private Binding newBinding;
private QueryMapEngine query;
private boolean hasMore;
private ExecutionContext context;
public QueryMapIterator(QueryMapEngine query, QueryIterator existingBindings, ExecutionContext context)
{
this.query = query;
this.existingBindings = existingBindings;
this.hasMore = true;
this.newBindings = null;
this.context = context;
calcHasMore();
}
/**
* Work out if there is anything else to do.
*
*/
private void calcHasMore()
{
if (!hasMore) return;
if (existingBinding == null) // initial state
{
if (!existingBindings.hasNext()) // why even bother me?
{
log.debug("Bailing very early...");
hasMore = false;
existingBindings.close();
return;
}
existingBinding = existingBindings.nextBinding();
newBindings = query.execute(existingBinding, context);
log.debug("Initialising... I have something to do? " + newBindings.hasNext());
}
if (!newBindings.hasNext()) // Finished a round of new bindings
{
if (!existingBindings.hasNext()) // Exhausted old bindings, finished
{
log.trace("Finished everything...");
hasMore = false;
existingBindings.close();
return;
}
existingBinding = existingBindings.nextBinding();
log.trace("Executing new query");
newBindings = query.execute(existingBinding, context); // Get new results with this binding
calcHasMore(); // Recurse...
}
Map<String, Node> possBinding = newBindings.next();
// We now have have an existing binding and a new binding. Are they consistent?
// This might always be the case for some optimisations, but blunder on for the moment...
if (consistentWith(possBinding, existingBinding))
{
newBinding = new BindingMapAug(possBinding, existingBinding); // join up
}
else
{
log.debug("New binding inconsistent");
calcHasMore();
}
}
private boolean consistentWith(Map<String, Node> possBinding, Binding aBinding)
{
for (String var: possBinding.keySet())
{
if (aBinding.contains(var) && !possBinding.get(var).equals(aBinding.get(var)))
{
log.debug("Clash: " + possBinding.get(var) + "!=" + aBinding.get(var));
return false;
}
}
return true;
}
@Override
protected boolean hasNextBinding()
{
return hasMore;
}
@Override
protected Binding moveToNextBinding()
{
if (!hasMore) return null;
Binding toReturn = newBinding;
calcHasMore();
return toReturn;
}
@Override
protected void closeIterator()
{
hasMore = false;
}
}
/*
* (c) Copyright 2006 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.
*/