kontext-e/jqassistant-plugins

Asciidoc: document an example for defining labels for types matched by regular expressions in tables

Opened this issue · 2 comments

Because in Asciidoc tables, the labels to be assigned are given as attributes and Neo4j does not allow to create lables from properties. Instead jQA capability of using a scripting language has to be used in a concept for doing this.

When the lables defined like this:
https://github.com/kontext-e/jqassistant-plugins/blob/master/asciidoc/src/test/asciidoc/testfile.adoc

your Asciidoc rule file could contain this snippet:

== Enhance Graph with design information from Architecture Documentation

[[structure:MarkAsciidocTypeRegex]]
[source,cypher,role=concept]
.Mark Asciidoc Table Cells that contain regular expressions for types that have to be marked with given labels.

MATCH
    (a:Asciidoc:Table)-[:BODY]->(body),
    (a)-[:HAS_ATTRIBUTE]->(att:Asciidoc:Attribute)
WHERE
    att.name='label' AND att.value='types'
WITH
    body
MATCH
    (body)-[:CONTAINS_CELLS]->(regexCell:Asciidoc:Cell {colnumber: 0}),
    (body)-[:CONTAINS_CELLS]->(labelCell:Asciidoc:Cell {colnumber: 1})
SET
    regexCell:RegularExpressionCell,
    labelCell:LabelCell
CREATE UNIQUE
    (regexCell)-[:REGEX_FOR_LABEL]->(labelCell)
RETURN
    regexCell, labelCell

[[structure:LabelTypesMatchedByRegex]]
[source,js,role=concept,requiresConcepts="structure:MarkAsciidocTypeRegex"]
.Mark types with labels matched by regex.

var graphDatabaseService = store.getGraphDatabaseService();
// Define the columns returned by the constraint
var columnNames = java.util.Arrays.asList("Type");
// Define the list of rows returned by the constraint
var rows = new java.util.ArrayList();

var result = graphDatabaseService.execute("    MATCH\n" +
                                                   "        (type:Type),\n" +
                                                   "        (regexCell:RegularExpressionCell)-[:REGEX_FOR_LABEL]->(labelCell:LabelCell)\n" +
                                                   "    WHERE\n" +
                                                   "        type.fqn =~ regexCell.text\n" +
                                                   "    RETURN\n" +
                                                   "        type, labelCell.text as label\n");

while(result.hasNext()) {
    var next = result.next();
    var node = next.get("type");
    var label = next.get("label");
    node.addLabel(org.neo4j.graphdb.DynamicLabel.label(label));
    var resultRow = new java.util.HashMap();
    resultRow.put("Class", node);
    rows.add(resultRow);
}

// Return the result
var status = com.buschmais.jqassistant.core.analysis.api.Result.Status.SUCCESS;
new com.buschmais.jqassistant.core.analysis.api.Result(rule, status, severity, columnNames, rows);

Rule is added to jqassistant/jqassistant-rules/index.adoc