public static void test() throws Exception { ServletContextParameterMap contextParameters = ContextParametersRegistry.getInstance().getDefault(); ModelingConfiguration modelingConfiguration = ModelingConfigurationRegistry.getInstance() .getModelingConfiguration(contextParameters.getId()); // String inputPath = Params.INPUT_DIR; String outputPath = Params.OUTPUT_DIR; String graphPath = Params.GRAPHS_DIR; // List<SemanticModel> semanticModels = ModelReader.importSemanticModels(inputPath); List<SemanticModel> semanticModels = ModelReader.importSemanticModelsFromJsonFiles(Params.MODEL_DIR, Params.MODEL_MAIN_FILE_EXT); // ModelEvaluation me2 = semanticModels.get(20).evaluate(semanticModels.get(20)); // System.out.println(me2.getPrecision() + "--" + me2.getRecall()); // if (true) // return; List<SemanticModel> trainingData = new ArrayList<SemanticModel>(); OntologyManager ontologyManager = new OntologyManager(contextParameters.getId()); File ff = new File(Params.ONTOLOGY_DIR); File[] files = ff.listFiles(); for (File f : files) { ontologyManager.doImport(f, "UTF-8"); } ontologyManager.updateCache(); // getStatistics1(semanticModels); // if (true) // return; ModelLearningGraph modelLearningGraph = null; ModelLearner_Old modelLearner; boolean iterativeEvaluation = false; boolean useCorrectType = false; int numberOfCRFCandidates = 4; int numberOfKnownModels; String filePath = Params.RESULTS_DIR; String filename = "results,k=" + numberOfCRFCandidates + ".csv"; PrintWriter resultFile = new PrintWriter(new File(filePath + filename)); StringBuffer[] resultsArray = new StringBuffer[semanticModels.size() + 2]; for (int i = 0; i < resultsArray.length; i++) { resultsArray[i] = new StringBuffer(); } for (int i = 0; i < semanticModels.size(); i++) { // for (int i = 0; i <= 10; i++) { // int i = 3; { resultFile.flush(); int newSourceIndex = i; SemanticModel newSource = semanticModels.get(newSourceIndex); logger.info("======================================================"); logger.info(newSource.getName() + "(#attributes:" + newSource.getColumnNodes().size() + ")"); System.out.println( newSource.getName() + "(#attributes:" + newSource.getColumnNodes().size() + ")"); logger.info("======================================================"); if (!iterativeEvaluation) numberOfKnownModels = semanticModels.size() - 1; else numberOfKnownModels = 0; if (resultsArray[0].length() > 0) resultsArray[0].append(" \t "); resultsArray[0].append( newSource.getName() + "(" + newSource.getColumnNodes().size() + ")" + "\t" + " " + "\t" + " "); if (resultsArray[1].length() > 0) resultsArray[1].append(" \t "); resultsArray[1].append("p \t r \t t"); while (numberOfKnownModels <= semanticModels.size() - 1) { trainingData.clear(); int j = 0, count = 0; while (count < numberOfKnownModels) { if (j != newSourceIndex) { trainingData.add(semanticModels.get(j)); count++; } j++; } modelLearningGraph = (ModelLearningGraphSparse) ModelLearningGraph.getEmptyInstance(ontologyManager, ModelLearningGraphType.Sparse); SemanticModel correctModel = newSource; List<ColumnNode> columnNodes = correctModel.getColumnNodes(); // if (useCorrectType && numberOfCRFCandidates > 1) // updateCrfSemanticTypesForResearchEvaluation(columnNodes); modelLearner = new ModelLearner_Old(ontologyManager, columnNodes); long start = System.currentTimeMillis(); String graphName = !iterativeEvaluation ? graphPath + semanticModels.get(newSourceIndex).getName() + Params.GRAPH_FILE_EXT : graphPath + semanticModels.get(newSourceIndex).getName() + ".knownModels=" + numberOfKnownModels + Params.GRAPH_FILE_EXT; if (new File(graphName).exists()) { // read graph from file try { logger.info("loading the graph ..."); DirectedWeightedMultigraph<Node, DefaultLink> graph = GraphUtil.importJson(graphName); modelLearner.graphBuilder = new GraphBuilder(ontologyManager, graph, false); modelLearner.nodeIdFactory = modelLearner.graphBuilder.getNodeIdFactory(); } catch (Exception e) { e.printStackTrace(); } } else { logger.info("building the graph ..."); for (SemanticModel sm : trainingData) modelLearningGraph.addModel(sm, false); modelLearner.graphBuilder = modelLearningGraph.getGraphBuilder(); modelLearner.nodeIdFactory = modelLearner.graphBuilder.getNodeIdFactory(); // save graph to file try { GraphUtil.exportJson( modelLearningGraph.getGraphBuilder().getGraph(), graphName, true, true); } catch (Exception e) { e.printStackTrace(); } } List<SortableSemanticModel_Old> hypothesisList = modelLearner.hypothesize(useCorrectType, numberOfCRFCandidates); long elapsedTimeMillis = System.currentTimeMillis() - start; float elapsedTimeSec = elapsedTimeMillis / 1000F; List<SortableSemanticModel_Old> topHypotheses = null; if (hypothesisList != null) { topHypotheses = hypothesisList.size() > modelingConfiguration.getNumCandidateMappings() ? hypothesisList.subList(0, modelingConfiguration.getNumCandidateMappings()) : hypothesisList; } Map<String, SemanticModel> models = new TreeMap<String, SemanticModel>(); // export to json // if (topHypotheses != null) // for (int k = 0; k < topHypotheses.size() && k < 3; k++) { // // String fileExt = null; // if (k == 0) fileExt = Params.MODEL_RANK1_FILE_EXT; // else if (k == 1) fileExt = Params.MODEL_RANK2_FILE_EXT; // else if (k == 2) fileExt = Params.MODEL_RANK3_FILE_EXT; // SortableSemanticModel m = topHypotheses.get(k); // new SemanticModel(m).writeJson(Params.MODEL_DIR + // newSource.getName() + fileExt); // // } ModelEvaluation me; models.put("1-correct model", correctModel); if (topHypotheses != null) for (int k = 0; k < topHypotheses.size(); k++) { SortableSemanticModel_Old m = topHypotheses.get(k); me = m.evaluate(correctModel); String label = "candidate" + k + m.getSteinerNodes().getScoreDetailsString() + "cost:" + roundTwoDecimals(m.getCost()) + // "-distance:" + me.getDistance() + "-precision:" + me.getPrecision() + "-recall:" + me.getRecall(); models.put(label, m); if (k == 0) { // first rank model System.out.println( "number of known models: " + numberOfKnownModels + ", precision: " + me.getPrecision() + ", recall: " + me.getRecall() + ", time: " + elapsedTimeSec); logger.info( "number of known models: " + numberOfKnownModels + ", precision: " + me.getPrecision() + ", recall: " + me.getRecall() + ", time: " + elapsedTimeSec); // resultFile.println("number of known models \t precision \t recall"); // resultFile.println(numberOfKnownModels + "\t" + me.getPrecision() + "\t" + // me.getRecall()); String s = me.getPrecision() + "\t" + me.getRecall() + "\t" + elapsedTimeSec; if (resultsArray[numberOfKnownModels + 2].length() > 0) resultsArray[numberOfKnownModels + 2].append(" \t "); resultsArray[numberOfKnownModels + 2].append(s); // resultFile.println(me.getPrecision() + "\t" + me.getRecall() + "\t" + // elapsedTimeSec); } } String outName = !iterativeEvaluation ? outputPath + semanticModels.get(newSourceIndex).getName() + Params.GRAPHVIS_OUT_DETAILS_FILE_EXT : outputPath + semanticModels.get(newSourceIndex).getName() + ".knownModels=" + numberOfKnownModels + Params.GRAPHVIS_OUT_DETAILS_FILE_EXT; // if (!iterativeEvaluation) { GraphVizUtil.exportSemanticModelsToGraphviz( models, newSource.getName(), outName, GraphVizLabelType.LocalId, GraphVizLabelType.LocalUri, false, false); // } numberOfKnownModels++; } // resultFile.println("======================================================="); } for (StringBuffer s : resultsArray) resultFile.println(s.toString()); resultFile.close(); }
@Override public UpdateContainer doIt(Workspace workspace) throws CommandException { ModelingConfiguration modelingConfiguration = ModelingConfigurationRegistry.getInstance() .getModelingConfiguration( WorkspaceKarmaHomeRegistry.getInstance().getKarmaHome(workspace.getId())); TripleStoreUtil utilObj = new TripleStoreUtil(); boolean showModelsWithoutMatching = modelingConfiguration.isShowModelsWithoutMatching(); try { HashMap<String, List<String>> metadata = utilObj.getMappingsWithMetadata(TripleStoreUrl, context); RepFactory factory = workspace.getFactory(); List<String> model_Names = metadata.get("model_names"); List<String> model_Urls = metadata.get("model_urls"); List<String> model_Times = metadata.get("model_publishtimes"); List<String> model_Contexts = metadata.get("model_contexts"); List<String> model_inputColumns = metadata.get("model_inputcolumns"); final List<JSONObject> list = new ArrayList<>(); Set<String> worksheetcolumns = new HashSet<>(); if (worksheetId != null && !worksheetId.trim().isEmpty()) { HTable htable = factory.getWorksheet(worksheetId).getHeaders(); getHNodesForWorksheet(htable, worksheetcolumns, factory); } Iterator<String> nameitr = model_Names.iterator(); Iterator<String> urlitr = model_Urls.iterator(); Iterator<String> timeitr = model_Times.iterator(); Iterator<String> contextitr = model_Contexts.iterator(); Iterator<String> inputitr = model_inputColumns.iterator(); while (nameitr.hasNext() && urlitr.hasNext() && timeitr.hasNext() && contextitr.hasNext() && inputitr.hasNext()) { JSONObject obj = new JSONObject(); Set<String> inputs = new HashSet<>(); obj.put("name", nameitr.next()); obj.put("url", urlitr.next()); obj.put("publishTime", timeitr.next()); obj.put("context", contextitr.next()); String columns = inputitr.next(); if (columns != null && !columns.isEmpty()) { JSONArray array = new JSONArray(columns); for (int i = 0; i < array.length(); i++) inputs.add(array.get(i).toString()); } else if (showModelsWithoutMatching) { list.add(obj); } if (worksheetId != null && !worksheetId.isEmpty()) { inputs.retainAll(worksheetcolumns); obj.put("inputColumns", inputs.size()); } else obj.put("inputColumns", 0); if (!inputs.isEmpty() || (worksheetId == null || worksheetId.trim().isEmpty())) list.add(obj); } Collections.sort( list, new Comparator<JSONObject>() { @Override public int compare(JSONObject a, JSONObject b) { return b.getInt("inputColumns") - a.getInt("inputColumns"); } }); return new UpdateContainer( new AbstractUpdate() { @Override public void generateJson(String prefix, PrintWriter pw, VWorkspace vWorkspace) { try { JSONArray array = new JSONArray(); for (JSONObject obj : list) { array.put(obj); } pw.print(array.toString()); } catch (Exception e) { logger.error("Error generating JSON!", e); } } }); } catch (Exception e) { return new UpdateContainer( new ErrorUpdate("Unable to get mappings with metadata: " + e.getMessage())); } }