-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
76196c8
commit 6cb0132
Showing
3 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
src/main/java/palaiologos/kamilalisp/runtime/graph/ChinesePostman.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package palaiologos.kamilalisp.runtime.graph; | ||
|
||
import org.jgrapht.alg.cycle.BergeGraphInspector; | ||
import org.jgrapht.graph.DefaultEdge; | ||
import palaiologos.kamilalisp.atom.Atom; | ||
import palaiologos.kamilalisp.atom.Environment; | ||
import palaiologos.kamilalisp.atom.Lambda; | ||
import palaiologos.kamilalisp.atom.PrimitiveFunction; | ||
|
||
import java.util.List; | ||
|
||
public class ChinesePostman extends PrimitiveFunction implements Lambda { | ||
@Override | ||
public Atom apply(Environment env, List<Atom> args) { | ||
assertArity(args, 1); | ||
GraphWrapper wp = args.get(0).getUserdata(GraphWrapper.class); | ||
var inspector = new org.jgrapht.alg.cycle.ChinesePostman<Atom, DefaultEdge>(); | ||
return new Atom(new GraphPath(inspector.getCPPSolution(wp.getGraph()))); | ||
} | ||
|
||
@Override | ||
protected String name() { | ||
return "graph:chinese-postman"; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/main/java/palaiologos/kamilalisp/runtime/graph/GraphPath.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package palaiologos.kamilalisp.runtime.graph; | ||
|
||
import org.jgrapht.graph.DefaultEdge; | ||
import palaiologos.kamilalisp.atom.Atom; | ||
import palaiologos.kamilalisp.atom.Userdata; | ||
|
||
import java.math.BigDecimal; | ||
import java.math.BigInteger; | ||
|
||
public class GraphPath implements Userdata { | ||
private final org.jgrapht.GraphPath<Atom, DefaultEdge> path; | ||
|
||
public GraphPath(org.jgrapht.GraphPath<Atom, DefaultEdge> path) { | ||
this.path = path; | ||
} | ||
|
||
@Override | ||
public Atom field(Object key) { | ||
if(!(key instanceof String s)) | ||
throw new IllegalArgumentException("key must be a string"); | ||
switch (s) { | ||
case "length" -> { | ||
return new Atom(BigInteger.valueOf(path.getLength())); | ||
} | ||
case "weight" -> { | ||
return new Atom(BigDecimal.valueOf(path.getWeight())); | ||
} | ||
case "start" -> { | ||
return path.getStartVertex(); | ||
} | ||
case "end" -> { | ||
return path.getEndVertex(); | ||
} | ||
case "vertices" -> { | ||
return new Atom(path.getVertexList()); | ||
} | ||
default -> { | ||
throw new IllegalArgumentException("unknown key: " + key); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public String toDisplayString() { | ||
return "graph:path of length " + path.getLength(); | ||
} | ||
|
||
@Override | ||
public String typeName() { | ||
return "graph:path"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters