comparison src/OPPLQueryGalaxy.java @ 19:cc270db37d33 draft

Directories re-arranged
author Mikel Egana Aranguren <mikel-egana-aranguren@toolshed.g2.bx.psu.edu>
date Sat, 06 Oct 2012 21:50:39 +0200
parents
children
comparison
equal deleted inserted replaced
18:d3616fac4ca5 19:cc270db37d33
1 /**
2 *
3 */
4 package es.upm.cbgp.opplquery.oppl.galaxy;
5
6 import java.io.File;
7 import java.io.FileNotFoundException;
8 import java.util.Iterator;
9 import java.util.Scanner;
10 import java.util.Set;
11 import java.util.logging.Logger;
12 import java.util.regex.Matcher;
13 import java.util.regex.Pattern;
14
15 import org.semanticweb.owlapi.expression.ParserException;
16 import org.semanticweb.owlapi.model.IRI;
17 import org.semanticweb.owlapi.model.OWLObject;
18 import org.semanticweb.owlapi.model.OWLOntologyCreationException;
19
20 import es.upm.fi.dia.oeg.oppl.galaxy.GalaxyOWLAPI;
21
22 import org.coode.oppl.ChangeExtractor;
23 import org.coode.oppl.ConstraintSystem;
24 import org.coode.oppl.OPPLParser;
25 import org.coode.oppl.OPPLScript;
26 import org.coode.oppl.ParserFactory;
27 import org.coode.parsers.ErrorListener;
28 import org.coode.parsers.LoggerErrorListener;
29 import org.coode.oppl.bindingtree.Assignment;
30 import org.coode.oppl.bindingtree.BindingNode;
31 import org.coode.oppl.exceptions.QuickFailRuntimeExceptionHandler;
32 import org.coode.oppl.exceptions.RuntimeExceptionHandler;
33
34 public class OPPLQueryGalaxy {
35
36 /**
37 * @param args
38 * @throws OWLOntologyCreationException
39 * @throws ParserException
40 * @throws FileNotFoundException
41 */
42 public static void main(String[] args) throws OWLOntologyCreationException, ParserException, FileNotFoundException {
43 // Get the arguments from command-line
44 String OWLFilePath = args [0]; // /home/mikel/UPM/OPPL_galaxy/OPPL/OPPL/test-data/ontology/single/test.owl
45 String reasoner_type = args [1]; // Pellet|FaCTPlusPlus|HermiT|Elk
46 String Answer_render = args [2]; // URI|URIfragment|URIfragment2OBO
47 String OPPL_script_file = args [3];
48
49 // String OPPL_script_file = args [3];
50 // File OPPL_script_file = new File("/home/mikel/UPM/OPPL_galaxy/OPPL/OPPL/test-data/oppl_script/test_oppl_query.oppl");
51 // File OPPL_script_file = new File("/home/mikel/UPM/OPPL_galaxy/OPPL/OPPL/test-data/oppl_script/test_oppl_query_object_prop.oppl");
52 // File OPPL_script_file = new File("/home/mikel/UPM/OPPL_galaxy/OPPL/OPPL/test-data/oppl_script/test_oppl_query_object_ind.oppl");
53
54
55 // Create the manager
56 GalaxyOWLAPI galaxyowlapi = new GalaxyOWLAPI();
57
58 // Load the main ontology and hope for the imported URIs to be resolvable
59 galaxyowlapi.loadMainOntology(OWLFilePath);
60
61 // Set the reasoner
62
63 // Pellet
64 if(reasoner_type.equals("Pellet")){
65 galaxyowlapi.setReasonerPellet();
66 }
67 // FaCTPlusPlus
68 else if (reasoner_type.equals("FaCTPlusPlus")){
69 galaxyowlapi.setReasonerFaCT();
70 }
71 // Elk
72 else if (reasoner_type.equals("Elk")){
73 galaxyowlapi.setReasonerElk();
74 }
75 // HermiT
76 else{
77 galaxyowlapi.setReasonerHermit();
78 }
79
80 // Load the OPPL flat file with script in memory
81 String OPPL_script_source = "";
82 File file = new File(OPPL_script_file);
83 Scanner input = new Scanner(file);
84 while(input.hasNext()) {
85 String nextToken = input.next();
86 OPPL_script_source = OPPL_script_source + " " + nextToken;
87 }
88 input.close();
89
90 OPPL_script_source = completeOPPLScript(OPPL_script_source);
91 // System.out.println(OPPL_script_source);
92
93 ParserFactory parserFactory = new ParserFactory(galaxyowlapi.getOWLManager(), galaxyowlapi.getMainOntology(), galaxyowlapi.getReasoner());
94 Logger logger = Logger.getLogger(OPPLQueryGalaxy.class.getName());
95 ErrorListener errorListener = (ErrorListener) new LoggerErrorListener(logger);
96 OPPLParser opplparser = parserFactory.build(errorListener);
97 OPPLScript OPPLscript = opplparser.parse(OPPL_script_source);
98 RuntimeExceptionHandler exceptionhandler = new QuickFailRuntimeExceptionHandler();
99 ChangeExtractor extractor = new ChangeExtractor(exceptionhandler, true);
100 extractor.visit(OPPLscript);
101 ConstraintSystem cs = OPPLscript.getConstraintSystem();
102 Set<BindingNode> nodes = cs.getLeaves();
103
104 galaxyowlapi.disposeReasoner();
105
106 Iterator NodesIterator = nodes.iterator();
107 while(NodesIterator.hasNext()){
108 Set Assignments = ((BindingNode)NodesIterator.next()).getAssignments();
109 Iterator AssignmentIterator = Assignments.iterator();
110 while(AssignmentIterator.hasNext()){
111 Assignment assignment = (Assignment)AssignmentIterator.next();
112 System.out.print(assignment.getAssignedVariable().toString());
113 System.out.print("\t");
114 String OWLObjectString = ((OWLObject)assignment.getAssignment()).toString();
115 IRI entityIRI = IRI.create(OWLObjectString.substring(1,OWLObjectString.length()-1));
116 print_result_entity(entityIRI, Answer_render);
117 System.out.print("\n");
118 }
119 }
120 }
121
122 //URI|URIfragment|URIfragment2OBO
123 private static void print_result_entity (IRI iri, String Answer_render){
124 if(Answer_render.equals("URI")){
125 System.out.print(iri);
126 }
127
128 // Weird bug: in eclipse it can print out the IRIs of every entity, but in Galaxy it can't!
129 // done manually
130 else if(Answer_render.equals("URIfragment")){
131 if(iri.toString().contains("#")){
132 System.out.print(iri.getFragment());
133 }
134 else{
135 String [] iri_tokens = iri.toString().split("/");
136 System.out.print(iri_tokens[iri_tokens.length-1]);
137 }
138 }
139 else{
140 if(iri.toString().contains("#")){
141 System.out.print((iri.getFragment()).replace("_", ":"));
142 }
143 else{
144 String [] iri_tokens = iri.toString().split("/");
145 System.out.print((iri_tokens[iri_tokens.length-1]).replace("_", ":"));
146 }
147 }
148 }
149
150 // Very crappy stuff: complete the query to make a whole OPPL script that can be parsed
151 private static String completeOPPLScript (String OPPLQuery){
152 // System.out.println(OPPLQuery);
153 // Get the first variable and add at least an axiom to make a whole script
154 Pattern p = Pattern.compile("(\\?\\w+):(CLASS|INDIVIDUAL|OBJECTPROPERTY|DATAPROPERTY|ANNOTATIONPROPERTY|CONSTANT)");
155 Matcher m = p.matcher(OPPLQuery);
156 m.find();
157 if (m.group(2).equals("CLASS")){
158 OPPLQuery = OPPLQuery + " BEGIN ADD " + m.group(1) + " SubClassOf !A END;";
159 }
160 else if (m.group(2).equals("INDIVIDUAL")){
161 OPPLQuery = OPPLQuery + " BEGIN ADD " + m.group(1) + " !p !b END;";
162 }
163 else if (m.group(2).equals("OBJECTPROPERTY")){
164 OPPLQuery = OPPLQuery + " BEGIN ADD Transitive ?p END;";
165 }
166 // else if (m.group(2).equals("DATAPROPERTY")){
167 // OPPLQuery = OPPLQuery + " BEGIN ADD ?whole SubClassOf !A END;";
168 // }
169 // else if (m.group(2).equals("ANNOTATIONPROPERTY")){
170 // OPPLQuery = OPPLQuery + " BEGIN ADD ?whole SubClassOf !A END;";
171 // }
172 // else if (m.group(2).equals("CONSTANT")){
173 // OPPLQuery = OPPLQuery + " BEGIN ADD ?whole SubClassOf !A END;";
174 // }
175 else{
176 OPPLQuery = "Malformed OPPL query";
177 }
178 return OPPLQuery;
179 }
180 }