/**
  * Applies variable bindings, replacing the values of one map with
  * the values from a given variables map.
  * @param map  the Map to modify
  * @param bindings  the current variable bindings
  */
 public static void applyBindings(Dictionary <IResource, IResource> map, Dictionary <String, IResource> bindings)
 {
     foreach (IResource property in new List <IResource>(map.Keys))
     {
         IResource value = map[property];
         IVariable var   = SPINFactory.asVariable(value);
         if (var != null)
         {
             String varName = var.getName();
             if (bindings.ContainsKey(varName))
             {
                 IResource b = bindings[varName];
                 if (b != null)
                 {
                     map[property] = b;
                 }
             }
         }
     }
 }
        public void PrintEnhancedSPARQL(IResource resource)
        {
            if (resource == null)
            {
                return;
            }
            IEnumerable <IResource> elements = resource.AsList();

            foreach (Resource element in elements)
            {
                if (element.canAs(SP.ClassQuery))
                {
                    SPINFactory.asQuery(element).PrintEnhancedSPARQL(this);
                    continue;
                }
                else if (element.canAs(SP.ClassCommand))
                {
                    SPINFactory.asUpdate(element).PrintEnhancedSPARQL(this);
                    continue;
                }
                else if (element.canAs(SP.ClassVariable))
                {
                    SPINFactory.asVariable(element).PrintEnhancedSPARQL(this);
                    continue;
                }
                else if (element.isUri())
                {
                    printURIResource(element);
                    continue;
                }
                IElement asElement = SPINFactory.asElement(element);
                if (asElement != null)
                {
                    asElement.PrintEnhancedSPARQL(this);
                }
            }
        }
Exemple #3
0
        internal static string StringForNode(IResource node, INamespaceMapper pm)
        {
            SpinWrappedDataset model = null; // TODO change this for a queryModel
            StringBuilder      sb    = new StringBuilder();

            if (node.canAs(SP.ClassExpression))
            {
                ((IPrintable)SPINFactory.asExpression(node)).print(new BaseSparqlFactory(model, sb));
            }
            else if (node.canAs(SP.ClassVariable))
            {
                ((IPrintable)SPINFactory.asVariable(node)).print(new BaseSparqlFactory(model, sb));
            }
            else if (RDFUtil.sameTerm(node.getResource(RDF.PropertyType), SP.PropertyNot))
            {
                sb.Append("!(");
                sb.Append(StringForNode(node.getObject(SP.PropertyArg1), pm));
                sb.Append(")");
            }
            else if (RDFUtil.sameTerm(node.getResource(RDF.PropertyType), SP.PropertyOr))
            {
                sb.Append(StringForNode(node.getObject(SP.PropertyArg1), pm));
                sb.Append(" || ");
                sb.Append(StringForNode(node.getObject(SP.PropertyArg2), pm));
            }
            else if (RDFUtil.sameTerm(node.getResource(RDF.PropertyType), SP.PropertyAnd))
            {
                sb.Append(StringForNode(node.getObject(SP.PropertyArg1), pm));
                sb.Append(" && ");
                sb.Append(StringForNode(node.getObject(SP.PropertyArg2), pm));
            }
            else if (RDFUtil.sameTerm(node.getResource(RDF.PropertyType), SP.PropertyEq))
            {
                sb.Append(StringForNode(node.getObject(SP.PropertyArg1), pm));
                sb.Append("=");
                sb.Append(StringForNode(node.getObject(SP.PropertyArg2), pm));
            }
            else if (RDFUtil.sameTerm(node.getResource(RDF.PropertyType), SP.PropertyNeq))
            {
                sb.Append(StringForNode(node.getObject(SP.PropertyArg1), pm));
                sb.Append("!=");
                sb.Append(StringForNode(node.getObject(SP.PropertyArg2), pm));
            }
            else if (RDFUtil.sameTerm(node.getResource(RDF.PropertyType), SP.PropertyLt))
            {
                sb.Append(StringForNode(node.getObject(SP.PropertyArg1), pm));
                sb.Append("<");
                sb.Append(StringForNode(node.getObject(SP.PropertyArg2), pm));
            }
            else if (RDFUtil.sameTerm(node.getResource(RDF.PropertyType), SP.PropertyLeq))
            {
                sb.Append(StringForNode(node.getObject(SP.PropertyArg1), pm));
                sb.Append("<=");
                sb.Append(StringForNode(node.getObject(SP.PropertyArg2), pm));
            }
            else if (RDFUtil.sameTerm(node.getResource(RDF.PropertyType), SP.PropertyGt))
            {
                sb.Append(StringForNode(node.getObject(SP.PropertyArg1), pm));
                sb.Append(">");
                sb.Append(StringForNode(node.getObject(SP.PropertyArg2), pm));
            }
            else if (RDFUtil.sameTerm(node.getResource(RDF.PropertyType), SP.PropertyGeq))
            {
                sb.Append(StringForNode(node.getObject(SP.PropertyArg1), pm));
                sb.Append(">=");
                sb.Append(StringForNode(node.getObject(SP.PropertyArg2), pm));
            }
            else if (RDFUtil.sameTerm(node.getResource(RDF.PropertyType), SP.PropertyBound))
            {
                sb.Append("bound(");
                sb.Append(StringForNode(node.getObject(SP.PropertyArg1), pm));
                sb.Append(")");
            }
            else if (node.isUri())
            {
                sb.Append("<");
                sb.Append(node.Uri().ToString());
                sb.Append(">");
            }
            else if (node.isLiteral())
            {
                sb.Append(((ILiteralNode)node.getSource()).Value);
            }
            else
            {
                throw new Exception("Missing translation for expression " + node.getResource(RDF.PropertyType).Uri().ToString());
            }
            return(sb.ToString());
        }
Exemple #4
0
        override protected void handleTriplePattern(ITriplePattern triplePattern, Dictionary <IResource, IResource> bindings)
        {
            bool      valid   = false;
            IResource subject = triplePattern.getSubject();

            if (RDFUtil.sameTerm(SPIN.Property_this, subject))
            {
                valid = true;
            }
            else if (bindings != null)
            {
                IVariable var = SPINFactory.asVariable(subject);
                if (var != null)
                {
                    String varName = var.getName();
                    foreach (IResource argPredicate in bindings.Keys)
                    {
                        if (varName.Equals(argPredicate.Uri().ToString().Replace(SP.BASE_URI, "").Replace(SPIN.BASE_URI, "")))
                        {
                            IResource b = bindings[argPredicate];
                            if (RDFUtil.sameTerm(SPIN.Property_this, b))
                            {
                                valid = true;
                                break;
                            }
                        }
                    }
                }
            }

            if (valid)
            {
                IResource predicate = triplePattern.getPredicate();
                if (predicate != null)
                {
                    IVariable variable = SPINFactory.asVariable(predicate);
                    if (variable == null)
                    {
                        Uri uri = predicate.Uri();
                        if (uri != null)
                        {
                            properties.Add(Resource.Get(predicate, _targetModel));
                        }
                    }
                    else if (bindings != null)
                    {
                        String varName = variable.getName();
                        foreach (IResource argPredicate in bindings.Keys)
                        {
                            if (varName.Equals(argPredicate.Uri().ToString().Replace(SP.BASE_URI, "").Replace(SPIN.BASE_URI, "")))
                            {
                                IResource b = bindings[argPredicate];
                                if (b != null && b.isUri())
                                {
                                    properties.Add(Resource.Get(b, _targetModel));
                                }
                            }
                        }
                    }
                }
            }
        }