Search in sources :

Example 71 with RDFNode

use of org.apache.jena.rdf.model.RDFNode in project jena by apache.

the class ReportEntry method generate.

public void generate(Model model, Resource report) {
    Resource entry = model.createResource();
    entry.addProperty(RDF.type, SHACLM.ValidationResult);
    report.addProperty(SHACLM.result, entry);
    Function<Node, RDFNode> rdfNode = (gn) -> ModelUtils.convertGraphNodeToRDFNode(gn, entry.getModel());
    Function<String, RDFNode> str = (s) -> entry.getModel().createLiteral(s);
    // Section 3.6.2
    // The properties sh:focusNode, sh:resultSeverity and
    // sh:sourceConstraintComponent are the only properties that are mandatory
    // for all validation results.
    Objects.requireNonNull(focusNode, "focus node");
    // Objects.requireNonNull(message,"message");
    Objects.requireNonNull(severity, "severity");
    Objects.requireNonNull(sourceConstraintComponent, "sourceConstraintComponent");
    entry.addProperty(SHACLM.focusNode, rdfNode.apply(focusNode));
    entry.addProperty(SHACLM.resultSeverity, rdfNode.apply(severity.level()));
    entry.addProperty(SHACLM.sourceConstraintComponent, rdfNode.apply(sourceConstraintComponent));
    for (Node msg : messages) entry.addProperty(SHACLM.resultMessage, rdfNode.apply(msg));
    if (sourceConstraint != null)
        entry.addProperty(SHACLM.sourceConstraint, rdfNode.apply(sourceConstraint));
    if (source != null)
        entry.addProperty(SHACLM.sourceShape, rdfNode.apply(source));
    if (resultPath != null) {
        Node pn = ShaclPaths.pathToRDF(model.getGraph()::add, resultPath);
        entry.addProperty(SHACLM.resultPath, rdfNode.apply(pn));
    }
    if (value != null)
        entry.addProperty(SHACLM.value, rdfNode.apply(value));
}
Also used : CollectionUtils(org.apache.jena.atlas.lib.CollectionUtils) RDF(org.apache.jena.vocabulary.RDF) NodeFactory(org.apache.jena.graph.NodeFactory) Collection(java.util.Collection) NodeFmtLib(org.apache.jena.riot.out.NodeFmtLib) Triple(org.apache.jena.graph.Triple) Function(java.util.function.Function) ModelUtils(org.apache.jena.sparql.util.ModelUtils) ArrayList(java.util.ArrayList) RDFNode(org.apache.jena.rdf.model.RDFNode) Objects(java.util.Objects) Model(org.apache.jena.rdf.model.Model) Constraint(org.apache.jena.shacl.parser.Constraint) ShaclPaths(org.apache.jena.shacl.engine.ShaclPaths) ShaclException(org.apache.jena.shacl.ShaclException) Path(org.apache.jena.sparql.path.Path) Node(org.apache.jena.graph.Node) Resource(org.apache.jena.rdf.model.Resource) StringJoiner(java.util.StringJoiner) ShLib.displayStr(org.apache.jena.shacl.lib.ShLib.displayStr) SHACLM(org.apache.jena.shacl.vocabulary.SHACLM) RDFNode(org.apache.jena.rdf.model.RDFNode) Node(org.apache.jena.graph.Node) Resource(org.apache.jena.rdf.model.Resource) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 72 with RDFNode

use of org.apache.jena.rdf.model.RDFNode in project jena by apache.

the class ShexValidationTest method testShexValidationStartFocus.

// Expected: ShexT.schema (with start), ShexT.data, ShexT.focus
static Runnable testShexValidationStartFocus(ManifestEntry entry) {
    Resource action = entry.getAction();
    Resource schema = action.getProperty(ShexT.schema).getResource();
    Resource data = action.getProperty(ShexT.data).getResource();
    // URI or literal.
    RDFNode focus = action.getProperty(ShexT.focus).getObject();
    return new ShexValidationTest(entry, data, schema, (Resource) null, (String) null, focus, TestType.StartFocus);
}
Also used : Resource(org.apache.jena.rdf.model.Resource) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 73 with RDFNode

use of org.apache.jena.rdf.model.RDFNode in project jena by apache.

the class ShexValidationTest method testShexValidationShapeFocus.

// Expected: ShexT.schema, ShexT.shape, ShexT.data, ShexT.focus
static Runnable testShexValidationShapeFocus(ManifestEntry entry) {
    Resource action = entry.getAction();
    Resource schema = action.getProperty(ShexT.schema).getResource();
    Resource shape = action.getProperty(ShexT.shape).getResource();
    Resource data = action.getProperty(ShexT.data).getResource();
    // URI or literal.
    RDFNode focus = action.getProperty(ShexT.focus).getObject();
    return new ShexValidationTest(entry, data, schema, shape, (String) null, focus, TestType.ShapeFocus);
}
Also used : Resource(org.apache.jena.rdf.model.Resource) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 74 with RDFNode

use of org.apache.jena.rdf.model.RDFNode in project jena by apache.

the class TextIndexLuceneAssembler method open.

/*
    <#index> a :TextIndexLucene ;
        #text:directory "mem" ;
        #text:directory "DIR" ;
        text:directory <file:DIR> ;
        text:entityMap <#entMap> ;
        .
    */
@SuppressWarnings("resource")
@Override
public TextIndex open(Assembler a, Resource root, Mode mode) {
    try {
        if (!GraphUtils.exactlyOneProperty(root, pDirectory))
            throw new TextIndexException("No 'text:directory' property on " + root);
        Directory directory;
        RDFNode n = root.getProperty(pDirectory).getObject();
        if (n.isLiteral()) {
            String literalValue = n.asLiteral().getLexicalForm();
            if (literalValue.equals("mem")) {
                directory = new ByteBuffersDirectory();
            } else {
                File dir = new File(literalValue);
                directory = FSDirectory.open(dir.toPath());
            }
        } else {
            Resource x = n.asResource();
            String path = IRILib.IRIToFilename(x.getURI());
            File dir = new File(path);
            directory = FSDirectory.open(dir.toPath());
        }
        String queryParser = null;
        Statement queryParserStatement = root.getProperty(pQueryParser);
        if (null != queryParserStatement) {
            RDFNode qpNode = queryParserStatement.getObject();
            if (!qpNode.isResource()) {
                throw new TextIndexException("Text query parser property is not a resource : " + qpNode);
            }
            Resource parserResource = (Resource) qpNode;
            queryParser = parserResource.getLocalName();
        }
        boolean isMultilingualSupport = false;
        Statement mlSupportStatement = root.getProperty(pMultilingualSupport);
        if (null != mlSupportStatement) {
            RDFNode mlsNode = mlSupportStatement.getObject();
            if (!mlsNode.isLiteral()) {
                throw new TextIndexException("text:multilingualSupport property must be a boolean : " + mlsNode);
            }
            isMultilingualSupport = mlsNode.asLiteral().getBoolean();
        }
        int maxBasicQueries = 1024;
        Statement maxBasicQueriesStatement = root.getProperty(pMaxBasicQueries);
        if (null != maxBasicQueriesStatement) {
            RDFNode mbqNode = maxBasicQueriesStatement.getObject();
            if (!mbqNode.isLiteral()) {
                throw new TextIndexException("text:maxBasicQueries property must be a int : " + mbqNode);
            }
            try {
                maxBasicQueries = mbqNode.asLiteral().getInt();
            } catch (RuntimeException ex) {
                // Problems with the integer.
                throw new TextIndexException("text:maxBasicQueries property must be a int : " + mbqNode + "(" + ex.getMessage() + ")");
            }
        }
        // define any property lists for text:query
        Statement propListsStmt = root.getProperty(pPropLists);
        if (null != propListsStmt) {
            RDFNode aNode = propListsStmt.getObject();
            if (!aNode.isResource()) {
                throw new TextIndexException("text:propLists property is not a resource (list) : " + aNode);
            }
            PropListsAssembler.open(a, (Resource) aNode);
        }
        // define any filters and tokenizers first so they can be referenced in analyzer definitions if need be
        Statement defAnalyzersStatement = root.getProperty(pDefAnalyzers);
        if (null != defAnalyzersStatement) {
            RDFNode aNode = defAnalyzersStatement.getObject();
            if (!aNode.isResource()) {
                throw new TextIndexException("text:defineAnalyzers property is not a resource (list) : " + aNode);
            }
            DefineFiltersAssembler.open(a, (Resource) aNode);
            DefineTokenizersAssembler.open(a, (Resource) aNode);
            boolean addedLangs = DefineAnalyzersAssembler.open(a, (Resource) aNode);
            // multilingual support is enabled
            if (addedLangs) {
                if (!isMultilingualSupport) {
                    Log.warn(this, "Multilingual support implicitly enabled by text:defineAnalyzers");
                }
                isMultilingualSupport = true;
            }
        }
        // initialize default analyzer and query analyzer after processing all analyzer definitions
        // so they can be referred to
        Analyzer analyzer = null;
        Statement analyzerStatement = root.getProperty(pAnalyzer);
        if (null != analyzerStatement) {
            RDFNode aNode = analyzerStatement.getObject();
            if (!aNode.isResource()) {
                throw new TextIndexException("Text analyzer property is not a resource : " + aNode);
            }
            Resource analyzerResource = (Resource) aNode;
            analyzer = (Analyzer) a.open(analyzerResource);
        }
        Analyzer queryAnalyzer = null;
        Statement queryAnalyzerStatement = root.getProperty(pQueryAnalyzer);
        if (null != queryAnalyzerStatement) {
            RDFNode qaNode = queryAnalyzerStatement.getObject();
            if (!qaNode.isResource()) {
                throw new TextIndexException("Text query analyzer property is not a resource : " + qaNode);
            }
            Resource analyzerResource = (Resource) qaNode;
            queryAnalyzer = (Analyzer) a.open(analyzerResource);
        }
        boolean storeValues = false;
        Statement storeValuesStatement = root.getProperty(pStoreValues);
        if (null != storeValuesStatement) {
            RDFNode svNode = storeValuesStatement.getObject();
            if (!svNode.isLiteral()) {
                throw new TextIndexException("text:storeValues property must be a boolean : " + svNode);
            }
            storeValues = svNode.asLiteral().getBoolean();
        }
        boolean ignoreIndexErrs = false;
        Statement ignoreIndexErrsStatement = root.getProperty(pIgnoreIndexErrors);
        if (null != ignoreIndexErrsStatement) {
            RDFNode iieNode = ignoreIndexErrsStatement.getObject();
            if (!iieNode.isLiteral()) {
                throw new TextIndexException("text:ignoreIndexErrors property must be a boolean : " + iieNode);
            }
            ignoreIndexErrs = iieNode.asLiteral().getBoolean();
        }
        // use query cache by default
        boolean cacheQueries = true;
        Statement cacheQueriesStatement = root.getProperty(pCacheQueries);
        if (null != cacheQueriesStatement) {
            RDFNode cqNode = cacheQueriesStatement.getObject();
            if (!cqNode.isLiteral()) {
                throw new TextIndexException("text:cacheQueries property must be a boolean : " + cqNode);
            }
            cacheQueries = cqNode.asLiteral().getBoolean();
        }
        Resource r = GraphUtils.getResourceValue(root, pEntityMap);
        EntityDefinition docDef = (EntityDefinition) a.open(r);
        TextIndexConfig config = new TextIndexConfig(docDef);
        config.setAnalyzer(analyzer);
        config.setQueryAnalyzer(queryAnalyzer);
        config.setQueryParser(queryParser);
        config.setMultilingualSupport(isMultilingualSupport);
        config.setMaxBasicQueries(maxBasicQueries);
        config.setValueStored(storeValues);
        config.setIgnoreIndexErrors(ignoreIndexErrs);
        docDef.setCacheQueries(cacheQueries);
        return TextDatasetFactory.createLuceneIndex(directory, config);
    } catch (IOException e) {
        IO.exception(e);
        return null;
    }
}
Also used : Statement(org.apache.jena.rdf.model.Statement) Resource(org.apache.jena.rdf.model.Resource) IOException(java.io.IOException) Analyzer(org.apache.lucene.analysis.Analyzer) File(java.io.File) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 75 with RDFNode

use of org.apache.jena.rdf.model.RDFNode in project jena by apache.

the class PropListsAssembler method open.

public static void open(Assembler assembler, Resource list) {
    Resource current = list;
    while (current != null && !current.equals(RDF.nil)) {
        Statement firstStmt = current.getProperty(RDF.first);
        if (firstStmt == null) {
            throw new TextIndexException("text:propLists list not well formed: " + current);
        }
        RDFNode first = firstStmt.getObject();
        if (!first.isResource()) {
            throw new TextIndexException("text:propLists element must be an anon resource : " + first);
        }
        // process the current list element to add a property list
        Resource adding = (Resource) first;
        if (adding.hasProperty(TextVocab.pPropListProp)) {
            Statement propListPropStmt = adding.getProperty(TextVocab.pPropListProp);
            RDFNode propListPropNode = propListPropStmt.getObject();
            if (!propListPropNode.isResource()) {
                throw new TextIndexException("text:propLists text:propListProp must be a resource: " + propListPropNode);
            }
            if (adding.hasProperty(TextVocab.pProps)) {
                Statement propsStmt = adding.getProperty(TextVocab.pProps);
                List<Resource> props = getPropsList(propsStmt);
                Util.addPropsList((Resource) propListPropNode, props);
            }
        } else {
            throw new TextIndexException("text:propLists text:propListProp must be a resource: " + adding.getProperty(TextVocab.pPropListProp));
        }
        Statement restStmt = current.getProperty(RDF.rest);
        if (restStmt == null) {
            throw new TextIndexException("text:propLists  list not terminated by rdf:nil");
        }
        RDFNode rest = restStmt.getObject();
        if (!rest.isResource()) {
            throw new TextIndexException("text:propLists list rest is not a resource : " + rest);
        }
        current = (Resource) rest;
    }
}
Also used : TextIndexException(org.apache.jena.query.text.TextIndexException) Statement(org.apache.jena.rdf.model.Statement) Resource(org.apache.jena.rdf.model.Resource) RDFNode(org.apache.jena.rdf.model.RDFNode)

Aggregations

RDFNode (org.apache.jena.rdf.model.RDFNode)145 Resource (org.apache.jena.rdf.model.Resource)51 Model (org.apache.jena.rdf.model.Model)30 Statement (org.apache.jena.rdf.model.Statement)27 Test (org.junit.Test)26 ArrayList (java.util.ArrayList)22 Node (org.apache.jena.graph.Node)20 QuerySolution (org.apache.jena.query.QuerySolution)20 TextIndexException (org.apache.jena.query.text.TextIndexException)17 Property (org.apache.jena.rdf.model.Property)17 Literal (org.apache.jena.rdf.model.Literal)15 SecuredRDFNode (org.apache.jena.permissions.model.SecuredRDFNode)13 Dataset (org.apache.jena.query.Dataset)13 Triple (org.apache.jena.graph.Triple)12 QueryExecution (org.apache.jena.query.QueryExecution)10 StmtIterator (org.apache.jena.rdf.model.StmtIterator)9 Query (org.apache.jena.query.Query)8 NodeIterator (org.apache.jena.rdf.model.NodeIterator)8 RDFList (org.apache.jena.rdf.model.RDFList)8 AccessDeniedException (org.apache.jena.shared.AccessDeniedException)7