Changed graph api to use vertex and edge object instead of ids.

This commit is contained in:
ghidravore
2020-10-09 14:27:29 -04:00
parent 3cd26120a3
commit 592b8a3cfc
14 changed files with 292 additions and 283 deletions
@@ -157,13 +157,13 @@ public class BlockGraphTask extends Task {
if (location != null) {
// initialize the graph location, but don't have the graph send an event
String id = listener.getVertexIdForAddress(location.getAddress());
display.setLocationFocus(id, EventTrigger.INTERNAL_ONLY);
AttributedVertex vertex = listener.getVertex(location.getAddress());
display.setFocusedVertex(vertex, EventTrigger.INTERNAL_ONLY);
}
if (selection != null && !selection.isEmpty()) {
List<String> selectedVertices = listener.getVertices(selection);
Set<AttributedVertex> selectedVertices = listener.getVertices(selection);
if (selectedVertices != null) {
// intialize the graph selection, but don't have the graph send an event
// initialize the graph selection, but don't have the graph send an event
display.selectVertices(selectedVertices, EventTrigger.INTERNAL_ONLY);
}
}
@@ -54,28 +54,28 @@ public class BlockModelGraphDisplayListener extends AddressBasedGraphDisplayList
}
@Override
protected String getVertexIdForAddress(Address address) {
protected String getVertexId(Address address) {
try {
CodeBlock[] blocks = blockModel.getCodeBlocksContaining(address, TaskMonitor.DUMMY);
if (blocks != null && blocks.length > 0) {
return super.getVertexIdForAddress(blocks[0].getFirstStartAddress());
return super.getVertexId(blocks[0].getFirstStartAddress());
}
}
catch (CancelledException e) {
// Will not happen with dummyMonitor
// Model has already done the work when the graph was created
}
return super.getVertexIdForAddress(address);
return super.getVertexId(address);
}
@Override
protected List<String> getVertices(AddressSetView addrSet) {
protected Set<AttributedVertex> getVertices(AddressSetView addrSet) {
if (addrSet.isEmpty()) {
return Collections.emptyList();
return Collections.emptySet();
}
// Identify all blocks which have an entry point within the selection address set
ArrayList<String> blockList = new ArrayList<String>();
Set<AttributedVertex> vertices = new HashSet<>();
try {
SymbolTable symTable = program.getSymbolTable();
CodeBlockIterator cbIter =
@@ -91,7 +91,10 @@ public class BlockModelGraphDisplayListener extends AddressBasedGraphDisplayList
else {
addrString = addr.toString();
}
blockList.add(addrString);
AttributedVertex vertex = graphDisplay.getGraph().getVertex(addrString);
if (vertex != null) {
vertices.add(vertex);
}
}
}
catch (CancelledException e) {
@@ -99,18 +102,18 @@ public class BlockModelGraphDisplayListener extends AddressBasedGraphDisplayList
// Model has already done the work when the graph was created
}
return blockList;
return vertices;
}
@Override
protected AddressSet getAddressSetForVertices(List<String> vertexIds) {
protected AddressSet getAddresses(Set<AttributedVertex> vertices) {
AddressSet addrSet = new AddressSet();
try {
// for each address string, translate it into a block
// and add it to the address set.
for (String vertexId : vertexIds) {
Address blockAddr = getAddressForVertexId(vertexId);
for (AttributedVertex vertex : vertices) {
Address blockAddr = getAddress(vertex);
if (!isValidAddress(blockAddr)) {
continue;
}
@@ -150,8 +153,8 @@ public class BlockModelGraphDisplayListener extends AddressBasedGraphDisplayList
}
private void updateVertexName(VertexGraphActionContext context) {
String vertexId = context.getClickedVertex().getId();
Address address = getAddressForVertexId(vertexId);
AttributedVertex vertex = context.getClickedVertex();
Address address = getAddress(vertex);
Symbol symbol = program.getSymbolTable().getPrimarySymbol(address);
if (symbol == null) {
@@ -165,8 +168,8 @@ public class BlockModelGraphDisplayListener extends AddressBasedGraphDisplayList
}
@Override
public GraphDisplayListener cloneWith(GraphDisplay graphDisplay) {
return new BlockModelGraphDisplayListener(tool, blockModel, graphDisplay);
public GraphDisplayListener cloneWith(GraphDisplay newGraphDisplay) {
return new BlockModelGraphDisplayListener(tool, blockModel, newGraphDisplay);
}
}
@@ -17,7 +17,8 @@ package ghidra.graph.program;
import static org.junit.Assert.*;
import java.util.*;
import java.util.HashSet;
import java.util.Set;
import org.junit.Test;
@@ -27,6 +28,7 @@ import ghidra.program.model.block.CodeBlockModel;
import ghidra.program.util.ProgramLocation;
import ghidra.program.util.ProgramSelection;
import ghidra.service.graph.AttributedGraph;
import ghidra.service.graph.AttributedVertex;
import ghidra.util.task.TaskMonitor;
public class BlockGraphEventTest extends AbstractBlockGraphTest {
@@ -54,13 +56,13 @@ public class BlockGraphEventTest extends AbstractBlockGraphTest {
@Test
public void testGhidraLocationChanged() {
codeBrowser.goTo(new ProgramLocation(program, addr(0x1002239)));
assertEquals("01002239", display.getFocusedVertex());
assertEquals("01002239", display.getFocusedVertex().getId());
codeBrowser.goTo(new ProgramLocation(program, addr(0x1002200)));
assertEquals("01002200", display.getFocusedVertex());
assertEquals("01002200", display.getFocusedVertex().getId());
// also try a location that is not the start of a block
codeBrowser.goTo(new ProgramLocation(program, addr(0x100223a)));
assertEquals("01002239", display.getFocusedVertex());
assertEquals("01002239", display.getFocusedVertex().getId());
}
@@ -71,38 +73,38 @@ public class BlockGraphEventTest extends AbstractBlockGraphTest {
@Test
public void testGhidraSelectionChanged() {
setSelection(addrSet(0x1002239, 0x1002241));
Set<String> selected = new HashSet<>(display.getSelectedVertices());
Set<AttributedVertex> selected = new HashSet<>(display.getSelectedVertices());
assertEquals(3, selected.size());
assertTrue(selected.contains("01002239"));
assertTrue(selected.contains("0100223c"));
assertTrue(selected.contains("0100223e"));
assertTrue(selected.contains(graph.getVertex("01002239")));
assertTrue(selected.contains(graph.getVertex("0100223c")));
assertTrue(selected.contains(graph.getVertex("0100223e")));
setSelection(new AddressSet(addr(0x1002200), addr(0x1002210)));
selected = new HashSet<>(display.getSelectedVertices());
assertEquals(2, selected.size());
assertTrue(selected.contains("01002200"));
assertTrue(selected.contains("01002203"));
assertTrue(selected.contains(graph.getVertex("01002200")));
assertTrue(selected.contains(graph.getVertex("01002203")));
}
@Test
public void testGraphNodeFocused() {
display.focusChanged("01002203");
display.focusChanged(graph.getVertex("01002203"));
assertEquals(addr(0x01002203), codeBrowser.getCurrentLocation().getAddress());
display.focusChanged("0100223c");
display.focusChanged(graph.getVertex("0100223c"));
assertEquals(addr(0x0100223c), codeBrowser.getCurrentLocation().getAddress());
}
@Test
public void testGraphNodesSelected() {
display.selectionChanged(Arrays.asList("01002239", "0100223c"));
display.selectionChanged(Set.of(graph.getVertex("01002239"), graph.getVertex("0100223c")));
ProgramSelection selection = codeBrowser.getCurrentSelection();
assertEquals(addr(0x01002239), selection.getMinAddress());
assertEquals(addr(0x0100223d), selection.getMaxAddress());
display.selectionChanged(Arrays.asList("01002200", "01002203"));
display.selectionChanged(Set.of(graph.getVertex("01002200"), graph.getVertex("01002203")));
selection = codeBrowser.getCurrentSelection();
assertEquals(addr(0x01002200), selection.getMinAddress());
assertEquals(addr(0x01002204), selection.getMaxAddress());
@@ -15,7 +15,8 @@
*/
package ghidra.graph.program;
import java.util.*;
import java.util.HashSet;
import java.util.Set;
import docking.action.DockingAction;
import docking.widgets.EventTrigger;
@@ -26,12 +27,11 @@ import ghidra.util.task.TaskMonitor;
public class TestGraphDisplay implements GraphDisplay {
private Set<String> definedVertexAttributes = new HashSet<>();
private Set<String> definedEdgeAttributes = new HashSet<>();
private String vertexAttributeName;
private AttributedGraph graph;
private String graphDescription;
private GraphDisplayListener listener;
private String currentFocusedVertex;
private List<String> currentSelection;
private AttributedVertex focusedVertex;
private Set<AttributedVertex> currentSelection;
@Override
public void setGraphDisplayListener(GraphDisplayListener listener) {
@@ -39,20 +39,22 @@ public class TestGraphDisplay implements GraphDisplay {
}
@Override
public void setLocationFocus(String vertexID, EventTrigger eventTrigger) {
currentFocusedVertex = vertexID;
}
public String getFocusedVertex() {
return currentFocusedVertex;
public void setFocusedVertex(AttributedVertex vertex, EventTrigger eventTrigger) {
focusedVertex = vertex;
}
@Override
public void selectVertices(List<String> vertexList, EventTrigger eventTrigger) {
public AttributedVertex getFocusedVertex() {
return focusedVertex;
}
@Override
public void selectVertices(Set<AttributedVertex> vertexList, EventTrigger eventTrigger) {
currentSelection = vertexList;
}
public List<String> getSelectedVertices() {
@Override
public Set<AttributedVertex> getSelectedVertices() {
return currentSelection;
}
@@ -74,7 +76,7 @@ public class TestGraphDisplay implements GraphDisplay {
@Override
public void setVertexLabel(String attributeName, int alignment, int size, boolean monospace,
int maxLines) {
vertexAttributeName = attributeName;
// nothing
}
@Override
@@ -91,7 +93,7 @@ public class TestGraphDisplay implements GraphDisplay {
}
@Override
public void updateVertexName(String id, String newName) {
public void updateVertexName(AttributedVertex vertex, String newName) {
// nothing
}
@@ -100,16 +102,17 @@ public class TestGraphDisplay implements GraphDisplay {
return graphDescription;
}
@Override
public AttributedGraph getGraph() {
return graph;
}
public void focusChanged(String vertexId) {
listener.locationFocusChanged(vertexId);
public void focusChanged(AttributedVertex vertex) {
listener.locationFocusChanged(vertex);
}
public void selectionChanged(List<String> vertexIds) {
listener.selectionChanged(vertexIds);
public void selectionChanged(Set<AttributedVertex> vertices) {
listener.selectionChanged(vertices);
}
@Override
@@ -117,14 +120,4 @@ public class TestGraphDisplay implements GraphDisplay {
// do nothing, actions are not supported by this display
}
@Override
public String getFocusedVertexId() {
return currentFocusedVertex;
}
@Override
public Set<String> getSelectedVertexIds() {
return new HashSet<String>(currentSelection);
}
}