Skip to content

Commit

Permalink
fix: allow dumping of interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
mirkolenz committed Feb 25, 2023
1 parent 1d1029a commit 0d0282c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 14 deletions.
31 changes: 27 additions & 4 deletions src/dump/aif.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,38 @@ const scheme2aif: { [key in model.SchemeType]: aifSchema.SchemeType } = {
preference: "PA",
};

export function aif(obj: model.Graph): aifSchema.Graph {
function label(node: model.NodeInterface): string {
if (node.type === "atom") {
return node.text;
} else if (node.type === "scheme") {
let text = "Unknown";

if (node.scheme.case !== undefined) {
const schemeType = node.scheme.case;
text = schemeType;

const schemeValue = model.scheme2string(node.scheme);

if (schemeValue !== "DEFAULT") {
// TODO
}
}

return text;
}

return "Unknown";
}

export function aif(obj: model.GraphInterface): aifSchema.Graph {
return {
nodes: Object.values(obj.nodes).map((entry) => nodeToAif(entry)),
edges: Object.values(obj.edges).map((entry) => edgeToAif(entry)),
locutions: [],
};
}

function nodeToAif(n: model.Node): aifSchema.Node {
function nodeToAif(n: model.NodeInterface): aifSchema.Node {
if (n.type === "atom") {
return {
nodeID: n.id,
Expand All @@ -28,7 +51,7 @@ function nodeToAif(n: model.Node): aifSchema.Node {
} else if (n.type === "scheme") {
return {
nodeID: n.id,
text: n.label(),
text: label(n),
type: n.scheme.case ? scheme2aif[n.scheme.case] : "",
timestamp: date.format(n.metadata.updated, aifSchema.DATE_FORMAT),
};
Expand All @@ -37,7 +60,7 @@ function nodeToAif(n: model.Node): aifSchema.Node {
throw new Error("Node type not supported");
}

function edgeToAif(e: model.Edge): aifSchema.Edge {
function edgeToAif(e: model.EdgeInterface): aifSchema.Edge {
return {
edgeID: e.id,
fromID: e.source,
Expand Down
2 changes: 1 addition & 1 deletion src/dump/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { protobuf as dumpProtobuf } from "./protobuf.js";
type JSONObject = { [key: string]: any };

export function json(
graph: model.Graph,
graph: model.GraphInterface,
format: "aif" | "arguebuf"
): JSONObject {
if (format === "aif") {
Expand Down
11 changes: 6 additions & 5 deletions src/dump/protobuf.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { version as libraryVersion } from "arg-services";
import * as pb from "arg-services/graph/v1/graph_pb";
import * as date from "../date.js";
import * as model from "../model/index.js";

function edgeToProtobuf(edge: model.Edge): pb.Edge {
function edgeToProtobuf(edge: model.EdgeInterface): pb.Edge {
return new pb.Edge({
source: edge.source,
target: edge.target,
Expand All @@ -14,7 +15,7 @@ function edgeToProtobuf(edge: model.Edge): pb.Edge {
});
}

function nodeToProtobuf(node: model.Node): pb.Node {
function nodeToProtobuf(node: model.NodeInterface): pb.Node {
if (node.type === "atom") {
return new pb.Node({
metadata: new pb.Metadata({
Expand Down Expand Up @@ -47,7 +48,7 @@ function nodeToProtobuf(node: model.Node): pb.Node {
}
}

export function protobuf(graph: model.Graph): pb.Graph {
export function protobuf(graph: model.GraphInterface): pb.Graph {
const edges: { [key: string]: pb.Edge } = Object.fromEntries(
Object.values(graph.edges).map((e) => [e.id, edgeToProtobuf(e)])
);
Expand Down Expand Up @@ -94,9 +95,9 @@ export function protobuf(graph: model.Graph): pb.Graph {
return new pb.Graph({
nodes: nodes,
edges: edges,
schemaVersion: graph.schemaVersion,
schemaVersion: 1,
analysts: graph.analysts,
libraryVersion: graph.libraryVersion,
libraryVersion: libraryVersion,
majorClaim: graph.majorClaim,
metadata: new pb.Metadata({
created: date.toProtobuf(graph.metadata.created),
Expand Down
3 changes: 0 additions & 3 deletions src/model/graph.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { version as arguebufVersion } from "arg-services";
import { Analyst, AnalystInterface } from "./analyst.js";
import { Edge, EdgeInterface } from "./edge.js";
import { Metadata, MetadataInterface } from "./metadata.js";
Expand Down Expand Up @@ -47,8 +46,6 @@ export class Graph implements GraphInterface {
protected _participants: Mapping<string, Participant> = {};
protected _analysts: Mapping<string, Analyst> = {};
majorClaim?: string;
readonly libraryVersion: string = arguebufVersion;
readonly schemaVersion: number = 1;
metadata: Metadata;
userdata: Userdata;

Expand Down
2 changes: 1 addition & 1 deletion src/model/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type Scheme = RawScheme["type"];
export type SchemeType = Exclude<Scheme["case"], undefined>;
export { Attack, Preference, Rephrase, Support };

const scheme2string = (scheme: Scheme) => {
export const scheme2string = (scheme: Scheme) => {
switch (scheme.case) {
case "attack":
return Attack[scheme.value];
Expand Down

0 comments on commit 0d0282c

Please # to comment.