Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disjunctions in Rules #192

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -21,22 +21,29 @@
*/

import java.util.List;
import java.util.Arrays;

/**
* Interface for representing conjunctions of {@link Literal}s, i.e., lists of
* (negated or positive) atomic formulas that are connected with logical AND.
* Conjunctions may have free variables, since they contain no quantifiers.
*
*
* @author Markus Krötzsch
* @author Lukas Gerlach
*
*/
public interface Conjunction<T extends Literal> extends Iterable<T>, SyntaxObject {
public interface Conjunction<T extends Literal> extends Disjunction<Conjunction<T>>, Iterable<T> {

/**
* Returns the list of literals that are part of this conjunction.
*
*
* @return list of literals
*/
List<T> getLiterals();

@Override
default List<Conjunction<T>> getConjunctions() {
return Arrays.asList(this);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.semanticweb.rulewerk.core.model.api;

/*-
* #%L
* Rulewerk Core Components
* %%
* Copyright (C) 2018 - 2020 Rulewerk Developers
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/

import java.util.List;

/**
* Interface for representing disjunctions of {@link Conjunction}s.
*
* @author Lukas Gerlach
*
*/
public interface Disjunction<T extends Conjunction<?>> extends SyntaxObject {

/**
* Returns the list of conjunctions that are part of this disjunction.
*
* @return list of conjunctions
*/
List<T> getConjunctions();

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -21,6 +21,8 @@
*/

import java.util.List;
import java.util.Arrays;
import java.util.Iterator;

/**
* Interface for literals. A positive literal is simply an atomic formula, i.e.,
Expand All @@ -30,14 +32,26 @@
*
* @author [email protected]
* @author Irina Dragoste
* @author Lukas Gerlach
*/
public interface Literal extends SyntaxObject {

// this would also be possible (including other changes)
// but it is not ideal since PositiveLiteral now also extends Conjunction<Literal>
// while it should really extend Conjunction<PositiveLiteral> (but maybe this is not too much of an issue...)
//
// public interface Literal extends Conjunction<Literal> {

// this produces a so called RawType when used without type argument, which seems to be generally considered
// bad practice because some type checking is not done for such types
// maybe this would not be an issue for us...
// what we actually would need is a self referential type that we can pass to Conjunction...
public interface Literal<T extends Literal> extends Conjunction<T> {

boolean isNegated();

/**
* The literal predicate.
*
*
* @return the literal predicate.
*/
Predicate getPredicate();
Expand All @@ -50,4 +64,9 @@ public interface Literal extends SyntaxObject {
*/
List<Term> getArguments();

@Override
default Iterator<T> iterator() {
return getLiterals().iterator();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -20,11 +20,18 @@
* #L%
*/

public interface NegativeLiteral extends Literal {
import java.util.List;
import java.util.Arrays;

public interface NegativeLiteral extends Literal<NegativeLiteral> {

@Override
default boolean isNegated() {
return true;
}

@Override
default List<NegativeLiteral> getLiterals() {
return Arrays.asList(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -20,10 +20,18 @@
* #L%
*/

public interface PositiveLiteral extends Literal {
import java.util.List;
import java.util.Arrays;

public interface PositiveLiteral extends Literal<PositiveLiteral> {

@Override
default boolean isNegated() {
return false;
}

@Override
default List<PositiveLiteral> getLiterals() {
return Arrays.asList(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -26,24 +26,25 @@
* specifying quantifiers. All variables in the body are considered universally
* quantified; all variables in the head that do not occur in the body are
* considered existentially quantified.
*
*
* @author Markus Krötzsch
* @author Lukas Gerlach
*
*/
public interface Rule extends SyntaxObject, Statement {

/**
* Returns the conjunction of head literals (the consequence of the rule).
* Returns the disjunction of conjunctions of head literals (the consequence of the rule).
*
* @return conjunction of literals
* @return disjunction of conjunctions of positive literals
*/
Conjunction<PositiveLiteral> getHead();
Disjunction<Conjunction<PositiveLiteral>> getHead();

/**
* Returns the conjunction of body literals (the premise of the rule).
* Returns the disjunction of conjunctions of body literals (the premise of the rule).
*
* @return conjunction of literals
* @return disjunction of conjunctions of literals
*/
Conjunction<Literal> getBody();
Disjunction<Conjunction<Literal>> getBody();

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -26,6 +26,7 @@
import java.util.stream.Stream;

import org.apache.commons.lang3.Validate;
import org.semanticweb.rulewerk.core.model.api.Disjunction;
import org.semanticweb.rulewerk.core.model.api.Literal;
import org.semanticweb.rulewerk.core.model.api.Predicate;
import org.semanticweb.rulewerk.core.model.api.Term;
Expand All @@ -38,8 +39,9 @@
*
* @author [email protected]
* @author Markus Krötzsch
* @author Lukas Gerlach
*/
public abstract class AbstractLiteralImpl implements Literal {
public abstract class AbstractLiteralImpl<T extends Literal> implements Literal<T> {

private final Predicate predicate;
private final List<Term> terms;
Expand Down Expand Up @@ -82,9 +84,12 @@ public boolean equals(final Object obj) {
if (obj == null) {
return false;
}
if (!(obj instanceof Literal)) {
if (!(obj instanceof Disjunction<?>)) {
return false;
}
if (!(obj instanceof Literal)) {
return obj.equals(this);
}
final Literal other = (Literal) obj;

return this.isNegated() == other.isNegated() && this.getPredicate().equals(other.getPredicate())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -27,12 +27,13 @@

import org.apache.commons.lang3.Validate;
import org.semanticweb.rulewerk.core.model.api.Conjunction;
import org.semanticweb.rulewerk.core.model.api.Disjunction;
import org.semanticweb.rulewerk.core.model.api.Literal;
import org.semanticweb.rulewerk.core.model.api.Term;

/**
* Simple implementation of {@link Conjunction}.
*
*
* @author Markus Krötzsch
*/
public class ConjunctionImpl<T extends Literal> implements Conjunction<T> {
Expand All @@ -41,7 +42,7 @@ public class ConjunctionImpl<T extends Literal> implements Conjunction<T> {

/**
* Constructor.
*
*
* @param literals a non-null list of literals, that cannot contain null
* elements.
*/
Expand All @@ -62,7 +63,9 @@ public Stream<Term> getTerms() {

@Override
public int hashCode() {
return this.literals.hashCode();
return this.literals.size() == 1
? this.literals.get(0).hashCode()
: this.literals.hashCode();
}

@Override
Expand All @@ -73,9 +76,12 @@ public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (!(obj instanceof Conjunction<?>)) {
if (!(obj instanceof Disjunction<?>)) {
return false;
}
if (!(obj instanceof Conjunction<?>)) {
return obj.equals(this);
}
final Conjunction<?> other = (Conjunction<?>) obj;
return this.literals.equals(other.getLiterals());
}
Expand Down
Loading