Commit 5de9f53b authored by Anurose Prakash's avatar Anurose Prakash
Browse files

Upload New File

parent 236617f1
package org.example;
import java.io.*;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import com.google.common.collect.Sets;
import org.apache.jena.graph.Node;
import org.apache.jena.graph.NodeFactory;
import org.apache.jena.graph.Triple;
import org.apache.jena.rdf.model.*;
import org.apache.jena.riot.Lang;
import org.apache.jena.riot.RDFDataMgr ;
import org.apache.jena.riot.lang.PipedRDFIterator;
import org.apache.jena.riot.lang.PipedRDFStream;
import org.apache.jena.riot.lang.PipedTriplesStream;
import org.dllearner.algorithms.celoe.CELOE;
import org.dllearner.algorithms.celoe.OEHeuristicRuntime;
import org.dllearner.core.AbstractCELA;
import org.dllearner.core.ComponentInitException;
import org.dllearner.core.KnowledgeSource;
import org.dllearner.kb.OWLAPIOntology;
import org.dllearner.learningproblems.Heuristics;
import org.dllearner.learningproblems.PosNegLPStandard;
import org.dllearner.reasoning.ClosedWorldReasoner;
import org.dllearner.reasoning.OWLAPIReasoner;
import org.dllearner.refinementoperators.RhoDRDown;
import org.dllearner.utilities.OwlApiJenaUtils;
import org.dllearner.utilities.statistics.Stat;
import org.semanticweb.owlapi.model.*;
import uk.ac.manchester.cs.owl.owlapi.OWLNamedIndividualImpl;
public class App {
private static final String ontologyFileName = "resources/carcinogenesis.owl";
private static final String algorithmDevSetFileName = "resources/kg-mini-project-train_v2.ttl";
static LearningProblem[] learningProblemsArray;
static LearningProblem lp;
static ArrayList<OWLIndividual> positiveSamples;
static ArrayList<OWLIndividual> negativeSamples;
static Triple triple;
public static void main(String[] args) throws OWLOntologyCreationException, IOException, ComponentInitException {
OWLOntology ontology;
Set<OWLIndividual> posTrainingSet ;
Set<OWLIndividual> negTrainingSet;
Set<OWLIndividual> posTestingSet;
Set<OWLIndividual> negTestingSet;
ontology = readDumpFiles();
ConvertToTTL outputWriter = new ConvertToTTL();
outputWriter.writeConstants();
Node lpClassNode = NodeFactory.createURI("https://lpbenchgen.org/class/LearningProblem");
Node includesResourcesNode = NodeFactory.createURI("https://lpbenchgen.org/property/includesResource");
learningProblemsArray = new LearningProblem[25];
positiveSamples = new ArrayList<>();
negativeSamples = new ArrayList<>();
Stat fScore = new Stat();
int learningProblemsIndex = 0;
// Setting up iterator for reading input ttl file
PipedRDFIterator<Triple> iter = new PipedRDFIterator<>();
final PipedRDFStream<Triple> inputStream = new PipedTriplesStream(iter);
ExecutorService executor = Executors.newSingleThreadExecutor();
Runnable parser = new Runnable() {
@Override
public void run() {
RDFDataMgr.parse(inputStream, algorithmDevSetFileName);
}
};
executor.submit(parser);
while (iter.hasNext()) {
triple = iter.next(); // retrieve next triple
Node subject = triple.getSubject(); // retrieve subject
Node predicate = triple.getPredicate(); // retrieve predicate
Node object = triple.getObject(); // retrieve object
if (object.equals(lpClassNode)) {
if (learningProblemsIndex > 0) {
lp.setPositiveSamples(positiveSamples);
lp.setNegativeSamples(negativeSamples);
learningProblemsArray[learningProblemsIndex-1] = lp;
}
learningProblemsIndex++;
lp = new LearningProblem();
positiveSamples = new ArrayList<>();
negativeSamples = new ArrayList<>();
} else {
if (lp.getLpID().isEmpty()) {
lp.setLpID(subject.getLocalName());
}
String obj_str = object.toString();
OWLIndividual resource = new OWLNamedIndividualImpl(IRI.create(obj_str));
if (predicate.equals(includesResourcesNode)) {
positiveSamples.add(resource);
} else {
negativeSamples.add(resource);
}
}
}
executor.shutdown();
// Setting the positive and negative individuals
lp.setPositiveSamples(positiveSamples);
lp.setNegativeSamples(negativeSamples);
learningProblemsArray[learningProblemsIndex-1] = lp;
System.out.println("Start initializing knowledge source...");
KnowledgeSource ks = new OWLAPIOntology(ontology);
ks.init();
System.out.println("Completed initializing knowledge source");
System.out.println("Start initializing reasoner");
OWLAPIReasoner baseReasoner = new OWLAPIReasoner(ks);
baseReasoner.setUseFallbackReasoner(true);
baseReasoner.init();
System.out.println("Completed initializing reasoner");
System.out.println("Start initializing reasoner component...");
ClosedWorldReasoner rc = new ClosedWorldReasoner(ks);
rc.setReasonerComponent(baseReasoner);
rc.setHandlePunning(true);
rc.setMaterializeExistentialRestrictions(true);
rc.init();
System.out.println("Completed initializing reasoner");
for (learningProblemsIndex = 0; learningProblemsIndex < learningProblemsArray.length; learningProblemsIndex++) {
String lp_id = learningProblemsArray[learningProblemsIndex].getLpID();
positiveSamples = learningProblemsArray[learningProblemsIndex].getPositiveSamples();
negativeSamples = learningProblemsArray[learningProblemsIndex].getNegativeSamples();
Collections.shuffle(positiveSamples, new Random(1));
Collections.shuffle(negativeSamples, new Random(2));
System.out.println("pos sample list size is ------"+ positiveSamples.size());
System.out.println("neg sample list size is ------"+ negativeSamples.size());
int posTrainingSize = (int) (positiveSamples.size() * 0.8);
int posTestingSize = positiveSamples.size() - posTrainingSize;
System.out.println("Positive training size is - "+posTrainingSize);
System.out.println("Positive testing size is - "+posTestingSize);
posTrainingSet = testTrainSplit(0,posTrainingSize,positiveSamples);
posTestingSet = testTrainSplit(posTrainingSize+1,positiveSamples.size()-1,positiveSamples);
int negTrainingSize = (int) (negativeSamples.size() * 0.8);
int negTestingSize = negativeSamples.size() - negTrainingSize;
System.out.println("Negative training size is - "+negTrainingSize);
System.out.println("Negative testing size is - "+negTestingSize);
negTrainingSet = testTrainSplit(0,negTrainingSize,negativeSamples);
negTestingSet = testTrainSplit(negTrainingSize+1,negativeSamples.size()-1,negativeSamples);
System.out.println("Start initializing learning problem...");
PosNegLPStandard lp = new PosNegLPStandard(rc);
lp.setPositiveExamples(posTrainingSet);
lp.setNegativeExamples(negTrainingSet);
lp.init();
System.out.println("Completed initializing learning problem");
System.out.println("initializing learning algorithm...");
AbstractCELA la;
OEHeuristicRuntime heuristic = new OEHeuristicRuntime();
heuristic.setExpansionPenaltyFactor(0.1);
la = new CELOE(lp, rc);
((CELOE) la).setHeuristic(heuristic);
((CELOE) la).setMaxExecutionTimeInSeconds(60);
((CELOE) la).setNoisePercentage(30);
((CELOE) la).setWriteSearchTree(false);
((CELOE) la).setReplaceSearchTree(true);
System.out.println("finished initializing learning algorithm");
System.out.println("Start initializing operator...");
RhoDRDown op = new RhoDRDown();
op.setInstanceBasedDisjoints(true);
op.setUseNegation(false);
op.setUseHasValueConstructor(true);
op.setUseAllConstructor(false);
op.setReasoner(rc);
op.setSubHierarchy(rc.getClassHierarchy());
op.setObjectPropertyHierarchy(rc.getObjectPropertyHierarchy());
op.setDataPropertyHierarchy(rc.getDatatypePropertyHierarchy());
op.init();
System.out.println("Completed initializing operator");
((CELOE) la).setOperator(op);
la.init();
la.start();
//CrossValidation obj = new CrossValidation(la, lp, rc, 3, false);
/**********************************TRIAL***********************************/
OWLClassExpression concept = la.getCurrentlyBestDescription();
Set<OWLIndividual> posResults = rc.hasType(concept, posTestingSet);
// Calculate F1-Score
int correctPosClassified = posResults.size();
int negAsPos = rc.hasType(concept, negTestingSet).size();
double precision = correctPosClassified + negAsPos == 0 ? 0 : correctPosClassified
/ (double) (correctPosClassified + negAsPos);
double recall = correctPosClassified / (double) posTestingSet.size();
double currFScore = Heuristics.getFScore(recall, precision);
fScore.addNumber(currFScore);
System.out.println("Learning Problem: "+ lp_id);
System.out.println("Precision is "+ precision);
System.out.println("Recall is "+ recall);
System.out.println("F1-Score is "+ currFScore);
}
System.out.println("Macro F1-Score is "+ fScore.getMeanAsPercentage());
}
public static OWLOntology readDumpFiles() throws IOException {
Model model = ModelFactory.createDefaultModel();
try (InputStream is = new FileInputStream(new File(ontologyFileName))) {
model.read(is, null, Lang.RDFXML.getName());
}
System.out.println("finished reading files");
//convert JENA model to OWL API ontology
System.out.println("converting to OWLApi ontology...");
OWLOntology ontology = OwlApiJenaUtils.getOWLOntology(model);
System.out.println("finished conversion");
// sanity check
if(containsErrorNamedClasses(ontology)){
System.exit(0);
}
return ontology;
}
private static boolean containsErrorNamedClasses(OWLOntology ontology){
for (OWLClass cls : ontology.getClassesInSignature()) {
if(cls.toStringID().startsWith("http://org.semanticweb.owlapi/error#")){
return true;
}
}
return false;
}
public static TreeSet<OWLIndividual> testTrainSplit(int startIndex, int endIndex, ArrayList<OWLIndividual> listToBeSplit){
int i;
int size = endIndex - startIndex;
List<OWLIndividual> List= new ArrayList<>(size);
for(i=startIndex;i<=endIndex;i++){
List.add(listToBeSplit.get(i));
}
TreeSet<OWLIndividual> newSet = new TreeSet<>(List);
return newSet;
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment