1 10 package jena; 11 12 13 import com.hp.hpl.jena.graph.*; 14 import com.hp.hpl.jena.rdf.model.*; 15 import com.hp.hpl.jena.reasoner.Reasoner; 16 import com.hp.hpl.jena.reasoner.rulesys.*; 17 import com.hp.hpl.jena.reasoner.rulesys.builtins.BaseBuiltin; 18 import com.hp.hpl.jena.util.FileManager; 19 import com.hp.hpl.jena.util.FileUtils; 20 21 import jena.cmdline.*; 22 import java.util.*; 23 import java.io.*; 24 25 44 public class RuleMap { 45 46 51 public static List loadRules(String filename, Map prefixes) throws IOException { 52 String fname = filename; 53 if (fname.startsWith("file:///")) { 54 fname = File.separator + fname.substring(8); 55 } else if (fname.startsWith("file:/")) { 56 fname = File.separator + fname.substring(6); 57 } else if (fname.startsWith("file:")) { 58 fname = fname.substring(5); 59 } 60 61 BufferedReader src = FileUtils.openResourceFile(fname); 62 return loadRules(src, prefixes); 63 } 64 65 70 public static List loadRules(BufferedReader src, Map prefixes) throws IOException { 71 Rule.Parser parser = Rule.rulesParserFromReader(src); 72 List rules = Rule.parseRules(parser); 73 prefixes.putAll(parser.getPrefixMap()); 74 return rules; 75 } 76 77 81 static class Deduce extends BaseBuiltin { 82 83 87 public String getName() { 88 return "deduce"; 89 } 90 91 94 public int getArgLength() { 95 return 3; 96 } 97 98 107 public void headAction(Node[] args, int length, RuleContext context) { 108 if (context.getGraph() instanceof FBRuleInfGraph) { 109 Triple t = new Triple(args[0], args[1], args[2]); 110 ((FBRuleInfGraph)context.getGraph()).addDeduction(t); 111 } else { 112 throw new BuiltinException(this, context, "Only usable in FBrule graphs"); 113 } 114 } 115 } 116 117 124 public static void main(String [] args) { 125 try { 126 127 CommandLine cl = new CommandLine(); 129 String usage = "Usage: RuleMap [-il inlang] [-ol outlang] [-d] rulefile infile"; 130 cl.setUsage(usage); 131 cl.add("il", true); 132 cl.add("ol", true); 133 cl.add("d", false); 134 cl.process(args); 135 if (cl.items().size() != 2) { 136 System.err.println(usage); 137 System.exit(1); 138 } 139 140 Arg il = cl.getArg("il"); 142 String inLang = (il == null) ? null : il.getValue(); 143 Model inModel = FileManager.get().loadModel((String )cl.items().get(1), inLang); 144 145 Arg ol = cl.getArg("ol"); 147 String outLang = (ol == null) ? "N3" : ol.getValue(); 148 149 Arg d = cl.getArg("d"); 150 boolean deductionsOnly = (d != null); 151 152 BuiltinRegistry.theRegistry.register(new Deduce()); 154 Map prefixes = new HashMap(); 155 List rules = loadRules((String )cl.items().get(0), prefixes); 156 Reasoner reasoner = new GenericRuleReasoner(rules); 157 158 InfModel infModel = ModelFactory.createInfModel(reasoner, inModel); 160 infModel.prepare(); 161 infModel.setNsPrefixes(prefixes); 162 163 PrintWriter writer = new PrintWriter(System.out); 165 if (deductionsOnly) { 166 Model deductions = infModel.getDeductionsModel(); 167 deductions.setNsPrefixes(prefixes); 168 deductions.setNsPrefixes(inModel); 169 deductions.write(writer, outLang); 170 } else { 171 infModel.write(writer, outLang); 172 } 173 writer.close(); 174 175 } catch (Throwable t) { 176 System.err.println("An error occured: \n" + t); 177 t.printStackTrace(); 178 } 179 } 180 181 } 182 183 212 | Popular Tags |