/// <summary>
        /// Prints the template required to process INSERT DATA commands.
        /// </summary>
        /// <param name="command">a IInsertData instance</param>
        public virtual void PrintEnhancedSPARQL(IInsertData command)
        {
            IResource data     = command.getResource(SP.PropertyData);
            String    template = SparqlTemplates.InsertData;

            if (data != null)
            {
                foreach (Resource graph in data.AsList())
                {
                    Uri graphUri = graph.getResource(SP.PropertyGraphNameNode).Uri;
                    Dataset.SetActiveGraph(graphUri);
                    if (Dataset.HasGraph(graphUri))
                    {
                        IGraph ucg = queryModel.GetModifiableGraph(graphUri);
                        foreach (Resource t in graph.getResource(SP.PropertyElements).AsList())
                        {
                            ITriplePattern triple = (ITriplePattern)t.As(typeof(TriplePatternImpl));
                            ucg.Assert(triple.getSubject().getSource(), triple.getPredicate().getSource(), triple.getObject().getSource());
                        }
                    }
                    else
                    {
                        // TODO transform those into a static SPARQL string
                        print("INSERT DATA { GRAPH <g> { <urn:a> <urn:b> <urn:c> . }}; ");
                    }
                }
            }
            print(template);
        }
 private bool templateContainsVar(IResource list)
 {
     if (list != null && !list.isLiteral())
     {
         IEnumerator <IResource> nodes = list.AsList().GetEnumerator();
         while (nodes.MoveNext())
         {
             IResource node = nodes.Current;
             if (node.hasProperty(RDF.PropertyType, SP.ClassNamedGraph))
             {
                 INamedGraph namedGraph = (INamedGraph)node.As(typeof(NamedGraphImpl));
                 foreach (IElement element in namedGraph.getElements())
                 {
                     if (element is ITriple)
                     {
                         if (containsVar((ITriple)element))
                         {
                             return(true);
                         }
                     }
                 }
             }
             else
             {
                 if (containsVar((ITripleTemplate)node.As(typeof(TripleTemplateImpl))))
                 {
                     return(true);
                 }
             }
         }
     }
     return(false);
 }
Esempio n. 3
0
        public List <IResource> getList(INode predicate)
        {
            IResource rawList = getObject(predicate);

            if (rawList != null)
            {
                return(rawList.AsList());
            }
            return(new List <IResource>());
        }
        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);
                }
            }
        }
Esempio n. 5
0
        protected bool printTemplates(ISparqlPrinter p, INode predicate, String keyword, bool force, IResource graphIRI)
        {
            IResource resource = getResource(predicate);

            if (resource == null)
            {
                return(false);
            }
            List <IResource> nodes = resource.AsList();

            if (nodes.Count > 0 || force)
            {
                if (keyword != null)
                {
                    p.printIndentation(p.getIndentation());
                    p.printKeyword(keyword);
                }
                p.print(" {");
                p.println();
                if (graphIRI != null)
                { // Legacy triple
                    p.setIndentation(p.getIndentation() + 1);
                    p.printIndentation(p.getIndentation());
                    p.printKeyword("GRAPH");
                    p.print(" ");
                    printVarOrResource(p, graphIRI);
                    p.print(" {");
                    p.println();
                }
                foreach (IResource node in nodes)
                {
                    p.printIndentation(p.getIndentation() + 1);
                    if (node.canAs(SP.ClassNamedGraph))
                    {
                        INamedGraph namedGraph = (INamedGraph)node.As(typeof(NamedGraphImpl));
                        p.setIndentation(p.getIndentation() + 1);
                        p.setNamedBNodeMode(true);
                        namedGraph.Print(p);
                        p.setNamedBNodeMode(false);
                        p.setIndentation(p.getIndentation() - 1);
                    }
                    else
                    {
                        ITripleTemplate template = (ITripleTemplate)node.As(typeof(TripleTemplateImpl));
                        template.Print(p);
                    }
                    p.print(" .");
                    p.println();
                }
                if (graphIRI != null)
                {
                    p.printIndentation(p.getIndentation());
                    p.setIndentation(p.getIndentation() - 1);
                    p.print("}");
                    p.println();
                }
                p.printIndentation(p.getIndentation());
                p.print("}");
                return(true);
            }
            else
            {
                return(false);
            }
        }