private static void addTemplateCallResults(List <ConstraintViolation> results, QueryOrTemplateCall qot, SpinWrapperDataset model, INode resource, bool matchValue, IProgressMonitor monitor) { ITemplateCall templateCall = qot.getTemplateCall(); ITemplate template = templateCall.getTemplate(); if (template != null && template.getBody() is IQuery) { IQuery spinQuery = (IQuery)template.getBody(); if (spinQuery is IAsk || spinQuery is IConstruct) { SparqlParameterizedString arq = DatasetUtil.createQuery(spinQuery); setInitialBindings(resource, templateCall, arq); if (spinQuery is IAsk) { if (((SparqlResultSet)model.Query(DatasetUtil.prepare(arq, model, null).ToString())).Result != matchValue) { List <SimplePropertyPath> paths = getPropertyPaths(resource, spinQuery.getWhere(), templateCall.getArgumentsMapByProperties()); String message = SPINLabels.getLabel(templateCall); message += "\n(SPIN constraint at " + SPINLabels.getLabel(qot.getCls()) + ")"; results.Add(createConstraintViolation(paths, NO_FIXES, model, resource, message, templateCall)); } } else if (spinQuery is IConstruct) { IGraph cm = model.MonitorConstruct(DatasetUtil.prepare(DatasetUtil.convertConstructToInsert(arq, spinQuery, model._transactionUri), model, null), spinQuery, null); INode source = getSource(qot); String label = SPINLabels.getLabel(templateCall); addConstructedProblemReports(cm, results, model, qot.getCls(), resource, label, source); } } } }
private static List <ITemplateCall> getFixes(IGraph cm, SpinProcessor model, INode vio) { List <ITemplateCall> fixes = new List <ITemplateCall>(); IEnumerator <Triple> fit = cm.GetTriplesWithSubjectPredicate(vio, SPIN.fix).GetEnumerator(); while (fit.MoveNext()) { Triple fs = fit.Current; if (!(fs.Object is IValuedNode)) { // ????????????? What is it for ? /* * MultiUnion union = JenaUtil.createMultiUnion(new Graph[] { * model.getGraph(), * cm.getGraph() * }); * Model unionModel = ModelFactory.createModelForGraph(union); */ IResource r = Resource.Get(fs.Object, model); ITemplateCall fix = SPINFactory.asTemplateCall(r); fixes.Add(fix); } } return(fixes); }
/** * Collects all queries or template calls at a given class. * @param cls the class to get the queries at * @param predicate the predicate such as <code>spin:rule</code> * @param results the List to add the results to */ public static void addQueryOrTemplateCalls(IResource cls, INode predicate, List <QueryOrTemplateCall> results) { IEnumerable <Triple> ss = cls.listProperties(predicate); // Special case: we might have an instance of a template call like spl:Attribute // Then try to find the Template in the registry if (!ss.Any() && cls != null && cls.isUri()) { ITemplate template = SPINModuleRegistry.getTemplate(cls.Uri(), null); if (template != null) { ss = template.listProperties(predicate); } } foreach (Triple s in ss) { if (!(s.Object is ILiteralNode)) { ITemplateCall templateCall = SPINFactory.asTemplateCall(Resource.Get(s.Object, cls.getModel())); if (templateCall != null) { results.Add(new QueryOrTemplateCall(cls, templateCall)); } else { IQuery query = SPINFactory.asQuery(Resource.Get(s.Object, cls.getModel())); if (query != null) { results.Add(new QueryOrTemplateCall(cls, query)); } } } } }
private static void setInitialBindings(INode resource, ITemplateCall templateCall, SparqlParameterizedString arq) { QuerySolutionMap arqBindings = new QuerySolutionMap(); arqBindings.Add(SPIN.THIS_VAR_NAME, resource); Dictionary <IArgument, IResource> args = templateCall.getArgumentsMap(); foreach (IArgument arg in args.Keys) { INode value = args[arg]; arq.SetParameter(arg.getVarName(), value); } }
/** * 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"); } } } }
/** * Constructs an instance representing a template call. * @param cls the class the template call is attached to * @param templateCall the template call */ public QueryOrTemplateCall(IResource cls, ITemplateCall templateCall) { this.cls = cls; this.templateCall = templateCall; }
/** * Checks whether a given INode is a TemplateCall. The condition for this * is stricter than for <code>asTemplateCall</code> as the node also must have * a valid template assigned to it, i.e. the type of the node must be an * instance of spin:Template. * @param node the INode to check * @return true if node is a TemplateCall */ public static bool isTemplateCall(IResource node) { ITemplateCall templateCall = asTemplateCall(node); return(templateCall != null && templateCall.getTemplate() != null); }
/** * Creates a new TemplateCall as a blank node instance of a given template. * @param model the Model to create a template call in * @param template the template class * @return the new TemplateCall or null */ public static ITemplateCall createTemplateCall(SpinProcessor model, INode template) { ITemplateCall templateCall = (ITemplateCall)model.CreateResource(template).As(typeof(TemplateCallImpl)); return(templateCall); }