Skip to content

Commit 8703063

Browse files
authored
Added graph rendering via the AIG graph component (TweetyProjectTeam#17)
- added AigGraphPlotter for rendering graphs in general - added AigSerialisationPlotter for rendering the serialisation of Dung-AFs
1 parent e61d3dd commit 8703063

File tree

14 files changed

+12240
-13
lines changed

14 files changed

+12240
-13
lines changed

org-tweetyproject-arg-deductive/src/main/java/org/tweetyproject/arg/deductive/util/RandomDeductiveKnowledgeBaseGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public DeductiveKnowledgeBase next() {
8989
return kb;
9090
}
9191

92-
92+
9393
/**
9494
* Description missing
9595
* @param <C> Description missing
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* This file is part of "TweetyProject", a collection of Java libraries for
3+
* logical aspects of artificial intelligence and knowledge representation.
4+
*
5+
* TweetyProject is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License version 3 as
7+
* published by the Free Software Foundation.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*
17+
* Copyright 2025 The TweetyProject Team <http://tweetyproject.org/contact/>
18+
*/
19+
package org.tweetyproject.arg.dung.examples;
20+
21+
import org.tweetyproject.arg.dung.semantics.Semantics;
22+
import org.tweetyproject.arg.dung.serialisability.util.AigSerialisationPlotter;
23+
import org.tweetyproject.arg.dung.syntax.Argument;
24+
import org.tweetyproject.arg.dung.syntax.DungTheory;
25+
import org.tweetyproject.arg.dung.util.DefaultDungTheoryGenerator;
26+
import org.tweetyproject.graphs.util.AigGraphPlotter;
27+
28+
import java.io.IOException;
29+
import java.net.URISyntaxException;
30+
31+
/**
32+
* Example Usage of the {@link AigGraphPlotter} which renders a given graph via html in the webbrowser
33+
*
34+
* @author Lars Bengel
35+
*/
36+
public class GraphPlotterExample {
37+
public static void main(String[] args) throws URISyntaxException, IOException {
38+
DungTheory theory = new DefaultDungTheoryGenerator(9, 0.2).next();
39+
40+
// Initialize plotter for argumentation framework
41+
AigGraphPlotter<DungTheory,Argument> plotter = new AigGraphPlotter<>(theory);
42+
43+
// Set options for rendering of the graph, eg
44+
plotter.setLinkDeletable(false);
45+
46+
// Render graph
47+
plotter.show();
48+
49+
// Plot an argumentation framework and its serialisation wrt. complete semantics
50+
//AigSerialisationPlotter.showSerialisation(theory, Semantics.CO);
51+
52+
53+
}
54+
}

org-tweetyproject-arg-dung/src/main/java/org/tweetyproject/arg/dung/serialisability/semantics/SerialisationGraph.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
*/
1919
package org.tweetyproject.arg.dung.serialisability.semantics;
2020

21-
import org.tweetyproject.arg.dung.reasoner.AbstractExtensionReasoner;
2221
import org.tweetyproject.arg.dung.reasoner.SerialisedExtensionReasoner;
2322
import org.tweetyproject.arg.dung.semantics.Extension;
2423
import org.tweetyproject.arg.dung.semantics.Semantics;
@@ -46,6 +45,8 @@ public class SerialisationGraph implements Graph<SerialisationState> {
4645
/** explicit storage of children for each node */
4746
private Map<SerialisationState, Set<SerialisationState>> children = new HashMap<>();
4847

48+
private Collection<GeneralEdge<SerialisationState>> edges = new HashSet<>();
49+
4950

5051
/**
5152
* Construct a serialisation graph for the given argumentation framework and serialisation reasoner
@@ -65,7 +66,7 @@ public SerialisationGraph(DungTheory theory, SerialisedExtensionReasoner reasone
6566
DungTheory reduct = theory.getReduct(ext);
6667
SerialisationState node = new SerialisationState(reduct, new Extension<>(ext), reasoner.isTerminal(reduct, ext));
6768
this.add(node);
68-
this.add(new DirectedEdge<>(predecessor, node));
69+
this.add(new DirectedEdge<>(predecessor, node, set.toString()));
6970
predecessor = node;
7071
}
7172
predecessor = root;
@@ -84,7 +85,7 @@ public SerialisationGraph(DungTheory theory, Semantics semantics) {
8485
/** Pretty print of the graph.
8586
* @return the pretty print of the graph.
8687
*/
87-
public String prettyPrint(){
88+
public String prettyPrint() {
8889
StringBuilder output = new StringBuilder();
8990
for (SerialisationState serialisationNode : this)
9091
output.append("node(").append(serialisationNode.toString()).append(").\n");
@@ -162,6 +163,7 @@ public boolean add(GeneralEdge<SerialisationState> edge) {
162163
result |= this.add(e.getNodeB());
163164
result |= children.get(e.getNodeA()).add(e.getNodeB());
164165
result |= parents.get(e.getNodeB()).add(e.getNodeA());
166+
result |= edges.add(e);
165167
return result;
166168
}
167169

@@ -190,19 +192,18 @@ public boolean areAdjacent(SerialisationState a, SerialisationState b) {
190192

191193
@Override
192194
public GeneralEdge<SerialisationState> getEdge(SerialisationState a, SerialisationState b) {
193-
return new DirectedEdge<>(a, b);
195+
for (GeneralEdge<SerialisationState> edge : edges) {
196+
DirectedEdge<SerialisationState> e = (DirectedEdge<SerialisationState>) edge;
197+
if (e.getNodeA().equals(a) && e.getNodeB().equals(b)) {
198+
return new DirectedEdge<>(e.getNodeA(), e.getNodeB(), e.getLabel());
199+
}
200+
}
201+
return null;
194202
}
195203

196204
@Override
197205
public Collection<? extends GeneralEdge<? extends SerialisationState>> getEdges() {
198-
Collection<DirectedEdge<SerialisationState>> edges = new HashSet<>();
199-
for (SerialisationState node: this) {
200-
for (SerialisationState succ: this.children.get(node)) {
201-
DirectedEdge<SerialisationState> edge = new DirectedEdge<>(node, succ);
202-
edges.add(edge);
203-
}
204-
}
205-
return edges;
206+
return new HashSet<>(this.edges);
206207
}
207208

208209
@Override
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package org.tweetyproject.arg.dung.serialisability.util;
2+
3+
import org.tweetyproject.arg.dung.reasoner.SerialisedExtensionReasoner;
4+
import org.tweetyproject.arg.dung.semantics.Extension;
5+
import org.tweetyproject.arg.dung.semantics.Semantics;
6+
import org.tweetyproject.arg.dung.serialisability.semantics.SerialisationGraph;
7+
import org.tweetyproject.arg.dung.serialisability.semantics.SerialisationState;
8+
import org.tweetyproject.arg.dung.syntax.Argument;
9+
import org.tweetyproject.arg.dung.syntax.DungTheory;
10+
import org.tweetyproject.graphs.util.AigGraphPlotter;
11+
12+
import java.awt.*;
13+
import java.io.File;
14+
import java.io.IOException;
15+
import java.nio.file.Files;
16+
import java.nio.file.Path;
17+
import java.nio.file.Paths;
18+
19+
20+
public abstract class AigSerialisationPlotter extends AigGraphPlotter<DungTheory,Argument> {
21+
22+
public AigSerialisationPlotter(DungTheory graph) {
23+
super(graph);
24+
}
25+
26+
public static void showSerialisation(DungTheory theory, Semantics semantics) {
27+
SerialisedExtensionReasoner reasoner = new SerialisedExtensionReasoner(semantics);
28+
29+
SerialisationGraph serialisation = reasoner.getSerialisationGraph(theory);
30+
SerialisationState root = new SerialisationState(theory, new Extension<>(), reasoner.isTerminal(theory, new Extension<>()));
31+
32+
AigGraphPlotter<DungTheory,Argument> plotter1 = new AigGraphPlotter<>(theory);
33+
AigGraphPlotter<SerialisationGraph,SerialisationState> plotter2 = new AigGraphPlotter<>(serialisation);
34+
plotter2.makeLeveled(root);
35+
36+
Path outputPath = Paths.get("index.html");
37+
try {
38+
String template = Files.readString(Paths.get(getResource("aiggraph/serialisation.template")));
39+
String output = String.format(template,
40+
plotter1.write(), plotter2.write(),
41+
getResource("aiggraph/favicon.ico"), getResource("aiggraph/style.css"),
42+
getResource("aiggraph/load-mathjax.js"), getResource("aiggraph/graph-component.js")
43+
);
44+
45+
Files.writeString(outputPath, output);
46+
47+
// show graph in web browser
48+
File htmlFile = new File("index.html");
49+
Desktop.getDesktop().browse(htmlFile.toURI());
50+
} catch (IOException e) {
51+
throw new RuntimeException(e);
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)