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

org.aksw.sparqlify.sparqlview.SparqlViewInstance Maven / Gradle / Ivy

There is a newer version: 3.17.0-1
Show newest version
package org.aksw.sparqlify.sparqlview;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.aksw.commons.collections.multimaps.IBiSetMultimap;
import org.aksw.jena_sparql_api.restriction.RestrictionImpl;
import org.aksw.jena_sparql_api.views.SparqlView;
import org.aksw.jena_sparql_api.views.TwoWayBinding;
import org.aksw.jena_sparql_api.views.VarDef;
import org.apache.jena.graph.Node;
import org.apache.jena.sparql.core.Quad;
import org.apache.jena.sparql.core.QuadPattern;
import org.apache.jena.sparql.core.Var;
import org.apache.jena.sparql.expr.E_Equals;
import org.apache.jena.sparql.expr.Expr;
import org.apache.jena.sparql.expr.ExprVar;
import org.apache.jena.sparql.expr.NodeValue;

import com.google.common.collect.BiMap;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.SetMultimap;

public class SparqlViewInstance
    extends ViewInstanceOld
{
    private SparqlView instance; // The substituted view
    private SparqlView parent; // The view this instance was created from


    public SparqlViewInstance(Quad queryQuad, Quad viewQuad, int instanceId, int subId, SparqlView parent, TwoWayBinding binding)
    {
        super(queryQuad, viewQuad, instanceId, subId, binding);

        this.parent = parent;
        if(true) {
            throw new RuntimeException("If we come here, we need to fix the following line");
        }
        //this.renamer = RdfViewSystemOld.createVariableMappingInstance(parent, instanceId);

        // TODO ugly copying
        this.binding = binding.copySubstitute(renamer);
        this.instance = parent.copySubstitute(renamer);
    }

    public SparqlViewInstance copy() {
        return new SparqlViewInstance(queryQuad, viewQuad, instanceId, subId, parent, binding.copySubstitute(null));
    }

    public SparqlView getInstance()
    {
        return instance;
    }

    public SparqlView getParent()
    {
        return parent;
    }


    /**
     * For a view variable, return its defining SQL expression.
     *
     * @param var
     * @return
     */
    public Expr getDefiningExpr(Var var) {
        Node parentName = renamer.inverse().get(var);
        return parent.getBinding().get(parentName);
    }

    /**
     * For a query variable, get all defining expressions,
     * taking equivalences into account
     *
     * The question is, whether it makes sense to
     * also treat constants as defining expressions.
     *
     * I guess yes, since a view quad such as
     * ?s rdf:type ?o
     * could be rewritten as
     * ?s ?p ?o . with ?p = rdf:type
     *
     *
     * @param var
     * @return
     */
    public List getInferredDefiningExprs(Var var) {
        List result = new ArrayList();

        for(Var e : binding.getEquiMap().getEquivalences(var, true)) {
            Expr definingExpr = getDefiningExpr(e);
            if(definingExpr != null) {
                result.add(definingExpr);
            }
        }

        Node constant = binding.getEquiMap().getKeyToValue().get(var);
        if(constant != null) {
            result.add(new E_Equals(new ExprVar(var), NodeValue.makeNode(constant)));
        }

        return result;
    }


    public boolean isViewVariable(Var var) {
        return var.getName().startsWith("view");
    }


    /**
     * Returns a map from query level to the parent, so the instance level
     * is omitted.
     *
     *
     * @return
     */
    public SetMultimap getQueryToParentBinding()
    {
        SetMultimap result = HashMultimap.create();
        for(Entry entry : binding.getEquiMap().getEquivalences().entries()) {
            Var back = (Var)renamer.inverse().get(entry.getValue());
            if(back == null) {
                continue;
            }

            result.put(entry.getKey(), back);
        }

        return result;
    }

    /**
     * How the query variables map to the parent view
     * Given a query with var a, and a virtual graph with ?s:
     *
     * e.g. {?view1_s=[?a]}
     *
     *
     * @return
     */
    public SetMultimap getParentToQueryBinding()
    {
        SetMultimap result = HashMultimap.create();
        for(Entry entry : binding.getEquiMap().getEquivalences().entries()) {
            Var back = (Var)renamer.inverse().get(entry.getValue());
            if(back == null) {
                continue;
            }

            result.put(back, entry.getKey());
        }

        return result;
    }


    public QuadPattern getQueryQuads()
    {
        return queryQuads;
    }

    public QuadPattern getViewQuads()
    {
        return viewQuads;
    }

    public Quad getQueryQuad()
    {
        return queryQuad;
    }
    public Quad getViewQuad()
    {
        return queryQuad;
    }
    public int getInstanceId()
    {
        return instanceId;
    }
    public BiMap getRenamer()
    {
        return renamer;
    }
    public TwoWayBinding getBinding()
    {
        return binding;
    }


    public Multimap getSqlBinding()
    {
        Multimap result = HashMultimap.create();
        Map parentBinding = parent.getBinding();
        for(Entry entry : parentBinding.entrySet()) {
            Var node = (Var)renamer.get(entry.getKey());

            RestrictionImpl r = parent.getRestrictions().getRestriction((Var)entry.getKey());

            IBiSetMultimap reverse = binding.getEquiMap().getEquivalences().getInverse();
            Set queryVars = reverse.get(node);

            for(Var queryVar : queryVars) {

                result.put(queryVar, new VarDef(entry.getValue(), r));
            }
        }

        return result;
    }

    /*
    public Multimap getSqlBinding()
    {
        Multimap result = HashMultimap.create();
        Map parentBinding = parent.getBinding();
        for(Entry entry : parentBinding.entrySet()) {
            Node node = renamer.get(entry.getKey());

            IBiSetMultimap reverse = binding.getEquiMap().getEquivalences().getInverse();
            Set queryVars = reverse.get(node);

            for(Var queryVar : queryVars) {
                result.put(queryVar, entry.getValue());
            }
        }

        return result;
    }
    */


    @Override
    public String toString()
    {
        //return "ViewBinding\n\tview=" + instance +"\n\tquad=" + queryQuad + "\n\tbinding=" + binding;
        return parent.getName() + " " + queryQuad + " " + binding;
        //return "ViewBinding\n\t" + instance +"\n\t" + queryQuad + "\n\t" + binding;
    }


    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + instanceId;
        result = prime * result + ((parent == null) ? 0 : parent.hashCode());
        result = prime * result + subId;
        return result;
    }


    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        SparqlViewInstance other = (SparqlViewInstance) obj;
        if (instanceId != other.instanceId)
            return false;
        if (parent == null) {
            if (other.parent != null)
                return false;
        } else if (!parent.equals(other.parent))
            return false;
        if (subId != other.subId)
            return false;
        return true;
    }
}






© 2015 - 2025 Weber Informatics LLC | Privacy Policy