From c0d0b62857e4a0b46d7cff842889dd707e68019d Mon Sep 17 00:00:00 2001 From: Julian Mendez Date: Sun, 13 Dec 2015 21:15:46 +0100 Subject: [PATCH] Reduce execution time of basic completion rules --- .../jcel/core/completion/basic/CR1SRule.java | 8 +- .../jcel/core/completion/basic/CR2SRule.java | 10 +- .../jcel/core/completion/basic/CR3SRule.java | 8 +- .../jcel/core/completion/basic/CR4RRule.java | 14 +-- .../jcel/core/completion/basic/CR4SRule.java | 18 ++-- .../jcel/core/completion/basic/CR5RRule.java | 9 +- .../jcel/core/completion/basic/CR6RRule.java | 26 ++--- .../core/completion/basic/CR6RTrRule.java | 20 ++-- .../jcel/core/completion/basic/CR7SRule.java | 15 ++- .../basic/CompletionRuleMonitor.java | 96 +++++++++++++++++++ 10 files changed, 160 insertions(+), 64 deletions(-) create mode 100644 jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/basic/CompletionRuleMonitor.java diff --git a/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/basic/CR1SRule.java b/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/basic/CR1SRule.java index f7a06e47..6cc4f9e0 100644 --- a/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/basic/CR1SRule.java +++ b/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/basic/CR1SRule.java @@ -86,12 +86,14 @@ public boolean apply(ClassifierStatus status, int subClass, int superClass) { } private boolean applyRule(ClassifierStatus status, int x, int a) { - return status.getExtendedOntology().getGCI0Axioms(a).stream().map(axiom -> { + CompletionRuleMonitor ret = new CompletionRuleMonitor(); + status.getExtendedOntology().getGCI0Axioms(a).forEach(axiom -> { int b = axiom.getSuperClass(); - return status.addNewSEntry(x, b); + ret.or(status.addNewSEntry(x, b)); - }).reduce(false, (accum, elem) -> (accum || elem)); + }); + return ret.get(); } @Override diff --git a/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/basic/CR2SRule.java b/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/basic/CR2SRule.java index bbcbc9ad..dbf51b6b 100644 --- a/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/basic/CR2SRule.java +++ b/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/basic/CR2SRule.java @@ -98,8 +98,9 @@ public boolean apply(ClassifierStatus status, int subClass, int superClass) { } private boolean applyRule(ClassifierStatus status, int x, int a) { + CompletionRuleMonitor ret = new CompletionRuleMonitor(); Collection subsumersOfX = status.getSubsumers(x); - return status.getExtendedOntology().getGCI1Axioms(a).stream().map(axiom -> { + status.getExtendedOntology().getGCI1Axioms(a).forEach(axiom -> { boolean valid = true; @@ -111,12 +112,11 @@ private boolean applyRule(ClassifierStatus status, int x, int a) { if (valid) { int b = axiom.getSuperClass(); - return status.addNewSEntry(x, b); - } else { - return false; + ret.or(status.addNewSEntry(x, b)); } - }).reduce(false, (accum, elem) -> (accum || elem)); + }); + return ret.get(); } @Override diff --git a/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/basic/CR3SRule.java b/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/basic/CR3SRule.java index c8de603a..5d01922b 100644 --- a/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/basic/CR3SRule.java +++ b/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/basic/CR3SRule.java @@ -85,13 +85,15 @@ public boolean apply(ClassifierStatus status, int subClass, int superClass) { } private boolean applyRule(ClassifierStatus status, int x, int a) { - return status.getExtendedOntology().getGCI2Axioms(a).stream().map(axiom -> { + CompletionRuleMonitor ret = new CompletionRuleMonitor(); + status.getExtendedOntology().getGCI2Axioms(a).forEach(axiom -> { int r = axiom.getPropertyInSuperClass(); int b = axiom.getClassInSuperClass(); - return status.addNewREntry(r, x, b); + ret.or(status.addNewREntry(r, x, b)); - }).reduce(false, (accum, elem) -> (accum || elem)); + }); + return ret.get(); } @Override diff --git a/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/basic/CR4RRule.java b/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/basic/CR4RRule.java index 4799fd74..dcdb5ce3 100644 --- a/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/basic/CR4RRule.java +++ b/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/basic/CR4RRule.java @@ -85,17 +85,17 @@ public boolean apply(ClassifierStatus status, int property, int leftClass, int r } private boolean applyRule(ClassifierStatus status, int r, int x, int y) { - return status.getSubsumers(y).stream().map(a -> + CompletionRuleMonitor ret = new CompletionRuleMonitor(); - status.getExtendedOntology().getGCI3rAAxioms(r, a).stream().map(axiom -> { + status.getSubsumers(y).forEach(a -> - int b = axiom.getSuperClass(); - return status.addNewSEntry(x, b); - - }).reduce(false, (accum, elem) -> (accum || elem))) + status.getExtendedOntology().getGCI3rAAxioms(r, a).forEach(axiom -> { - .reduce(false, (accum, elem) -> (accum || elem)); + int b = axiom.getSuperClass(); + ret.or(status.addNewSEntry(x, b)); + })); + return ret.get(); } @Override diff --git a/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/basic/CR4SRule.java b/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/basic/CR4SRule.java index 7758832a..27a20cfd 100644 --- a/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/basic/CR4SRule.java +++ b/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/basic/CR4SRule.java @@ -85,21 +85,19 @@ public boolean apply(ClassifierStatus status, int subClass, int superClass) { } private boolean applyRule(ClassifierStatus status, int y, int a) { - return status.getObjectPropertiesBySecond(y).stream().map(r -> + CompletionRuleMonitor ret = new CompletionRuleMonitor(); - status.getExtendedOntology().getGCI3rAAxioms(r, a).stream().map(axiom -> + status.getObjectPropertiesBySecond(y).forEach(r -> - status.getFirstBySecond(r, y).stream().map(x -> { + status.getExtendedOntology().getGCI3rAAxioms(r, a).forEach(axiom -> - int b = axiom.getSuperClass(); - return status.addNewSEntry(x, b); - - }).reduce(false, (accum, elem) -> (accum || elem))) + status.getFirstBySecond(r, y).forEach(x -> { - .reduce(false, (accum, elem) -> (accum || elem))) - - .reduce(false, (accum, elem) -> (accum || elem)); + int b = axiom.getSuperClass(); + ret.or(status.addNewSEntry(x, b)); + }))); + return ret.get(); } @Override diff --git a/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/basic/CR5RRule.java b/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/basic/CR5RRule.java index 90ceb11d..1cea0489 100644 --- a/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/basic/CR5RRule.java +++ b/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/basic/CR5RRule.java @@ -85,12 +85,15 @@ public boolean apply(ClassifierStatus status, int property, int leftClass, int r } private boolean applyRule(ClassifierStatus status, int r, int x, int y) { - return status.getExtendedOntology().getRI2rAxioms(r).stream().map(axiom -> { + CompletionRuleMonitor ret = new CompletionRuleMonitor(); + + status.getExtendedOntology().getRI2rAxioms(r).forEach(axiom -> { int s = axiom.getSuperProperty(); - return status.addNewREntry(s, x, y); + ret.or(status.addNewREntry(s, x, y)); - }).reduce(false, (accum, elem) -> (accum || elem)); + }); + return ret.get(); } @Override diff --git a/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/basic/CR6RRule.java b/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/basic/CR6RRule.java index 6925a5fc..17a7e5c0 100644 --- a/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/basic/CR6RRule.java +++ b/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/basic/CR6RRule.java @@ -88,33 +88,35 @@ public boolean apply(ClassifierStatus status, int property, int leftClass, int r } private boolean apply1(ClassifierStatus status, int r, int x, int y) { - return status.getExtendedOntology().getRI3AxiomsByLeft(r).stream().map(axiom -> { + CompletionRuleMonitor ret = new CompletionRuleMonitor(); + + status.getExtendedOntology().getRI3AxiomsByLeft(r).forEach(axiom -> { int s = axiom.getRightSubProperty(); int t = axiom.getSuperProperty(); - return status.getSecondByFirst(s, y).stream().map(z -> - - status.addNewREntry(t, x, z) + status.getSecondByFirst(s, y).forEach(z -> - ).reduce(false, (accum, elem) -> (accum || elem)); + ret.or(status.addNewREntry(t, x, z))); - }).reduce(false, (accum, elem) -> (accum || elem)); + }); + return ret.get(); } private boolean apply2(ClassifierStatus status, int s, int y, int z) { - return status.getExtendedOntology().getRI3AxiomsByRight(s).stream().map(axiom -> { + CompletionRuleMonitor ret = new CompletionRuleMonitor(); + + status.getExtendedOntology().getRI3AxiomsByRight(s).forEach(axiom -> { int r = axiom.getLeftSubProperty(); int t = axiom.getSuperProperty(); - return status.getFirstBySecond(r, y).stream().map(x -> - - status.addNewREntry(t, x, z) + status.getFirstBySecond(r, y).forEach(x -> - ).reduce(false, (accum, elem) -> (accum || elem)); + ret.or(status.addNewREntry(t, x, z))); - }).reduce(false, (accum, elem) -> (accum || elem)); + }); + return ret.get(); } @Override diff --git a/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/basic/CR6RTrRule.java b/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/basic/CR6RTrRule.java index 033660fb..ce671cae 100644 --- a/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/basic/CR6RTrRule.java +++ b/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/basic/CR6RTrRule.java @@ -96,29 +96,25 @@ public boolean apply(ClassifierStatus status, int property, int leftClass, int r } private boolean apply1(ClassifierStatus status, int r, int x, int y) { - if (status.getExtendedOntology().getTransitiveObjectProperties().contains(r)) { - return status.getSecondByFirst(r, y).stream().map(z -> + CompletionRuleMonitor ret = new CompletionRuleMonitor(); - status.addNewREntry(r, x, z) + if (status.getExtendedOntology().getTransitiveObjectProperties().contains(r)) { - ).reduce(false, (accum, elem) -> (accum || elem)); + status.getSecondByFirst(r, y).forEach(z -> ret.or(status.addNewREntry(r, x, z))); - } else { - return false; } + return ret.get(); } private boolean apply2(ClassifierStatus status, int r, int y, int z) { - if (status.getExtendedOntology().getTransitiveObjectProperties().contains(r)) { - return status.getFirstBySecond(r, y).stream().map(x -> + CompletionRuleMonitor ret = new CompletionRuleMonitor(); - status.addNewREntry(r, x, z) + if (status.getExtendedOntology().getTransitiveObjectProperties().contains(r)) { - ).reduce(false, (accum, elem) -> (accum || elem)); + status.getFirstBySecond(r, y).forEach(x -> ret.or(status.addNewREntry(r, x, z))); - } else { - return false; } + return ret.get(); } @Override diff --git a/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/basic/CR7SRule.java b/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/basic/CR7SRule.java index 7a381c38..b9b5ad37 100644 --- a/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/basic/CR7SRule.java +++ b/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/basic/CR7SRule.java @@ -85,21 +85,18 @@ public boolean apply(ClassifierStatus status, int subClass, int superClass) { } private boolean applyRule(ClassifierStatus status, int y, int a) { - if (a == IntegerEntityManager.bottomClassId) { - - return status.getObjectPropertiesBySecond(y).stream().map(r -> + CompletionRuleMonitor ret = new CompletionRuleMonitor(); - status.getFirstBySecond(r, y).stream().map(x -> + if (a == IntegerEntityManager.bottomClassId) { - status.addNewSEntry(x, IntegerEntityManager.bottomClassId) + status.getObjectPropertiesBySecond(y).forEach(r -> - ).reduce(false, (accum, elem) -> (accum || elem)) + status.getFirstBySecond(r, y).forEach(x -> - ).reduce(false, (accum, elem) -> (accum || elem)); + ret.or(status.addNewSEntry(x, IntegerEntityManager.bottomClassId)))); - } else { - return false; } + return ret.get(); } @Override diff --git a/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/basic/CompletionRuleMonitor.java b/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/basic/CompletionRuleMonitor.java new file mode 100644 index 00000000..8f8d49b5 --- /dev/null +++ b/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/basic/CompletionRuleMonitor.java @@ -0,0 +1,96 @@ +/* + * + * Copyright (C) 2009-2015 Julian Mendez + * + * + * This file is part of jcel. + * + * + * The contents of this file are subject to the GNU Lesser General Public License + * version 3 + * + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * + * Alternatively, the contents of this file may be used under the terms + * of the Apache License, Version 2.0, in which case the + * provisions of the Apache License, Version 2.0 are applicable instead of those + * above. + * + * + * 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. + * + */ + +package de.tudresden.inf.lat.jcel.core.completion.basic; + +/** + * An object of this class keeps track whether a completion rule has been + * applied. + * + * @author Julian Mendez + * + */ +public class CompletionRuleMonitor { + + private Object monitor = new Object(); + private boolean state = false; + + public CompletionRuleMonitor() { + } + + public boolean get() { + return this.state; + } + + public synchronized void or(boolean b) { + synchronized (this.monitor) { + this.state |= b; + } + } + + @Override + public int hashCode() { + return this.state ? 1 : 0; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } else if (!(obj instanceof CompletionRuleMonitor)) { + return false; + } else { + CompletionRuleMonitor other = (CompletionRuleMonitor) obj; + return this.state == other.state; + } + } + + @Override + public String toString() { + return "" + this.state; + } + +}