semRewrite.SemRewriteRuleCheck Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sigma-nlp Show documentation
Show all versions of sigma-nlp Show documentation
Natural language processing toolbox using Sigma knowledge engineering system.
package semRewrite;
/*
Copyright 2014-2015 IPsoft
Author: Peigen You [email protected]
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program ; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston,
MA 02111-1307 USA
*/
import semRewrite.RuleSet;
import java.util.*;
/************************************************************
* A class for check Rewrite Rule subsume condition
*
* Functions:
* checkRuleSet will check the rule set subsume condition
* isRuleSubsumedByRuleSet will check the rule subsume condition with the exsiting ruleset
*/
public class SemRewriteRuleCheck {
private static boolean isIgnoreCNFOnRight = true;
/***********************************************************
* //TODO
*/
private static void printRuleSetTree(ArrayList rset, Map> links){
class RuleNode{
RuleNode p=null;
Integer r=null;
ArrayList getSubsumedList;
ArrayList childCNFList;
RuleNode(Integer r,RuleNode p){
this.r=r;
this.p=p;
}
}
ArrayList rootNodes=new ArrayList();
RuleNode[] visited=new RuleNode[rset.size()];
Arrays.fill(visited,null);
HashSet children=null;
for(int i=rset.size()-1;i>=0;--i){
children=links.get(i);
RuleNode rn=visited[i];
if(rn==null){
rn=new RuleNode(i,null);
rootNodes.add(rn);
visited[i]=rn;
}
if(children==null)
continue;
for(Integer k:children){
RuleNode rnc=visited[k];
if(rnc==null) {
rnc = new RuleNode(k, rn);
visited[k] = rnc;
}else continue;
if(rn.getSubsumedList==null) rn.getSubsumedList=new ArrayList();
rn.getSubsumedList.add(rnc);
}
}
for(RuleNode rn:rootNodes){
System.out.print("Root:");
Queue q=new LinkedList();
q.offer(rn);
RuleNode father=null;
while(!q.isEmpty()){
RuleNode e=q.poll();
if(e.r==null){
System.out.print("=>");
father=null;
continue;
}
if(father!=e.p){
System.out.print("||");
father=e.p;
q.offer(new RuleNode(null,null));
}
System.out.print(e.r+" ");
if(e.getSubsumedList!=null) q.addAll(e.getSubsumedList);
}
System.out.println();
}
}
/***********************************************************
* Check the whole Ruleset. Print all possible subsumptions.
* Change isIgnoreCNFOnRight value to control if CNF on right rule conflict should be printed.
*/
public static Map> checkRuleSet(semRewrite.RuleSet rs) {
HashMap> subsumMap = new HashMap>();
int len = rs.rules.size();
for (int i = 0; i < len; ++i) {
Rule r = rs.rules.get(i);
ArrayList getSubsumedRules = new ArrayList();
ArrayList subsumerRules = new ArrayList();
isRuleSubsumedByRuleSet(r, rs, getSubsumedRules, subsumerRules);
if (subsumerRules.size() != 0) {
HashSet l = subsumMap.get(i);
if (l == null)
l = new HashSet();
for (int k : subsumerRules) {
if (k != i)
l.add(k);
}
if (l.size() != 0)
subsumMap.put(i, l);
}
if (getSubsumedRules.size() != 0) {
HashSet l;
for (int h : getSubsumedRules) {
if (h == i)
continue;
l = subsumMap.get(h);
if (l == null)
l = new HashSet();
l.add(i);
subsumMap.put(h, l);
}
}
}
int count = 0;
for (Integer r : subsumMap.keySet()) {
for (Integer k : subsumMap.get(r)) {
if (k > r) {
if (isIgnoreCNFOnRight)
if (rs.rules.get(r).rhs.cnf != null)
continue;
count++;
System.out.println("\nLine " + rs.rules.get(r).startLine + " and " + rs.rules.get(k).startLine);
System.out.println("rules = \n" + rs.rules.get(r) + "\n.\n" + rs.rules.get(k));
}
}
}
System.out.println("There are total " + count + " need to be check.");
return subsumMap;
}
/***********************************************************
* get all the Rules in Ruleset that would be subsumed or subsums the rule r
* should pass in the arraylist as result space
*/
public static void isRuleSubsumedByRuleSet(Rule r, RuleSet rs, ArrayList getSubsumedRules, ArrayList subsumerRules) {
getSubsumedRules.clear();
subsumerRules.clear();
CNF lc = Clausifier.clausify(r.lhs.deepCopy());
for (int i = 0; i < rs.rules.size(); ++i) {
CNF rc = rs.rules.get(i).cnf.deepCopy();
if (isCNFSubsumedNaive(lc, rc)) {
subsumerRules.add(i);
}
if (isCNFSubsumedNaive(rc, lc)) {
getSubsumedRules.add(i);
}
}
}
/***********************************************************
* check if CNF subsumed, naive implementation
*/
private static boolean isCNFSubsumedNaive(CNF subsumer, CNF subsumed) {
HashMap binding = subsumer.unify(subsumed);
if (binding != null) {
subsumed = subsumer.applyBindings(binding);
}
subsumed.clearBound();
subsumer.clearBound();
if (subsumed.clauses.size() < subsumer.clauses.size()) return false;
//check if subsumed has all the clauses that subsumer has.
if (subsumed.clauses.containsAll(subsumer.clauses))
return true;
return false;
}
/***********************************************************
* //TODO
*/
private static boolean isCNFSubsumed(CNF subsumer, CNF subsumed) {
return false;
}
}