Skip to content

Commit

Permalink
added output adapter so view multiplex graph on multiplexgraph.html a…
Browse files Browse the repository at this point in the history
…nalog zu customgraphs on graph.html
  • Loading branch information
MarenHanke committed Jan 9, 2024
1 parent 6d9d6c4 commit 849a1ae
Show file tree
Hide file tree
Showing 12 changed files with 380 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,6 @@ public Response getGraphs(@DefaultValue("0") @QueryParam("firstIndex") String fi
* using the "-" delimiter.
* @return The graphs. Or an error xml.
*/

@GET
@Path("multiplexgraphs")
@Produces(MediaType.TEXT_XML)
Expand Down Expand Up @@ -819,6 +818,31 @@ public Response getMultiplexGraphs(@DefaultValue("0") @QueryParam("firstIndex")
}
}

/**
* Returns the ids (or meta information) of multiple graphs.
*
* @param keyMultiplexStr key of the multiplex graph we want to get the customgraph layers of
* @return The graphs. Or an error xml.
*/
@GET
@Path("multiplexlayers")
@Produces(MediaType.TEXT_XML)
@ApiResponses(value = { @ApiResponse(code = 200, message = "Success"),
@ApiResponse(code = 401, message = "Unauthorized") })
@ApiOperation(tags = {"show"}, value = "Get Infos of a MultiplexGraphs layers/CustomGraphs", notes = "Returns the meta information of the layers of a multiplexgraph.")
public Response getLayersOfMultiplexGraph(@DefaultValue("0") @QueryParam("keyMultiplex") String keyMultiplexStr) {
try {
String username = ((UserAgent) Context.getCurrent().getMainAgent()).getLoginName();
List<CustomGraphMeta> queryResults;
queryResults = database.getMultiplexGraphMetaDataOfLayersEfficiently(username, keyMultiplexStr);
String responseStr = requestHandler.writeGraphMetasEfficiently(queryResults);
return Response.ok(responseStr).build();
} catch (Exception e) {
requestHandler.log(Level.SEVERE, "", e);
return requestHandler.writeError(Error.INTERNAL, "Internal system error.");
}
}

/**
* Returns a graph in a specified output format.
*
Expand Down Expand Up @@ -864,6 +888,51 @@ public Response getGraph(@DefaultValue("GRAPH_ML") @QueryParam("outputFormat") S

}

/**
* Returns a graph in a specified output format.
*
* @param graphIdStr
* The graph id.
* @param graphOutputFormatStr
* The name of the graph output format.
* @return The graph output. Or an error xml.
*/
@GET
@Produces(MediaType.TEXT_PLAIN)
@ApiResponses(value = { @ApiResponse(code = 200, message = "Success"),
@ApiResponse(code = 401, message = "Unauthorized") })
@Path("multiplexgraphs/{graphId}")
@ApiOperation(tags = {"export"}, value = "Export Graph", notes = "Returns a graph in a specified output format.")
public Response getMultiplexGraph(@DefaultValue("GRAPH_ML") @QueryParam("outputFormat") String graphOutputFormatStr,
@PathParam("graphId") String graphIdStr) {
try {

String username = ((UserAgent) Context.getCurrent().getMainAgent()).getLoginName();
GraphOutputFormat format;
try {

format = GraphOutputFormat.valueOf(graphOutputFormatStr);
} catch (Exception e) {
requestHandler.log(Level.WARNING, "user: " + username, e);
return requestHandler.writeError(Error.PARAMETER_INVALID,
"Specified graph output format does not exist.");
}

MultiplexGraph graph = database.getMultiplexGraph(username, graphIdStr); //done

if (graph == null)
return requestHandler.writeError(Error.PARAMETER_INVALID,
"Graph does not exist: graph key " + graphIdStr); //done

generalLogger.getLogger().log(Level.INFO, "user " + username + ": get cover " + graphIdStr + " in format " + graphOutputFormatStr );
return Response.ok(requestHandler.writeMultiplexGraph(graph, format)).build();
} catch (Exception e) {
requestHandler.log(Level.SEVERE, "", e);
return requestHandler.writeError(Error.INTERNAL, "Internal system error.");
}

}

/**
* Deletes a graph. All covers based on the graph are removed as well.
* If a benchmark is currently calculating the graph the execution is
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package i5.las2peer.services.ocd.adapters.graphOutput;

import i5.las2peer.services.ocd.adapters.AbstractOutputAdapter;

/**
* An abstract class for graph output adapters.
* @author Sebastian
*
*/
public abstract class AbstractMultiplexGraphOutputAdapter extends AbstractOutputAdapter implements MultiplexGraphOutputAdapter {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package i5.las2peer.services.ocd.adapters.graphOutput;

import i5.las2peer.services.ocd.adapters.AdapterException;
import i5.las2peer.services.ocd.adapters.InputAdapter;
import i5.las2peer.services.ocd.adapters.OutputAdapter;
import i5.las2peer.services.ocd.graphs.MultiplexGraph;

import java.text.ParseException;
import java.util.Map;

/**
* The common interface of all graph output adapters.
* @author Sebastian
*
*/
public interface CommonGraphOutputAdapter<T> extends OutputAdapter {

/**
* Writes a graph and closes the writer.
* @throws AdapterException if the adapter failed
*/
public void writeGraph(T graph) throws AdapterException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @author Sebastian
*
*/
public interface GraphOutputAdapter extends OutputAdapter {
public interface GraphOutputAdapter extends CommonGraphOutputAdapter<CustomGraph> {

/**
* Writes a graph and closes the writer.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
* @author Sebastian
*
*/
public class GraphOutputAdapterFactory implements SimpleFactory<GraphOutputAdapter, GraphOutputFormat>{
public class GraphOutputAdapterFactory implements SimpleFactory<CommonGraphOutputAdapter, GraphOutputFormat>{

@Override
public GraphOutputAdapter getInstance(GraphOutputFormat outputFormat) throws InstantiationException, IllegalAccessException {
public CommonGraphOutputAdapter getInstance(GraphOutputFormat outputFormat) throws InstantiationException, IllegalAccessException {
return outputFormat.getAdapterClass().newInstance();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,15 @@ public enum GraphOutputFormat implements EnumDisplayNames{
/**
* Format corresponding to the PropertiesXMLGraphOutputAdapter.
*/
PROPERTIES_XML ("Properties XML", PropertiesXmlGraphOutputAdapter.class, 3);
PROPERTIES_XML ("Properties XML", PropertiesXmlGraphOutputAdapter.class, 3),
/**
* Format corresponding to the MetaXmlMultiplexGraphOutputAdapter.
*/
MULTIPLEX_META_XML ("Multiplex Meta XML", MetaXmlMultiplexGraphOutputAdapter.class, 4);;
/**
* The adapter class corresponding to the format.
*/
private final Class<? extends GraphOutputAdapter> adapterClass;
private final Class<? extends CommonGraphOutputAdapter> adapterClass;

/**
* Reserved for persistence or other purposes.
Expand All @@ -53,7 +57,7 @@ public enum GraphOutputFormat implements EnumDisplayNames{
* @param adapterClass Defines the adapterClass attribute.
* @param id Defines the id attribute.
*/
private GraphOutputFormat(String displayName, Class<? extends GraphOutputAdapter> adapterClass, int id) {
private GraphOutputFormat(String displayName, Class<? extends CommonGraphOutputAdapter> adapterClass, int id) {
this.displayName = displayName;
this.adapterClass = adapterClass;
this.id = id;
Expand All @@ -63,7 +67,7 @@ private GraphOutputFormat(String displayName, Class<? extends GraphOutputAdapter
* Returns the GraphOutputAdapter subclass corresponding to the type.
* @return The corresponding class.
*/
protected Class<? extends GraphOutputAdapter> getAdapterClass() {
protected Class<? extends CommonGraphOutputAdapter> getAdapterClass() {
return this.adapterClass;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package i5.las2peer.services.ocd.adapters.graphOutput;

import i5.las2peer.services.ocd.adapters.AdapterException;
import i5.las2peer.services.ocd.graphs.MultiplexGraph;
import i5.las2peer.services.ocd.graphs.GraphType;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

/**
* A graph output adapter for the meta XML format.
* The output contains meta information about the graph in XML format, but not the actual graph structure or other node or edge related meta data.
* @author Sebastian
*
*/
public class MetaXmlMultiplexGraphOutputAdapter extends AbstractMultiplexGraphOutputAdapter {

@Override
public void writeGraph(MultiplexGraph graph) throws AdapterException {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document doc = builder.newDocument();
Element graphElt = doc.createElement("Graph");
doc.appendChild(graphElt);
/*
* Basic Attributes
*/
Element graphIdElt = doc.createElement("Id");
graphIdElt.appendChild(doc.createTextNode(graph.getKey()));
graphElt.appendChild(graphIdElt);
Element graphNameElt = doc.createElement("Name");
graphNameElt.appendChild(doc.createTextNode(graph.getName()));
graphElt.appendChild(graphNameElt);
Element graphNodeCountElt = doc.createElement("NodeCount");
graphNodeCountElt.appendChild(doc.createTextNode(Integer.toString(graph.getNodeCount())));
graphElt.appendChild(graphNodeCountElt);
Element graphEdgeCountElt = doc.createElement("EdgeCount");
graphEdgeCountElt.appendChild(doc.createTextNode(Integer.toString(graph.getEdgeCount())));
graphElt.appendChild(graphEdgeCountElt);
Element graphLayerCountElt = doc.createElement("LayerCount");
graphLayerCountElt.appendChild(doc.createTextNode(Integer.toString(graph.getLayerCount())));
graphElt.appendChild(graphLayerCountElt);
/*
* Graph Types
*/
Element typesElt = doc.createElement("Types");
for(GraphType type : graph.getTypes()) {
Element typeElt = doc.createElement("Type");
typeElt.appendChild(doc.createTextNode(type.name()));
typeElt.setAttribute("displayName", type.getDisplayName());
typesElt.appendChild(typeElt);
}
graphElt.appendChild(typesElt);
/*
* Creation Method
*/
Element creationMethodElt = doc.createElement("CreationMethod");
Element creationMethodTypeElt = doc.createElement("Type");
creationMethodTypeElt.appendChild(doc.createTextNode(graph.getCreationMethod().getType().name()));
creationMethodTypeElt.setAttribute("displayName", graph.getCreationMethod().getType().getDisplayName());
creationMethodElt.appendChild(creationMethodTypeElt);
Element creationMethodStatus = doc.createElement("Status");
creationMethodStatus.appendChild(doc.createTextNode(graph.getCreationMethod().getStatus().name()));
creationMethodElt.appendChild(creationMethodStatus);
graphElt.appendChild(creationMethodElt);
/*
* XML output
*/
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource domSource = new DOMSource(doc);
StreamResult streamResult = new StreamResult(this.writer);
transformer.transform(domSource, streamResult);
}
catch(Exception e) {
throw new AdapterException(e);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package i5.las2peer.services.ocd.adapters.graphOutput;

import i5.las2peer.services.ocd.adapters.AdapterException;
import i5.las2peer.services.ocd.adapters.OutputAdapter;
import i5.las2peer.services.ocd.adapters.graphInput.CommonGraphInputAdapter;
import i5.las2peer.services.ocd.graphs.CustomGraph;
import i5.las2peer.services.ocd.graphs.MultiplexGraph;

/**
* The common interface of all graph output adapters.
* @author Sebastian
*
*/
public interface MultiplexGraphOutputAdapter extends CommonGraphOutputAdapter<MultiplexGraph> {

/**
* Writes a graph and closes the writer.
* @param graph The graph to write.
* @throws AdapterException if the adapter failed
*/
public void writeGraph(MultiplexGraph graph) throws AdapterException;

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package i5.las2peer.services.ocd.graphs;

import com.arangodb.ArangoCollection;
import com.arangodb.ArangoCursor;
import com.arangodb.ArangoDatabase;
import com.arangodb.entity.BaseDocument;
import com.arangodb.entity.BaseEdgeDocument;
import com.arangodb.model.*;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.*;

/**
Expand Down Expand Up @@ -34,6 +38,11 @@ public class MultiplexGraph {
*/
private String key = "";

/**
* System generated persistence id.
*/
private long id;

/**
* The name of the user owning the graph.
*/
Expand Down Expand Up @@ -162,7 +171,7 @@ public GraphCreationLog getCreationMethod() {
*
* @return The number of nodes.
*/
public long getNodeCount(){return this.graphNodeCount;}
public int getNodeCount(){return (int)this.graphNodeCount;}

/**
* Getter for the number of nodes.
Expand All @@ -175,7 +184,7 @@ public GraphCreationLog getCreationMethod() {
*
* @return The number of edges.
*/
public long getEdgeCount(){return this.graphEdgeCount;}
public int getEdgeCount(){return (int)this.graphEdgeCount;}
/**
* Getter for the number of edges.
*
Expand Down Expand Up @@ -277,6 +286,42 @@ protected CustomGraph getCustomGraph(CustomGraph customGraph) {
return mapCustomGraphs.get(customGraph.getId());
}

public static MultiplexGraph load(String key, ArangoDatabase db, String transId) {
MultiplexGraph graph = null;
ArangoCollection collection = db.collection(collectionName);
DocumentReadOptions readOpt = new DocumentReadOptions().streamTransactionId(transId);
AqlQueryOptions queryOpt = new AqlQueryOptions().streamTransactionId(transId);
BaseDocument bd = collection.getDocument(key, BaseDocument.class, readOpt);

if (bd != null) {
graph = new MultiplexGraph();
ObjectMapper om = new ObjectMapper();
Object objId = bd.getAttribute(idColumnName);
if(objId!= null) {
graph.id = Long.parseLong(objId.toString());
}
graph.key = key;
graph.userName = bd.getAttribute(userColumnName).toString();
//graph.path = bd.getAttribute(pathColumnName).toString();
graph.name = bd.getAttribute(nameColumnName).toString();
Object objTypes = bd.getAttribute(typesColumnName);
graph.types = om.convertValue(objTypes, Set.class);
//Object objProperties = bd.getAttribute(propertiesColumnName);
//graph.properties = om.convertValue(objProperties, List.class);
String creationMethodKey = bd.getAttribute(creationMethodKeyColumnName).toString();
graph.graphNodeCount = om.convertValue(bd.getAttribute(nodeCountColumnName), Long.class);
graph.graphEdgeCount = om.convertValue(bd.getAttribute(edgeCountColumnName), Long.class);
graph.layerCount = om.convertValue(bd.getAttribute(layerCountColumnName), Integer.class);
graph.creationMethod = GraphCreationLog.load(creationMethodKey, db, readOpt);
Object objLayerKeys = bd.getAttribute(layerKeysColumnName);
graph.layerKeys = om.convertValue(objLayerKeys, List.class);
}
else {
System.out.println("Empty Graph document");
System.out.println(" DB name: " + db.dbName().get());
}
return graph;
}
public void persist(ArangoDatabase db, String transId) throws InterruptedException {
ArangoCollection collection = db.collection(collectionName);
BaseDocument bd = new BaseDocument();
Expand Down
Loading

0 comments on commit 849a1ae

Please sign in to comment.