Created
September 18, 2019 00:46
-
-
Save ognev-zair/d9241ddb8d1fc7938be1793f31956c79 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package rulesengine; | |
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.Iterator; | |
import java.util.List; | |
import jess.JessException; | |
import jess.Rete; | |
import jess.Value; | |
import jess.ValueVector; | |
import util.CodingUtils; | |
public class ReteWrapper { | |
private static Rete engine = new Rete(); | |
private String rulesAsString; | |
private HashMap table; | |
private final String UPDATE_KEY = "update"; | |
private String currentModule; | |
private List<String> updates; | |
public void setRulesAsString(File rules, boolean isEncoded) throws IOException { | |
if (isEncoded) { | |
this.rulesAsString = CodingUtils.decodeAsString(rules); | |
} else { | |
this.rulesAsString = this.readFile(rules.getAbsolutePath()); | |
} | |
} | |
public void setTable(HashMap table) { | |
this.table = table; | |
} | |
public void setCurrentModule(String currModule) { | |
this.currentModule = currModule; | |
} | |
String readFile(String filename) throws IOException { | |
String theString = null; | |
String newline = System.getProperty("line.separator"); | |
BufferedReader reader = new BufferedReader(new FileReader(filename)); | |
StringBuffer sb = new StringBuffer(); | |
String line = null; | |
while((line = reader.readLine()) != null) { | |
sb.append(line + newline); | |
} | |
theString = sb.toString(); | |
return theString; | |
} | |
public synchronized void runRules() throws OperatingException, ValidationException { | |
this.clearEngine(); | |
this.loadJessFiles(); | |
this.prepareData(); | |
this.execute(); | |
} | |
public List<String> getUpdates() { | |
return this.updates; | |
} | |
private void clearEngine() throws OperatingException { | |
try { | |
engine.clear(); | |
} catch (JessException var2) { | |
throw new OperatingException(var2.getMessage()); | |
} | |
} | |
private void loadJessFiles() throws OperatingException { | |
try { | |
engine.eval(this.rulesAsString); | |
} catch (JessException var2) { | |
throw new OperatingException(var2.getMessage()); | |
} | |
} | |
private void prepareData() throws OperatingException { | |
try { | |
Iterator it = this.table.keySet().iterator(); | |
while(it.hasNext()) { | |
String key = (String)it.next(); | |
Object ob = this.table.get(key); | |
engine.definstance(key, ob, true); | |
} | |
} catch (JessException var4) { | |
var4.printStackTrace(); | |
throw new OperatingException(var4.getMessage()); | |
} | |
} | |
private void execute() throws OperatingException, ValidationException { | |
try { | |
engine.reset(); | |
if (this.currentModule != null) { | |
engine.setFocus(this.currentModule); | |
} | |
engine.run(); | |
this.setUpdateValues(engine.fetch("update")); | |
} catch (JessException var3) { | |
Throwable x = null; | |
if ((x = var3.getCause()) instanceof ValidationException) { | |
throw new ValidationException(x.getMessage()); | |
} else { | |
throw new OperatingException(var3.getCause().getMessage()); | |
} | |
} | |
} | |
private void setUpdateValues(Value v) throws OperatingException { | |
try { | |
if (v != null) { | |
this.updates = new ArrayList(); | |
ValueVector vv = v.listValue(engine.getGlobalContext()); | |
int size = vv.size(); | |
for(int i = 0; i < size; ++i) { | |
this.updates.add(vv.get(i).stringValue(engine.getGlobalContext())); | |
} | |
} | |
} catch (JessException var5) { | |
throw new OperatingException(var5.getMessage()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment