?? parser.java
字號:
package expertSystem.knowledgeBase;
import java.awt.peer.SystemTrayPeer;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import com.sun.org.apache.regexp.internal.recompile;
import expertSystem.exceptions.RuleAlreadyExistsException;
import expertSystem.exceptions.WrongFileTypeException;
import expertSystem.exceptions.WrongKnowledgBaseSyntaxException;
import expertSystem.presentation.Window;
public class Parser {
public boolean isLoad;
List<Fact> tempFactList;
List<Rule> tempRuleList;
Rule R;
Fact F;
public Parser(){
tempFactList = new ArrayList<Fact>();
tempRuleList = new ArrayList<Rule>();
}
public String wordTest(String S, int lineNumber)throws WrongKnowledgBaseSyntaxException{
int j = 0;
while(j<=S.length()-1){
if(S.codePointAt(j)==32 || S.codePointAt(j)==58 || S.codePointAt(j)==59)throw new WrongKnowledgBaseSyntaxException(lineNumber);
j++;
}
return S;
}
public String questionTest(String S, int lineNumber) throws WrongKnowledgBaseSyntaxException{
int j = 0;
while(j<=S.length()-1){
if(S.codePointAt(j)==58 || S.codePointAt(j)==59)throw new WrongKnowledgBaseSyntaxException(lineNumber);
j++;
}
return S;
}
public String probabilityTest(String S, int lineNumber) throws WrongKnowledgBaseSyntaxException{
int j = 0;
int i=S.indexOf('.');
if(i==-1 || S.indexOf('.', i+1)!=-1)throw new WrongKnowledgBaseSyntaxException(lineNumber);
while(j<=S.length()-1){
if(S.codePointAt(j)!=46 && (S.codePointAt(j)<48 || S.codePointAt(j)>57))throw new WrongKnowledgBaseSyntaxException(lineNumber);
j++;
}
return S+":";
}
public String atributesManager(String atributes, int lineNumber) throws WrongKnowledgBaseSyntaxException{
if(atributes.charAt(0)==' ' && atributes.charAt(atributes.length()-1)!=' '){
atributes=atributes.substring(1, atributes.length());
String parts[] = atributes.split(" ");
if((parts.length>3 || parts.length<2) || parts[0].isEmpty() || parts[1].isEmpty())throw new WrongKnowledgBaseSyntaxException(lineNumber);
if(parts.length==2)return wordTest(parts[0], lineNumber)+":"+wordTest(parts[1], lineNumber)+"::";
else return wordTest(parts[0], lineNumber)+":"+wordTest(parts[1], lineNumber)+":"+wordTest(parts[2], lineNumber)+":";
}else throw new WrongKnowledgBaseSyntaxException(lineNumber);
}
public String atributesManager2(String atributes, int lineNumber) throws WrongKnowledgBaseSyntaxException{
if(atributes.charAt(0)==' ' && atributes.charAt(atributes.length()-1)!=' '){
atributes=atributes.substring(1, atributes.length());
String parts[] = atributes.split(" ");
if(parts.length!=3 || parts[0].isEmpty() || parts[1].isEmpty()|| parts[2].isEmpty())throw new WrongKnowledgBaseSyntaxException(lineNumber);
return wordTest(parts[0], lineNumber)+":"+wordTest(parts[1], lineNumber)+":"+wordTest(parts[2], lineNumber)+":";
}else throw new WrongKnowledgBaseSyntaxException(lineNumber);
}
public String questionManager(String question, int lineNumber) throws WrongKnowledgBaseSyntaxException{
if(question.charAt(0)!=' ' && question.charAt(question.length()-1)!=' '){
String parts[] = question.split(",");
if(parts.length<3) throw new WrongKnowledgBaseSyntaxException(lineNumber);
boolean flag;
if(parts[0].isEmpty()){
flag=true;
for(int i=1;i<parts.length;i++){
if(!parts[i].isEmpty())flag=false;
}
if(!flag)throw new WrongKnowledgBaseSyntaxException(lineNumber);
return ":";
}else {
flag=false;
for(int i=1;i<parts.length;i++){
if(parts[i].isEmpty())flag=true;
}
if(flag)throw new WrongKnowledgBaseSyntaxException(lineNumber);
String s="";
for(int i=1;i<parts.length;i++){
s+=wordTest(parts[i], lineNumber)+":";
}
return questionTest(parts[0], lineNumber)+":"+s;
}
}return "";
}
public Rule ruleManager(String preANDcon, int lineNumber) throws WrongKnowledgBaseSyntaxException{
List<Fact> tempPrerequisiteList = new ArrayList<Fact>();
List<Fact> tempConclusionList = new ArrayList<Fact>();
String parts[] = preANDcon.split("THEN");
if(parts.length==2){
for (int i=0;i<parts.length;i++){
if(parts[i].charAt(0)==' ' && parts[i].charAt(parts[i].length()-1)==' '){
String parts2[] = parts[i].split("AND");
for(int j=0;j<parts2.length;j++){
if(parts2[j].equals(" ") || parts2[j].equals(""))throw new WrongKnowledgBaseSyntaxException(lineNumber);
if(parts2[j].charAt(0)!=' ' || parts2[j].charAt(parts2[j].length()-1)!=' ')throw new WrongKnowledgBaseSyntaxException(lineNumber);
parts2[j]=parts2[j].substring(0, parts2[j].length()-1);
preANDcon=atributesManager2(parts2[j], lineNumber);
if(!isLoad)continue;
Fact f;
String parts3[] = preANDcon.split(":");
if(parts3.length==2){f = new Fact(parts3[0], parts3[1], null);}
else {f = new Fact(parts3[0], parts3[1], parts3[2]);}
if(i==0)tempPrerequisiteList.add(f);
else tempConclusionList.add(f);
}
}else throw new WrongKnowledgBaseSyntaxException(lineNumber);
}
}else throw new WrongKnowledgBaseSyntaxException(lineNumber);
return new Rule(false, tempPrerequisiteList, tempConclusionList);
}
public Fact createFact(String parts[]){
Fact f;
double dob;
if(parts[2].isEmpty()){
if(parts[3].isEmpty()){
f = new Fact(parts[0], parts[1], null, false, dob=Double.parseDouble(parts[4]));
return f;
}
else {
f = new Fact(parts[0], parts[1], null);
f.setIsQustion(true);
f.setQuestion(parts[3]);
for(int i=4;i<parts.length-1;i++){
f.getAnswers().add(parts[i]);
}
f.setProbability(dob=Double.parseDouble(parts[parts.length-1]));
return f;
}
}else{
if(parts[3].isEmpty()){
f = new Fact(parts[0], parts[1], parts[2], false, dob=Double.parseDouble(parts[4]));
return f;
}
else {
f = new Fact(parts[0], parts[1], parts[2]);
f.setIsQustion(true);
f.setQuestion(parts[3]);
for(int i=4;i<parts.length-1;i++){
f.getAnswers().add(parts[i]);
}
f.setProbability(dob=Double.parseDouble(parts[parts.length-1]));
return f;
}
}
}
public void addFact(Fact F){
if(tempFactList.contains(F))System.out.println("Fact juz jest");
else{
tempFactList.add(F);
}
}
public void addRule(Rule R){
tempRuleList.add(R);
}
public KnowledgeBase checkORload(String fileName, boolean _isLoad) throws WrongFileTypeException, WrongKnowledgBaseSyntaxException, IOException, RuleAlreadyExistsException{
Window.baseFactNumberText.setText("");
this.isLoad = _isLoad;
String strResult="";
List<String> lines = new ArrayList<String>();
String line="";
if((fileName.length()<5)||(fileName.charAt(fileName.length()-4)!='.')||(fileName.charAt(fileName.length()-3)!='t')||(fileName.charAt(fileName.length()-2)!='x')||(fileName.charAt(fileName.length()-1)!='t')){
throw new WrongFileTypeException();
}
BufferedReader in = new BufferedReader(new FileReader(fileName));
do{
line = in.readLine();
if (line != null){
switch (lines.size()){
case 0:{
if(line.length()>=8 && line.startsWith("<TITLE>")){
if(line.charAt(7)!=' '){
Window.baseNameText.setText(line.substring(7,line.length()));
}else throw new WrongKnowledgBaseSyntaxException(1);
} else throw new WrongKnowledgBaseSyntaxException(1);
lines.add(line);
break;
}
case 1:{
if(line.length()>=9 && line.startsWith("<TARGET>")){
if(line.charAt(8)!=' '&& line.charAt(line.length()-1)==';'){
strResult=atributesManager(" "+line.substring(8, line.length()-1),2);
//if(!isLoad)break;
String parts[]=strResult.split(":");
if(parts.length==2)Window.machine.target=new Fact(parts[0],parts[1],null);
else Window.machine.target=new Fact(parts[0],parts[1],parts[2]);
System.out.println(Window.machine.target.toString()+" TO JEST CEL");
}else throw new WrongKnowledgBaseSyntaxException(2);
} else throw new WrongKnowledgBaseSyntaxException(2);
lines.add(line);
break;
}
default:{
lines.add(line);
break;
}
}
}
}while (line != null);
in.close();
for(int i=2;i<lines.size();i++){ //System.out.println(" test "+lines.size());
String oneLine=lines.get(i);
if(oneLine.isEmpty() || (oneLine.length()==1 && oneLine.charAt(0)!='#')) throw new WrongKnowledgBaseSyntaxException(i+1);
else if(oneLine.length()>0 && oneLine.charAt(0)=='#')continue;
else if(oneLine.length()>=10 && oneLine.startsWith("FACT")){
F=new Fact();
oneLine=oneLine.substring(4);
if(oneLine.charAt(0)==' ' && oneLine.charAt(oneLine.length()-1)==';'){
String parts[];
parts=oneLine.split(";");
if(parts.length!=3 || parts[0].isEmpty() || parts[2].isEmpty())throw new WrongKnowledgBaseSyntaxException(i+1);
oneLine=atributesManager(parts[0],i+1);
if(!parts[1].isEmpty())oneLine+=questionManager(parts[1],i+1);
else oneLine+=":";
oneLine+=probabilityTest(parts[2],i+1);
if(!isLoad)continue;
parts=oneLine.split(":");
F=createFact(parts);
if(tempFactList.isEmpty())tempFactList.add(F);
else addFact(F);
}else throw new WrongKnowledgBaseSyntaxException(i+1);
}else if(oneLine.length()>=20 && oneLine.startsWith("IF")){
R = new Rule();
for(int iii=0;iii<tempRuleList.size();iii++){
//System.out.println("RULE_"+(iii+1)+" : "+tempRuleList.get(iii).toString()+"\n");
}
if(oneLine.charAt(2)==' ' && oneLine.charAt(oneLine.length()-1)==';'){
oneLine=oneLine.substring(2, oneLine.length()-1);
oneLine+=" ";
Rule R = ruleManager(oneLine,i+1);
if(!isLoad)continue;
if(tempRuleList.size()!=0)
for(Rule r : tempRuleList){
if(r.equals(R))throw new RuleAlreadyExistsException(i+1);
}
if(tempRuleList.isEmpty())tempRuleList.add(R);
else addRule(R);//System.out.println(" ZNACZNIK222222");
}else throw new WrongKnowledgBaseSyntaxException(i+1);
}else throw new WrongKnowledgBaseSyntaxException(i+1);
}
/*for(int i=0;i<tempFactList.size();i++){
System.out.println("FACT_"+(i+1)+" : "+tempFactList.get(i).toString()+"\n");
}
for(int i=0;i<tempRuleList.size();i++){
System.out.println("RULE_"+(i+1)+" : "+tempRuleList.get(i).toString()+"\n");
}*/
Window.baseFactNumberText.setText(""+tempFactList.size());
Window.baseRuleNumberText.setText(""+tempRuleList.size());
return new KnowledgeBase(tempFactList, tempRuleList);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -