⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rule.java

📁 这是外国一个开源推理机
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*  Sesame - Storage and Querying architecture for RDF and RDF Schema *  Copyright (C) 2003 OntoText Lab, Sirma AI OOD * *  Contact: *	Sirma AI OOD, OntoText Lab. *	38A, Christo Botev Blvd. *  1000 Sofia, Bulgaria *	tel. +359(2)981 00 18 *	fax. +359(2)981 90 58 *	info@ontotext.com * * 	http://www.ontotext.com/ * *  This library is free software; you can redistribute it and/or *  modify it under the terms of the GNU Lesser General Public *  License as published by the Free Software Foundation; either *  version 2.1 of the License, or (at your option) any later version. * *  This library is distributed in the hope that it will be useful, *  but WITHOUT ANY WARRANTY; without even the implied warranty of *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *  Lesser General Public License for more details. * *  You should have received a copy of the GNU Lesser General Public *  License along with this library; if not, write to the Free Software *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */package org.openrdf.sesame.sailimpl.rdbms.rules;/** * <p>Title: Custom Inference Rules</p> * <p>Description:</p> * The class is used to produce SQL queries used for statement and dependancy inferencing. * It keeps several TripleTemplates defining relationships between triples that lead * to the inference of new ones. The templates consist of three Component instances for * subject, predicate and object and can represent an URI, variable or regular expression. * * <p>Company: Ontotext Lab. Sirma AI</p> * @author Damyan Ognyanoff * @version 1.0 */import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import org.openrdf.sesame.sailimpl.rdbms.CustomInferenceServices;import org.openrdf.sesame.sailimpl.rdbms.TableNames;public class Rule implements TableNames {	final static String C_SELECT = "SELECT DISTINCT ";	/**	 * the map that hold id's (as Integer variables) fro each non-variable string found in the rules.	 * These ids are afterwards matchd against the real ids of those resources with	 * same URIs and used within generated SQL queries.	 */	static HashMap idMap = new HashMap();	/**	 * collection of all triple templates for the rule	 */	ArrayList premiseList;	/**	 * The consequent template	 */	TripleTemplate consequent;	/**	 * The name of the Rule	 */	String ruleName;	ArrayList triggersRule = null;	HashMap regExpByVars;	void normalizeRegExpVar(Component var) {		if (var.isRegExp()) {			if (regExpByVars == null) {				regExpByVars = new HashMap();			}			regExpByVars.put(var.value(), var);		}	}	static String getFROM() {		return " FROM " + NEW_TRIPLES_TABLE + " nt ";	}	static String getLEFT_JOIN(int id) {		return "LEFT JOIN " + TRIPLES_TABLE + " t" + id + " ON ";	}	public static Iterator constantsIter() {		return idMap.keySet().iterator();	}	public static String getId(Component constant) {		Integer val = (Integer)idMap.get(constant.value());		if (val == null) {			if (!constant.isVar()) {				val = setId(constant, idMap.size());			}			else {				return null;			}		}		return val.toString();	}	public static int getIntId(Component constant) {		Integer val = (Integer)idMap.get(constant.value());		if (val == null) {			if (!constant.isVar()) {				val = setId(constant, idMap.size());			}			else {				return -1;			}		}		return val.intValue();	}	public static Integer setId(Component constant, int id) {		if (!constant.isVar()) {			return setId(constant.value(), id);		}		return new Integer(id);	}	public static Integer setId(String constant, int id) {		Integer i = new Integer(id);		idMap.put(constant, i);		return i;	}	public Rule(String name) {		premiseList = new ArrayList();		consequent = null;		ruleName = name;	}	public void addPremise(TripleTemplate t) {		if (!t.subject.isVar()) {			getId(t.subject);		}		if (!t.predicate.isVar()) {			getId(t.predicate);		}		if (!t.object.isVar()) {			getId(t.object);		}		normalizeRegExpVar(t.subject);		normalizeRegExpVar(t.predicate);		normalizeRegExpVar(t.object);		premiseList.add(t);	}	public void setConsequent(TripleTemplate t) {		if (!t.subject.isVar()) {			getId(t.subject);		}		if (!t.predicate.isVar()) {			getId(t.predicate);		}		if (!t.object.isVar()) {			getId(t.object);		}		normalizeRegExpVar(t.subject);		normalizeRegExpVar(t.predicate);		normalizeRegExpVar(t.object);		consequent = t;	}	private boolean matchwasbyobject = false;	String match(Component component, int until) {		matchwasbyobject = false;		if (!component.isVar()) {			return Rule.getId(component);		}		int count = 0;		Iterator iter = premiseList.iterator();		while (iter.hasNext()) {			if (count >= until) {				break;			}			TripleTemplate tt = (TripleTemplate)iter.next();			if (0 == component.compareTo(tt.subject)) {				return "" + ((count == 0) ? "nt" : ("t" + count)) + ".subj";			}			if (0 == component.compareTo(tt.predicate)) {				return "" + ((count == 0) ? "nt" : ("t" + count)) + ".pred";			}			if (0 == component.compareTo(tt.object)) {				matchwasbyobject = true;				return "" + ((count == 0) ? "nt" : ("t" + count)) + ".obj";			}			count++;		}		return null;	}	private String where = "";	void processJoin(StringBuffer buffer, int id, TripleTemplate tt) {		buffer.append(' ');		buffer.append(Rule.getLEFT_JOIN(id));		boolean bFirst = false;		String s = match(tt.subject, id);		if (s != null) {			buffer.append("t" + id);			buffer.append(".subj = ");			buffer.append(s);			if (matchwasbyobject) {				if (where.length() != 0) {					where += " AND ";				}				where += s + " > 0"; // non-literal restriction			}			bFirst = true;		}		s = match(tt.predicate, id);		if (s != null) {			if (bFirst) {				buffer.append(" AND ");			}			buffer.append("t" + id);			buffer.append(".pred= ");			buffer.append(s);			bFirst = true;		}		s = match(tt.object, id);		if (s != null) {			if (bFirst) {				buffer.append(" AND ");			}			buffer.append("t" + id);			buffer.append(".obj= ");			buffer.append(s);		}		processjoinWithRegExpr(buffer, tt.subject, "t" + id, "subj");		processjoinWithRegExpr(buffer, tt.predicate, "t" + id, "pred");		processjoinWithRegExpr(buffer, tt.object, "t" + id, "obj");	}	public ArrayList getAllSQLs() {		ArrayList result = new ArrayList();		result.add(getSQL());		if (premiseList.size() < 2) {			return result;		}		for (int i = 1; i < premiseList.size(); i++) {			Object f = premiseList.remove(0);			premiseList.add(f);			result.add(getSQL());		}		//restore		Object f = premiseList.remove(0);		premiseList.add(f);		return result;	}	String getSQL() {		alreadyJoined = null;		where = "";		StringBuffer buffer = new StringBuffer();		buffer.append(C_SELECT);		buffer.append(match(consequent.subject, premiseList.size()));		buffer.append(',');		buffer.append(match(consequent.predicate, premiseList.size()));		buffer.append(',');		buffer.append(match(consequent.object, premiseList.size()));		buffer.append('\n');		buffer.append(Rule.getFROM());		buffer.append('\n');		//    processjoinWithRegExpr(buffer, consequent.subj, "nt", "subj");		//    processjoinWithRegExpr(buffer, consequent.pred, "nt", "pred");		//    processjoinWithRegExpr(buffer, consequent.obj, "nt", "obj");		for (int i = 1; i < premiseList.size(); i++) {			TripleTemplate tt = (TripleTemplate)premiseList.get(i);			processJoin(buffer, i, tt);		}		processJoin(buffer, premiseList.size(), consequent);		TripleTemplate ttFirst = (TripleTemplate)premiseList.get(0);		buffer.append(" WHERE ");		boolean ifFirst = false;		if (where.length() > 0) {			buffer.append(where);			ifFirst = true;		}		if (!ttFirst.subject.isVar()) {			if (ifFirst) {				buffer.append(" AND ");			}			buffer.append("nt.subj= ");			buffer.append(getId(ttFirst.subject));			ifFirst = true;		}		if (!ttFirst.predicate.isVar()) {			if (ifFirst) {				buffer.append(" AND ");			}			buffer.append("nt.pred = ");			buffer.append(getId(ttFirst.predicate));			ifFirst = true;		}		if (!ttFirst.object.isVar()) {			if (ifFirst) {				buffer.append(" AND ");			}			buffer.append("nt.obj = ");			buffer.append(getId(ttFirst.object));			ifFirst = true;		}		for (int i = 1; i < premiseList.size(); i++) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -