[17] | 1 | package hozo.sparql.edit;
|
---|
[9] | 2 |
|
---|
| 3 | import java.util.ArrayList;
|
---|
| 4 | import java.util.List;
|
---|
| 5 |
|
---|
[39] | 6 |
|
---|
[9] | 7 | import org.openrdf.repository.RepositoryConnection;
|
---|
| 8 | import com.franz.agraph.jena.AGGraph;
|
---|
| 9 | import com.franz.agraph.jena.AGGraphMaker;
|
---|
| 10 | import com.franz.agraph.jena.AGModel;
|
---|
| 11 | import com.franz.agraph.repository.AGCatalog;
|
---|
| 12 | import com.franz.agraph.repository.AGRepository;
|
---|
| 13 | import com.franz.agraph.repository.AGRepositoryConnection;
|
---|
| 14 | import com.franz.agraph.repository.AGServer;
|
---|
| 15 | import com.hp.hpl.jena.rdf.model.Model;
|
---|
| 16 | import com.hp.hpl.jena.rdf.model.Property;
|
---|
| 17 | import com.hp.hpl.jena.rdf.model.RDFNode;
|
---|
| 18 | import com.hp.hpl.jena.rdf.model.Resource;
|
---|
| 19 | import com.hp.hpl.jena.rdf.model.impl.PropertyImpl;
|
---|
| 20 |
|
---|
[17] | 21 | import hozo.sparql.AllegroAccessor;
|
---|
[9] | 22 |
|
---|
| 23 | /**
|
---|
| 24 | * SparqlEndpoint縺ィ縺励※蜈ャ髢九&繧後※縺�kAllegroGraph縺ョRepository縺ォ蟇セ縺励※邱ィ髮�r陦後≧縺溘a縺ョ繧ッ繝ゥ繧ケ
|
---|
| 25 | * @author kato
|
---|
| 26 | *
|
---|
| 27 | */
|
---|
| 28 | public class AllegroEditor {
|
---|
| 29 |
|
---|
| 30 | private AllegroAccessor accessor;
|
---|
| 31 |
|
---|
| 32 | private String user;
|
---|
| 33 |
|
---|
| 34 | private String pass;
|
---|
| 35 |
|
---|
| 36 | private String url;
|
---|
| 37 |
|
---|
| 38 | private String repository;
|
---|
| 39 |
|
---|
| 40 |
|
---|
| 41 | public AllegroEditor(AllegroAccessor sa, String user, String pass) throws Exception{
|
---|
| 42 | this.accessor = sa;
|
---|
| 43 |
|
---|
| 44 | this.user = user;
|
---|
| 45 | this.pass = pass;
|
---|
| 46 |
|
---|
| 47 |
|
---|
| 48 | init();
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | public AllegroEditor(AllegroAccessor sa, String url, String repo, String user, String pass) throws Exception{
|
---|
| 52 | this.accessor = sa;
|
---|
| 53 |
|
---|
| 54 | this.url = url;
|
---|
| 55 | this.repository = repo;
|
---|
| 56 |
|
---|
| 57 | this.user = user;
|
---|
| 58 | this.pass = pass;
|
---|
| 59 |
|
---|
| 60 | try {
|
---|
| 61 | getModel();
|
---|
| 62 | } catch(Exception e){
|
---|
| 63 | throw new Exception("AllegroGraph縺ョrepository險ュ螳壹′荳肴ュ」縺ァ縺�, e);
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 |
|
---|
| 68 | private void init() throws Exception {
|
---|
| 69 | String endpoint = this.accessor.getSetting().getEndpoint();
|
---|
| 70 |
|
---|
| 71 | int index = endpoint.indexOf("/endpoint/");
|
---|
| 72 | if (index >= 0){
|
---|
| 73 | this.url = endpoint.substring(0, index);
|
---|
| 74 | this.repository = endpoint.substring(index + "/endpoint/".length());
|
---|
| 75 | System.out.println("url:"+url);
|
---|
| 76 | System.out.println("repo:"+repository);
|
---|
| 77 | } else {
|
---|
| 78 | throw new Exception("AllegroGraph繧オ繝シ繝舌〒縺ッ縺ゅj縺セ縺帙s");
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | public AllegroAccessor getAccessor(){
|
---|
| 83 | return this.accessor;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | /**
|
---|
| 87 | * 遐エ譽�凾縺ォclose縺吶k
|
---|
| 88 | */
|
---|
| 89 | public void close(){
|
---|
| 90 | closeAll();
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | public boolean contains(RDFNode s, RDFNode p, RDFNode o) throws Exception{
|
---|
| 94 | Model model = getModel();
|
---|
| 95 | return contains(s, p, o, model);
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 |
|
---|
| 99 | private boolean isTriple(RDFNode s, RDFNode p, RDFNode o){
|
---|
| 100 | if (!s.isResource()){
|
---|
| 101 | return false;
|
---|
| 102 | }
|
---|
| 103 | if (!p.isResource()){
|
---|
| 104 | return false;
|
---|
| 105 | }
|
---|
| 106 | return true;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | private boolean contains(RDFNode s, RDFNode p, RDFNode o, Model model){
|
---|
| 110 | if (!isTriple(s, p, o)){
|
---|
| 111 | return false;
|
---|
| 112 | }
|
---|
| 113 | Resource ss = s.asResource();
|
---|
| 114 | Property pp = new PropertyImpl(p.asResource().getURI());
|
---|
| 115 |
|
---|
| 116 | return model.contains(ss, pp, o);
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 |
|
---|
| 120 |
|
---|
| 121 | public boolean add(RDFNode s, RDFNode p, String o) throws Exception{
|
---|
| 122 | Model model = getModel();
|
---|
| 123 | RDFNode oo = model.createLiteral(o);
|
---|
| 124 | return add(s, p, oo, model);
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | public boolean add(RDFNode s, RDFNode p, RDFNode o) throws Exception{
|
---|
| 128 | Model model = getModel();
|
---|
| 129 | return add(s, p, o, model);
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | private boolean add(RDFNode s, RDFNode p, RDFNode o, Model model){
|
---|
| 133 | if (!isTriple(s, p, o)){
|
---|
| 134 | return false;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | Resource ss = s.asResource();
|
---|
| 138 | Property pp = new PropertyImpl(p.asResource().getURI());
|
---|
| 139 |
|
---|
| 140 | if (!model.contains(ss, pp, o)){
|
---|
| 141 | model.add(ss, pp, o);
|
---|
| 142 | return true;
|
---|
| 143 | }
|
---|
| 144 | return false;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | public boolean delete(RDFNode s, RDFNode p, RDFNode o) throws Exception{
|
---|
| 148 | Model model = getModel();
|
---|
| 149 | return delete(s, p, o, model);
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | private boolean delete(RDFNode s, RDFNode p, RDFNode o, Model model){
|
---|
| 153 | if (!isTriple(s, p, o)){
|
---|
| 154 | return false;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | Resource ss = s.asResource();
|
---|
| 158 | Property pp = new PropertyImpl(p.asResource().getURI());
|
---|
| 159 |
|
---|
| 160 | if (model.contains(ss, pp, o)){
|
---|
| 161 | model.remove(ss, pp, o);
|
---|
| 162 | return true;
|
---|
| 163 | }
|
---|
| 164 | return false;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 |
|
---|
| 168 | public boolean edit(RDFNode s, RDFNode p, RDFNode o, RDFNode s2, RDFNode p2, RDFNode o2) throws Exception{
|
---|
| 169 | Model model = getModel();
|
---|
| 170 | return edit(s, p, o, s2, p2, o2, model);
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | private boolean edit(RDFNode s, RDFNode p, RDFNode o, RDFNode s2, RDFNode p2, RDFNode o2, Model model){
|
---|
| 174 | if (!isTriple(s, p, o) || !isTriple(s2, p2, o2)){
|
---|
| 175 | return false;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | Resource ss = s.asResource();
|
---|
| 179 | Property pp = new PropertyImpl(p.asResource().getURI());
|
---|
| 180 | Resource ss2 = s2.asResource();
|
---|
| 181 | Property pp2 = new PropertyImpl(p2.asResource().getURI());
|
---|
| 182 | if (model.contains(ss, pp, o)){
|
---|
| 183 | model.remove(ss, pp, o);
|
---|
| 184 | model.add(ss2, pp2, o2);
|
---|
| 185 | return true;
|
---|
| 186 | }
|
---|
| 187 | return false;
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 |
|
---|
| 191 |
|
---|
| 192 | public Model getModel() throws Exception {
|
---|
| 193 | AGServer server = new AGServer(this.url, this.user, this.pass);
|
---|
| 194 | AGCatalog catalog = server.getCatalog("/");
|
---|
| 195 | AGRepository myRepository = catalog.openRepository(this.repository);
|
---|
| 196 |
|
---|
| 197 | AGRepositoryConnection conn = myRepository.getConnection();
|
---|
| 198 | closeBeforeExit(conn);
|
---|
| 199 | AGGraphMaker maker = new AGGraphMaker(conn);
|
---|
| 200 |
|
---|
| 201 | AGGraph graph = maker.getGraph();
|
---|
| 202 | AGModel model = new AGModel(graph);
|
---|
| 203 |
|
---|
| 204 | closeBeforeExit(myRepository, model);
|
---|
| 205 |
|
---|
| 206 | return model;
|
---|
| 207 |
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | private void closeBeforeExit(AGRepository rep, AGModel model) {
|
---|
| 211 | toCloseRep.add(rep);
|
---|
| 212 | toCloseModel.add(model);
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 |
|
---|
| 216 | protected void closeAll() {
|
---|
| 217 | while (toCloseRep.isEmpty() == false) {
|
---|
| 218 | AGRepository conn = toCloseRep.get(0);
|
---|
| 219 | close(conn);
|
---|
| 220 | while (toCloseRep.remove(conn)) {
|
---|
| 221 | }
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | while (toCloseModel.isEmpty() == false) {
|
---|
| 225 | AGModel conn = toCloseModel.get(0);
|
---|
| 226 | close(conn);
|
---|
| 227 | while (toCloseModel.remove(conn)) {
|
---|
| 228 | }
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | while (toClose.isEmpty() == false) {
|
---|
| 232 | RepositoryConnection conn = toClose.get(0);
|
---|
| 233 | close(conn);
|
---|
| 234 | while (toClose.remove(conn)) {
|
---|
| 235 | }
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 |
|
---|
| 241 | static void close(RepositoryConnection conn) {
|
---|
| 242 | try {
|
---|
| 243 | conn.close();
|
---|
| 244 | } catch (Exception e) {
|
---|
| 245 | System.err.println("Error closing repository connection: " + e);
|
---|
| 246 | e.printStackTrace();
|
---|
| 247 | }
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | void close(AGRepository conn) {
|
---|
| 251 | try {
|
---|
| 252 | conn.shutDown();
|
---|
| 253 | } catch (Exception e) {
|
---|
| 254 | System.err.println("Error closing repository repository: " + e);
|
---|
| 255 | e.printStackTrace();
|
---|
| 256 | }
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | void close(AGModel conn) {
|
---|
| 260 | try {
|
---|
| 261 | conn.close();
|
---|
| 262 | } catch (Exception e) {
|
---|
| 263 | System.err.println("Error closing repository model: " + e);
|
---|
| 264 | e.printStackTrace();
|
---|
| 265 | }
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 |
|
---|
| 269 | private static List<RepositoryConnection> toClose = new ArrayList<RepositoryConnection>();
|
---|
| 270 | private static List<AGRepository> toCloseRep = new ArrayList<AGRepository>();
|
---|
| 271 | private static List<AGModel> toCloseModel = new ArrayList<AGModel>();
|
---|
| 272 |
|
---|
| 273 | private static void closeBeforeExit(RepositoryConnection conn) {
|
---|
| 274 | toClose.add(conn);
|
---|
| 275 | }
|
---|
| 276 |
|
---|
| 277 | }
|
---|