i created a java application which is working fine. I want to now port it to web application. However i am quite new to web application. So i just created a simple controller and used my class inside to test if it works. However when my controller runs , i call the below class function which works on back end does some file / prolog processing. For the first time the results are ok but when i refresh the page to re run the process it takes too much time and sometime it gives me Array List out of bond error.
Why is this happening? It works absolutely perfect in normal java app. Is there any logic behind if you refresh the page the controller loads everything from memory and doesn't create new initialization? Any help please.
public class DeductiveDB {
private String extractedAttr;
private String orignalText;
public List<String> baseClass;
public List<String> attrName;
public DeductiveDB() {
}
/**
* Querying the knowledge base.
*
* @throws Throwable
*/
public void consultKB() {
// Queries prolog
// Queries evalAMl.pl
String evalAML = "consult('evalAML.pl')";
System.out.println(evalAML + " " + (Query.hasSolution(evalAML) ? "succeeded" : "failed"));
// Queries eval.
String eval = "eval";
Query.hasSolution(eval);
// Queries writePredicates.
String writeFiles = "writePredicates";
Query.hasSolution(writeFiles);
// formats the output.txt in java objects
try {
readOutput();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String attributes[] = extractedAttr.split(",");
attrName = new ArrayList<String>();
// loops through all atributes
for (int m = 0; m < attributes.length; m++) {
// performs query to get the attribute name
Map<String, Term>[] results = Query.allSolutions("hasAttributeName(" + attributes[m] + ",Y)");
for (int i = 0; i < results.length; i++) {
// stores in array
attrName.add(results[i].get("Y").toString());
// updates output.txt
orignalText = orignalText.replaceAll(attributes[m], results[i].get("Y").toString());
}
}
// writes the attributes names in the output.txt
PrintWriter prologWriter = null;
try {
prologWriter = new PrintWriter(new File(ConfigManager.getFilePath() + "/output.txt"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
prologWriter.println(orignalText);
prologWriter.close();
try {
addBaseClass(attributes);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Reads the output.txt for mapping the attributes to names or values so
* that integration can be performed. Mapping is important to identify the
* attributes in AML files. This extracts the attributes from datalog format
* to java string objects so that query can be made on them.
*
* @param extractedAttr
* @param orignalText
* @throws Exception
*/
public void readOutput() throws Exception {
BufferedReader br = new BufferedReader(new FileReader(ConfigManager.getFilePath() + "/output.txt"));
try {
StringBuilder sb = new StringBuilder();
StringBuilder orignal = new StringBuilder();
String line = br.readLine();
while (line != null) {
orignal.append(line);
orignal.append(System.lineSeparator());
int a = line.indexOf('(');
int b = line.indexOf(')');
line = line.substring(a + 1, b);
sb.append(line + ",");
line = br.readLine();
}
extractedAttr = sb.toString();
orignalText = orignal.toString();
} finally {
br.close();
}
}
/**
* (Work in progress) This function adds a new output.txt for integration
* which mentions the attribute names and the classes it belongs to. This is
* required for identification of attributes if there are multiple
* attributes with same name. This helps in integration process.
*
* @param attributes
* @throws FileNotFoundException
*/
void addBaseClass(String attributes[]) throws FileNotFoundException {
baseClass = new ArrayList<String>();
for (int z = 0; z < attributes.length; z++) {
if (Query.hasSolution("hasAttribute(Y," + attributes[z] + ")")) {
Map<String, Term>[] results2 = Query.allSolutions("hasAttribute(Y," + attributes[z] + ")");
for (int i = 0; i < results2.length; i++) {
Map<String, Term>[] results3 = Query
.allSolutions("hasAttributeName(" + results2[i].get("Y").toString() + ",Y)");
for (int k = 0; k < results3.length; k++) {
baseClass.add(results3[k].get("Y").toString());
}
}
} else {
baseClass.add(attributes[z]);
}
}
StringBuilder sb = new StringBuilder();
for (int w = 0; w < baseClass.size(); w++) {
if (!sb.toString().contains(baseClass.get(w) + "," + attrName.get(w))) {
sb.append(baseClass.get(w) + "," + attrName.get(w));
sb.append(System.lineSeparator());
}
}
PrintWriter prologWriter = new PrintWriter(new File(ConfigManager.getFilePath() + "/output2.txt"));
prologWriter.println(sb.toString());
prologWriter.close();
baseClass.clear();
attrName.clear();
}
}
Aucun commentaire:
Enregistrer un commentaire