use of org.apache.jena.shex.sys.ReportItem in project jena by apache.
the class NumLengthConstraint method nodeSatisfies.
@Override
public ReportItem nodeSatisfies(ValidationContext vCxt, Node n) {
if (!n.isLiteral()) {
String msg = format("NumericConstraint: Not numeric: %s ", ShexLib.displayStr(n));
return new ReportItem(msg, n);
}
RDFDatatype rdfDT = n.getLiteralDatatype();
if (!(rdfDT instanceof XSDDatatype)) {
String msg = format("NumericConstraint: Not a numeric: %s ", ShexLib.displayStr(n));
return new ReportItem(msg, n);
}
if (XSDDatatype.XSDfloat.equals(rdfDT) || XSDDatatype.XSDdouble.equals(rdfDT)) {
String msg = format("NumericConstraint: Numeric not compatible with xsd:decimal: %s ", ShexLib.displayStr(n));
return new ReportItem(msg, n);
}
String lexicalForm = n.getLiteralLexicalForm();
if (!rdfDT.isValid(lexicalForm)) {
String msg = format("NumericConstraint: Not a valid xsd:decimal: %s ", ShexLib.displayStr(n));
return new ReportItem(msg, n);
}
String str = lexicalForm;
int N = str.length();
int idx = str.indexOf('.');
switch(lengthType) {
case FRACTIONDIGITS:
{
// Does not include trailing zeros.
if (idx < 0)
return null;
// int before = idx;
int after = str.length() - idx - 1;
for (int i = N - 1; i > idx; i--) {
if (str.charAt(i) != '0')
break;
after--;
}
if (after <= length)
return null;
break;
}
case TOTALDIGITS:
{
// Canonical form.
int start = 0;
char ch1 = str.charAt(0);
if (ch1 == '+' || ch1 == '-')
start++;
// Leading zeros
for (int i = start; i < N; i++) {
if (str.charAt(i) != '0')
break;
start++;
}
int finish = N;
// Trailing zeros
if (idx >= 0) {
finish--;
for (int i = N - 1; i > idx; i--) {
if (str.charAt(i) != '0')
break;
finish--;
}
}
int digits = finish - start;
if (digits <= length)
return null;
break;
}
default:
break;
}
String msg = format("Expected %s %d : got = %d", lengthType.label(), length, str.length());
return new ReportItem(msg, n);
}
use of org.apache.jena.shex.sys.ReportItem in project jena by apache.
the class ShapeExprNOT method satisfies.
@Override
public boolean satisfies(ValidationContext vCxt, Node data) {
ValidationContext vCxt2 = ValidationContext.create(vCxt);
boolean innerSatisfies = other.satisfies(vCxt2, data);
if (!innerSatisfies)
return true;
ReportItem item = new ReportItem("NOT: Term reject because it conforms", data);
vCxt.reportEntry(item);
return false;
}
use of org.apache.jena.shex.sys.ReportItem in project jena by apache.
the class ShapeExprFalse method satisfies.
@Override
public boolean satisfies(ValidationContext vCxt, Node data) {
ReportItem r = new ReportItem("False", data);
vCxt.reportEntry(r);
return false;
}
use of org.apache.jena.shex.sys.ReportItem in project jena by apache.
the class ShapeEval method matchesTripleExprRef.
private static boolean matchesTripleExprRef(ValidationContext vCxt, Set<Triple> matchables, Node node, TripleExprRef ref, Set<Node> extras) {
Node label = ref.ref();
if (label == null) {
}
TripleExpression tripleExpr = vCxt.getTripleExpression(label);
if (tripleExpr == null) {
ReportItem rItem = new ReportItem("Failed to get triple expression from reference", label);
vCxt.reportEntry(rItem);
}
return matches(vCxt, matchables, node, tripleExpr, extras);
}
use of org.apache.jena.shex.sys.ReportItem in project jena by apache.
the class ShapeEvalTripleConstraint method matchesCardinalityTC.
/**
* Triple Constraint, with cardinality
*/
static boolean matchesCardinalityTC(ValidationContext vCxt, Set<Triple> matchables, Node node, TripleConstraint tripleConstraint, Set<Node> extras) {
Node predicate = tripleConstraint.getPredicate();
if (tripleConstraint.reverse()) {
// [shex] A bit of a fudge.
matchables = G.find(vCxt.getData(), null, predicate, node).toSet();
} else {
if (!matchables.stream().allMatch(t -> predicate.equals(t.getPredicate()))) {
// Other predicates present.
return false;
}
}
// Find same predicate.
Set<Triple> triples = StreamOps.toSet(matchables.stream().filter(t -> predicate.equals(t.getPredicate())));
int min = tripleConstraint.min();
int max = tripleConstraint.max();
ShapeExpression shExpr = tripleConstraint.getShapeExpression();
Set<Triple> positive = triples.stream().filter(t -> {
Node v = tripleConstraint.reverse() ? t.getSubject() : t.getObject();
return shExpr.satisfies(vCxt, v);
}).collect(Collectors.toSet());
int N = positive.size();
if (min >= 0 && N < min) {
vCxt.reportEntry(new ReportItem("Cardinality violation (min=" + min + "): " + N, null));
return false;
}
// Remove extras.
if (extras == null || !extras.contains(predicate)) {
if (positive.size() != triples.size())
// Something did not match.
return false;
}
if (max >= 0 && N > max) {
vCxt.reportEntry(new ReportItem("Cardinality violation (max=" + max + "): " + N, null));
return false;
}
return true;
}
Aggregations