All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mindswap.pellet.jena.graph.query.TripleQueryHandler Maven / Gradle / Ivy

The newest version!
// Copyright (c) 2006 - 2008, Clark & Parsia, LLC. 
// This source code is available under the terms of the Affero General Public License v3.
//
// Please see LICENSE.txt for full license terms, including the availability of proprietary exceptions.
// Questions, comments, or requests for clarification: [email protected]

package org.mindswap.pellet.jena.graph.query;

import static org.mindswap.pellet.utils.iterator.IteratorUtils.flatten;

import java.util.Collection;
import java.util.Iterator;
import java.util.Set;

import org.mindswap.pellet.KnowledgeBase;
import org.mindswap.pellet.jena.JenaUtils;
import org.mindswap.pellet.jena.PelletInfGraph;
import org.mindswap.pellet.jena.graph.loader.GraphLoader;

import aterm.ATermAppl;

import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
import com.hp.hpl.jena.util.iterator.Map1;
import com.hp.hpl.jena.util.iterator.WrappedIterator;

public abstract class TripleQueryHandler {
	public TripleQueryHandler() {
	}
	
	public abstract boolean contains(KnowledgeBase kb, GraphLoader loader, Node subj, Node pred, Node obj);
	
	public abstract ExtendedIterator find(KnowledgeBase kb, PelletInfGraph pellet, Node subj, Node pred, Node obj);
	
	protected ExtendedIterator objectFiller(Node s, Node p, Collection objects) {
		return objectFiller( s, p, objects.iterator() );
	}

	protected ExtendedIterator objectFiller(final Node s, final Node p, final Iterator objects) {
		Map1 map = new Map1() {
		    public Triple map1( ATermAppl o ) {
		         return Triple.create( s, p, JenaUtils.makeGraphNode( o ) );
		    }
		};
		
		return WrappedIterator.create( objects ).mapWith( map );	
	}
	
	protected ExtendedIterator objectSetFiller(Node s, Node p, Set> objectSets) {
		return objectFiller( s, p, flatten( objectSets.iterator() ) );
	}
	
	protected ExtendedIterator propertyFiller(Node s, Collection properties, Node o) {
		return propertyFiller( s, properties.iterator(), o );
	}
	
	protected ExtendedIterator propertyFiller(final Node s, final Iterator properties, final Node o) {
		Map1 map = new Map1() {
		    public Triple map1( ATermAppl p ) {
		         return Triple.create( s, JenaUtils.makeGraphNode( p ), o );
		    }
		};
		
		return WrappedIterator.create( properties ).mapWith( map );	
	}
	
	protected ExtendedIterator subjectFiller(Collection subjects, Node p, Node o) {
		return subjectFiller( subjects.iterator(), p, o );
	}
	
	protected ExtendedIterator subjectFiller(final Iterator subjects, final Node p, final Node o) {
		Map1 map = new Map1() {
		    public Triple map1( ATermAppl s ) {
		         return Triple.create( JenaUtils.makeGraphNode( s ), p, o );
		    }
		};
		
		return WrappedIterator.create( subjects ).mapWith( map );	
	}

	protected ExtendedIterator subjectSetFiller(Set> subjectSets, Node p, Node o) {
		return subjectFiller( flatten( subjectSets.iterator() ), p, o );
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy