/*
* Created on 02.02.2005
*
* COPYRIGHT NOTICE
*
* Copyright (C) 2005 DFKI GmbH, Germany
* Developed by Benedikt Fries, Matthias Klusch
*
* The code is free for non-commercial use only.
* You can redistribute it and/or modify it under the terms
* of the Mozilla Public License version 1.1 as
* published by the Mozilla Foundation at
* http://www.mozilla.org/MPL/MPL-1.1.txt
*/
package owlsmx.examples;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Vector;
import owlsmx.SimilarityMatchingEngine;
import owlsmx.analysis.MemoryContainer;
import owlsmx.analysis.PassedTime;
import owlsmx.analysis.RecallPrecision;
import owlsmx.data.DOM;
import owlsmx.data.MatchedService;
import owlsmx.exceptions.MatchingException;
import owlsmx.similaritymeasures.ConstraintSimilarity;
import owlsmx.similaritymeasures.SimilarityMeasure;
/**
* Used for our own performance measurement in the pre GUI stage
*
* @author bEn
*
*/
public class PerformanceMeasurement {
static String servicePath = "L:\\Webserver\\xampp\\htdocs\\services\\1.1";
static String serverServicePath = "http://127.0.0.1/services/1.1/";
static String relevantForQueryDocument = "D:\\Diplomarbeit\\Services\\TC2005-03-16\\AllServices\\relevant\\q";
static String queryPath = "L:\\Webserver\\xampp\\htdocs\\queries\\1.1";
static String queryServerPath = "http://127.0.0.1/queries/1.1/";
static String relevantPath = "L:\\Webserver\\xampp\\htdocs\\relevant";
static Vector log = new Vector();
static boolean logging=true;
static String similarityMeasure="";
static int count = 200;
static int queries = 30;//1;
static FileWriter stream;
public static void log(String text) {
try {
stream.write(text + "\n");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//System.out.println(text);
if (logging)
log.add(text);
}
public static int min(int a, int b) {
if (a<b)
return a;
return b;
}
public static boolean isValidFile(File files) {
return files.getAbsolutePath().toLowerCase().endsWith("owls");
/*
( (!files.getAbsolutePath().contains("geographical-region_weatherseason_service.owls")) &&
(!files.getAbsolutePath().contains("geographical-region_weathersystem_service.owls")) &&
(!files.getAbsolutePath().contains("geopolitical-entity_weatherseasonproposition_service.owls")) &&
(!files.getAbsolutePath().contains("geopolitical-entity_weatherseasontimeposition_service.owls")) &&
(!files.getAbsolutePath().contains("geopolitical-entity_weatherseason_service.owls")) &&
(!files.getAbsolutePath().contains("geopolitical-entity_weathersystem_service.owls")) &&
(!files.getAbsolutePath().contains("geographical-region_weatherseason_service.owls")) &&
(!files.getAbsolutePath().contains("_icing_Germanservice.owls")) &&
(!files.getAbsolutePath().contains("AcceptCostAndHealingPlan_service.owls")) &&
(!files.getAbsolutePath().contains("BookMedicalFlight_service.owls")) &&
(!files.getAbsolutePath().contains("BookNonMedicalFlight_service.owls")) &&
(!files.getAbsolutePath().contains("COE_service.owls")) &&
(!files.getAbsolutePath().contains("ProvideMedicalTransportInformation_service.owls")) &&
(!files.getAbsolutePath().contains("SeePatientMedicalRecords_service.owls")) &&
(!files.getAbsolutePath().contains("HospitalPhysicianServices.owls")) &&
(!files.getAbsolutePath().contains("SelectTransport_service.owls")) &&
(!files.getAbsolutePath().contains("bookpersoncreditaccount__Beaservice.owls")) &&
(!files.getAbsolutePath().contains("bookpersoncreditaccount__service.owls")) &&
(!files.getAbsolutePath().contains("bookpersoncreditcardaccount__BShopservice.owls")) &&
(!files.getAbsolutePath().contains("bookpersoncreditcardaccount__service.owls")) &&
(!files.getAbsolutePath().contains("bookpersoncreditcardaccount_price_service.owls")) &&
(!files.getAbsolutePath().contains("bookpersoncreditcardaccount_recommendedprice_service.owls")) &&
(!files.getAbsolutePath().contains("bookpersoncreditcardaccount_taxedfreeprice_service.owls")) &&
(!files.getAbsolutePath().contains("bookusercreditcardaccount__service.owls")) &&
(!files.getAbsolutePath().contains("breadorbiscuit_recPricetaxedpriceineuro_service.owls")) &&
(!files.getAbsolutePath().contains("car_installment_service.owls")) &&
(!files.getAbsolutePath().contains("SelectFlight_service.owls")) &&
(!files.getAbsolutePath().contains("AcademicBookNumberSearch.owls")) &&
(!files.getAbsolutePath().contains("AcademicBookNumberOrISBNSearch.owls")) &&
(!files.getAbsolutePath().contains("BookSearchService.owls")) &&
(!files.getAbsolutePath().contains("BookPrice.owls")) &&
(!files.getAbsolutePath().contains("BookFinder.owls")) &&
(!files.getAbsolutePath().contains("monographpersoncreditcardaccount_recommendedprice_service.owls")) &&
(!files.getAbsolutePath().contains("publication_book_service.owls")) &&
(!files.getAbsolutePath().contains("preparedfood_SpainishTax_service.owls")) &&
(!files.getAbsolutePath().contains("preparedfood_GSprice_service.owls")) &&
(!files.getAbsolutePath().contains("personbookliabilityaccount__service.owls")) &&
(!files.getAbsolutePath().contains("personmonographcreditcardaccount__service.owls")) &&
(!files.getAbsolutePath().contains("personmonographcreditcardaccount__service.owls")) &&
(!files.getAbsolutePath().contains("country_skilledoccupationparttimeposition_service.owls")) &&
(!files.getAbsolutePath().contains("objectpersoncreditaccount_price_service.owls")) &&
(!files.getAbsolutePath().contains("weaponmissile_funding_Iraqservice.owls")) &&
(!files.getAbsolutePath().contains("car_pricedebt_service.owls")) &&
(!files.getAbsolutePath().contains("Apothecary_service.owls")) &&
(!files.getAbsolutePath().contains("printedmaterialpersoncreditcardaccount__service.owls")) &&
(!files.getAbsolutePath().contains("printedmaterialpersoncreditcardaccount_price_service.owls")) &&
(!files.getAbsolutePath().contains("_warmfront_Italyservice.owls")) );
*/
}
public static void storeLog() {
Date today = new Date();
try {
FileWriter writer = new FileWriter("Results" + similarityMeasure + count + ".data",false);
writer.write("Performance Measurement " +today.toString() +"\n");
for(int i=0;i<log.size();i++) {
writer.write((String)log.get(i) +"\n");
}
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static Vector getServiceURIsFromDirectory(String services) throws URISyntaxException {
Vector result = new Vector();
SortedSet temporary = new TreeSet();
File dir = new File(servicePath);
File[] files = dir.listFiles();
for (int i=0;i<files.length;i++) {
if (files[i].isFile() && isValidFile(files[i]) ){
//System.out.println(files[i].getName());
temporary.add(new URI(serverServicePath + files[i].getName()) );
}
}
Iterator iter = temporary.iterator();
while (iter.hasNext()) {
result.add(iter.next());
}
return result;
}
public static ArrayList getRelevantForQuery(URI query) throws IOException, URISyntaxException {
ArrayList result = new ArrayList();
File relevant ;
String relevantName = query.toString();
relevantName = relevantName.substring(relevantName.lastIndexOf("/"),relevantName.lastIndexOf(".")) +".txt";
relevant = new File(relevantPath + "\\" + relevantName);
BufferedReader reader = new BufferedReader(new FileReader(relevant));
String line = "";
log("Relevant for query " + query.toString());
while ( (line = reader.readLine()) != null) {
if (line != "") {
log(line);
result.add(line.trim());
}
}
reader.close();
return result;
}
public static Vector getQueryURIsFromDirectory() throws URISyntaxException {
Vector result = new Vector();
File[] files = new File(queryPath).listFiles();
for (int i=0;i<files.length;i++) {
if (files[i].getName().indexOf(".owls")>=0) {
result.add(new URI(queryServerPath + files[i].getName()));
}
}
return result;
}
public static void parseCandidates(SimilarityMatchingEngine engine, Vector services, int count) throws Exception {//, SimilarityMatchingEngine engine) {
long current = System.currentTimeMillis();
if (count > services.size()) {
count = services.size();
}
for (int i = 0; i<count;i++) {
if ((i % 10)==0)
MemoryContainer.getInstance().addState(i);
System.out.println("[ADD] " + (i+1) + "/" + count + ((URI)services.get(i)).toString());
engine.addService((URI)services.get(i));
//System.out.println(((URI)services.get(i)).toString());
}
current = System.currentTimeMillis()-current;
long msperservice = current/services.size();
System.out.println("Parsed " + services.size() + " in " + current + " ms (" + msperservice +"msPerService)" );
return ;
}
public static void printMap(Map map, boolean just_values) {
Iterator iter = map.entrySet().iterator();
Map.Entry me;
while(iter.hasNext()) {
me = (Map.Entry) iter.next();
if (just_values)
log(((Double)me.getValue()).toString());
else
log(((Integer)me.getKey()).toString() + " - " + ((Double)me.getValue()).toString());
}
}
public static void wait(int seconds) {
System.gc();
try {
Thread.sleep(1000 * seconds);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static SortedMap getAveragePrecisionFromRPVector(Vector results) {
double[]queryresult;
SortedMap rpcurve = new TreeMap();
Double value;
Integer key;
for (int i=0;i<results.size();i++) {
queryresult=(double[]) results.get(i);
for (int j=0;j<queryresult.length;j++) {
key = new Integer(j);
if (rpcurve.containsKey(key))
value=new Double(queryresult[j] + ((Double)rpcurve.get(key)).floatValue());
else
value=new Double(queryresult[j]);
rpcurve.put(key, value);
}
}
SortedMap result = new TreeMap();
Iterator iter = rpcurve.entrySet().iterator();
Map.Entry me;
while(iter.hasNext()) {
me = (Map.Entry) iter.next();
result.put(me.getKey(),new Double((((Double)me.getValue()).floatValue()/ results.size() )*100));
}
return result;
}
public static ArrayList sortedSetToArrayList(SortedSet set,int query) {
ArrayList array = new ArrayList();
Iterator iter = set.iterator();
String name="";
MatchedService service;
while(iter.hasNext()){
service = (MatchedService)iter.next();
name = (service).serviceURI.toString();
log("" + query + " - " + service.toString());
array.add(name.substring(name.lastIndexOf("/")+1));
}
return array;
}
public static void logDoubleArray(double[] var) {
for (int i=0; i<var.length;i++) {
log("0." + i + " " + var[i]);
}
}
public static void logRecallResult(ArrayList result) {
for (int i=0; i<result.size();i++) {
log("" + result.get(i));
}
}
public static ArrayList query(SimilarityMatchingEngine engine, URI queryURI, int query, boolean isM0) throws MatchingException {
if (!isM0) {
ArrayList result = sortedSetToArrayList(engine.matchRequest(queryURI), query);
System.out.println(result);
return result;
}
else
return sortedSetToArrayList(engine.matchRequest(queryURI,DOM.SUBSUMED_BY ,0.0), query);
}
//,SimilarityMatchingEngine engine
public static void performTest(SimilarityMatchingEngine engine, String service_number, short similarity_measure, boolean isM0) throws Exception {
engine.setSimilarityMeasure(similarity_measure);
ArrayList relevant;
RecallPrecision recall = new RecallPrecision();
List list;
Vector results = new Vector();
Vector queryVector = getQueryURIsFromDirectory();
URI queryURI;
long query = System.currentTimeMillis();
int max =min(queries, queryVector.size());
for (int i=0;i<max;i++) {
queryURI = (URI) queryVector.get(i);
// if (queryURI.toString().indexOf("bookpersoncreditcardaccount__service.owls")>=0)
// i++;
System.out.println("Query " + (i+1) +"/" + max + ": " + queryURI.toString());
log("Query " + (i+1) +"/" + max + ": " + queryURI.toString());
relevant=getRelevantForQuery((URI) queryVector.get(i));
list = query(engine,queryURI,i,isM0);
// getMemory();
logDoubleArray(recall.processRecallPrecision(list,relevant));
}
query = System.currentTimeMillis()-query;
long querytime = query/queries;
long memory_queries = getMemory();
log("Total:");
log("");
log("Total Query processing time " + query + "ms");
log("Average Query processing time " + querytime + "ms");
log("Memory consumption after querying " + memory_queries + "bytes");
log("");
log("RP-New");
logRecallResult(recall.getMicroAveragePrecisions());
log("");
log("RP-Old");
logDoubleArray(recall.getAveragePrecisionAtElevenPoints());
printMap(getAveragePrecisionFromRPVector(results),false);
}
public static long getMemory2() {
return Runtime.getRuntime().totalMemory();
}
public static long getMemory() {
System.out.println("Total Memory: " + Runtime.getRuntime().totalMemory());
System.out.println("Free Memory: " + Runtime.getRuntime().freeMemory());
System.out.println("Used Memory: " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()));
System.out.println("");
return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
}
public static void main(String[] args) {
try {
stream = new FileWriter(new File("test.dat"));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
MemoryContainer.getInstance().setGUIMemory();
SimilarityMeasure loi = new ConstraintSimilarity();
// SimilarityMeasure exJ = new ExtendedJaccardMeasure();
// SimilarityMeasure JS = new JensenShannonMeasure();
// SimilarityMeasure cos = new CosineSimilarity();
long memory_before = getMemory();
String service_number = "AllServices";
Vector services = getServiceURIsFromDirectory(service_number);
long parsing = System.currentTimeMillis();
SimilarityMatchingEngine engine = new SimilarityMatchingEngine(loi);
parseCandidates(engine,services,count);
parsing = System.currentTimeMillis()-parsing;
long averageparsing = parsing/services.size();
wait(2);
getMemory();
log("Total Parsing processing time " + parsing + "ms");
log("Average Parsing processing time " + averageparsing + "ms");
long memory_parsed = getMemory();
log("Memory consumption before parsing ads " + memory_before + "bytes");
log("Memory consumption after parsing ads " + memory_parsed + "bytes");
log("");
log("");
//similarityMeasure = "Semantic";
PassedTime.start();
log("START Similarity Measure: LOI");
performTest(engine, service_number,SimilarityMeasure.SIMILARITY_LOI,false);
log("END Similarity Measure: LOI");
log("");
log("");
log("" + PassedTime.getTime("LOI: "));
// PassedTime.start();
// log("START Similarity Measure: EXTENDED_JACCARD");
// performTest(engine, service_number,SimilarityMeasure.SIMILARITY_EXTENDED_JACCARD,false);
// log("END Similarity Measure: EXTENDED_JACCARD");
// log("");
// log("");
// log("" + PassedTime.getTime("EXTENDED_JACCARD: "));
//
// PassedTime.start();
// log("START Similarity Measure: COSINE");
// performTest(engine, service_number,SimilarityMeasure.SIMILARITY_COSINE,false);
// log("END Similarity Measure: COSINE");
// log("");
// log("");
// log("" + PassedTime.getTime("COSINE: "));
// PassedTime.start();
// log("START Similarity Measure: JENSEN_SHANNON");
// performTest(engine, service_number,SimilarityMeasure.SIMILARITY_JENSEN_SHANNON,false);
// log("END Similarity Measure: JENSEN_SHANNON");
// log("");
// log("");
// log("" + PassedTime.getTime("JENSEN_SHANNON: "));
//
// PassedTime.start();
// log("START Semantic Matcher");
// performTest(engine, service_number,SimilarityMeasure.SIMILARITY_NONE,true);
// log("END Semantic Matcher");
// log("");
// log("");
// log("" + PassedTime.getTime("Semantic Matcher: "));
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("Waiting for some more seconds for memory measurement");
wait(20);
log("Final memory " + getMemory());
log("");
log(MemoryContainer.getInstance().toString());
storeLog();
stream.close();
System.out.println("Matching test complete, stored in Results" + similarityMeasure + count +".data");
System.out.println(MemoryContainer.getInstance().toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}