Execution Directives
Execution Directives are CPGQL Directives which execute the traversals they suffix and return the result in a specific format. The most straightforward Execution Directive is toList which, as the name suggests, executes the CPGQL Query it suffixes and returns the results in a list:
toList
ocular> cpg.call.name.toList
res0: List[String] = List(
"exit",
"printf",
"exit",
"fprintf",
"<operator>.indirectIndexAccess",
"strcmp",
"<operator>.equals",
"<operator>.greaterThan",
"<operator>.logicalAnd"
)l
toList has a shorthand named l:
ocular> cpg.call.name.l
res0: List[String] = List(
"exit",
"printf",
"exit",
"fprintf",
"<operator>.indirectIndexAccess",
"strcmp",
"<operator>.equals",
"<operator>.greaterThan",
"<operator>.logicalAnd"
)head
Executes the traversal and returns the first result
size
size executes the traversal and returns the size of the result set
Execution Directives and the Ocular Interpreter
Execution Directives sit at the border between CPQGL Queries and the Ocular Interpreter, that is at the border between querying Code Property Graphs, and using the results of those queries for further processing using the Scala programming language. For example, say you'd like to take the results of a query execution and write them to a text file:
You'll see your results written to a file, ready for post-analysis:
Writing the results of a query to a file can also be done in a more concise way using the |> operator provided by the Ocular Interpreter:
p
p executes the traversal and pretty-prints the results:
toJson
toJson executes the traversal and returns the results in a JSON string:
toJsonPretty
toJsonPretty executes the traversal and returns the results in a pretty-printed JSON string:
size
size executes the traversal and returns the number of results:
Last updated
Was this helpful?