org.openprovenance.prov.validation.EventMatrix Maven / Gradle / Ivy
The newest version!
package org.openprovenance.prov.validation;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.*;
//import no.uib.cipr.matrix.DenseMatrix;
//import no.uib.cipr.matrix.Matrix;
//import no.uib.cipr.matrix.sparse.CompDiagMatrix;
//import no.uib.cipr.matrix.sparse.FlexCompColMatrix;
//import no.uib.cipr.matrix.Matrices;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.openprovenance.prov.validation.matrix.Pair;
import org.openprovenance.prov.validation.matrix.SparseMatrix;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import java.awt.image.BufferedImage;
public class EventMatrix {
static Logger logger = LogManager.getLogger(EventMatrix.class);
final public SparseMatrix m;
final SparseMatrix next;
final int size;
final Vector eventKinds;
public EventMatrix(int size, Vector eventKinds) {
this.size = size;
this.eventKinds = eventKinds;
m = new SparseMatrix(size, size);
next = new SparseMatrix(size, size);
logger.debug("created matrix");
}
final public static double epsilon = 0.001;
final public static double minus_one = -1.00;
public void floydWarshall() {
m.floydWarshall(next);
}
/*
* public void floydWarshall_original () { for (int k=0; k epsilon && m_k_j > epsilon &&
* ((m_i_j=m.g(i,j))==null || m_i_j <= epsilon || m_i_k+m_k_j < m_i_j )) {
* m.set(i,j,m_i_k+m_k_j); next.set(i,j,(double) k); } } } } }
*/
@SuppressWarnings("unused")
public String displayMatrix2() {
StringBuilder sb = new StringBuilder();
sb.append(" ");
for (int i = 0; i < size; i++) {
sb.append(eventKinds.get(i));
}
sb.append("\n");
for (int i = 0; i < size; i++) {
sb.append(eventKinds.get(i));
for (int j = 0; j < size; j++) {
Pair p;
if ((p = m.g(i, j)) != null) {
if (p.getValue() > 0) {
sb.append("X");
} else {
if (i == j) {
sb.append("\\");
} else {
sb.append(".");
}
}
} else {
if (i == j) {
sb.append("\\");
} else {
sb.append(".");
}
}
}
sb.append("\n");
}
return sb.toString();
}
@SuppressWarnings("unused")
public String displayMatrix3() {
StringBuilder sb = new StringBuilder();
sb.append(" ");
for (int i = 0; i < size; i++) {
sb.append(eventKinds.get(i));
}
sb.append("\n");
for (int i = 0; i < size; i++) {
sb.append(eventKinds.get(i));
for (int j = 0; j < size; j++) {
Pair p;
if ((p = m.g(i, j)) != null) {
int vv=p.getValue();
if (vv > 0) {
if (vv<10) {
sb.append(vv);
} else {
sb.append("X");
}
} else {
if (i == j) {
sb.append("\\");
} else {
sb.append(".");
}
}
} else {
if (i == j) {
sb.append("\\");
} else {
sb.append(".");
}
}
}
sb.append("\n");
}
return sb.toString();
}
@SuppressWarnings("unused")
public void generateImage1(String filename) throws IOException {
logger.debug("--- generatedImage1 ");
Iterator writers = ImageIO.getImageWritersByFormatName("png");
ImageWriter writer = writers.next();
File f = new File(filename);
ImageOutputStream ios = ImageIO.createImageOutputStream(f);
writer.setOutput(ios);
generateImage(writer, ios);
}
@SuppressWarnings("unused")
public void generateImage1(OutputStream os) throws IOException {
logger.debug("--- generatedImage1 ");
Iterator writers = ImageIO.getImageWritersByFormatName("png");
ImageWriter writer = writers.next();
ImageOutputStream ios = ImageIO.createImageOutputStream(os);
writer.setOutput(ios);
generateImage(writer, ios);
}
public void generateImage(ImageWriter writer, ImageOutputStream ios) throws IOException {
if (size <= 1) return;
BufferedImage bi = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
int maxValue = 7 - 1;
logger.debug("--- generatedImage1 ");
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
Pair p;
if ((p = m.g(i, j)) != null && p.getValue() > 0) {
int val = p.getValue();
bi.setRGB(j, i, 255 * (maxValue + 1 - val) / maxValue * 256
* 256 + 255 * (val - 1) / maxValue);
// System.out.println(" value is ------ " + m.g(i,j));
} else {
if (i == j) {
bi.setRGB(j, i, 70 * 256);
} else {
bi.setRGB(j, i, 0);
}
}
}
}
logger.debug("--- generatedImage1 ");
writer.write(bi);
ios.flush();
ios.close();
logger.debug("--- generatedImage1 (end) ");
}
public List