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

com.bigdata.rdf.sparql.ast.BindingsClause Maven / Gradle / Ivy

/**

Copyright (C) SYSTAP, LLC DBA Blazegraph 2006-2016.  All rights reserved.

Contact:
     SYSTAP, LLC DBA Blazegraph
     2501 Calvert ST NW #106
     Washington, DC 20008
     [email protected]

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
/*
 * Created on Feb 29, 2012
 */

package com.bigdata.rdf.sparql.ast;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.bigdata.bop.BOp;
import com.bigdata.bop.IBindingSet;
import com.bigdata.bop.IVariable;

/**
 * The solutions declared by a BINDINGS clause.
 *  
 * @author Bryan Thompson
 * @version $Id$
 */
public class BindingsClause extends GroupMemberNodeBase 
        implements IBindingProducerNode, IJoinNode {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    
    public interface Annotations extends ASTBase.Annotations, 
            IJoinNode.Annotations {

        /**
         * The ordered set of declared variables for which there MIGHT be a
         * binding in any given solution.
         */
        String DECLARED_VARS = "declaredVars";

        /**
         * The binding sets.
         */
        String BINDING_SETS = "bindingSets";

    }
    
    /**
     * Deep copy constructor.
     * @param bindings
     */
    public BindingsClause(final BindingsClause bindings) {
        
        super(bindings);
        
    }

    public BindingsClause(final BOp[] args, final Map anns) {

        super(args, anns);
        
    }

    /**
     * 
     * @param declaredVars
     *            The ordered set of declared variables.
     * @param bindingSets
     *            The set of solutions.
     */
    public BindingsClause(final LinkedHashSet> declaredVars,
            final List bindingSets) {

        super(NOARGS, new HashMap(2));

        if (declaredVars == null)
            throw new IllegalArgumentException();

        if (bindingSets == null)
            throw new IllegalArgumentException();

        setDeclaredVariables(declaredVars);

        setBindingSets(bindingSets);
                
    }

    /**
     * Return the #of declared variables.
     */
    public final int getDeclaredVariableCount() {
        
        return getDeclaredVariables().size();
        
    }
    
    /**
     * Return the ordered set of declared variables for the BINDINGS clause. The
     * declared variables MIGHT have a binding in any given solution, but there
     * is no guarantee that any given variable is ever bound within a solution.
     */
    @SuppressWarnings("unchecked")
    public final LinkedHashSet> getDeclaredVariables() {

        return (LinkedHashSet>) getProperty(Annotations.DECLARED_VARS);

    }

    public final void setDeclaredVariables(
            final LinkedHashSet> declaredVars) {

        setProperty(Annotations.DECLARED_VARS, declaredVars);

    }

    /**
     * Return the #of binding sets.
     */
    public final int getBindingSetsCount() {

        final List bindingSets = getBindingSets();

        if (bindingSets == null)
            return 0;

        return bindingSets.size();

    }

    /**
     * The binding sets -or- null.
     */
    @SuppressWarnings("unchecked")
    public final List getBindingSets() {

        return (List) getProperty(Annotations.BINDING_SETS);

    }

    public final void setBindingSets(final List bindingSets) {

        setProperty(Annotations.BINDING_SETS, bindingSets);

    }

    @Override
    public String toString(final int indent) {

        final LinkedHashSet> declaredVars = getDeclaredVariables();
        
        final List bindingSets = getBindingSets();
        
        final String s = indent(indent);

        final String s1 = indent(indent + 1);

        final StringBuilder sb = new StringBuilder();

        sb.append("\n");
        
        sb.append(s);
        
        sb.append("BindingsClause");

        for(IVariable var : declaredVars) {
            
            sb.append(" ?");
            
            sb.append(var.getName());
            
        }

        sb.append("\n");

        sb.append(s);

        sb.append("{");
        
        if (bindingSets.size() <= 10) {
        	
	        for(IBindingSet bset : bindingSets) {
	        
	            sb.append("\n");
	            
	            sb.append(s1);
	            
	            sb.append(bset.toString());
	        
	        }
	        
        } else {
        	
            sb.append("\n");
            
            sb.append(s1);
            
            sb.append("[ count=" + bindingSets.size() + " ]");
        	
        }
        
        sb.append("\n");

        sb.append(s);

        sb.append("}");

        return sb.toString();
        
    }
    
    @Override
    final public List getAttachedJoinFilters() {

        @SuppressWarnings("unchecked")
        final List filters = (List) getProperty(Annotations.FILTERS);

        if (filters == null) {

            return Collections.emptyList();

        }

        return Collections.unmodifiableList(filters);

    }

    @Override
    final public void setAttachedJoinFilters(final List filters) {

        setProperty(Annotations.FILTERS, filters);

    }

    @Override
    public boolean isOptional() {
        return false;
    }

    @Override
    public boolean isMinus() {
        return false;
    }

    @Override
    public Set> getRequiredBound(StaticAnalysis sa) {
       return new HashSet>();
    }

    @Override
    public Set> getDesiredBound(StaticAnalysis sa) {
       return new HashSet>();
    }    
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy