use of org.eclipse.elk.graph.ElkPort in project elk by eclipse.
the class RandomGraphGenerator method connect.
/**
* Connects two nodes with an edge.
*
* @param source
* the source node
* @param target
* the target node
* @param directed
* whether the edge should be directed or undirected
* @return the edge
*/
private ElkEdge connect(final ElkConnectableShape source, final ElkConnectableShape target) {
ElkNode sourceNode = null;
ElkNode targetNode = null;
ElkPort sourcePort = null;
ElkPort targetPort = null;
if (source instanceof ElkNode) {
sourceNode = (ElkNode) source;
}
if (source instanceof ElkPort) {
sourcePort = (ElkPort) source;
sourceNode = sourcePort.getParent();
}
if (target instanceof ElkPort) {
targetPort = (ElkPort) target;
targetNode = targetPort.getParent();
}
if (target instanceof ElkNode) {
targetNode = (ElkNode) target;
}
ElkEdge edge = ElkGraphUtil.createEdge(null);
if (get(GeneratorOptions.ENABLE_PORTS)) {
if (source instanceof ElkNode) {
sourcePort = retrievePort(sourceNode, true);
}
if (!isHypernode(sourceNode)) {
// remove node
if (edge.getSources().contains(sourceNode)) {
edge.getSources().remove(sourceNode);
}
edge.getSources().add(sourcePort);
// edge.setSourcePort(sourcePort);
}
if (!isHypernode(targetNode)) {
if (targetPort == null) {
targetPort = retrievePort(targetNode, false);
}
if (edge.getTargets().contains(targetNode)) {
edge.getTargets().remove(targetNode);
}
edge.getTargets().remove(target);
edge.getTargets().add(targetPort);
// edge.setTargetPort(targetPort);
}
} else {
edge.getSources().add(sourceNode);
edge.getTargets().add(targetNode);
}
if (get(GeneratorOptions.EDGE_LABELS)) {
addEdgeLabel(edge);
}
ElkGraphUtil.updateContainment(edge);
return edge;
}
use of org.eclipse.elk.graph.ElkPort in project elk by eclipse.
the class RandomGraphGenerator method retrievePort.
/**
* Retrieves a port for a new edge to connect to the given node through. This can either be a
* newly created port, or an existing one. Which one it is depends on the chance of ports to be
* reused.
*
* <p>
* An outgoing edge will only ever try to reuse ports that only have outgoing edges connected to
* them. The same is true for incoming edges and ports with only incoming edges.
* </p>
*
* @param node
* the node to add the port to.
* @param source
* {@code true} if the port will be used as a source port, {@code false} if it will
* be used as a target port.
* @return the new or existing port.
*/
private ElkPort retrievePort(final ElkNode node, final boolean source) {
// We might want to reuse an existing port
float reusePortsChance = get(GeneratorOptions.USE_EXISTING_PORTS_CHANCE).floatVal(random);
if (reusePortsChance > 0.0f && random.nextFloat() < reusePortsChance) {
// Collect candidate ports for reuse
List<ElkPort> reuseCandidates = Lists.newLinkedList();
for (ElkPort port : node.getPorts()) {
// Two flags indicating whether the port already has edges pointing in the right
// or wrong direction connected to it
boolean connectedToDesiredEdges = false;
boolean connectedToBadEdges = false;
// for (ElkEdge edge : port.getEdges()) {
// connectedToDesiredEdges = (source && edge.getSourcePort() == port)
// || (!source && edge.getTargetPort() == port);
// connectedToBadEdges = (source && edge.getTargetPort() == port)
// || (!source && edge.getSourcePort() == port);
// }
// TODO -apo- not exactly the same, but makes more sense...
connectedToDesiredEdges = (!port.getIncomingEdges().isEmpty() && !source) || (!port.getOutgoingEdges().isEmpty() && source);
connectedToBadEdges = (!port.getIncomingEdges().isEmpty() && source) || (!port.getOutgoingEdges().isEmpty() && !source);
// the port, it qualifies as a candidate for reuse
if (connectedToDesiredEdges && !connectedToBadEdges) {
reuseCandidates.add(port);
}
}
// If there are candidates for reuse, choose one at random
if (!reuseCandidates.isEmpty()) {
return reuseCandidates.get(randomInt(0, reuseCandidates.size() - 1));
}
}
// We were unable to reuse an existing port, so create a new one and return that
return createPort(node, source);
}
use of org.eclipse.elk.graph.ElkPort in project elk by eclipse.
the class RandomGraphGenerator method findNodeWithDFS.
/**
* Returns whether a node can be reached from another node. PRECONDITION: the graph containing
* the nodes has to be acyclic! If that condition is not met the method is likely to loop
* infinitely!
*
* @param start
* the start node
* @param end
* the end node
* @return whether the end node can be reached from the start node
*/
private static boolean findNodeWithDFS(final ElkNode start, final ElkNode end) {
Queue<ElkNode> nodes = new LinkedList<ElkNode>();
nodes.add(start);
do {
ElkNode node = nodes.poll();
if (node == end) {
return true;
}
for (ElkEdge edge : node.getOutgoingEdges()) {
for (ElkConnectableShape target : edge.getTargets()) {
if (target instanceof ElkNode) {
nodes.add((ElkNode) target);
} else if (target instanceof ElkPort) {
ElkPort port = (ElkPort) target;
nodes.add(port.getParent());
}
}
}
} while (!nodes.isEmpty());
return false;
}
use of org.eclipse.elk.graph.ElkPort in project elk by eclipse.
the class RandomGraphGenerator method createPort.
/**
* Creates a port.
*
* @param node
* the containing node
* @param source
* {@code true} if the port will be used as a source port, {@code false} if it will
* be used as a target port.
* @return the port
*/
private ElkPort createPort(final ElkNode node, final boolean source) {
ElkPort port = ElkGraphUtil.createPort(null);
node.getPorts().add(port);
// create label and identifier
String portId = String.valueOf(portLabelCounter++);
if (get(GeneratorOptions.CREATE_PORT_LABELS)) {
ElkGraphUtil.createLabel("P" + portId, port);
}
port.setIdentifier("p" + portId);
// set size of the port
if (get(GeneratorOptions.SET_PORT_SIZE)) {
port.setWidth(get(GeneratorOptions.PORT_WIDTH).floatVal(random));
port.setHeight(get(GeneratorOptions.PORT_HEIGHT).floatVal(random));
}
if (get(GeneratorOptions.PORT_CONSTRAINTS).isSideFixed()) {
// determine a random node side
int northProb, eastProb, southProb, westProb;
if (source) {
northProb = get(GeneratorOptions.OUTGOING_NORTH_SIDE);
eastProb = get(GeneratorOptions.OUTGOING_EAST_SIDE);
southProb = get(GeneratorOptions.OUTGOING_SOUTH_SIDE);
westProb = get(GeneratorOptions.OUTGOING_WEST_SIDE);
} else {
northProb = get(GeneratorOptions.INCOMING_NORTH_SIDE);
eastProb = get(GeneratorOptions.INCOMING_EAST_SIDE);
southProb = get(GeneratorOptions.INCOMING_SOUTH_SIDE);
westProb = get(GeneratorOptions.INCOMING_WEST_SIDE);
}
int p = random.nextInt(northProb + eastProb + southProb + westProb);
PortSide portSide;
if (p < northProb) {
portSide = PortSide.NORTH;
} else if (p < northProb + eastProb) {
portSide = PortSide.EAST;
} else if (p < northProb + eastProb + southProb) {
portSide = PortSide.SOUTH;
} else {
portSide = PortSide.WEST;
}
port.setProperty(CoreOptions.PORT_SIDE, portSide);
}
return port;
}
use of org.eclipse.elk.graph.ElkPort in project elk by eclipse.
the class AbstractElkGraphSemanticSequencer method sequence.
@Override
public void sequence(ISerializationContext context, EObject semanticObject) {
EPackage epackage = semanticObject.eClass().getEPackage();
ParserRule rule = context.getParserRule();
Action action = context.getAssignedAction();
Set<Parameter> parameters = context.getEnabledBooleanParameters();
if (epackage == ElkGraphPackage.eINSTANCE)
switch(semanticObject.eClass().getClassifierID()) {
case ElkGraphPackage.ELK_BEND_POINT:
sequence_ElkBendPoint(context, (ElkBendPoint) semanticObject);
return;
case ElkGraphPackage.ELK_EDGE:
sequence_EdgeLayout_ElkEdge(context, (ElkEdge) semanticObject);
return;
case ElkGraphPackage.ELK_EDGE_SECTION:
if (rule == grammarAccess.getElkEdgeSectionRule()) {
sequence_ElkEdgeSection(context, (ElkEdgeSection) semanticObject);
return;
} else if (rule == grammarAccess.getElkSingleEdgeSectionRule()) {
sequence_ElkSingleEdgeSection(context, (ElkEdgeSection) semanticObject);
return;
} else
break;
case ElkGraphPackage.ELK_LABEL:
sequence_ElkLabel_ShapeLayout(context, (ElkLabel) semanticObject);
return;
case ElkGraphPackage.ELK_NODE:
if (rule == grammarAccess.getElkNodeRule()) {
sequence_ElkNode_ShapeLayout(context, (ElkNode) semanticObject);
return;
} else if (rule == grammarAccess.getRootNodeRule()) {
sequence_RootNode_ShapeLayout(context, (ElkNode) semanticObject);
return;
} else
break;
case ElkGraphPackage.ELK_PORT:
sequence_ElkPort_ShapeLayout(context, (ElkPort) semanticObject);
return;
case ElkGraphPackage.ELK_PROPERTY_TO_VALUE_MAP_ENTRY:
sequence_Property(context, (Map.Entry) semanticObject);
return;
}
if (errorAcceptor != null)
errorAcceptor.accept(diagnosticProvider.createInvalidContextOrTypeDiagnostic(semanticObject, context));
}
Aggregations