001/**
002 * Copyright (C) 2007 - 2016, Jens Lehmann
003 *
004 * This file is part of DL-Learner.
005 *
006 * DL-Learner is free software; you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation; either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * DL-Learner is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018 */
019package org.dllearner.algorithms.qtl.operations.lgg;
020
021import com.google.common.collect.Sets;
022import org.apache.jena.datatypes.RDFDatatype;
023import org.apache.jena.graph.Node;
024import org.apache.jena.graph.NodeFactory;
025import org.dllearner.algorithms.qtl.QueryTreeUtils;
026import org.dllearner.algorithms.qtl.datastructures.NodeInv;
027import org.dllearner.algorithms.qtl.datastructures.impl.RDFResourceTree;
028import org.dllearner.algorithms.qtl.filters.QueryTreeFilter;
029import org.dllearner.algorithms.qtl.util.filters.TreeFilter;
030
031import java.util.*;
032import java.util.stream.Collectors;
033
034/**
035 * An LGG generator based on syntax and structure only, i.e. without taking into account any type of
036 * Semantics.
037 *
038 * @author Lorenz Bühmann
039 *
040 */
041public class LGGGeneratorExt extends LGGGeneratorSimple {
042
043        private boolean complete = true;
044
045        Set<TreeFilter<RDFResourceTree>> treeFilters = new HashSet<>();
046
047        public void setTreeFilters(Set<TreeFilter<RDFResourceTree>> treeFilters) {
048                this.treeFilters = treeFilters;
049        }
050
051        @Override
052        protected RDFResourceTree postProcess(RDFResourceTree tree) {
053                RDFResourceTree newTree = super.postProcess(tree);
054                // apply filters
055                for (TreeFilter<RDFResourceTree> filter : treeFilters) {
056                        newTree = filter.apply(newTree);
057                }
058                return newTree;
059        }
060
061        public boolean isComplete() {
062                return complete;
063        }
064
065        public static void main(String[] args) throws Exception {
066                // knowledge base
067//              SparqlEndpoint endpoint = SparqlEndpoint.getEndpointDBpedia();
068//              QueryExecutionFactory qef = FluentQueryExecutionFactory
069//                              .http(endpoint.getURL().toString(), endpoint.getDefaultGraphURIs()).config()
070//                              .withCache(CacheUtilsH2.createCacheFrontend("/tmp/cache", false, TimeUnit.DAYS.toMillis(60)))
071//                              .withPagination(10000).withDelay(50, TimeUnit.MILLISECONDS).end().create();
072//
073//              // tree generation
074//              ConciseBoundedDescriptionGenerator cbdGenerator = new ConciseBoundedDescriptionGeneratorImpl(qef);
075//              int maxDepth = 2;
076//              cbdGenerator.setRecursionDepth(maxDepth);
077//
078//              QueryTreeFactory treeFactory = new QueryTreeFactoryBase();
079//              treeFactory.setMaxDepth(maxDepth);
080//              treeFactory.addDropFilters(
081//                              new PredicateDropStatementFilter(StopURIsDBpedia.get()),
082//                              new PredicateDropStatementFilter(StopURIsRDFS.get()),
083//                              new PredicateDropStatementFilter(StopURIsOWL.get()),
084//                              new NamespaceDropStatementFilter(
085//                                              Sets.newHashSet(
086//                                                              "http://dbpedia.org/property/",
087//                                                              "http://purl.org/dc/terms/",
088//                                                              "http://dbpedia.org/class/yago/",
089//                                                              "http://www.w3.org/2003/01/geo/wgs84_pos#",
090//                                                              "http://www.georss.org/georss/",
091//                                                              FOAF.getURI()
092//                                                              )
093//                                                              )
094//                              );
095//              List<RDFResourceTree> trees = new ArrayList<>();
096//              List<String> resources = Lists.newArrayList("http://dbpedia.org/resource/Leipzig", "http://dbpedia.org/resource/Dresden");
097//              for(String resource : resources){
098//                      try {
099//                              System.out.println(resource);
100//                              Model model = cbdGenerator.getConciseBoundedDescription(resource);
101//                              RDFResourceTree tree = treeFactory.getQueryTree(ResourceFactory.createResource(resource), model);
102//                              System.out.println(tree.getStringRepresentation());
103//                              trees.add(tree);
104//                      } catch (Exception e) {
105//                              e.printStackTrace();
106//                      }
107//              }
108                
109                // LGG computation
110                LGGGenerator lggGen = new LGGGeneratorExt();
111//              RDFResourceTree lgg = lggGen.getLGG(trees);
112//
113//              System.out.println("LGG");
114//              System.out.println(lgg.getStringRepresentation());
115//              System.out.println(QueryTreeUtils.toSPARQLQueryString(lgg));
116//              System.out.println(QueryTreeUtils.toOWLClassExpression(lgg));
117
118                Node edge = NodeFactory.createURI("p");
119                Node edgeInv = new NodeInv(NodeFactory.createURI("p"));
120
121                RDFResourceTree tree1 = new RDFResourceTree(NodeFactory.createURI("urn:a"));
122                tree1.addChild(new RDFResourceTree(NodeFactory.createURI("urn:c")), edge);
123                tree1.addChild(new RDFResourceTree(NodeFactory.createURI("urn:d")), edgeInv);
124                System.out.println(tree1.getStringRepresentation());
125
126                RDFResourceTree tree2 = new RDFResourceTree(NodeFactory.createURI("urn:b"));
127                tree2.addChild(new RDFResourceTree(NodeFactory.createURI("urn:c")), edge);
128                tree2.addChild(new RDFResourceTree(NodeFactory.createURI("urn:d")), edgeInv);
129                System.out.println(tree2.getStringRepresentation());
130
131                RDFResourceTree lgg = lggGen.getLGG(tree1, tree2);
132                System.out.println("LGG");
133                System.out.println(lgg.getStringRepresentation());
134                System.out.println(QueryTreeUtils.toSPARQLQueryString(lgg));
135        }
136
137}