Пример #1
0
	public static void toStringElementList(StringBuilder buffer, INode resource) {
		RDFList list = resource.As(RDFList);
		for(IEnumerator<INode> it = list.GetEnumerator(); it.MoveNext(); ) {
			INode item = (INode) it.Current;
			Element e = SPINFactory.asElement(item);
			buffer.append(e.ToString());
			if(it.MoveNext()) {
				buffer.append(" .\n"));
			}
		}
	}
Пример #2
0
 /**
  * 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;
                 }
             }
         }
     }
 }
Пример #3
0
        internal IQuery BuildQuery(String sparqlQuery)
        {
            IQuery spinQuery = null;

            if (queryCache.ContainsKey(sparqlQuery))
            {
                spinQuery = (IQuery)queryCache[sparqlQuery];
            }
            else
            {
                _currentSparqlGraph         = new Graph();
                _currentSparqlGraph.BaseUri = UriFactory.Create("sparql-query:" + sparqlQuery);
                INode q = new SparqlQueryParser().ParseFromString(sparqlQuery).ToSpinRdf(_currentSparqlGraph);
                if (!_currentSparqlGraph.IsEmpty)
                {
                    _spinConfiguration.AddGraph(_currentSparqlGraph);
                    spinQuery = SPINFactory.asQuery(Resource.Get(q, this));
                    queryCache[sparqlQuery] = spinQuery;
                }
            }
            return(spinQuery);
        }
Пример #4
0
 /**
  * Attempts to convert a given INode to a String so that it can be parsed into
  * a Jena query object.  The node must be either a string Literal, or a sp:Query node
  * or a template call.  If it's a template call then the resulting query string will
  * "hard-bind" the template variables.
  * @param node  the INode to convert
  * @param usePrefixes  true to use qname abbreviations
  * @return the String representation of node
  * @throws ArgumentException  if the node is not a valid SPIN Query or a String
  * @deprecated for the same reason as {@link TemplateCall.getQueryString}
  */
 public static String getQueryString(IResource node, bool usePrefixes)
 {
     if (node.isLiteral())
     {
         return(((IValuedNode)node.getSource()).AsString());
     }
     else
     {
         ICommand spinCommand = SPINFactory.asCommand(node);
         if (spinCommand != null)
         {
             if (usePrefixes)
             {
                 //StringSparqlPrinter p = new StringSparqlPrinter();
                 //p.setUsePrefixes(usePrefixes);
                 //spinCommand.print(p);
                 //return p.getString();
                 return(String.Empty);
             }
             else
             {
                 return("");//ARQFactory.get().createCommandString(spinCommand);
             }
         }
         else
         {
             ITemplateCall templateCall = SPINFactory.asTemplateCall(node);
             if (templateCall != null)
             {
                 return(templateCall.getQueryString());
             }
             else
             {
                 throw new ArgumentException("INode must be either literal or a SPIN query or a SPIN template call");
             }
         }
     }
 }
Пример #5
0
        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);
                }
            }
        }
Пример #6
0
        public virtual void PrintEnhancedSPARQL(TripleImpl pattern)
        {
            if (CurrentSparqlContext != SparqlContext.QueryContext)
            {
                pattern.Print(this);
                return;
            }
            IResource graphContext = null;

            if (_currentGraphContext.Count > 0)
            {
                graphContext = _currentGraphContext.Peek();
            }
            else
            {
                graphContext = SPINFactory.createVariable(Dataset.spinProcessor, GetAnonVariable());
            }
            String surrogate = null;

            if (CurrentSparqlContext == SparqlContext.QueryContext)
            {
                surrogate = CreateGraphMapping(graphContext);
                if (!String.IsNullOrEmpty(surrogate))
                {
                    print("{ ");
                }
            }

            print(" GRAPH ");
            if (graphContext.isUri())
            {
                printURIResource(graphContext);
            }
            else
            {
                printVariable(graphContext.getString(SP.PropertyVarName));
            }
            print(" {");
            println(1);
            pattern.Print(this);
            if (!String.IsNullOrEmpty(surrogate))
            {
                print(". FILTER (NOT EXISTS{ GRAPH ?" + surrogate + " { ");
                pattern.print(pattern.getSubject(), this);
                print(" dnr:resets ");
                pattern.print(pattern.getPredicate(), this, false);
                print("} })");
            }
            println(-1);
            print("} ");
            if (!String.IsNullOrEmpty(surrogate))
            {
                print("} UNION { GRAPH ");
                printVariable(surrogate);
                print(" {");
                println(1);
                pattern.Print(this);
                println(-1);
                print("} }");
            }
            print(" .");
            println();
        }
Пример #7
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());
        }
Пример #8
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));
                                }
                            }
                        }
                    }
                }
            }
        }
Пример #9
0
        internal IEnumerable <IUpdate> GetConstructorsForClass(INode cls)
        {
            List <IUpdate> constructors = GetTriplesWithSubjectPredicate(cls, SPIN.PropertyConstructor).Select(t => SPINFactory.asUpdate(Resource.Get(t.Object, this))).ToList();

            return(constructors);
        }