-
Notifications
You must be signed in to change notification settings - Fork 2
can add more edge if graph is directed #50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ import * as React from 'react'; | |
| import {ReadableAdapter} from "./ReadableAdapter"; | ||
| import {select} from "d3-selection"; | ||
| import * as d3 from 'd3'; | ||
| import {IVertex, Vertex, Edge, IEdge} from "graphlabs.core.graphs"; | ||
| import {IVertex, Vertex, Edge, IEdge, DirectedEdge} from "graphlabs.core.graphs"; | ||
| import {GeometricEdge, GeometricVertex} from ".."; | ||
|
|
||
| export class WritableAdapter extends ReadableAdapter { | ||
|
|
@@ -62,84 +62,168 @@ export class WritableAdapter extends ReadableAdapter { | |
| } | ||
|
|
||
| public addEdge() { | ||
| console.log(this.graphVisualizer); | ||
| super.addEdge(); | ||
| console.log('vert1' + this.vertexOne); | ||
| console.log('vert2' + this.vertexTwo); | ||
| console.log('vert2' + this.vertexTwo) | ||
| let edge: Edge; | ||
| if (this.vertexOne.name != '' && this.vertexTwo.name != '' && this.numberOfSelectedVertices() === 2) { | ||
| // добавление ребра (именование вручную) | ||
| if (this.props.edgeNaming === true) { | ||
| let edgeName = prompt('Enter the name of the edge', ''); | ||
| if (edgeName !== '' && edgeName !== null) { | ||
| // if (this.vertexOne.name != '' && this.vertexTwo.name != '') { | ||
| if (!this.props.isDirected) { | ||
| console.log('not directed'); | ||
| if (this.vertexOne.name != '' && this.vertexTwo.name != '' && this.numberOfSelectedVertices() === 2) { | ||
| // добавление ребра (именование вручную) | ||
| if (this.props.edgeNaming === true) { | ||
| let edgeName = prompt('Enter the name of the edge', ''); | ||
| if (edgeName !== '' && edgeName !== null) { | ||
| edge = new Edge(new Vertex(this.vertexOne.name), new Vertex(this.vertexTwo.name), edgeName); | ||
| /* this.props.graph.addEdge(edge); | ||
| this.graphVisualizer.geometric.edges.push(new GeometricEdge(edge)); | ||
| this.addEdgeToSVG(new GeometricEdge(edge)); | ||
| this.updateSvg();*/ | ||
| // } | ||
| } else { | ||
| return; | ||
| } | ||
| } else { | ||
| return; | ||
| // добавление ребра (автоматическое именование) | ||
| if (this.props.namedEdges == true) { | ||
| if (this.graphVisualizer.geometric.edges.length != 0) { | ||
| let edgeNumbers = []; | ||
| for (let i = 0; i < this.graphVisualizer.geometric.edges.length; i++) { | ||
| edgeNumbers[i] = Number(this.graphVisualizer.geometric.edges[i].edge.name); | ||
| } | ||
| let maxNum = Math.max.apply(null, edgeNumbers); | ||
| edge = new Edge(new Vertex(this.vertexOne.name), new Vertex(this.vertexTwo.name), (maxNum + 1).toString()); | ||
| } else { | ||
| edge = new Edge(new Vertex(this.vertexOne.name), new Vertex(this.vertexTwo.name), '0'); | ||
| } | ||
| } else { | ||
| // добавленеи неименованных ребер | ||
| edge = new Edge(new Vertex(this.vertexOne.name), new Vertex(this.vertexTwo.name)); | ||
| } | ||
|
|
||
| } | ||
| // проверка ребра на уникальность (нет ребер с таким же именем и нет ребер между данными двумя вершинами) | ||
| let isRepeated2: boolean; | ||
| if (this.props.graph.edges.some(e => e.name === edge.name | ||
| || (e.vertexOne.name === edge.vertexOne.name && e.vertexTwo.name === edge.vertexTwo.name) | ||
| || (e.vertexOne.name === edge.vertexTwo.name && e.vertexTwo.name === edge.vertexOne.name))) { | ||
| isRepeated2 = true; | ||
| console.log('Repeated edge!'); | ||
| } | ||
| // добавление ребра в модель, геом.граф и в svg | ||
| if (isRepeated2 !== true) { | ||
| console.log(edge); | ||
| this.props.graph.addEdge(edge); | ||
| this.graphVisualizer.geometric.edges.push(new GeometricEdge(edge)); | ||
| this.addEdgeToSVG(new GeometricEdge(edge)); | ||
| this.updateSvg(); | ||
| } | ||
| } else { | ||
| // if (this.vertexOne.name != '' && this.vertexTwo.name != '') { | ||
| alert('Для добавления ребра необходимо выбрать две вершины!') | ||
| } | ||
|
|
||
| /* let isRepeated: boolean; | ||
| for (let i = 0; i < this.props.graph.edges.length; i++) { | ||
| if (this.props.graph.edges[i].vertexOne.name == this.vertexOne.name && this.props.graph.edges[i].vertexTwo.name == this.vertexTwo.name | ||
| || this.props.graph.edges[i].vertexOne.name == this.vertexTwo.name && this.props.graph.edges[i].vertexTwo.name == this.vertexOne.name) { | ||
| isRepeated = true; | ||
| } | ||
| this.vertexOne.rename(''); | ||
| this.vertexTwo.rename(''); | ||
| } | ||
| else{ | ||
| console.log('Directed'); | ||
| if (this.numberOfSelectedVertices() ===0 ) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. странно, что эта проверка есть для ориентированного графа и её нет для неориентированного.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. она есть, там идет проверка - что выбелено две вершины, иначе ошибка |
||
| alert('Для добавления ребра необходимо выбрать хотя бы одну вершину!') | ||
| } | ||
|
|
||
| if (this.vertexOne.name!='' && this.numberOfSelectedVertices() ===1 ) { | ||
| console.log('one vertex'); | ||
| if (this.props.edgeNaming === true) { | ||
| let edgeName = prompt('Введите имя дуги:', ''); | ||
| if (edgeName !== '' && edgeName !== null) { | ||
| edge = new DirectedEdge(new Vertex(this.vertexOne.name), new Vertex(this.vertexOne.name), edgeName); | ||
| } else { | ||
| return; | ||
| } | ||
| if (isRepeated == true) { | ||
| console.log("Repeated item!"); | ||
| } else { */ | ||
| // добавление ребра (автоматическое именование) | ||
| if (this.props.namedEdges == true) { | ||
| if (this.graphVisualizer.geometric.edges.length != 0) { | ||
| let edgeNumbers = []; | ||
| for (let i = 0; i < this.graphVisualizer.geometric.edges.length; i++) { | ||
| edgeNumbers[i] = Number(this.graphVisualizer.geometric.edges[i].edge.name); | ||
| } | ||
| let maxNum = Math.max.apply(null, edgeNumbers); | ||
| edge = new Edge(new Vertex(this.vertexOne.name), new Vertex(this.vertexTwo.name), (maxNum + 1).toString()); | ||
| } else { | ||
| edge = new Edge(new Vertex(this.vertexOne.name), new Vertex(this.vertexTwo.name), '0'); | ||
| } else { | ||
| // добавление ребра (автоматическое именование) | ||
| console.log('adding'); | ||
| if (this.props.namedEdges == true) { | ||
| if (this.graphVisualizer.geometric.edges.length != 0) { | ||
| let edgeNumbers = []; | ||
| for (let i = 0; i < this.graphVisualizer.geometric.edges.length; i++) { | ||
| edgeNumbers[i] = Number(this.graphVisualizer.geometric.edges[i].edge.name); | ||
| } | ||
| } else { // добавленеи неименованных ребер | ||
| edge = new Edge(new Vertex(this.vertexOne.name), new Vertex(this.vertexTwo.name)); | ||
| let maxNum = Math.max.apply(null, edgeNumbers); | ||
| edge = new DirectedEdge(new Vertex(this.vertexOne.name), new Vertex(this.vertexOne.name), (maxNum + 1).toString()); | ||
| } else { | ||
| edge = new DirectedEdge(new Vertex(this.vertexOne.name), new Vertex(this.vertexOne.name), '0'); | ||
| } | ||
| /*console.log(edge); | ||
| } else { | ||
| // добавленеи неименованных ребер | ||
| edge = new DirectedEdge(new Vertex(this.vertexOne.name), new Vertex(this.vertexOne.name)); | ||
| } | ||
|
|
||
| let isRepeated2: boolean; | ||
| if (this.props.graph.edges.some(e => e.name === edge.name | ||
| || (e.vertexOne.name === edge.vertexOne.name && e.vertexTwo.name === edge.vertexOne.name))) { | ||
| isRepeated2 = true; | ||
| console.log('Repeated edge!'); | ||
| } | ||
|
|
||
| if (isRepeated2 !== true) { | ||
| console.log(edge); | ||
| this.props.graph.addEdge(edge); | ||
| this.graphVisualizer.geometric.edges.push(new GeometricEdge(edge)); | ||
| this.addEdgeToSVG(new GeometricEdge(edge)); | ||
| this.updateSvg();*/ | ||
|
Comment on lines
+164
to
-117
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. выглядит как дублирующийся кусок кода, который тоже можно вытащить в отдельный метод |
||
| // } | ||
| // } | ||
| } | ||
| // проверка ребра на уникальность (нет ребер с таким же именем и нет ребер между данными двумя вершинами) | ||
| let isRepeated2: boolean; | ||
| if (this.props.graph.edges.some(e => e.name === edge.name | ||
| || (e.vertexOne.name === edge.vertexOne.name && e.vertexTwo.name === edge.vertexTwo.name) | ||
| || (e.vertexOne.name === edge.vertexTwo.name && e.vertexTwo.name === edge.vertexOne.name))) { | ||
| isRepeated2 = true; | ||
| console.log('Repeated edge!'); | ||
| this.updateSvg(); | ||
| } | ||
|
|
||
| this.vertexOne.rename(''); | ||
| } | ||
|
|
||
| } | ||
| // добавление ребра в модель, геом.граф и в svg | ||
| if (isRepeated2 !== true) { | ||
| console.log(edge); | ||
| this.props.graph.addEdge(edge); | ||
| this.graphVisualizer.geometric.edges.push(new GeometricEdge(edge)); | ||
| this.addEdgeToSVG(new GeometricEdge(edge)); | ||
| this.updateSvg(); | ||
| else | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. else if this.numberOfSelectedVertices() === 2 |
||
| { | ||
| // добавление ребра (именование вручную) | ||
| if (this.props.edgeNaming === true) { | ||
| let edgeName = prompt('Введите имя дуги:', ''); | ||
| if (edgeName !== '' && edgeName !== null) { | ||
| edge = new DirectedEdge(new Vertex(this.vertexOne.name), new Vertex(this.vertexTwo.name), edgeName); | ||
| } else { | ||
| return; | ||
| } | ||
| } else { | ||
| // добавление ребра (автоматическое именование) | ||
| console.log('adding'); | ||
| if (this.props.namedEdges == true) { | ||
| if (this.graphVisualizer.geometric.edges.length != 0) { | ||
| let edgeNumbers = []; | ||
| for (let i = 0; i < this.graphVisualizer.geometric.edges.length; i++) { | ||
| edgeNumbers[i] = Number(this.graphVisualizer.geometric.edges[i].edge.name); | ||
| } | ||
| let maxNum = Math.max.apply(null, edgeNumbers); | ||
| edge = new DirectedEdge(new Vertex(this.vertexOne.name), new Vertex(this.vertexTwo.name), (maxNum + 1).toString()); | ||
| } else { | ||
| edge = new DirectedEdge(new Vertex(this.vertexOne.name), new Vertex(this.vertexTwo.name), '0'); | ||
| } | ||
| } else { | ||
| // добавленеи неименованных ребер | ||
| edge = new DirectedEdge(new Vertex(this.vertexOne.name), new Vertex(this.vertexTwo.name)); | ||
| } | ||
|
Comment on lines
+177
to
+202
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Есть мнение, что вот этот кусок кода повторяет случай с рефлексивным ребром с точностью до имени второй вершины. |
||
|
|
||
| let isRepeated2: boolean; | ||
| if (this.props.graph.edges.some(e => e.name === edge.name | ||
| || (e.vertexOne.name === edge.vertexOne.name && e.vertexTwo.name === edge.vertexTwo.name))) { | ||
| isRepeated2 = true; | ||
| console.log('Repeated edge!'); | ||
| } | ||
|
|
||
| if (isRepeated2 !== true) { | ||
| console.log(edge); | ||
| this.props.graph.addEdge(edge); | ||
| this.graphVisualizer.geometric.edges.push(new GeometricEdge(edge)); | ||
| this.addEdgeToSVG(new GeometricEdge(edge)); | ||
| this.updateSvg(); | ||
| } | ||
|
|
||
| this.vertexOne.rename(''); | ||
| this.vertexTwo.rename(''); | ||
|
|
||
| } | ||
| } | ||
|
|
||
|
|
||
| } | ||
| else { | ||
| alert('Для добавления ребра необходимо выбрать две вершины!') | ||
| } | ||
| this.vertexOne.rename(''); | ||
| this.vertexTwo.rename(''); | ||
| } | ||
|
|
||
| public removeVertex() { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
вот тут, наверное, нужен else и показать алерт, что что-то не в порядке!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
логично, странно, что не добавили алерт, а просто сделали вывод в консоль
добавлю