Java Semantic Network Programming Series 6: Jena native Reasoning

Source: Internet
Author: User

Rule Description:

A rule is a way to represent knowledge. It generally exceeds the expression capability of owl1. A Semantic web rule is a typical Condition Statement: if-then. New knowledge is added only when a specific statement is true.

The Jena framework comes with a reasoning engine and a recognized rule language. Its advantages are that the jena framework is relatively simple to use and its disadvantages are not w3c reasoning standards.

Ontology modeling:

 

 

1 import com. clarkparsia. sparqlowl. parser. anlr. sparqlOwlParser. string_return; 2 3 import com. hp. hpl. jena. ontology. ontModel; 4 5 import com. hp. hpl. jena. ontology. ontModelSpec; 6 7 import com. hp. hpl. jena. rdf. model. infModel; 8 9 import com. hp. hpl. jena. rdf. model. model; 10 11 import com. hp. hpl. jena. rdf. model. modelFactory; 12 13 import com. hp. hpl. jena. reasoner. reasoner; 14 15 import com. hp. hpl. jena. reaso Ner. rulesys. genericRuleReasoner; 16 17 import com. hp. hpl. jena. reasoner. rulesys. rule; 18 19 20 21 public final class JenaRule 22 23 {24 25 JenaModel jenaModel = null; 26 27 28 29/** 30 31 * jena native inference engine reasoning 32 33 * @ param ruleString 34 35 * @ param filePathString 36 37 */38 39 public void reasoner (String ruleString, string filePathString) 40 41 {42 43 jenaModel = new JenaModel (); 44 45 OntModel ontM Odel = jenaModel. createOntologyModel (OntModelSpec. OWL_DL_MEM, filePathString); 46 47 Reasoner reasoner = new GenericRuleReasoner (Rule. parseRules (ruleString); 48 49 InfModel resulInfModel = ModelFactory. createInfModel (reasoner, ontModel); 50 51 jenaModel. outPut (resulInfModel ); 52 53} 54 55 56 57/** 58 59 * join predicates 60 61 * @ param prefixString 62 63 * @ param predicateString 64 65 * @ return 66 67 */68 69 public String getPredicate (String prefixString, String predicateString) 70 71 {72 73 return "<" + prefixString + predicateString + "> "; 74 75} 76 77} 78 79 80 81 Rule rules: 82 83 @ Test 84 85 public void testJenaRuleReasoner () 86 87 {88 89 JenaModel jenaModel = new JenaModel (); 90 91 jenaModel. outPut (jenaModel. createOntologyModel (OntModelSpec. OWL_DL_MEM, fileString); 92 93 94 95 System. out. println ("After inference"); 96 97 98 99 JenaRule jenaRule = new JenaRule (); 100 101 StringBuffer ruleStringBuffer = new StringBuffer (); 102 103 // rule 1: mother's male compatriot is Uncle 104 105 ruleStringBuffer. append ("[rule1:"); 106 107 ruleStringBuffer. append ("(? Person "+ jenaRule. getPredicate (prefixString," hasParent ") + "? Parent), "); 108 109 ruleStringBuffer. append ("(? Parent "+ jenaRule. getPredicate (prefixString," hasSex ") + "? Sex), "); 110 111 ruleStringBuffer. append (" equal (? Sex, \ "female \"), "); 112 113 ruleStringBuffer. append ("(? Parent "+ jenaRule. getPredicate (prefixString," hasSibling ") + "? Sibling), "); 114 115 ruleStringBuffer. append ("(? Sibling "+ jenaRule. getPredicate (prefixString," hasSex ") + "? Siblingsex), "); 116 117 ruleStringBuffer. append (" equal (? Siblingsex, \ "male \") "); 118 119 ruleStringBuffer. append ("-> "); 120 121 ruleStringBuffer. append ("(? Person "+ jenaRule. getPredicate (prefixString," hasUncle ") + "? Sibling)] "); 122 123 jenaRule. reasoner (ruleStringBuffer. toString (), fileString); 124 125}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.