package paper.reasoning;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.InfModel;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.reasoner.Reasoner;
import com.hp.hpl.jena.reasoner.rulesys.GenericRuleReasoner;
import com.hp.hpl.jena.reasoner.rulesys.Rule;
import com.hp.hpl.jena.util.PrintUtil;
public class TestRule {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
OntModel model = ModelFactory
.createOntologyModel(OntModelSpec.OWL_DL_MEM);
model.read(new FileInputStream(new File("qa.owl")), null);
PrintUtil.registerPrefix("qa", "http://www.example.com/qa.owl#");
String ruleSrc = "[rule1: (?key rdf:type qa:����Ҫ��) " + " (?key qa:��ֵ ?points) " +
" (?answer rdf:type qa:�Ծ�����Ļش�) " +
" (?question rdf:type qa:�Ծ�����) " +
" (?standard rdf:type qa:����) " +
" (?answer qa:�Ծ������� ?question)" +
" (?question qa:������ ?standard) " +
" (?standard qa:Ҫ���� ?key) " +
" (?answer qa:Ҫ���� ?key) " +
" noValue(?key qa:�÷�) " +
" -> " +
" (?key qa:�÷� ?points)"
+ "]";
List rules = Rule.parseRules(ruleSrc);
Reasoner reasoner = new GenericRuleReasoner(rules);
InfModel inf = ModelFactory.createInfModel(reasoner, model);
PrintUtil.printOut(inf.getDeductionsModel().listStatements());
System.out.println("-------");
// ruleSrc = "[rule2: (?key rdf:type qa:����Ҫ��) noValue(?key qa:�÷�) -> (?key qa:�÷� 0.0) ]";
ruleSrc = "[rule2: (?keya rdf:type qa:����Ҫ��) " +
" (?keyb rdf:type qa:����Ҫ��) " +
" (?keyb qa:��ֵ ?points) " +
" (?keya ?p ?keyb)" +
" (?answer rdf:type qa:�Ծ�����Ļش�) " +
" (?question rdf:type qa:�Ծ�����) " +
" (?standard rdf:type qa:����) " +
" (?answer qa:�Ծ������� ?question)" +
" (?question qa:������ ?standard) " +
" (?standard qa:Ҫ���� ?keya) " +
" (?standard qa:Ҫ���� ?keyb) " +
" (?answer qa:Ҫ���� ?keya) " +
" noValue(?answer qa:Ҫ���� ?keyb) " +
" product(?points, 0.5, ?wpoints ) " +
" -> " +
" (?keyb qa:�÷� ?wpoints) " +
" ]";
rules = Rule.parseRules(ruleSrc);
reasoner = new GenericRuleReasoner(rules);
InfModel inf2 = ModelFactory.createInfModel(reasoner, inf);
PrintUtil.printOut(inf2.getDeductionsModel().listStatements());
System.out.println("-------");
ruleSrc = "[rule3: (?key rdf:type qa:����Ҫ��) noValue(?key qa:�÷�) -> (?key qa:�÷� 0.0) ]";
rules = Rule.parseRules(ruleSrc);
reasoner = new GenericRuleReasoner(rules);
InfModel inf3 = ModelFactory.createInfModel(reasoner, inf2);
PrintUtil.printOut(inf3.getDeductionsModel().listStatements());
System.out.println("-------");
ruleSrc = "[rule4: (qa:�Ӱ� qa:�÷� ?jbpoints) " +
" (qa:��������λ�� qa:�÷� ?wzpoints) " +
" (qa:н�� qa:�÷� ?xcpoints) " +
" (qa:�������� qa:�÷� ?hjpoints ) " +
" sum(?jbpoints, ?wzpoints, ?temp1) " +
" sum(?temp1, ?xcpoints, ?temp2) " +
" sum(?temp2, ?hjpoints, ?score) " +
" (?answer rdf:type qa:�Ծ�����Ļش�) " +
" (?question rdf:type qa:�Ծ�����) " +
" (?standard rdf:type qa:����) " +
" (?answer qa:�Ծ������� ?question)" +
" (?question qa:������ ?standard) " +
" -> " +
" (?answer qa:�÷� ?score) " +
"]";
rules = Rule.parseRules(ruleSrc);
reasoner = new GenericRuleReasoner(rules);
InfModel inf4 = ModelFactory.createInfModel(reasoner, inf3);
PrintUtil.printOut(inf4.getDeductionsModel().listStatements());
System.out.println("-------");
}
}