├── .github └── workflows │ └── maven.yml ├── .gitignore ├── README.md ├── example └── tc.dtlg ├── license.txt ├── make_release.sh ├── pom.xml ├── src ├── main │ └── java │ │ └── edu │ │ └── harvard │ │ └── seas │ │ └── pl │ │ └── abcdatalog │ │ ├── ast │ │ ├── BinaryDisunifier.java │ │ ├── BinaryUnifier.java │ │ ├── Clause.java │ │ ├── Constant.java │ │ ├── Head.java │ │ ├── HeadHelpers.java │ │ ├── NegatedAtom.java │ │ ├── PositiveAtom.java │ │ ├── PredicateSym.java │ │ ├── Premise.java │ │ ├── Term.java │ │ ├── TermHelpers.java │ │ ├── Variable.java │ │ ├── package-info.java │ │ ├── validation │ │ │ ├── DatalogValidationException.java │ │ │ ├── DatalogValidator.java │ │ │ ├── StratifiedNegationGraph.java │ │ │ ├── StratifiedNegationValidator.java │ │ │ ├── StratifiedProgram.java │ │ │ ├── UnstratifiedProgram.java │ │ │ └── package-info.java │ │ └── visitors │ │ │ ├── CrashHeadVisitor.java │ │ │ ├── CrashPremiseVisitor.java │ │ │ ├── DefaultConjunctVisitor.java │ │ │ ├── DefaultTermVisitor.java │ │ │ ├── HeadVisitor.java │ │ │ ├── HeadVisitorBuilder.java │ │ │ ├── PremiseVisitor.java │ │ │ ├── PremiseVisitorBuilder.java │ │ │ ├── TermVisitor.java │ │ │ ├── TermVisitorBuilder.java │ │ │ └── package-info.java │ │ ├── engine │ │ ├── ConjunctiveQueryHelper.java │ │ ├── DatalogEngine.java │ │ ├── DatalogEngineWithProvenance.java │ │ ├── EngineExample.java │ │ ├── bottomup │ │ │ ├── AnnotatedAtom.java │ │ │ ├── BottomUpEngineFrame.java │ │ │ ├── BottomUpEngineFrameWithProvenance.java │ │ │ ├── ClauseEvaluator.java │ │ │ ├── EvalManager.java │ │ │ ├── EvalManagerWithProvenance.java │ │ │ ├── SemiNaiveClauseAnnotator.java │ │ │ ├── concurrent │ │ │ │ ├── BottomUpEvalManager.java │ │ │ │ ├── ConcurrentBottomUpEngine.java │ │ │ │ ├── ConcurrentChunkedBottomUpEngine.java │ │ │ │ ├── ConcurrentStratifiedNegationBottomUpEngine.java │ │ │ │ ├── ExtensibleBottomUpEvalManager.java │ │ │ │ ├── StratifiedNegationEvalManager.java │ │ │ │ └── package-info.java │ │ │ ├── package-info.java │ │ │ └── sequential │ │ │ │ ├── SemiNaiveEngine.java │ │ │ │ ├── SemiNaiveEvalManager.java │ │ │ │ └── package-info.java │ │ ├── package-info.java │ │ └── topdown │ │ │ ├── AbstractQsqEngine.java │ │ │ ├── AdornedAtom.java │ │ │ ├── AdornedClause.java │ │ │ ├── AdornedPredicateSym.java │ │ │ ├── IterativeQsqEngine.java │ │ │ ├── MstEngine.java │ │ │ ├── QsqSupRelation.java │ │ │ ├── QsqTemplate.java │ │ │ ├── RecursiveQsqEngine.java │ │ │ ├── Relation.java │ │ │ ├── TermSchema.java │ │ │ ├── Tuple.java │ │ │ └── package-info.java │ │ ├── executor │ │ ├── DatalogExecutor.java │ │ ├── DatalogListener.java │ │ ├── DatalogParallelExecutor.java │ │ ├── ExecutorExample.java │ │ └── package-info.java │ │ ├── gui │ │ ├── DatalogGui.java │ │ ├── TextEditor.java │ │ └── package-info.java │ │ ├── package-info.java │ │ ├── parser │ │ ├── DatalogParseException.java │ │ ├── DatalogParser.java │ │ ├── DatalogTokenizer.java │ │ └── package-info.java │ │ └── util │ │ ├── Box.java │ │ ├── ExecutorServiceCounter.java │ │ ├── Utilities.java │ │ ├── datastructures │ │ ├── ConcurrentFactIndexer.java │ │ ├── ConcurrentFactTrie.java │ │ ├── ConcurrentLinkedBag.java │ │ ├── FactIndexer.java │ │ ├── FactIndexerFactory.java │ │ ├── IndexableFactCollection.java │ │ └── package-info.java │ │ ├── graph │ │ ├── Digraph.java │ │ ├── DirectedEdge.java │ │ └── package-info.java │ │ ├── package-info.java │ │ └── substitution │ │ ├── ClauseSubstitution.java │ │ ├── ConstOnlySubstitution.java │ │ ├── SimpleConstSubstitution.java │ │ ├── Substitution.java │ │ ├── SubstitutionUtils.java │ │ ├── TermUnifier.java │ │ ├── UnionFindBasedUnifier.java │ │ └── package-info.java └── test │ └── java │ └── edu │ └── harvard │ └── seas │ └── pl │ └── abcdatalog │ ├── engine │ ├── AbstractTests.java │ ├── ConcurrentBottomUpEngineTest.java │ ├── ConcurrentChunkedBottomUpEngineTest.java │ ├── ConcurrentStratifiedNegationBottomUpEngineTest.java │ ├── ConjunctiveQueryTests.java │ ├── CoreTests.java │ ├── ExplicitUnificationTests.java │ ├── IterativeQsqEngineTest.java │ ├── MstEngineTest.java │ ├── RecursiveQsqEngineTest.java │ ├── SemiNaiveEngineTest.java │ └── StratifiedNegationTests.java │ ├── executor │ └── DatalogParallelExecutorTest.java │ └── util │ └── datastructures │ └── FactIndexerTest.java └── third-party-licenses ├── THIRD-PARTY.txt ├── eclipse public license 1.0 - epl-v10.html ├── flatlaf license - apache license 2.0.html └── new bsd license - bsd-license.php.html /.github/workflows/maven.yml: -------------------------------------------------------------------------------- 1 | # This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time 2 | # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven 3 | 4 | # This workflow uses actions that are not certified by GitHub. 5 | # They are provided by a third-party and are governed by 6 | # separate terms of service, privacy policy, and support 7 | # documentation. 8 | 9 | name: Java CI with Maven 10 | 11 | on: 12 | push: 13 | branches: [ "master" ] 14 | pull_request: 15 | branches: [ "master" ] 16 | 17 | jobs: 18 | build: 19 | 20 | runs-on: ubuntu-latest 21 | 22 | steps: 23 | - uses: actions/checkout@v3 24 | - name: Set up JDK 21 25 | uses: actions/setup-java@v3 26 | with: 27 | java-version: '21' 28 | distribution: 'temurin' 29 | cache: maven 30 | - name: Build with Maven 31 | run: mvn -B package --file pom.xml 32 | 33 | # Optional: Uploads the full dependency graph to GitHub to improve the quality of Dependabot alerts this repository can receive 34 | # - name: Update dependency graph 35 | # uses: advanced-security/maven-dependency-submission-action@571e99aab1055c2e71a1e2309b9691de18d6b7d6 36 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | 3 | # Ignore Eclipse metadata. 4 | .classpath 5 | .project 6 | .settings/ 7 | .idea/ 8 | 9 | .vscode/ 10 | .DS_Store 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Introduction 2 | 3 | AbcDatalog is an open-source Java implementation of Datalog, a logic 4 | programming language. It provides ready-to-use implementations of common 5 | Datalog evaluation algorithms, and is designed to be easily extensible with new 6 | evaluation engines and new language features. We hope that it proves to be 7 | useful for both research and pedagogy. 8 | 9 | For more information, please see the 10 | [AbcDatalog website](https://abcdatalog.seas.harvard.edu/). 11 | 12 | # Licensing 13 | 14 | AbcDatalog is released under a BSD License, a copy of which is included in this 15 | directory. 16 | 17 | AbcDatalog uses third party libraries; a list of them and their associated 18 | licenses can be found in the [`third-party-licenses/`](third-party-licenses/) 19 | subdirectory. 20 | 21 | # Requirements 22 | 23 | * Java 21+ 24 | * Maven (v3.9.9 is known to work); not necessary for running the pre-built JAR 25 | 26 | # Setup 27 | 28 | ## Pre-Built JAR 29 | 30 | A pre-built JAR can be found on the 31 | [Releases](https://github.com/HarvardPL/AbcDatalog/releases) section of the GitHub repository. 32 | 33 | ## Maven Central 34 | 35 | AbcDatalog is released on [Maven Central](https://central.sonatype.com/artifact/io.github.harvardpl/AbcDatalog) 36 | and can be easily added as a library to another Maven project by including a 37 | snippet like this in that project's `pom.xml` file: 38 | 39 | ``` 40 | 41 | io.github.harvardpl 42 | AbcDatalog 43 | [X.Y.Z] 44 | 45 | ``` 46 | 47 | Replace `[X.Y.Z]` with the most recent AbcDatalog version. 48 | 49 | ## Compilation 50 | 51 | If you desire, you can compile the source code into a JAR using Maven. From 52 | the project root directory, run `mvn package` to build the archive 53 | `target/AbcDatalog-[X.Y.Z]-jar-with-dependencies.jar` (where `[X.Y.Z]` is the 54 | version number). 55 | 56 | # Usage 57 | 58 | Please see the [AbcDatalog website](https://abcdatalog.seas.harvard.edu/) for 59 | information on how to use the AbcDatalog graphical user interface and how to 60 | interface with AbcDatalog from Java programs. 61 | 62 | # Contributing 63 | 64 | Contributions are encouraged! 65 | In the past, students have contributed some great improvements to the UI. 66 | If you are interested in contributing, open a [GitHub issue](https://github.com/HarvardPL/AbcDatalog/issues) discussing the improvements you would like to make. 67 | 68 | # People 69 | 70 | The primary contributors to AbcDatalog are: 71 | 72 | * Aaron Bembenek 73 | * Stephen Chong 74 | * Marco Gaboardi 75 | 76 | Question, comment, bug report? Please raise a [GitHub issue](https://github.com/HarvardPL/AbcDatalog/issues). 77 | 78 | Thanks to João Gonçalves for helping transition AbcDatalog to GitHub! 79 | 80 | # Acknowledgements 81 | 82 | AbcDatalog has been developed as part of the Privacy Tools for Sharing Research 83 | Data project at Harvard University and is supported by the National Science 84 | Foundation under Grant Nos. 1237235 and 1054172. 85 | -------------------------------------------------------------------------------- /example/tc.dtlg: -------------------------------------------------------------------------------- 1 | % A program that computes graph transitive closure. 2 | 3 | % The `edge` predicate is an EDB. That is, it represents facts known before 4 | % evaluation. Predicates and constants begin with lowercase letters. 5 | edge(a,b). 6 | edge(b,c). 7 | edge(c,c). 8 | edge(c,d). 9 | 10 | % The `tc` predicate is an IDB. That is, it represents facts that are generated 11 | % during evaluation. Variables begin with uppercase letters. 12 | tc(X,Y) :- edge(X,Y). 13 | tc(X,Y) :- edge(X,Z), tc(Z,Y). 14 | 15 | % After we have loaded the program, we can query the existence of certain facts. 16 | % For instance, to see the entire transitive closure relationship we'd query 17 | % `tc(X,Y)?`, and to see every node reachable from `a` we'd query `tc(a,X)?`. 18 | 19 | % We could alternatively compute transitive closure this way, using non-linear 20 | % recursion. 21 | tc_nonlinear(X,Y) :- edge(X,Y). 22 | tc_nonlinear(X,Y) :- tc_nonlinear(X,Z), tc_nonlinear(Z,Y). 23 | 24 | % We have a few different options if we want to compute every node that is part 25 | % of a cycle. The following two programs will generate the same relation: 26 | in_cycle1(X) :- tc(X,Y), X=Y. 27 | in_cycle2(X) :- tc(X,X). 28 | % The operator `=` explicitly unifies two terms. 29 | 30 | % Similarly, the operator `!=` explicitly disunifies two terms. For example, 31 | % the following rule computes a relation consisting of members of `tc` where 32 | % the first argument of the tuple is different than the second one. 33 | distinct_tc(X,Y) :- tc(X,Y), X!=Y. 34 | 35 | % We can use negation to compute the pairs of nodes that are not in the `tc` 36 | % relationship. 37 | node(X) :- edge(X,_). 38 | node(X) :- edge(_,X). 39 | not_tc(X,Y) :- node(X), node(Y), not tc(X,Y). 40 | % We needed to create the `node` relation so that we would have a way to bind 41 | % the variables `X` and `Y` in the `not_tc` rule. The following statement of 42 | % the rule is invalid because `X` and `Y` are not bound: 43 | % invalid_not_tc(X,Y) :- not tc(X,Y). 44 | % A variable is bound if it appears in a positive (i.e., non-negated) body atom, 45 | % or is explicitly unified with a constant or a variable that is bound. 46 | -------------------------------------------------------------------------------- /license.txt: -------------------------------------------------------------------------------- 1 | BSD License 2 | 3 | Copyright (c) 2016-2021, President and Fellows of Harvard College 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are 8 | met: 9 | 10 | Redistributions of source code must retain the above copyright 11 | notice, this list of conditions and the following disclaimer. 12 | 13 | Redistributions in binary form must reproduce the above copyright 14 | notice, this list of conditions and the following disclaimer in 15 | the documentation and/or other materials provided with the 16 | distribution. 17 | 18 | Neither the name of the copyright holder nor the names of its 19 | contributors may be used to endorse or promote products derived 20 | from this software without specific prior written permission. 21 | 22 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 25 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 | HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 28 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 32 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 | -------------------------------------------------------------------------------- /make_release.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # This script is used to partially automate the release process. You still need 4 | # to update the version number in `pom.xml`. 5 | 6 | set -e 7 | 8 | script_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd) 9 | cd "$script_dir" 10 | 11 | git checkout master 12 | mvn clean 13 | # Make sure all files have licenses 14 | mvn license:update-file-header 15 | # Make sure code is formatted consistently 16 | mvn com.spotify.fmt:fmt-maven-plugin:format 17 | # Make sure code builds 18 | mvn package 19 | # Generate Javadocs 20 | mvn javadoc:javadoc -Prelease 21 | if [ -n "$(git status --porcelain)" ]; then 22 | echo "Directory not clean" 23 | exit 1 24 | fi 25 | 26 | # Update the Javadocs on the website 27 | git checkout gh-pages 28 | rm -rf apidocs 29 | cp -r target/site/apidocs . 30 | git add apidocs 31 | git commit -m "Update Javadocs." 32 | git push 33 | 34 | git checkout master 35 | 36 | # To upload the new release to Maven Central, run: 37 | # 38 | # mvn clean deploy -Prelease 39 | # 40 | # (This assumes you have a settings.xml file with the GPG passphrase and a 41 | # Sonatype token.) Once the package is validated, you can publish it using the 42 | # interface on . 43 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/ast/BinaryDisunifier.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.ast; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.ast.visitors.PremiseVisitor; 37 | import edu.harvard.seas.pl.abcdatalog.util.substitution.Substitution; 38 | import java.util.Arrays; 39 | 40 | /** 41 | * This premise explicitly disallows the unification of two terms and is represented by the operator 42 | * {@code !=}. For example, if {@code X!=a}, then the variable {@code X} cannot be unified with the 43 | * constant {@code a}. 44 | */ 45 | public class BinaryDisunifier implements Premise { 46 | private final Term left, right; 47 | 48 | public BinaryDisunifier(Term left, Term right) { 49 | this.left = left; 50 | this.right = right; 51 | } 52 | 53 | public Term getLeft() { 54 | return this.left; 55 | } 56 | 57 | public Term getRight() { 58 | return this.right; 59 | } 60 | 61 | public Iterable getArgsIterable() { 62 | return Arrays.asList(this.left, this.right); 63 | } 64 | 65 | @Override 66 | public O accept(PremiseVisitor visitor, I state) { 67 | return visitor.visit(this, state); 68 | } 69 | 70 | @Override 71 | public String toString() { 72 | return this.left + " != " + this.right; 73 | } 74 | 75 | @Override 76 | public Premise applySubst(Substitution subst) { 77 | return new BinaryDisunifier(left.applySubst(subst), right.applySubst(subst)); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/ast/BinaryUnifier.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.ast; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.ast.visitors.PremiseVisitor; 37 | import edu.harvard.seas.pl.abcdatalog.util.substitution.Substitution; 38 | import java.util.Arrays; 39 | 40 | /** 41 | * This premise explicitly unifies two terms and is visually represented as the operator {@code =}. 42 | * For example, if {@code X=a}, then the variable {@code X} is bound to the constant {@code a}. 43 | */ 44 | public class BinaryUnifier implements Premise { 45 | private final Term left, right; 46 | 47 | public BinaryUnifier(Term left, Term right) { 48 | this.left = left; 49 | this.right = right; 50 | } 51 | 52 | public Term getLeft() { 53 | return this.left; 54 | } 55 | 56 | public Term getRight() { 57 | return this.right; 58 | } 59 | 60 | public Iterable getArgsIterable() { 61 | return Arrays.asList(this.left, this.right); 62 | } 63 | 64 | @Override 65 | public O accept(PremiseVisitor visitor, I state) { 66 | return visitor.visit(this, state); 67 | } 68 | 69 | @Override 70 | public String toString() { 71 | return this.left + " = " + this.right; 72 | } 73 | 74 | @Override 75 | public Premise applySubst(Substitution subst) { 76 | return new BinaryUnifier(left.applySubst(subst), right.applySubst(subst)); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/ast/Clause.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.ast; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import java.util.List; 37 | 38 | /** 39 | * A clause consisting of a head and a body, the latter of which is a list of premises. The standard 40 | * interpretation is that the head of a clause is considered to hold if each premise in the body 41 | * holds. 42 | */ 43 | public class Clause { 44 | 45 | private final Head head; 46 | protected final List body; 47 | 48 | public Clause(Head head, List body) { 49 | this.head = head; 50 | this.body = body; 51 | } 52 | 53 | /** 54 | * Returns the head of this clause. 55 | * 56 | * @return the head 57 | */ 58 | public Head getHead() { 59 | return this.head; 60 | } 61 | 62 | /** 63 | * Returns the body of this clause. 64 | * 65 | * @return the body 66 | */ 67 | public List getBody() { 68 | return this.body; 69 | } 70 | 71 | @Override 72 | public int hashCode() { 73 | final int prime = 31; 74 | int result = 1; 75 | result = prime * result + ((body == null) ? 0 : body.hashCode()); 76 | result = prime * result + ((head == null) ? 0 : head.hashCode()); 77 | return result; 78 | } 79 | 80 | @Override 81 | public boolean equals(Object obj) { 82 | if (this == obj) return true; 83 | if (obj == null) return false; 84 | if (getClass() != obj.getClass()) return false; 85 | Clause other = (Clause) obj; 86 | if (body == null) { 87 | if (other.body != null) return false; 88 | } else if (!body.equals(other.body)) return false; 89 | if (head == null) { 90 | if (other.head != null) return false; 91 | } else if (!head.equals(other.head)) return false; 92 | return true; 93 | } 94 | 95 | @Override 96 | public String toString() { 97 | StringBuilder sb = new StringBuilder(); 98 | sb.append(this.getHead()); 99 | if (!this.getBody().isEmpty()) { 100 | sb.append(" :- "); 101 | for (int i = 0; i < this.getBody().size(); ++i) { 102 | sb.append(this.getBody().get(i)); 103 | if (i < this.getBody().size() - 1) { 104 | sb.append(", "); 105 | } 106 | } 107 | } 108 | sb.append('.'); 109 | return sb.toString(); 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/ast/Constant.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.ast; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.ast.visitors.TermVisitor; 37 | import edu.harvard.seas.pl.abcdatalog.util.substitution.Substitution; 38 | import java.util.concurrent.ConcurrentHashMap; 39 | import java.util.concurrent.ConcurrentMap; 40 | 41 | /** A zero-ary function symbol (i.e., a constant in Datalog). */ 42 | public class Constant implements Term { 43 | /** Identifier of the constant. */ 44 | private final String name; 45 | 46 | /** A map for memoization. */ 47 | private static final ConcurrentMap memo = new ConcurrentHashMap<>(); 48 | 49 | /** 50 | * Returns a constant with the given string identifier. 51 | * 52 | * @param name the string identifier 53 | * @return the constant 54 | */ 55 | public static Constant create(String name) { 56 | Constant c = memo.get(name); 57 | if (c != null) { 58 | return c; 59 | } 60 | // try creating it 61 | c = new Constant(name); 62 | Constant existing = memo.putIfAbsent(name, c); 63 | if (existing != null) { 64 | return existing; 65 | } 66 | return c; 67 | } 68 | 69 | /** 70 | * Constructs a constant with the given name. 71 | * 72 | * @param name name 73 | */ 74 | private Constant(String name) { 75 | this.name = name; 76 | } 77 | 78 | public String getName() { 79 | return name; 80 | } 81 | 82 | @Override 83 | public String toString() { 84 | return this.getName(); 85 | } 86 | 87 | @Override 88 | public O accept(TermVisitor visitor, I state) { 89 | return visitor.visit(this, state); 90 | } 91 | 92 | @Override 93 | public Term applySubst(Substitution subst) { 94 | return this; 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/ast/Head.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.ast; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.ast.visitors.HeadVisitor; 37 | import edu.harvard.seas.pl.abcdatalog.util.substitution.Substitution; 38 | 39 | /** 40 | * The head of a clause. This interface is under-specified to allow the addition of new language 41 | * features. 42 | */ 43 | public interface Head { 44 | O accept(HeadVisitor visitor, I state); 45 | 46 | Head applySubst(Substitution subst); 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/ast/HeadHelpers.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.ast; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | /** A utility class for accessing the head of a clause. */ 37 | public final class HeadHelpers { 38 | 39 | private HeadHelpers() { 40 | // Cannot be instantiated. 41 | } 42 | 43 | public static PositiveAtom forcePositiveAtom(Head head) { 44 | return (PositiveAtom) head; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/ast/NegatedAtom.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.ast; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.ast.visitors.PremiseVisitor; 37 | import edu.harvard.seas.pl.abcdatalog.util.substitution.Substitution; 38 | 39 | /** 40 | * A negated atom. Typically a negated atom is considered to hold during evaluation if the atom it 41 | * negates is not provable. 42 | */ 43 | public class NegatedAtom implements Premise { 44 | private final PositiveAtom atom; 45 | 46 | public NegatedAtom(PredicateSym pred, Term[] args) { 47 | this.atom = PositiveAtom.create(pred, args); 48 | } 49 | 50 | public NegatedAtom(PositiveAtom atom) { 51 | this.atom = atom; 52 | } 53 | 54 | @Override 55 | public O accept(PremiseVisitor visitor, I state) { 56 | return visitor.visit(this, state); 57 | } 58 | 59 | public Term[] getArgs() { 60 | return this.atom.getArgs(); 61 | } 62 | 63 | public PredicateSym getPred() { 64 | return this.atom.getPred(); 65 | } 66 | 67 | public boolean isGround() { 68 | return this.atom.isGround(); 69 | } 70 | 71 | public PositiveAtom asPositiveAtom() { 72 | return this.atom; 73 | } 74 | 75 | @Override 76 | public int hashCode() { 77 | final int prime = 31; 78 | int result = 1; 79 | result = prime * result + ((atom == null) ? 0 : atom.hashCode()); 80 | return result; 81 | } 82 | 83 | @Override 84 | public boolean equals(Object obj) { 85 | if (this == obj) return true; 86 | if (obj == null) return false; 87 | if (getClass() != obj.getClass()) return false; 88 | NegatedAtom other = (NegatedAtom) obj; 89 | if (atom == null) { 90 | if (other.atom != null) return false; 91 | } else if (!atom.equals(other.atom)) return false; 92 | return true; 93 | } 94 | 95 | @Override 96 | public String toString() { 97 | return "not " + atom; 98 | } 99 | 100 | @Override 101 | public Premise applySubst(Substitution subst) { 102 | return new NegatedAtom(atom.applySubst(subst)); 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/ast/Premise.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.ast; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.ast.visitors.PremiseVisitor; 37 | import edu.harvard.seas.pl.abcdatalog.util.substitution.Substitution; 38 | 39 | /** 40 | * A premise in the body of a clause. This interface is under-specified to allow the addition of new 41 | * language features. 42 | */ 43 | public interface Premise { 44 | O accept(PremiseVisitor visitor, I state); 45 | 46 | Premise applySubst(Substitution subst); 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/ast/Term.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.ast; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.ast.visitors.TermVisitor; 37 | import edu.harvard.seas.pl.abcdatalog.util.substitution.Substitution; 38 | 39 | /** A Datalog term (i.e., a constant or variable). */ 40 | public interface Term { 41 | public O accept(TermVisitor visitor, I state); 42 | 43 | public Term applySubst(Substitution subst); 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/ast/TermHelpers.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.ast; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.ast.visitors.TermVisitor; 37 | import edu.harvard.seas.pl.abcdatalog.util.substitution.ConstOnlySubstitution; 38 | import edu.harvard.seas.pl.abcdatalog.util.substitution.TermUnifier; 39 | 40 | /** A utility class for common operations on terms. */ 41 | public final class TermHelpers { 42 | private TermHelpers() {} 43 | 44 | public static T fold(Iterable terms, TermVisitor tv, T init) { 45 | T acc = init; 46 | for (Term t : terms) { 47 | acc = t.accept(tv, acc); 48 | } 49 | return acc; 50 | } 51 | 52 | public static T fold(Term[] terms, TermVisitor tv, T init) { 53 | T acc = init; 54 | for (Term t : terms) { 55 | acc = t.accept(tv, acc); 56 | } 57 | return acc; 58 | } 59 | 60 | public static boolean unify(Term u, Term v, ConstOnlySubstitution s) { 61 | if (u instanceof Variable) { 62 | Constant c = s.get((Variable) u); 63 | if (c != null) { 64 | u = c; 65 | } 66 | } 67 | 68 | if (v instanceof Variable) { 69 | Constant c = s.get((Variable) v); 70 | if (c != null) { 71 | v = c; 72 | } 73 | } 74 | 75 | boolean uVar = u instanceof Variable; 76 | boolean vVar = v instanceof Variable; 77 | 78 | if (uVar && vVar) { 79 | throw new IllegalArgumentException("Cannot unify two variables."); 80 | } else if (uVar) { 81 | assert v instanceof Constant; 82 | s.add((Variable) u, (Constant) v); 83 | } else if (vVar) { 84 | assert u instanceof Constant; 85 | s.add((Variable) v, (Constant) u); 86 | } else { 87 | assert u instanceof Constant && v instanceof Constant; 88 | return u.equals(v); 89 | } 90 | return true; 91 | } 92 | 93 | public static boolean unify(Term u, Term v, TermUnifier s) { 94 | if (u instanceof Variable) { 95 | Term t = s.get((Variable) u); 96 | if (t != null) { 97 | u = t; 98 | } 99 | } 100 | 101 | if (v instanceof Variable) { 102 | Term t = s.get((Variable) v); 103 | if (t != null) { 104 | v = t; 105 | } 106 | } 107 | 108 | boolean uVar = u instanceof Variable; 109 | boolean vVar = v instanceof Variable; 110 | 111 | if (uVar) { 112 | return s.unify((Variable) u, v); 113 | } else if (vVar) { 114 | assert u instanceof Constant; 115 | return s.unify((Variable) v, u); 116 | } else { 117 | assert u instanceof Constant && v instanceof Constant; 118 | return u.equals(v); 119 | } 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/ast/Variable.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.ast; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.ast.visitors.TermVisitor; 37 | import edu.harvard.seas.pl.abcdatalog.util.substitution.Substitution; 38 | import java.util.concurrent.ConcurrentHashMap; 39 | import java.util.concurrent.ConcurrentMap; 40 | 41 | /** A Datalog variable. */ 42 | public class Variable implements Term { 43 | /** Identifier for this variable. */ 44 | private final String name; 45 | 46 | /** Map for memoization. */ 47 | private static final ConcurrentMap memo = new ConcurrentHashMap<>(); 48 | 49 | /** 50 | * Returns a variable with the given string identifier. 51 | * 52 | * @param name the string identifier 53 | * @return the variable 54 | */ 55 | public static Variable create(String name) { 56 | Variable c = memo.get(name); 57 | if (c != null) { 58 | return c; 59 | } 60 | // try creating it 61 | c = new Variable(name); 62 | Variable existing = memo.putIfAbsent(name, c); 63 | if (existing != null) { 64 | return existing; 65 | } 66 | return c; 67 | } 68 | 69 | /** 70 | * Constructs a variable from an identifier. 71 | * 72 | * @param name identifier 73 | */ 74 | protected Variable(String name) { 75 | this.name = name; 76 | } 77 | 78 | public String getName() { 79 | return name; 80 | } 81 | 82 | @Override 83 | public String toString() { 84 | return this.getName(); 85 | } 86 | 87 | @Override 88 | public O accept(TermVisitor visitor, I state) { 89 | return visitor.visit(this, state); 90 | } 91 | 92 | public static Variable createFreshVariable() { 93 | return new Variable("_"); 94 | } 95 | 96 | @Override 97 | public Term applySubst(Substitution subst) { 98 | Term s = subst.get(this); 99 | return (s != null) ? s : this; 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/ast/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * This packages contains classes representing the core abstract syntax tree for AbcDatalog. Many of 3 | * these classes support the visitor design pattern; see {@link 4 | * edu.harvard.seas.pl.abcdatalog.ast.visitors}. 5 | */ 6 | package edu.harvard.seas.pl.abcdatalog.ast; 7 | 8 | /*- 9 | * #%L 10 | * AbcDatalog 11 | * %% 12 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 13 | * %% 14 | * Redistribution and use in source and binary forms, with or without modification, 15 | * are permitted provided that the following conditions are met: 16 | * 17 | * 1. Redistributions of source code must retain the above copyright notice, this 18 | * list of conditions and the following disclaimer. 19 | * 20 | * 2. Redistributions in binary form must reproduce the above copyright notice, 21 | * this list of conditions and the following disclaimer in the documentation 22 | * and/or other materials provided with the distribution. 23 | * 24 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 25 | * may be used to endorse or promote products derived from this software without 26 | * specific prior written permission. 27 | * 28 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 29 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 30 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 31 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 32 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 33 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 34 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 35 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 36 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 37 | * OF THE POSSIBILITY OF SUCH DAMAGE. 38 | * #L% 39 | */ 40 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/ast/validation/DatalogValidationException.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.ast.validation; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | public class DatalogValidationException extends Exception { 37 | 38 | public DatalogValidationException() {} 39 | 40 | public DatalogValidationException(String message) { 41 | super(message); 42 | } 43 | 44 | public DatalogValidationException(Throwable cause) { 45 | super(cause); 46 | } 47 | 48 | public DatalogValidationException(String message, Throwable cause) { 49 | super(message, cause); 50 | } 51 | 52 | public DatalogValidationException( 53 | String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { 54 | super(message, cause, enableSuppression, writableStackTrace); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/ast/validation/StratifiedNegationValidator.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.ast.validation; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.ast.PositiveAtom; 37 | import edu.harvard.seas.pl.abcdatalog.ast.PredicateSym; 38 | import edu.harvard.seas.pl.abcdatalog.ast.validation.DatalogValidator.ValidClause; 39 | import java.util.List; 40 | import java.util.Map; 41 | import java.util.Set; 42 | 43 | /** 44 | * A class for validating that an unstratified program can be successfully stratified for negation. 45 | */ 46 | public final class StratifiedNegationValidator { 47 | 48 | private StratifiedNegationValidator() { 49 | // Cannot be instantiated. 50 | } 51 | 52 | /** 53 | * Validates that the given unstratified program can be stratified for negation and returns a 54 | * witness stratified program. 55 | * 56 | * @param prog the unstratified program 57 | * @return the stratified program 58 | * @throws DatalogValidationException if the given program cannot be stratified for negation 59 | */ 60 | public static StratifiedProgram validate(UnstratifiedProgram prog) 61 | throws DatalogValidationException { 62 | StratifiedNegationGraph g = StratifiedNegationGraph.create(prog); 63 | return new StratifiedProgram() { 64 | 65 | @Override 66 | public Set getRules() { 67 | return prog.getRules(); 68 | } 69 | 70 | @Override 71 | public Set getInitialFacts() { 72 | return prog.getInitialFacts(); 73 | } 74 | 75 | @Override 76 | public Set getEdbPredicateSyms() { 77 | return prog.getEdbPredicateSyms(); 78 | } 79 | 80 | @Override 81 | public Set getIdbPredicateSyms() { 82 | return prog.getIdbPredicateSyms(); 83 | } 84 | 85 | @Override 86 | public List> getStrata() { 87 | return g.getStrata(); 88 | } 89 | 90 | @Override 91 | public Map getPredToStratumMap() { 92 | return g.getPredToStratumMap(); 93 | } 94 | }; 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/ast/validation/StratifiedProgram.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.ast.validation; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.ast.PredicateSym; 37 | import java.util.List; 38 | import java.util.Map; 39 | import java.util.Set; 40 | 41 | /** A Datalog program that has been stratified; for instance, to support stratified negation. */ 42 | public interface StratifiedProgram extends UnstratifiedProgram { 43 | List> getStrata(); 44 | 45 | Map getPredToStratumMap(); 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/ast/validation/UnstratifiedProgram.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.ast.validation; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.ast.PositiveAtom; 37 | import edu.harvard.seas.pl.abcdatalog.ast.PredicateSym; 38 | import edu.harvard.seas.pl.abcdatalog.ast.validation.DatalogValidator.ValidClause; 39 | import java.util.Set; 40 | 41 | /** 42 | * A Datalog program for which each rule and initial fact has been independently validated, but the 43 | * program as a whole has not been validated. That is, it guarantees that each clause of a program 44 | * is independently valid, but says nothing about whether the clauses taken together make sense. 45 | * This might be a concern for language features such as negation, where certain dependencies 46 | * between clauses are undesirable. 47 | */ 48 | public interface UnstratifiedProgram { 49 | Set getRules(); 50 | 51 | Set getInitialFacts(); 52 | 53 | Set getEdbPredicateSyms(); 54 | 55 | Set getIdbPredicateSyms(); 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/ast/validation/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * This package contains classes to check that a clause or a set of clauses (such as those parsed 3 | * from user input) are valid (i.e., can be successfully evaluated). 4 | */ 5 | package edu.harvard.seas.pl.abcdatalog.ast.validation; 6 | 7 | /*- 8 | * #%L 9 | * AbcDatalog 10 | * %% 11 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 12 | * %% 13 | * Redistribution and use in source and binary forms, with or without modification, 14 | * are permitted provided that the following conditions are met: 15 | * 16 | * 1. Redistributions of source code must retain the above copyright notice, this 17 | * list of conditions and the following disclaimer. 18 | * 19 | * 2. Redistributions in binary form must reproduce the above copyright notice, 20 | * this list of conditions and the following disclaimer in the documentation 21 | * and/or other materials provided with the distribution. 22 | * 23 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 24 | * may be used to endorse or promote products derived from this software without 25 | * specific prior written permission. 26 | * 27 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 28 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 29 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 30 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 31 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 32 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 33 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 34 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 35 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 36 | * OF THE POSSIBILITY OF SUCH DAMAGE. 37 | * #L% 38 | */ 39 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/ast/visitors/CrashHeadVisitor.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.ast.visitors; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.ast.PositiveAtom; 37 | 38 | public class CrashHeadVisitor implements HeadVisitor { 39 | 40 | @Override 41 | public O visit(PositiveAtom atom, I state) { 42 | throw new UnsupportedOperationException(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/ast/visitors/CrashPremiseVisitor.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.ast.visitors; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.ast.BinaryDisunifier; 37 | import edu.harvard.seas.pl.abcdatalog.ast.BinaryUnifier; 38 | import edu.harvard.seas.pl.abcdatalog.ast.NegatedAtom; 39 | import edu.harvard.seas.pl.abcdatalog.ast.PositiveAtom; 40 | import edu.harvard.seas.pl.abcdatalog.engine.bottomup.AnnotatedAtom; 41 | 42 | public class CrashPremiseVisitor implements PremiseVisitor { 43 | 44 | @Override 45 | public O visit(PositiveAtom atom, I state) { 46 | throw new UnsupportedOperationException(); 47 | } 48 | 49 | @Override 50 | public O visit(AnnotatedAtom atom, I state) { 51 | throw new UnsupportedOperationException(); 52 | } 53 | 54 | @Override 55 | public O visit(BinaryUnifier u, I state) { 56 | throw new UnsupportedOperationException(); 57 | } 58 | 59 | @Override 60 | public O visit(BinaryDisunifier u, I state) { 61 | throw new UnsupportedOperationException(); 62 | } 63 | 64 | @Override 65 | public O visit(NegatedAtom atom, I state) { 66 | throw new UnsupportedOperationException(); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/ast/visitors/DefaultConjunctVisitor.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.ast.visitors; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.ast.BinaryDisunifier; 37 | import edu.harvard.seas.pl.abcdatalog.ast.BinaryUnifier; 38 | import edu.harvard.seas.pl.abcdatalog.ast.NegatedAtom; 39 | import edu.harvard.seas.pl.abcdatalog.ast.PositiveAtom; 40 | import edu.harvard.seas.pl.abcdatalog.engine.bottomup.AnnotatedAtom; 41 | 42 | public class DefaultConjunctVisitor implements PremiseVisitor { 43 | 44 | @Override 45 | public O visit(PositiveAtom atom, I state) { 46 | return null; 47 | } 48 | 49 | @Override 50 | public O visit(AnnotatedAtom atom, I state) { 51 | return null; 52 | } 53 | 54 | @Override 55 | public O visit(BinaryUnifier u, I state) { 56 | return null; 57 | } 58 | 59 | @Override 60 | public O visit(BinaryDisunifier u, I state) { 61 | return null; 62 | } 63 | 64 | @Override 65 | public O visit(NegatedAtom atom, I state) { 66 | return null; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/ast/visitors/DefaultTermVisitor.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.ast.visitors; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.ast.Constant; 37 | import edu.harvard.seas.pl.abcdatalog.ast.Variable; 38 | 39 | public class DefaultTermVisitor implements TermVisitor { 40 | 41 | @Override 42 | public O visit(Variable t, I state) { 43 | return null; 44 | } 45 | 46 | @Override 47 | public O visit(Constant t, I state) { 48 | return null; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/ast/visitors/HeadVisitor.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.ast.visitors; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.ast.PositiveAtom; 37 | 38 | public interface HeadVisitor { 39 | public O visit(PositiveAtom atom, I state); 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/ast/visitors/HeadVisitorBuilder.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.ast.visitors; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.ast.Head; 37 | import edu.harvard.seas.pl.abcdatalog.ast.PositiveAtom; 38 | import java.util.function.BiFunction; 39 | 40 | public class HeadVisitorBuilder { 41 | private BiFunction onPositiveAtom; 42 | 43 | public HeadVisitorBuilder onPositiveAtom(BiFunction onPositiveAtom) { 44 | this.onPositiveAtom = onPositiveAtom; 45 | return this; 46 | } 47 | 48 | public HeadVisitor or(BiFunction f) { 49 | return new Visitor(f); 50 | } 51 | 52 | public HeadVisitor orNull() { 53 | return this.or((head, state) -> null); 54 | } 55 | 56 | public HeadVisitor orCrash() { 57 | return this.or( 58 | (head, state) -> { 59 | throw new UnsupportedOperationException(); 60 | }); 61 | } 62 | 63 | private class Visitor implements HeadVisitor { 64 | private final BiFunction _onPositiveAtom; 65 | private final BiFunction otherwise; 66 | 67 | public Visitor(BiFunction otherwise) { 68 | this._onPositiveAtom = HeadVisitorBuilder.this.onPositiveAtom; 69 | this.otherwise = otherwise; 70 | } 71 | 72 | @Override 73 | public O visit(PositiveAtom atom, I state) { 74 | if (this._onPositiveAtom != null) { 75 | return this._onPositiveAtom.apply(atom, state); 76 | } 77 | return this.otherwise.apply(atom, state); 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/ast/visitors/PremiseVisitor.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.ast.visitors; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.ast.BinaryDisunifier; 37 | import edu.harvard.seas.pl.abcdatalog.ast.BinaryUnifier; 38 | import edu.harvard.seas.pl.abcdatalog.ast.NegatedAtom; 39 | import edu.harvard.seas.pl.abcdatalog.ast.PositiveAtom; 40 | import edu.harvard.seas.pl.abcdatalog.engine.bottomup.AnnotatedAtom; 41 | 42 | public interface PremiseVisitor { 43 | public O visit(PositiveAtom atom, I state); 44 | 45 | public O visit(AnnotatedAtom atom, I state); 46 | 47 | public O visit(BinaryUnifier u, I state); 48 | 49 | public O visit(BinaryDisunifier u, I state); 50 | 51 | public O visit(NegatedAtom atom, I state); 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/ast/visitors/TermVisitor.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.ast.visitors; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.ast.Constant; 37 | import edu.harvard.seas.pl.abcdatalog.ast.Variable; 38 | 39 | public interface TermVisitor { 40 | public O visit(Variable t, I state); 41 | 42 | public O visit(Constant t, I state); 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/ast/visitors/TermVisitorBuilder.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.ast.visitors; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.ast.Constant; 37 | import edu.harvard.seas.pl.abcdatalog.ast.Term; 38 | import edu.harvard.seas.pl.abcdatalog.ast.Variable; 39 | import java.util.function.BiFunction; 40 | 41 | public class TermVisitorBuilder { 42 | private BiFunction onVariable; 43 | private BiFunction onConstant; 44 | 45 | public TermVisitorBuilder onVariable(BiFunction f) { 46 | this.onVariable = f; 47 | return this; 48 | } 49 | 50 | public TermVisitorBuilder onConstant(BiFunction f) { 51 | this.onConstant = f; 52 | return this; 53 | } 54 | 55 | public TermVisitor or(BiFunction f) { 56 | return new Visitor(f); 57 | } 58 | 59 | public TermVisitor orNull() { 60 | return this.or((t, state) -> null); 61 | } 62 | 63 | public TermVisitor orCrash() { 64 | return this.or( 65 | (t, state) -> { 66 | throw new UnsupportedOperationException(); 67 | }); 68 | } 69 | 70 | private class Visitor implements TermVisitor { 71 | private final BiFunction _onVariable; 72 | private final BiFunction _onConstant; 73 | private final BiFunction otherwise; 74 | 75 | public Visitor(BiFunction otherwise) { 76 | this._onVariable = TermVisitorBuilder.this.onVariable; 77 | this._onConstant = TermVisitorBuilder.this.onConstant; 78 | this.otherwise = otherwise; 79 | } 80 | 81 | @Override 82 | public O visit(Variable t, I state) { 83 | if (_onVariable != null) { 84 | return _onVariable.apply(t, state); 85 | } 86 | return otherwise.apply(t, state); 87 | } 88 | 89 | @Override 90 | public O visit(Constant t, I state) { 91 | if (_onConstant != null) { 92 | return _onConstant.apply(t, state); 93 | } 94 | return otherwise.apply(t, state); 95 | } 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/ast/visitors/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * This package contains classes that implement the visitor design pattern for visiting AST nodes. 3 | */ 4 | package edu.harvard.seas.pl.abcdatalog.ast.visitors; 5 | 6 | /*- 7 | * #%L 8 | * AbcDatalog 9 | * %% 10 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 11 | * %% 12 | * Redistribution and use in source and binary forms, with or without modification, 13 | * are permitted provided that the following conditions are met: 14 | * 15 | * 1. Redistributions of source code must retain the above copyright notice, this 16 | * list of conditions and the following disclaimer. 17 | * 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, 19 | * this list of conditions and the following disclaimer in the documentation 20 | * and/or other materials provided with the distribution. 21 | * 22 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 23 | * may be used to endorse or promote products derived from this software without 24 | * specific prior written permission. 25 | * 26 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 27 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 28 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 29 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 30 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 31 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 32 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 33 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 34 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 35 | * OF THE POSSIBILITY OF SUCH DAMAGE. 36 | * #L% 37 | */ 38 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/engine/ConjunctiveQueryHelper.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.engine; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.ast.Constant; 37 | import edu.harvard.seas.pl.abcdatalog.ast.PositiveAtom; 38 | import edu.harvard.seas.pl.abcdatalog.ast.Term; 39 | import edu.harvard.seas.pl.abcdatalog.ast.Variable; 40 | import edu.harvard.seas.pl.abcdatalog.util.substitution.ConstOnlySubstitution; 41 | import edu.harvard.seas.pl.abcdatalog.util.substitution.SimpleConstSubstitution; 42 | import java.util.LinkedHashSet; 43 | import java.util.List; 44 | import java.util.Set; 45 | 46 | /** 47 | * A helper that adds (naive) conjunctive query support to an arbitrary DatalogEngine by turning a 48 | * conjunctive query into multiple singleton queries. 49 | */ 50 | public final class ConjunctiveQueryHelper { 51 | 52 | private ConjunctiveQueryHelper() { 53 | throw new AssertionError("impossible"); 54 | } 55 | 56 | public static Set query(DatalogEngine engine, List query) { 57 | Set r = new LinkedHashSet<>(); 58 | query(engine, query, 0, new SimpleConstSubstitution(), r); 59 | return r; 60 | } 61 | 62 | private static void query( 63 | DatalogEngine engine, 64 | List query, 65 | int pos, 66 | SimpleConstSubstitution subst, 67 | Set acc) { 68 | if (pos >= query.size()) { 69 | acc.add(subst); 70 | return; 71 | } 72 | PositiveAtom curQ = query.get(pos).applySubst(subst); 73 | for (PositiveAtom fact : engine.query(curQ)) { 74 | SimpleConstSubstitution curSubst = new SimpleConstSubstitution(subst); 75 | Term[] qArgs = curQ.getArgs(); 76 | Term[] fArgs = fact.getArgs(); 77 | for (int i = 0; i < qArgs.length; ++i) { 78 | Term qArg = qArgs[i]; 79 | if (qArg instanceof Variable) { 80 | curSubst.put((Variable) qArg, (Constant) fArgs[i]); 81 | } 82 | } 83 | query(engine, query, pos + 1, curSubst, acc); 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/engine/DatalogEngine.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.engine; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.ast.Clause; 37 | import edu.harvard.seas.pl.abcdatalog.ast.PositiveAtom; 38 | import edu.harvard.seas.pl.abcdatalog.ast.validation.DatalogValidationException; 39 | import edu.harvard.seas.pl.abcdatalog.util.substitution.ConstOnlySubstitution; 40 | import java.util.List; 41 | import java.util.Set; 42 | 43 | /** 44 | * A Datalog evaluation engine. Datalog engines are initialized with a set of clauses that represent 45 | * initial facts and rules that can be used to derive new facts. After initialization, clients can 46 | * query about whether certain facts are derivable. 47 | */ 48 | public interface DatalogEngine { 49 | /** 50 | * Initializes engine with a Datalog program, including EDB facts. The set that is passed into 51 | * this method should include rules for deriving new facts as well as the initial facts, which can 52 | * be encoded as clauses with empty bodies. 53 | * 54 | * @param program program to evaluate 55 | * @throws DatalogValidationException 56 | * @throws IllegalStateException if this engine has already been initialized 57 | * @throws DatalogValidationException if the given program is invalid 58 | */ 59 | void init(Set program) throws DatalogValidationException; 60 | 61 | /** 62 | * Returns all facts that 1) can be derived from the rules and initial facts that were used to 63 | * initialize this engine and 2) unify with the query. 64 | * 65 | * @param q the query 66 | * @return facts 67 | * @throws IllegalStateException if this engine has not been initialized with a program 68 | */ 69 | Set query(PositiveAtom q); 70 | 71 | /** 72 | * Returns the set of all (minimal) substitutions that 1) ground the given conjunctive query, and 73 | * 2) make it true with respect to the Datalog program backing this engine. To get a concrete 74 | * solution to the conjunctive query, apply one of the returned substitutions to the list 75 | * representing the query (using, e.g., {@link 76 | * edu.harvard.seas.pl.abcdatalog.util.substitution.SubstitutionUtils#applyToPositiveAtoms(Substitution, 77 | * Iterable) SubstitutionUtils.applyToPositiveAtoms}). 78 | * 79 | * @param query the conjunctive query 80 | * @return the set of minimal satisfying substitutions 81 | * @throws IllegalStateException if this engine has not been initialized with a program 82 | */ 83 | default Set query(List query) { 84 | return ConjunctiveQueryHelper.query(this, query); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/engine/DatalogEngineWithProvenance.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.engine; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.ast.Clause; 37 | import edu.harvard.seas.pl.abcdatalog.ast.PositiveAtom; 38 | 39 | /** 40 | * A Datalog evaluation engine that retains fact provenance. Datalog engines are initialized with a 41 | * set of clauses that represent initial facts and rules that can be used to derive new facts. After 42 | * initialization, clients can query about whether certain facts are derivable. 43 | * 44 | *

This engine also supports why-provenance queries; that is, querying for the justification for 45 | * why a fact was derived. 46 | */ 47 | public interface DatalogEngineWithProvenance extends DatalogEngine { 48 | 49 | /** 50 | * Return the last rule used in the justification of the given atom. The returned rule should be 51 | * ground (variable-free). Return null if the given atom is not a fact or was not derived.
52 | *
53 | * A client can recursively invoke this method on the premises of the returned clause to build a 54 | * provenance tree. 55 | * 56 | * @param fact the fact 57 | * @return the last rule used to justify that fact 58 | */ 59 | Clause getJustification(PositiveAtom fact); 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/engine/bottomup/AnnotatedAtom.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.engine.bottomup; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.ast.PositiveAtom; 37 | import edu.harvard.seas.pl.abcdatalog.ast.PredicateSym; 38 | import edu.harvard.seas.pl.abcdatalog.ast.Premise; 39 | import edu.harvard.seas.pl.abcdatalog.ast.Term; 40 | import edu.harvard.seas.pl.abcdatalog.ast.visitors.PremiseVisitor; 41 | import edu.harvard.seas.pl.abcdatalog.util.substitution.Substitution; 42 | 43 | /** 44 | * An annotated atom. In certain evaluation algorithms (such as semi-naive evaluation) it is helpful 45 | * to have an annotation associated with an atom. 46 | */ 47 | public class AnnotatedAtom implements Premise { 48 | private final PositiveAtom atom; 49 | private final Annotation anno; 50 | 51 | public AnnotatedAtom(PositiveAtom atom, Annotation anno) { 52 | this.atom = atom; 53 | this.anno = anno; 54 | } 55 | 56 | public enum Annotation { 57 | IDB, 58 | IDB_PREV, 59 | EDB, 60 | DELTA; 61 | } 62 | 63 | public Term[] getArgs() { 64 | return atom.getArgs(); 65 | } 66 | 67 | public PredicateSym getPred() { 68 | return atom.getPred(); 69 | } 70 | 71 | public PositiveAtom asUnannotatedAtom() { 72 | return atom; 73 | } 74 | 75 | public Annotation getAnnotation() { 76 | return anno; 77 | } 78 | 79 | @Override 80 | public O accept(PremiseVisitor visitor, I state) { 81 | return visitor.visit(this, state); 82 | } 83 | 84 | @Override 85 | public String toString() { 86 | String a = ""; 87 | switch (anno) { 88 | case IDB: 89 | a = "IDB"; 90 | break; 91 | case IDB_PREV: 92 | a = "IDB_PREV"; 93 | break; 94 | case EDB: 95 | a = "EDB"; 96 | break; 97 | case DELTA: 98 | a = "DELTA"; 99 | break; 100 | default: 101 | assert false; 102 | } 103 | return atom + "<" + a + ">"; 104 | } 105 | 106 | @Override 107 | public Premise applySubst(Substitution subst) { 108 | return new AnnotatedAtom(atom.applySubst(subst), anno); 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/engine/bottomup/BottomUpEngineFrame.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.engine.bottomup; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.ast.Clause; 37 | import edu.harvard.seas.pl.abcdatalog.ast.PositiveAtom; 38 | import edu.harvard.seas.pl.abcdatalog.ast.validation.DatalogValidationException; 39 | import edu.harvard.seas.pl.abcdatalog.engine.DatalogEngine; 40 | import edu.harvard.seas.pl.abcdatalog.util.datastructures.IndexableFactCollection; 41 | import java.util.HashSet; 42 | import java.util.Set; 43 | 44 | /** A framework for a bottom-up Datalog engine. */ 45 | public class BottomUpEngineFrame implements DatalogEngine { 46 | /** The evaluation manager for this engine. */ 47 | protected final E manager; 48 | 49 | /** The set of facts that can be derived from the current program. */ 50 | private volatile IndexableFactCollection facts; 51 | 52 | /** Has the engine been initialized? */ 53 | private volatile boolean isInitialized = false; 54 | 55 | /** 56 | * Constructs a bottom-up engine with the provided evaluation manager. 57 | * 58 | * @param manager the manager 59 | */ 60 | public BottomUpEngineFrame(E manager) { 61 | this.manager = manager; 62 | } 63 | 64 | @Override 65 | public synchronized void init(Set program) throws DatalogValidationException { 66 | if (this.isInitialized) { 67 | throw new IllegalStateException("Cannot initialize an engine more than once."); 68 | } 69 | 70 | this.manager.initialize(program); 71 | this.facts = this.manager.eval(); 72 | this.isInitialized = true; 73 | } 74 | 75 | @Override 76 | public Set query(PositiveAtom q) { 77 | if (!this.isInitialized) { 78 | throw new IllegalStateException("Engine must be initialized before it can be queried."); 79 | } 80 | 81 | Set r = new HashSet<>(); 82 | for (PositiveAtom a : this.facts.indexInto(q)) { 83 | if (q.unify(a) != null) { 84 | r.add(a); 85 | } 86 | } 87 | return r; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/engine/bottomup/BottomUpEngineFrameWithProvenance.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.engine.bottomup; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.ast.Clause; 37 | import edu.harvard.seas.pl.abcdatalog.ast.PositiveAtom; 38 | import edu.harvard.seas.pl.abcdatalog.engine.DatalogEngineWithProvenance; 39 | 40 | public class BottomUpEngineFrameWithProvenance 41 | extends BottomUpEngineFrame implements DatalogEngineWithProvenance { 42 | 43 | public BottomUpEngineFrameWithProvenance(EvalManagerWithProvenance manager) { 44 | super(manager); 45 | } 46 | 47 | @Override 48 | public Clause getJustification(PositiveAtom fact) { 49 | return manager.getJustification(fact); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/engine/bottomup/EvalManager.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.engine.bottomup; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.ast.Clause; 37 | import edu.harvard.seas.pl.abcdatalog.ast.validation.DatalogValidationException; 38 | import edu.harvard.seas.pl.abcdatalog.util.datastructures.IndexableFactCollection; 39 | import java.util.Set; 40 | 41 | /** The saturating evaluation manager for a bottom-up Datalog evaluation engine. */ 42 | public interface EvalManager { 43 | /** 44 | * Initialize this manager with a program. 45 | * 46 | * @param program the program 47 | * @throws DatalogValidationException if the program is invalid 48 | */ 49 | void initialize(Set program) throws DatalogValidationException; 50 | 51 | /** 52 | * Saturate all facts derivable from the program with which this manager has been initialized. 53 | * 54 | * @param program the program 55 | * @return the facts 56 | */ 57 | IndexableFactCollection eval(); 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/engine/bottomup/EvalManagerWithProvenance.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.engine.bottomup; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.ast.Clause; 37 | import edu.harvard.seas.pl.abcdatalog.ast.PositiveAtom; 38 | 39 | public interface EvalManagerWithProvenance extends EvalManager { 40 | 41 | /** 42 | * Return the last rule used in the justification of the given atom. The returned rule should be 43 | * ground (variable-free). Return null if the given atom is not a fact or was not derived. 44 | * 45 | * @param fact the fact 46 | * @return the last rule used to justify that fact 47 | */ 48 | Clause getJustification(PositiveAtom fact); 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/engine/bottomup/concurrent/ConcurrentBottomUpEngine.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.engine.bottomup.concurrent; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.engine.bottomup.BottomUpEngineFrame; 37 | import edu.harvard.seas.pl.abcdatalog.engine.bottomup.EvalManager; 38 | 39 | /** 40 | * A concurrent bottom-up Datalog engine that employs a saturation algorithm similar to semi-naive 41 | * evaluation. It supports explicit unification. 42 | */ 43 | public class ConcurrentBottomUpEngine extends BottomUpEngineFrame { 44 | 45 | public ConcurrentBottomUpEngine() { 46 | super(new BottomUpEvalManager()); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/engine/bottomup/concurrent/ConcurrentStratifiedNegationBottomUpEngine.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.engine.bottomup.concurrent; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.engine.bottomup.BottomUpEngineFrame; 37 | import edu.harvard.seas.pl.abcdatalog.engine.bottomup.EvalManager; 38 | 39 | /** 40 | * This class implements an experimental multi-threaded Datalog evaluation algorithm that supports 41 | * explicit unification and stratified negation. 42 | */ 43 | public class ConcurrentStratifiedNegationBottomUpEngine extends BottomUpEngineFrame { 44 | 45 | public ConcurrentStratifiedNegationBottomUpEngine() { 46 | super(new StratifiedNegationEvalManager()); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/engine/bottomup/concurrent/package-info.java: -------------------------------------------------------------------------------- 1 | /** This package contains multi-threaded bottom-up Datalog evaluation engines. */ 2 | package edu.harvard.seas.pl.abcdatalog.engine.bottomup.concurrent; 3 | 4 | /*- 5 | * #%L 6 | * AbcDatalog 7 | * %% 8 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 9 | * %% 10 | * Redistribution and use in source and binary forms, with or without modification, 11 | * are permitted provided that the following conditions are met: 12 | * 13 | * 1. Redistributions of source code must retain the above copyright notice, this 14 | * list of conditions and the following disclaimer. 15 | * 16 | * 2. Redistributions in binary form must reproduce the above copyright notice, 17 | * this list of conditions and the following disclaimer in the documentation 18 | * and/or other materials provided with the distribution. 19 | * 20 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 21 | * may be used to endorse or promote products derived from this software without 22 | * specific prior written permission. 23 | * 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 26 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 27 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 28 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 32 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 33 | * OF THE POSSIBILITY OF SUCH DAMAGE. 34 | * #L% 35 | */ 36 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/engine/bottomup/package-info.java: -------------------------------------------------------------------------------- 1 | /** This package contains classes and interfaces that are used in all of the bottom-up engines. */ 2 | package edu.harvard.seas.pl.abcdatalog.engine.bottomup; 3 | 4 | /*- 5 | * #%L 6 | * AbcDatalog 7 | * %% 8 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 9 | * %% 10 | * Redistribution and use in source and binary forms, with or without modification, 11 | * are permitted provided that the following conditions are met: 12 | * 13 | * 1. Redistributions of source code must retain the above copyright notice, this 14 | * list of conditions and the following disclaimer. 15 | * 16 | * 2. Redistributions in binary form must reproduce the above copyright notice, 17 | * this list of conditions and the following disclaimer in the documentation 18 | * and/or other materials provided with the distribution. 19 | * 20 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 21 | * may be used to endorse or promote products derived from this software without 22 | * specific prior written permission. 23 | * 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 26 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 27 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 28 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 32 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 33 | * OF THE POSSIBILITY OF SUCH DAMAGE. 34 | * #L% 35 | */ 36 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/engine/bottomup/sequential/SemiNaiveEngine.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.engine.bottomup.sequential; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.ast.Clause; 37 | import edu.harvard.seas.pl.abcdatalog.ast.PositiveAtom; 38 | import edu.harvard.seas.pl.abcdatalog.ast.Premise; 39 | import edu.harvard.seas.pl.abcdatalog.engine.DatalogEngine; 40 | import edu.harvard.seas.pl.abcdatalog.engine.DatalogEngineWithProvenance; 41 | import edu.harvard.seas.pl.abcdatalog.engine.bottomup.BottomUpEngineFrameWithProvenance; 42 | import edu.harvard.seas.pl.abcdatalog.parser.DatalogParser; 43 | import edu.harvard.seas.pl.abcdatalog.parser.DatalogTokenizer; 44 | import java.io.Reader; 45 | import java.io.StringReader; 46 | import java.util.Set; 47 | 48 | /** 49 | * A Datalog engine that implements the classic semi-naive bottom-up evaluation algorithm. It 50 | * supports explicit unification and stratified negation. 51 | */ 52 | public class SemiNaiveEngine extends BottomUpEngineFrameWithProvenance { 53 | 54 | public static DatalogEngine newEngine() { 55 | return new SemiNaiveEngine(false); 56 | } 57 | 58 | public static DatalogEngineWithProvenance newEngineWithProvenance() { 59 | return new SemiNaiveEngine(true); 60 | } 61 | 62 | public SemiNaiveEngine(boolean collectProv) { 63 | super(new SemiNaiveEvalManager(collectProv)); 64 | } 65 | 66 | public static void main(String[] args) throws Exception { 67 | String[] lines = { 68 | "edge(a, b).", 69 | "edge(b, c).", 70 | "edge(c, d).", 71 | "edge(d, c).", 72 | "tc(X, Y) :- edge(X, Y).", 73 | "tc(X, Y) :- tc(X, Z), tc(Z, Y)." 74 | }; 75 | StringBuilder sb = new StringBuilder(); 76 | for (String line : lines) { 77 | sb.append(line); 78 | } 79 | Reader r = new StringReader(sb.toString()); 80 | DatalogTokenizer t = new DatalogTokenizer(r); 81 | Set prog = DatalogParser.parseProgram(t); 82 | DatalogEngineWithProvenance e = SemiNaiveEngine.newEngineWithProvenance(); 83 | e.init(prog); 84 | t = new DatalogTokenizer(new StringReader("tc(c, c)?")); 85 | PositiveAtom q = DatalogParser.parseQuery(t); 86 | Clause lastCl = e.getJustification(q); 87 | System.out.println(lastCl); 88 | for (Premise p : lastCl.getBody()) { 89 | if (p instanceof PositiveAtom) { 90 | Clause secondLastCl = e.getJustification((PositiveAtom) p); 91 | System.out.println(secondLastCl); 92 | for (Premise p2 : secondLastCl.getBody()) { 93 | System.out.println(e.getJustification((PositiveAtom) p2)); 94 | } 95 | } 96 | } 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/engine/bottomup/sequential/package-info.java: -------------------------------------------------------------------------------- 1 | /** This package contains single-threaded bottom-up Datalog evaluation engines. */ 2 | package edu.harvard.seas.pl.abcdatalog.engine.bottomup.sequential; 3 | 4 | /*- 5 | * #%L 6 | * AbcDatalog 7 | * %% 8 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 9 | * %% 10 | * Redistribution and use in source and binary forms, with or without modification, 11 | * are permitted provided that the following conditions are met: 12 | * 13 | * 1. Redistributions of source code must retain the above copyright notice, this 14 | * list of conditions and the following disclaimer. 15 | * 16 | * 2. Redistributions in binary form must reproduce the above copyright notice, 17 | * this list of conditions and the following disclaimer in the documentation 18 | * and/or other materials provided with the distribution. 19 | * 20 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 21 | * may be used to endorse or promote products derived from this software without 22 | * specific prior written permission. 23 | * 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 26 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 27 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 28 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 32 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 33 | * OF THE POSSIBILITY OF SUCH DAMAGE. 34 | * #L% 35 | */ 36 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/engine/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * This package contains the standard interface for a Datalog engine (see {@link 3 | * edu.harvard.seas.pl.abcdatalog.executor.DatalogExecutor} for an alternative interface). 4 | */ 5 | package edu.harvard.seas.pl.abcdatalog.engine; 6 | 7 | /*- 8 | * #%L 9 | * AbcDatalog 10 | * %% 11 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 12 | * %% 13 | * Redistribution and use in source and binary forms, with or without modification, 14 | * are permitted provided that the following conditions are met: 15 | * 16 | * 1. Redistributions of source code must retain the above copyright notice, this 17 | * list of conditions and the following disclaimer. 18 | * 19 | * 2. Redistributions in binary form must reproduce the above copyright notice, 20 | * this list of conditions and the following disclaimer in the documentation 21 | * and/or other materials provided with the distribution. 22 | * 23 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 24 | * may be used to endorse or promote products derived from this software without 25 | * specific prior written permission. 26 | * 27 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 28 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 29 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 30 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 31 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 32 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 33 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 34 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 35 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 36 | * OF THE POSSIBILITY OF SUCH DAMAGE. 37 | * #L% 38 | */ 39 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/engine/topdown/AdornedAtom.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.engine.topdown; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.ast.Term; 37 | 38 | /** An adorned atom (i.e., an atom where every argument is marked as either bound or free). */ 39 | public class AdornedAtom { 40 | private final AdornedPredicateSym pred; 41 | private final Term[] args; 42 | 43 | /** 44 | * Constructs an adorned atom with the given predicate symbol and arguments. 45 | * 46 | * @param pred adorned predicate symbol 47 | * @param args arguments 48 | */ 49 | public AdornedAtom(AdornedPredicateSym pred, Term[] args) { 50 | this.pred = pred; 51 | this.args = args; 52 | } 53 | 54 | @Override 55 | public String toString() { 56 | StringBuilder sb = new StringBuilder(); 57 | sb.append(pred); 58 | if (this.args.length > 0) { 59 | sb.append("("); 60 | for (int i = 0; i < args.length; ++i) { 61 | sb.append(args[i]); 62 | if (i < args.length - 1) { 63 | sb.append(", "); 64 | } 65 | } 66 | sb.append(")"); 67 | } 68 | return sb.toString(); 69 | } 70 | 71 | public AdornedPredicateSym getPred() { 72 | return pred; 73 | } 74 | 75 | public Term[] getArgs() { 76 | return args; 77 | } 78 | 79 | @Override 80 | public int hashCode() { 81 | final int prime = 31; 82 | int result = 1; 83 | result = prime * result + ((args == null) ? 0 : args.hashCode()); 84 | result = prime * result + ((pred == null) ? 0 : pred.hashCode()); 85 | return result; 86 | } 87 | 88 | @Override 89 | public boolean equals(Object obj) { 90 | if (this == obj) return true; 91 | if (obj == null) return false; 92 | if (getClass() != obj.getClass()) return false; 93 | AdornedAtom other = (AdornedAtom) obj; 94 | if (args == null) { 95 | if (other.args != null) return false; 96 | } else if (!args.equals(other.args)) return false; 97 | if (pred == null) { 98 | if (other.pred != null) return false; 99 | } else if (!pred.equals(other.pred)) return false; 100 | return true; 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/engine/topdown/QsqSupRelation.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.engine.topdown; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | /** 37 | * A supplementary relation used in QSQ evaluation.
38 | *
39 | * It is modeled as a node in a linked list. The intention is that each node contains the tuples for 40 | * the supplementary relation itself, a pointer to the atom that appears after the supplementary 41 | * relation in the relevant rule, and a pointer to the next supplementary relation in the same rule. 42 | */ 43 | public class QsqSupRelation extends Relation { 44 | /** Points to the next supplementary relation in the rule being evaluated. */ 45 | public QsqSupRelation next; 46 | 47 | /** Points to the atom that follows this supplementary relation in the rule being evaluated. */ 48 | public AdornedAtom nextAtom; 49 | 50 | /** 51 | * Constructs an empty supplementary relation with the supplied schema. 52 | * 53 | * @param schema schema of supplementary relation 54 | */ 55 | public QsqSupRelation(TermSchema schema) { 56 | super(schema); 57 | nextAtom = null; 58 | next = null; 59 | } 60 | 61 | @Override 62 | public String toString() { 63 | StringBuilder sb = new StringBuilder(); 64 | sb.append(super.toString()); 65 | sb.append("; Next atom: "); 66 | if (nextAtom != null) { 67 | sb.append(nextAtom); 68 | } 69 | return sb.toString(); 70 | } 71 | 72 | @Override 73 | public int hashCode() { 74 | final int prime = 31; 75 | int result = super.hashCode(); 76 | result = prime * result + ((next == null) ? 0 : next.hashCode()); 77 | result = prime * result + ((nextAtom == null) ? 0 : nextAtom.hashCode()); 78 | return result; 79 | } 80 | 81 | @Override 82 | public boolean equals(Object obj) { 83 | if (this == obj) return true; 84 | if (!super.equals(obj)) return false; 85 | if (getClass() != obj.getClass()) return false; 86 | QsqSupRelation other = (QsqSupRelation) obj; 87 | if (next == null) { 88 | if (other.next != null) return false; 89 | } else if (!next.equals(other.next)) return false; 90 | if (nextAtom == null) { 91 | if (other.nextAtom != null) return false; 92 | } else if (!nextAtom.equals(other.nextAtom)) return false; 93 | return true; 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/engine/topdown/Tuple.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.engine.topdown; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.ast.Term; 37 | import edu.harvard.seas.pl.abcdatalog.util.substitution.Substitution; 38 | import edu.harvard.seas.pl.abcdatalog.util.substitution.UnionFindBasedUnifier; 39 | import java.util.Arrays; 40 | import java.util.List; 41 | 42 | /** A tuple of terms, i.e., an ordered list of fixed arity. */ 43 | public class Tuple { 44 | /** The terms in this tuple. */ 45 | public final Term[] elts; 46 | 47 | /** 48 | * Constructs a tuple from a list of terms. 49 | * 50 | * @param elts the list of terms 51 | */ 52 | public Tuple(List elts) { 53 | Term[] tmp = new Term[elts.size()]; 54 | this.elts = elts.toArray(tmp); 55 | } 56 | 57 | public Tuple(Term[] elts) { 58 | this.elts = elts; 59 | } 60 | 61 | /** 62 | * Returns the term at the ith position in this tuple (0-indexed). 63 | * 64 | * @param i the position 65 | * @return the term 66 | */ 67 | public Term get(int i) { 68 | return this.elts[i]; 69 | } 70 | 71 | /** 72 | * Returns the arity of this tuple. 73 | * 74 | * @return the arity 75 | */ 76 | public int size() { 77 | return this.elts.length; 78 | } 79 | 80 | /** 81 | * Attempts to unify this tuple with another tuple. 82 | * 83 | * @param other the other tuple 84 | * @return the substitution resulting from the unification, or null if the unification fails 85 | */ 86 | public Tuple unify(Tuple other) { 87 | Substitution subst = UnionFindBasedUnifier.fromTerms(this.elts, other.elts); 88 | if (subst != null) { 89 | return new Tuple(subst.apply(this.elts)); 90 | } 91 | return null; 92 | } 93 | 94 | @Override 95 | public String toString() { 96 | StringBuilder sb = new StringBuilder(); 97 | sb.append("<"); 98 | for (int i = 0; i < this.elts.length; ++i) { 99 | sb.append(elts[i]); 100 | if (i < this.elts.length - 1) { 101 | sb.append(", "); 102 | } 103 | } 104 | sb.append(">"); 105 | return sb.toString(); 106 | } 107 | 108 | @Override 109 | public int hashCode() { 110 | final int prime = 31; 111 | int result = 1; 112 | result = prime * result + Arrays.hashCode(elts); 113 | return result; 114 | } 115 | 116 | @Override 117 | public boolean equals(Object obj) { 118 | if (this == obj) return true; 119 | if (obj == null) return false; 120 | if (getClass() != obj.getClass()) return false; 121 | Tuple other = (Tuple) obj; 122 | if (!Arrays.equals(elts, other.elts)) return false; 123 | return true; 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/engine/topdown/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * This package contains implementations of standard top-down Datalog evaluation algorithms. The 3 | * implementations here should be viewed as less-trustworthy than the bottom-up implementations in 4 | * {@link edu.harvard.seas.pl.abcdatalog.engine.bottomup.sequential} and {@link 5 | * edu.harvard.seas.pl.abcdatalog.engine.bottomup.concurrent}. 6 | */ 7 | package edu.harvard.seas.pl.abcdatalog.engine.topdown; 8 | 9 | /*- 10 | * #%L 11 | * AbcDatalog 12 | * %% 13 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 14 | * %% 15 | * Redistribution and use in source and binary forms, with or without modification, 16 | * are permitted provided that the following conditions are met: 17 | * 18 | * 1. Redistributions of source code must retain the above copyright notice, this 19 | * list of conditions and the following disclaimer. 20 | * 21 | * 2. Redistributions in binary form must reproduce the above copyright notice, 22 | * this list of conditions and the following disclaimer in the documentation 23 | * and/or other materials provided with the distribution. 24 | * 25 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 26 | * may be used to endorse or promote products derived from this software without 27 | * specific prior written permission. 28 | * 29 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 30 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 31 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 32 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 33 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 34 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 35 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 36 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 37 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 38 | * OF THE POSSIBILITY OF SUCH DAMAGE. 39 | * #L% 40 | */ 41 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/executor/DatalogExecutor.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.executor; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.ast.Clause; 37 | import edu.harvard.seas.pl.abcdatalog.ast.PositiveAtom; 38 | import edu.harvard.seas.pl.abcdatalog.ast.PredicateSym; 39 | import edu.harvard.seas.pl.abcdatalog.ast.validation.DatalogValidationException; 40 | import java.util.Set; 41 | 42 | /** 43 | * A interface to an ongoing Datalog evaluation that allows for callbacks to be registered that are 44 | * invoked when relevant new facts are derived and for new EDB facts to be added in the midst of 45 | * evaluation. 46 | */ 47 | public interface DatalogExecutor { 48 | /** 49 | * Initializes the Datalog engine with a program and specifies which EDB relations can be extended 50 | * (with DatalogExecutor.addFactAsynchronously()) during evaluation. This should only be called 51 | * once. 52 | * 53 | * @param program the program 54 | * @param extendibleEdbPreds the extendible EDB relations 55 | * @throws DatalogValidationException if program is invalid 56 | */ 57 | void initialize(Set program, Set extendibleEdbPreds) 58 | throws DatalogValidationException; 59 | 60 | /** 61 | * Starts the Datalog evaluation. 62 | * 63 | * @throws IllegalStateException if the executor has not been initialized or the evaluation has 64 | * already been started 65 | */ 66 | void start(); 67 | 68 | /** 69 | * Asynchronously adds a new EDB fact to the Datalog evaluation. The EDB fact must be part of a 70 | * relation that is specified in DatalogExecutor.initialize() as being extendible. A fact is a 71 | * ground atom (i.e., an atom without any variables). 72 | * 73 | * @param edbFact the new EDB fact 74 | * @throws IllegalStateException if the executor has not been initialized 75 | * @throws IllegalArgumentException if the provided atom is not ground, or if it is not part of a 76 | * relation specified during initialization as being extendible 77 | */ 78 | void addFactAsynchronously(PositiveAtom edbFact); 79 | 80 | /** 81 | * Associates a listener with a given predicate symbol, so that if any fact is derived during 82 | * evaluation with that predicate symbol, the listener will be invoked with that fact. The 83 | * listener can be executed in an arbitrary thread and should not block. 84 | * 85 | * @param p the predicate symbol 86 | * @param listener the listener 87 | * @throws IllegalStateException if the evaluation has already been started 88 | */ 89 | void registerListener(PredicateSym p, DatalogListener listener); 90 | 91 | /** Shuts down the executor, which cannot be reused. */ 92 | void shutdown(); 93 | } 94 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/executor/DatalogListener.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.executor; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.ast.PositiveAtom; 37 | 38 | /** A callback that is registered with a Datalog executor and is invoked during evaluation. */ 39 | public interface DatalogListener { 40 | /** 41 | * Is invoked when a relevant new fact is derived during Datalog evaluation. Note that 42 | * fact.isGround() will be true (i.e., a fact is a ground atom). 43 | * 44 | * @param fact the new fact 45 | */ 46 | void newFactDerived(PositiveAtom fact); 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/executor/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * This package contains classes that implement a Datalog executor, which is a Datalog engine that 3 | * runs asynchronously. New facts can be added to the engine in the midst of evaluation. 4 | */ 5 | package edu.harvard.seas.pl.abcdatalog.executor; 6 | 7 | /*- 8 | * #%L 9 | * AbcDatalog 10 | * %% 11 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 12 | * %% 13 | * Redistribution and use in source and binary forms, with or without modification, 14 | * are permitted provided that the following conditions are met: 15 | * 16 | * 1. Redistributions of source code must retain the above copyright notice, this 17 | * list of conditions and the following disclaimer. 18 | * 19 | * 2. Redistributions in binary form must reproduce the above copyright notice, 20 | * this list of conditions and the following disclaimer in the documentation 21 | * and/or other materials provided with the distribution. 22 | * 23 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 24 | * may be used to endorse or promote products derived from this software without 25 | * specific prior written permission. 26 | * 27 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 28 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 29 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 30 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 31 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 32 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 33 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 34 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 35 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 36 | * OF THE POSSIBILITY OF SUCH DAMAGE. 37 | * #L% 38 | */ 39 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/gui/package-info.java: -------------------------------------------------------------------------------- 1 | /** This package contains a graphical user interface for a Datalog editor and interpreter. */ 2 | package edu.harvard.seas.pl.abcdatalog.gui; 3 | 4 | /*- 5 | * #%L 6 | * AbcDatalog 7 | * %% 8 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 9 | * %% 10 | * Redistribution and use in source and binary forms, with or without modification, 11 | * are permitted provided that the following conditions are met: 12 | * 13 | * 1. Redistributions of source code must retain the above copyright notice, this 14 | * list of conditions and the following disclaimer. 15 | * 16 | * 2. Redistributions in binary form must reproduce the above copyright notice, 17 | * this list of conditions and the following disclaimer in the documentation 18 | * and/or other materials provided with the distribution. 19 | * 20 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 21 | * may be used to endorse or promote products derived from this software without 22 | * specific prior written permission. 23 | * 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 26 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 27 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 28 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 32 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 33 | * OF THE POSSIBILITY OF SUCH DAMAGE. 34 | * #L% 35 | */ 36 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * AbcDatalog, an implementation of Datalog that is intended to be easily extensible with new 3 | * language features and new evaluation engines. 4 | */ 5 | package edu.harvard.seas.pl.abcdatalog; 6 | 7 | /*- 8 | * #%L 9 | * AbcDatalog 10 | * %% 11 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 12 | * %% 13 | * Redistribution and use in source and binary forms, with or without modification, 14 | * are permitted provided that the following conditions are met: 15 | * 16 | * 1. Redistributions of source code must retain the above copyright notice, this 17 | * list of conditions and the following disclaimer. 18 | * 19 | * 2. Redistributions in binary form must reproduce the above copyright notice, 20 | * this list of conditions and the following disclaimer in the documentation 21 | * and/or other materials provided with the distribution. 22 | * 23 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 24 | * may be used to endorse or promote products derived from this software without 25 | * specific prior written permission. 26 | * 27 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 28 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 29 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 30 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 31 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 32 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 33 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 34 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 35 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 36 | * OF THE POSSIBILITY OF SUCH DAMAGE. 37 | * #L% 38 | */ 39 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/parser/DatalogParseException.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.parser; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | /** An exception signifying a parsing error. */ 37 | public class DatalogParseException extends Exception { 38 | /** Constructs an exception signifying a parsing error. */ 39 | public DatalogParseException() {} 40 | 41 | /** 42 | * Constructs an exception signifying a parsing error. 43 | * 44 | * @param message the error message 45 | */ 46 | public DatalogParseException(String message) { 47 | super(message); 48 | } 49 | 50 | /** 51 | * Constructs an exception signifying a parsing error. 52 | * 53 | * @param cause the exception that caused this exception 54 | */ 55 | public DatalogParseException(Throwable cause) { 56 | super(cause); 57 | } 58 | 59 | /** 60 | * Constructs an exception signifying a parsing error. 61 | * 62 | * @param message the error message 63 | * @param cause the exception that caused this exception 64 | */ 65 | public DatalogParseException(String message, Throwable cause) { 66 | super(message, cause); 67 | } 68 | 69 | /** 70 | * Constructs an exception signifying a parsing error. 71 | * 72 | * @param message the error message 73 | * @param cause the exception that caused this exception 74 | * @param enableSuppression whether or not suppression is enabled or disabled 75 | * @param writableStackTrace whether or not the stack trace should be writable 76 | */ 77 | public DatalogParseException( 78 | String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { 79 | super(message, cause, enableSuppression, writableStackTrace); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/parser/package-info.java: -------------------------------------------------------------------------------- 1 | /** This package contains classes that implement a parser for Datalog source code. */ 2 | package edu.harvard.seas.pl.abcdatalog.parser; 3 | 4 | /*- 5 | * #%L 6 | * AbcDatalog 7 | * %% 8 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 9 | * %% 10 | * Redistribution and use in source and binary forms, with or without modification, 11 | * are permitted provided that the following conditions are met: 12 | * 13 | * 1. Redistributions of source code must retain the above copyright notice, this 14 | * list of conditions and the following disclaimer. 15 | * 16 | * 2. Redistributions in binary form must reproduce the above copyright notice, 17 | * this list of conditions and the following disclaimer in the documentation 18 | * and/or other materials provided with the distribution. 19 | * 20 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 21 | * may be used to endorse or promote products derived from this software without 22 | * specific prior written permission. 23 | * 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 26 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 27 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 28 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 32 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 33 | * OF THE POSSIBILITY OF SUCH DAMAGE. 34 | * #L% 35 | */ 36 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/util/Box.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.util; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | public class Box { 37 | public T value; 38 | 39 | public Box() {} 40 | ; 41 | 42 | public Box(T value) { 43 | this.value = value; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/util/Utilities.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.util; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import java.util.Collections; 37 | import java.util.LinkedHashSet; 38 | import java.util.Map; 39 | import java.util.Set; 40 | import java.util.concurrent.ConcurrentHashMap; 41 | import java.util.concurrent.ConcurrentMap; 42 | 43 | /** "Static" class containing utility methods. */ 44 | public final class Utilities { 45 | 46 | private Utilities() { 47 | // Cannot be instantiated. 48 | } 49 | 50 | public static final int concurrency = Runtime.getRuntime().availableProcessors(); 51 | 52 | public static Set createConcurrentSet() { 53 | return Collections.newSetFromMap(createConcurrentMap()); 54 | } 55 | 56 | public static ConcurrentMap createConcurrentMap() { 57 | return new ConcurrentHashMap<>(16, 0.75f, concurrency); 58 | } 59 | 60 | /** 61 | * Returns the set in map associated with key, creating a new set if needed. 62 | * 63 | * @param map the map 64 | * @param key the key 65 | * @return the set 66 | */ 67 | public static Set getSetFromMap(Map> map, K key) { 68 | Set vals = map.get(key); 69 | if (vals == null) { 70 | vals = new LinkedHashSet<>(); 71 | map.put(key, vals); 72 | } 73 | return vals; 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/util/datastructures/FactIndexer.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.util.datastructures; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.ast.PositiveAtom; 37 | 38 | public interface FactIndexer extends IndexableFactCollection { 39 | /** 40 | * Adds a fact to the FactIndexer. 41 | * 42 | * @param fact a fact 43 | */ 44 | public void add(PositiveAtom fact); 45 | 46 | /** 47 | * Adds some number of facts to the FactIndexer. 48 | * 49 | * @param facts some facts 50 | */ 51 | public void addAll(Iterable facts); 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/util/datastructures/FactIndexerFactory.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.util.datastructures; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.ast.PositiveAtom; 37 | import edu.harvard.seas.pl.abcdatalog.util.Utilities; 38 | import java.util.Queue; 39 | import java.util.Set; 40 | import java.util.concurrent.ConcurrentLinkedQueue; 41 | 42 | /** A factory for creating some useful fact indexers. */ 43 | public final class FactIndexerFactory { 44 | 45 | private FactIndexerFactory() {} 46 | 47 | /** 48 | * Creates a fact indexer that uses concurrent sets for the base container. 49 | * 50 | * @return the fact indexer 51 | */ 52 | public static ConcurrentFactIndexer> createConcurrentSetFactIndexer() { 53 | return new ConcurrentFactIndexer<>(Utilities::createConcurrentSet, Set::add, Set::size); 54 | } 55 | 56 | /** 57 | * Creates a fact indexer that uses concurrent queues for the base container. 58 | * 59 | * @return the fact indexer 60 | */ 61 | public static ConcurrentFactIndexer> createConcurrentQueueFactIndexer() { 62 | return new ConcurrentFactIndexer<>(ConcurrentLinkedQueue::new, Queue::add, Queue::size); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/util/datastructures/IndexableFactCollection.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.util.datastructures; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.ast.PositiveAtom; 37 | import edu.harvard.seas.pl.abcdatalog.ast.PredicateSym; 38 | import edu.harvard.seas.pl.abcdatalog.util.substitution.ConstOnlySubstitution; 39 | import java.util.Set; 40 | 41 | /** 42 | * A fixed collection of facts that only allows a basic query operation: return the atoms in the 43 | * collection that might match a given atom. 44 | */ 45 | public interface IndexableFactCollection { 46 | /** 47 | * Returns the atoms in the collection that potentially "match" the provided atom. There is no 48 | * guarantee that the returned atoms can actually be unified with the provided atom. 49 | * 50 | * @param atom the atom to match 51 | * @return the matching facts 52 | */ 53 | public Iterable indexInto(PositiveAtom atom); 54 | 55 | /** 56 | * Returns the atoms in the collection that potentially "match" the provided atom, after the given 57 | * substitution has been applied. There is no guarantee that the returned atoms can actually be 58 | * unified with the provided atom. 59 | * 60 | * @param atom the atom to match 61 | * @return the matching facts 62 | */ 63 | public Iterable indexInto(PositiveAtom atom, ConstOnlySubstitution subst); 64 | 65 | /** 66 | * Returns the atoms in the collection with the given predicate symbol. 67 | * 68 | * @param pred the predicate symbol 69 | * @return the matching facts 70 | */ 71 | public Iterable indexInto(PredicateSym pred); 72 | 73 | /** 74 | * Returns whether the collection is empty. 75 | * 76 | * @return whether the collection is empty 77 | */ 78 | public boolean isEmpty(); 79 | 80 | /** 81 | * Returns the set of the predicate symbols represented in this collection. 82 | * 83 | * @return the predicate symbols 84 | */ 85 | Set getPreds(); 86 | } 87 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/util/datastructures/package-info.java: -------------------------------------------------------------------------------- 1 | /** This package contains data structures for storing and indexing Datalog facts. */ 2 | package edu.harvard.seas.pl.abcdatalog.util.datastructures; 3 | 4 | /*- 5 | * #%L 6 | * AbcDatalog 7 | * %% 8 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 9 | * %% 10 | * Redistribution and use in source and binary forms, with or without modification, 11 | * are permitted provided that the following conditions are met: 12 | * 13 | * 1. Redistributions of source code must retain the above copyright notice, this 14 | * list of conditions and the following disclaimer. 15 | * 16 | * 2. Redistributions in binary form must reproduce the above copyright notice, 17 | * this list of conditions and the following disclaimer in the documentation 18 | * and/or other materials provided with the distribution. 19 | * 20 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 21 | * may be used to endorse or promote products derived from this software without 22 | * specific prior written permission. 23 | * 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 26 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 27 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 28 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 32 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 33 | * OF THE POSSIBILITY OF SUCH DAMAGE. 34 | * #L% 35 | */ 36 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/util/graph/DirectedEdge.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.util.graph; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | public interface DirectedEdge { 37 | public V getSource(); 38 | 39 | public V getDest(); 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/util/graph/package-info.java: -------------------------------------------------------------------------------- 1 | /** This package contains utility classes for graph operations. */ 2 | package edu.harvard.seas.pl.abcdatalog.util.graph; 3 | 4 | /*- 5 | * #%L 6 | * AbcDatalog 7 | * %% 8 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 9 | * %% 10 | * Redistribution and use in source and binary forms, with or without modification, 11 | * are permitted provided that the following conditions are met: 12 | * 13 | * 1. Redistributions of source code must retain the above copyright notice, this 14 | * list of conditions and the following disclaimer. 15 | * 16 | * 2. Redistributions in binary form must reproduce the above copyright notice, 17 | * this list of conditions and the following disclaimer in the documentation 18 | * and/or other materials provided with the distribution. 19 | * 20 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 21 | * may be used to endorse or promote products derived from this software without 22 | * specific prior written permission. 23 | * 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 26 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 27 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 28 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 32 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 33 | * OF THE POSSIBILITY OF SUCH DAMAGE. 34 | * #L% 35 | */ 36 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/util/package-info.java: -------------------------------------------------------------------------------- 1 | /** This package contains basic utilities. */ 2 | package edu.harvard.seas.pl.abcdatalog.util; 3 | 4 | /*- 5 | * #%L 6 | * AbcDatalog 7 | * %% 8 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 9 | * %% 10 | * Redistribution and use in source and binary forms, with or without modification, 11 | * are permitted provided that the following conditions are met: 12 | * 13 | * 1. Redistributions of source code must retain the above copyright notice, this 14 | * list of conditions and the following disclaimer. 15 | * 16 | * 2. Redistributions in binary form must reproduce the above copyright notice, 17 | * this list of conditions and the following disclaimer in the documentation 18 | * and/or other materials provided with the distribution. 19 | * 20 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 21 | * may be used to endorse or promote products derived from this software without 22 | * specific prior written permission. 23 | * 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 26 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 27 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 28 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 32 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 33 | * OF THE POSSIBILITY OF SUCH DAMAGE. 34 | * #L% 35 | */ 36 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/util/substitution/ConstOnlySubstitution.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.util.substitution; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.ast.Constant; 37 | import edu.harvard.seas.pl.abcdatalog.ast.Variable; 38 | 39 | /** 40 | * A mapping from variables to constants. This is a restriction of a more general substitution, 41 | * which is from variables to terms. 42 | */ 43 | public interface ConstOnlySubstitution extends Substitution { 44 | /** 45 | * Retrieves the mapping of a variable. 46 | * 47 | * @param x the variable 48 | * @return the constant that the variable is bound to, or null if the variable is not in the 49 | * substitution 50 | */ 51 | @Override 52 | Constant get(Variable x); 53 | 54 | /** 55 | * Attempts to add a mapping to the substitution. Returns true if the mapping was made 56 | * successfully (i.e., if the variable was not already mapped to another constant). 57 | * 58 | * @param x the variable 59 | * @param c the constant 60 | * @return whether the mapping was successfully added 61 | */ 62 | boolean add(Variable x, Constant c); 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/util/substitution/Substitution.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.util.substitution; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.ast.Term; 37 | import edu.harvard.seas.pl.abcdatalog.ast.Variable; 38 | 39 | /** A mapping from variables to terms. */ 40 | public interface Substitution { 41 | /** 42 | * Apply this substitution to a list of terms, creating a new list. 43 | * 44 | * @param original the original list 45 | * @return the new list 46 | */ 47 | Term[] apply(Term[] original); 48 | 49 | /** 50 | * Retrieves the mapping of a variable. 51 | * 52 | * @param x the variable 53 | * @return the term that the variable is bound to, or null if the variable is not in the 54 | * substitution 55 | */ 56 | Term get(Variable x); 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/util/substitution/SubstitutionUtils.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.util.substitution; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.ast.Clause; 37 | import edu.harvard.seas.pl.abcdatalog.ast.Head; 38 | import edu.harvard.seas.pl.abcdatalog.ast.PositiveAtom; 39 | import edu.harvard.seas.pl.abcdatalog.ast.Premise; 40 | import java.util.ArrayList; 41 | import java.util.Collection; 42 | import java.util.List; 43 | 44 | public final class SubstitutionUtils { 45 | 46 | private SubstitutionUtils() { 47 | throw new AssertionError("impossible"); 48 | } 49 | 50 | /** 51 | * Apply a substitution to the given positive atoms, adding the resulting atoms to the provided 52 | * collection (in order). 53 | * 54 | * @param subst the substitution 55 | * @param atoms the atoms 56 | * @param acc the collection to add the atoms to 57 | */ 58 | public static void applyToPositiveAtoms( 59 | Substitution subst, Iterable atoms, Collection acc) { 60 | for (PositiveAtom atom : atoms) { 61 | acc.add(atom.applySubst(subst)); 62 | } 63 | } 64 | 65 | /** 66 | * Apply a substitution to the given positive atoms, returning a list of the resulting atoms (in 67 | * order). 68 | * 69 | * @param subst the substitution 70 | * @param atoms the atoms 71 | * @return a list of the atoms that result from applying the substitution 72 | */ 73 | public static List applyToPositiveAtoms( 74 | Substitution subst, Iterable atoms) { 75 | List ret = new ArrayList<>(); 76 | applyToPositiveAtoms(subst, atoms, ret); 77 | return ret; 78 | } 79 | 80 | public static Clause applyToClause(Substitution subst, Clause cl) { 81 | Head newHead = cl.getHead().applySubst(subst); 82 | List newBody = new ArrayList<>(); 83 | for (Premise p : cl.getBody()) { 84 | newBody.add(p.applySubst(subst)); 85 | } 86 | return new Clause(newHead, newBody); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/util/substitution/TermUnifier.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.util.substitution; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.ast.Term; 37 | import edu.harvard.seas.pl.abcdatalog.ast.Variable; 38 | 39 | /** 40 | * A substitution that allows a variable to be mapped to (i.e., unified with) multiple terms, as 41 | * long as no two of those terms are constants. 42 | */ 43 | public interface TermUnifier extends Substitution { 44 | /** 45 | * Attempts to unify a variable with a term. Returns a boolean representing whether the 46 | * unification was successful. Unification fails if it would lead to a variable being unified with 47 | * two distinct constants. 48 | * 49 | * @param u the variable 50 | * @param v the term 51 | * @return whether the unification was successful 52 | */ 53 | public boolean unify(Variable u, Term v); 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/edu/harvard/seas/pl/abcdatalog/util/substitution/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * This package contains classes that represents substitutions (i.e., mappings from variables to 3 | * terms). 4 | */ 5 | package edu.harvard.seas.pl.abcdatalog.util.substitution; 6 | 7 | /*- 8 | * #%L 9 | * AbcDatalog 10 | * %% 11 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 12 | * %% 13 | * Redistribution and use in source and binary forms, with or without modification, 14 | * are permitted provided that the following conditions are met: 15 | * 16 | * 1. Redistributions of source code must retain the above copyright notice, this 17 | * list of conditions and the following disclaimer. 18 | * 19 | * 2. Redistributions in binary form must reproduce the above copyright notice, 20 | * this list of conditions and the following disclaimer in the documentation 21 | * and/or other materials provided with the distribution. 22 | * 23 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 24 | * may be used to endorse or promote products derived from this software without 25 | * specific prior written permission. 26 | * 27 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 28 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 29 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 30 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 31 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 32 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 33 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 34 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 35 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 36 | * OF THE POSSIBILITY OF SUCH DAMAGE. 37 | * #L% 38 | */ 39 | -------------------------------------------------------------------------------- /src/test/java/edu/harvard/seas/pl/abcdatalog/engine/ConcurrentBottomUpEngineTest.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.engine; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.engine.bottomup.concurrent.ConcurrentBottomUpEngine; 37 | import org.junit.runner.RunWith; 38 | import org.junit.runners.Suite; 39 | 40 | @RunWith(Suite.class) 41 | @Suite.SuiteClasses({ 42 | ConcurrentBottomUpEngineTest.MyCoreTests.class, 43 | ConcurrentBottomUpEngineTest.MyUnificationTests.class, 44 | ConcurrentBottomUpEngineTest.MyConjunctiveQueryTests.class 45 | }) 46 | public class ConcurrentBottomUpEngineTest { 47 | public static class MyCoreTests extends CoreTests { 48 | 49 | public MyCoreTests() { 50 | super(ConcurrentBottomUpEngine::new); 51 | } 52 | } 53 | 54 | public static class MyUnificationTests extends ExplicitUnificationTests { 55 | 56 | public MyUnificationTests() { 57 | super(ConcurrentBottomUpEngine::new); 58 | } 59 | } 60 | 61 | public static class MyConjunctiveQueryTests extends ConjunctiveQueryTests { 62 | 63 | public MyConjunctiveQueryTests() { 64 | super(ConcurrentBottomUpEngine::new); 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/test/java/edu/harvard/seas/pl/abcdatalog/engine/ConcurrentChunkedBottomUpEngineTest.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.engine; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.engine.bottomup.concurrent.ConcurrentChunkedBottomUpEngine; 37 | import org.junit.runner.RunWith; 38 | import org.junit.runners.Suite; 39 | 40 | @RunWith(Suite.class) 41 | @Suite.SuiteClasses({ 42 | ConcurrentChunkedBottomUpEngineTest.MyCoreTests.class, 43 | ConcurrentChunkedBottomUpEngineTest.MyUnificationTests.class, 44 | }) 45 | public class ConcurrentChunkedBottomUpEngineTest { 46 | 47 | public static class MyCoreTests extends CoreTests { 48 | 49 | public MyCoreTests() { 50 | super(() -> new ConcurrentChunkedBottomUpEngine(4)); 51 | } 52 | } 53 | 54 | public static class MyUnificationTests extends ExplicitUnificationTests { 55 | 56 | public MyUnificationTests() { 57 | super(() -> new ConcurrentChunkedBottomUpEngine(4)); 58 | } 59 | } 60 | 61 | public static class MyConjunctiveQueryTests extends ConjunctiveQueryTests { 62 | 63 | public MyConjunctiveQueryTests() { 64 | super(() -> new ConcurrentChunkedBottomUpEngine(4)); 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/test/java/edu/harvard/seas/pl/abcdatalog/engine/ConcurrentStratifiedNegationBottomUpEngineTest.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.engine; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.engine.bottomup.concurrent.ConcurrentStratifiedNegationBottomUpEngine; 37 | import org.junit.runner.RunWith; 38 | import org.junit.runners.Suite; 39 | 40 | @RunWith(Suite.class) 41 | @Suite.SuiteClasses({ 42 | ConcurrentStratifiedNegationBottomUpEngineTest.MyCoreTests.class, 43 | ConcurrentStratifiedNegationBottomUpEngineTest.MyUnificationTests.class, 44 | ConcurrentStratifiedNegationBottomUpEngineTest.MyNegationTests.class 45 | }) 46 | public class ConcurrentStratifiedNegationBottomUpEngineTest { 47 | public static class MyCoreTests extends CoreTests { 48 | 49 | public MyCoreTests() { 50 | super(ConcurrentStratifiedNegationBottomUpEngine::new); 51 | } 52 | } 53 | 54 | public static class MyUnificationTests extends ExplicitUnificationTests { 55 | 56 | public MyUnificationTests() { 57 | super(ConcurrentStratifiedNegationBottomUpEngine::new); 58 | } 59 | } 60 | 61 | public static class MyNegationTests extends StratifiedNegationTests { 62 | 63 | public MyNegationTests() { 64 | super(ConcurrentStratifiedNegationBottomUpEngine::new); 65 | } 66 | } 67 | 68 | public static class MyConjunctiveQueryTests extends ConjunctiveQueryTests { 69 | 70 | public MyConjunctiveQueryTests() { 71 | super(ConcurrentStratifiedNegationBottomUpEngine::new); 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/test/java/edu/harvard/seas/pl/abcdatalog/engine/IterativeQsqEngineTest.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.engine; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.engine.topdown.IterativeQsqEngine; 37 | import org.junit.runner.RunWith; 38 | import org.junit.runners.Suite; 39 | 40 | @RunWith(Suite.class) 41 | @Suite.SuiteClasses({ 42 | IterativeQsqEngineTest.MyCoreTests.class, 43 | IterativeQsqEngineTest.MyConjunctiveQueryTests.class 44 | }) 45 | public class IterativeQsqEngineTest { 46 | public static class MyCoreTests extends CoreTests { 47 | 48 | public MyCoreTests() { 49 | super(IterativeQsqEngine::new); 50 | } 51 | } 52 | 53 | public static class MyConjunctiveQueryTests extends ConjunctiveQueryTests { 54 | 55 | public MyConjunctiveQueryTests() { 56 | super(IterativeQsqEngine::new); 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/test/java/edu/harvard/seas/pl/abcdatalog/engine/MstEngineTest.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.engine; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.engine.topdown.MstEngine; 37 | import org.junit.runner.RunWith; 38 | import org.junit.runners.Suite; 39 | 40 | @RunWith(Suite.class) 41 | @Suite.SuiteClasses({MstEngineTest.MyCoreTests.class, MstEngineTest.MyConjunctiveQueryTests.class}) 42 | public class MstEngineTest { 43 | public static class MyCoreTests extends CoreTests { 44 | 45 | public MyCoreTests() { 46 | super(MstEngine::new); 47 | } 48 | } 49 | 50 | public static class MyConjunctiveQueryTests extends ConjunctiveQueryTests { 51 | 52 | public MyConjunctiveQueryTests() { 53 | super(MstEngine::new); 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/test/java/edu/harvard/seas/pl/abcdatalog/engine/RecursiveQsqEngineTest.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.engine; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.engine.topdown.RecursiveQsqEngine; 37 | import org.junit.runner.RunWith; 38 | import org.junit.runners.Suite; 39 | 40 | @RunWith(Suite.class) 41 | @Suite.SuiteClasses({ 42 | RecursiveQsqEngineTest.MyCoreTests.class, 43 | RecursiveQsqEngineTest.MyConjunctiveQueryTests.class 44 | }) 45 | public class RecursiveQsqEngineTest { 46 | public static class MyCoreTests extends CoreTests { 47 | 48 | public MyCoreTests() { 49 | super(RecursiveQsqEngine::new); 50 | } 51 | } 52 | 53 | public static class MyConjunctiveQueryTests extends ConjunctiveQueryTests { 54 | 55 | public MyConjunctiveQueryTests() { 56 | super(RecursiveQsqEngine::new); 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/test/java/edu/harvard/seas/pl/abcdatalog/engine/SemiNaiveEngineTest.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.engine; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2021 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.engine.bottomup.sequential.SemiNaiveEngine; 37 | import org.junit.runner.RunWith; 38 | import org.junit.runners.Suite; 39 | 40 | @RunWith(Suite.class) 41 | @Suite.SuiteClasses({ 42 | SemiNaiveEngineTest.MyCoreTests.class, 43 | SemiNaiveEngineTest.MyUnificationTests.class, 44 | SemiNaiveEngineTest.MyNegationTests.class, 45 | SemiNaiveEngineTest.MyConjunctiveQueryTests.class 46 | }) 47 | public class SemiNaiveEngineTest { 48 | public static class MyCoreTests extends CoreTests { 49 | 50 | public MyCoreTests() { 51 | super(() -> new SemiNaiveEngine(true)); 52 | } 53 | } 54 | 55 | public static class MyUnificationTests extends ExplicitUnificationTests { 56 | 57 | public MyUnificationTests() { 58 | super(() -> new SemiNaiveEngine(true)); 59 | } 60 | } 61 | 62 | public static class MyNegationTests extends StratifiedNegationTests { 63 | 64 | public MyNegationTests() { 65 | super(() -> new SemiNaiveEngine(true)); 66 | } 67 | } 68 | 69 | public static class MyConjunctiveQueryTests extends ConjunctiveQueryTests { 70 | 71 | public MyConjunctiveQueryTests() { 72 | super(() -> new SemiNaiveEngine(true)); 73 | } 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/test/java/edu/harvard/seas/pl/abcdatalog/util/datastructures/FactIndexerTest.java: -------------------------------------------------------------------------------- 1 | package edu.harvard.seas.pl.abcdatalog.util.datastructures; 2 | 3 | /*- 4 | * #%L 5 | * AbcDatalog 6 | * %% 7 | * Copyright (C) 2016 - 2024 President and Fellows of Harvard College 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the President and Fellows of Harvard College nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import edu.harvard.seas.pl.abcdatalog.ast.*; 37 | import edu.harvard.seas.pl.abcdatalog.engine.AbstractTests; 38 | import java.util.function.Supplier; 39 | import org.junit.Assert; 40 | import org.junit.Test; 41 | import org.junit.runner.RunWith; 42 | import org.junit.runners.Suite; 43 | 44 | @RunWith(Suite.class) 45 | @Suite.SuiteClasses({ 46 | FactIndexerTest.SetTests.class, 47 | FactIndexerTest.ConcurrentLinkedBagTests.class 48 | }) 49 | public class FactIndexerTest { 50 | public static class SetTests extends AbstractFactIndexerTests { 51 | public SetTests() { 52 | super(FactIndexerFactory::createConcurrentSetFactIndexer); 53 | } 54 | } 55 | 56 | public static class ConcurrentLinkedBagTests extends AbstractFactIndexerTests { 57 | public ConcurrentLinkedBagTests() { 58 | super(FactIndexerFactory::createConcurrentQueueFactIndexer); 59 | } 60 | } 61 | 62 | public abstract static class AbstractFactIndexerTests extends AbstractTests { 63 | private final Supplier factIndexerFactory; 64 | 65 | public AbstractFactIndexerTests(Supplier factIndexerFactory) { 66 | super( 67 | () -> { 68 | throw new Error("Tests do not use engine="); 69 | }); 70 | this.factIndexerFactory = factIndexerFactory; 71 | } 72 | 73 | @Test 74 | public void testSmallestFactSetIsReturnedFromFineIndex() { 75 | FactIndexer indexer = factIndexerFactory.get(); 76 | indexer.addAll(parseFacts("f(a,x,b). f(b,x,b). f(c,x1,b). f(c,x2,b). f(c,x,d).")); 77 | 78 | Iterable result = indexer.indexInto(parseQuery("f(c,_,d)?")); 79 | int size = 0; 80 | for (PositiveAtom ignored : result) { 81 | ++size; 82 | } 83 | Assert.assertEquals(1, size); 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /third-party-licenses/THIRD-PARTY.txt: -------------------------------------------------------------------------------- 1 | 2 | Lists of 3 third-party dependencies. 3 | (Eclipse Public License 1.0) JUnit (junit:junit:4.13.2 - http://junit.org) 4 | (New BSD License) Hamcrest Core (org.hamcrest:hamcrest-core:1.3 - https://github.com/hamcrest/JavaHamcrest/hamcrest-core) 5 | (Apache License 2.0) FlatLaf (FlatLaf:3.1.1 - https://github.com/JFormDesigner/FlatLaf) --------------------------------------------------------------------------------