Difference between revisions of "Attributes and Tags"
m |
(Update to Atlas 2.5.2) |
||
Line 4: | Line 4: | ||
Let’s take a look at an example written in Java that shows how to access <code>GraphElement</code> objects after starting with a <code>Q</code>. | Let’s take a look at an example written in Java that shows how to access <code>GraphElement</code> objects after starting with a <code>Q</code>. | ||
− | <pre>Q methods = Common.universe().nodesTaggedWithAny( | + | <pre>Q methods = Common.universe().nodesTaggedWithAny(XCSG.name); |
Graph methodsGraph = methods.eval(); | Graph methodsGraph = methods.eval(); | ||
AtlasSet<GraphElement> methodNodes = methodsGraph.nodes(); | AtlasSet<GraphElement> methodNodes = methodsGraph.nodes(); | ||
Line 12: | Line 12: | ||
== Attributes == | == Attributes == | ||
− | An attribute is a key that corresponds to a value in the <code>GraphElement</code> attribute map. An attribute that is common to almost all nodes and edges is | + | An attribute is a key that corresponds to a value in the <code>GraphElement</code> attribute map. An attribute that is common to almost all nodes and edges is XCSG.name. Here we iterate over each node in the <code>methodNodes</code> set and get the methods name attribute value. |
<pre>for(GraphElement methodNode : methodNodes){ | <pre>for(GraphElement methodNode : methodNodes){ | ||
− | String name = (String) methodNode. | + | String name = (String) methodNode.getAttr(XCSG.name); |
}</pre> | }</pre> | ||
We can store lots of information in the <code>GraphElement</code> attribute map. Another common attribute is the source correspondence that stores the file and character offset of where the node corresponds to in source code. Try double clicking on a node or edge to jump to source code! | We can store lots of information in the <code>GraphElement</code> attribute map. Another common attribute is the source correspondence that stores the file and character offset of where the node corresponds to in source code. Try double clicking on a node or edge to jump to source code! | ||
− | Attributes can be used to select nodes out of a graph. For example from the <code>graph</code> we can select all method nodes with the attribute key | + | Attributes can be used to select nodes out of a graph. For example from the <code>graph</code> we can select all method nodes with the attribute key XCSG.name that have the value "main". |
− | <code>Q mainMethods = graph.nodesTaggedWithAny( | + | <code>Q mainMethods = graph.nodesTaggedWithAny(XCSG.Method).selectNode(XCSG.name, "main");</code> |
== Tags == | == Tags == | ||
− | Tags can be thought of as special attributes where the value is always a Boolean true. The presence of a tag denotes that a node or edge is a member of a set. For example all nodes in the Atlas index that are methods are tagged with | + | Tags can be thought of as special attributes where the value is always a Boolean true. The presence of a tag denotes that a node or edge is a member of a set. For example all nodes in the Atlas index that are methods are tagged with XCSG.Method. A tag is just a string key, but for Atlas provided tags such as XCSG.Method, we have defined constants for you that should be used to make your code cleaner (and safer from possible Schema changes in the future!). |
We've already seen how to use tags using the <code>nodesTaggedWithAny</code>/<code>nodesTaggedWithAll</code> and <code>edgesTaggedWithAny</code>/<code>edgesTaggedWithAll</code> <code>Q</code> methods, but you should take some time to look through the [[Atlas Schema]] to see what tags are predefined for you. You get a lot for free! | We've already seen how to use tags using the <code>nodesTaggedWithAny</code>/<code>nodesTaggedWithAll</code> and <code>edgesTaggedWithAny</code>/<code>edgesTaggedWithAll</code> <code>Q</code> methods, but you should take some time to look through the [[Atlas Schema]] to see what tags are predefined for you. You get a lot for free! | ||
<br /><center>[[Common Queries|← Common Queries]] | [[Learning Atlas]] | [[Atlas Schema|Atlas Schema →]]</center> | <br /><center>[[Common Queries|← Common Queries]] | [[Learning Atlas]] | [[Atlas Schema|Atlas Schema →]]</center> |
Latest revision as of 15:46, 13 October 2015
Graph Elements
In Atlas a Q
object can be thought of as a recipe to a constraint satisfaction problem (CSP). Building and chaining together Q
’s costs you almost nothing, but when you ask to see what is in the Q (by showing or evaluating the Q
) Atlas must evaluate the query and execute the graph traversals. The evaluated result is a Graph
. A Graph
is a set of GraphElement
objects. In Atlas both a node and an edge are GraphElement
objects.
Let’s take a look at an example written in Java that shows how to access GraphElement
objects after starting with a Q
.
Q methods = Common.universe().nodesTaggedWithAny(XCSG.name); Graph methodsGraph = methods.eval(); AtlasSet<GraphElement> methodNodes = methodsGraph.nodes(); AtlasSet<GraphElement> methodEdges = methodsGraph.edges();
Note that we know methodEdges.size()
would return 0 here because nodesTaggedWithAny
does not return edges.
Attributes
An attribute is a key that corresponds to a value in the GraphElement
attribute map. An attribute that is common to almost all nodes and edges is XCSG.name. Here we iterate over each node in the methodNodes
set and get the methods name attribute value.
for(GraphElement methodNode : methodNodes){ String name = (String) methodNode.getAttr(XCSG.name); }
We can store lots of information in the GraphElement
attribute map. Another common attribute is the source correspondence that stores the file and character offset of where the node corresponds to in source code. Try double clicking on a node or edge to jump to source code!
Attributes can be used to select nodes out of a graph. For example from the graph
we can select all method nodes with the attribute key XCSG.name that have the value "main".
Q mainMethods = graph.nodesTaggedWithAny(XCSG.Method).selectNode(XCSG.name, "main");
Tags
Tags can be thought of as special attributes where the value is always a Boolean true. The presence of a tag denotes that a node or edge is a member of a set. For example all nodes in the Atlas index that are methods are tagged with XCSG.Method. A tag is just a string key, but for Atlas provided tags such as XCSG.Method, we have defined constants for you that should be used to make your code cleaner (and safer from possible Schema changes in the future!).
We've already seen how to use tags using the nodesTaggedWithAny
/nodesTaggedWithAll
and edgesTaggedWithAny
/edgesTaggedWithAll
Q
methods, but you should take some time to look through the Atlas Schema to see what tags are predefined for you. You get a lot for free!