Mentions légales du service

Skip to content
Snippets Groups Projects
Commit 1958d44e authored by Guillaume Kihli's avatar Guillaume Kihli
Browse files

Using soft references and weak hash maps in the SameObjectTermFactory so that...

Using soft references and weak hash maps in the SameObjectTermFactory so that the memory can be freed when there is not enough memory (the soft references guarantee that the terms will remain in the factory as long as there is enough memory for the program, otherwise the garbage collector can remove unused terms to free memory)
parent b258bb6c
No related branches found
No related tags found
1 merge request!64Draft: New views
Pipeline #1142329 passed
......@@ -9,9 +9,10 @@ import fr.boreal.model.logicalElements.impl.identityObjects.IdentityFreshVariabl
import fr.boreal.model.logicalElements.impl.identityObjects.IdentityLiteralImpl;
import fr.boreal.model.logicalElements.impl.identityObjects.IdentityVariableImpl;
import java.util.HashMap;
import java.lang.ref.SoftReference;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
/**
* This factory creates Terms For each call at a same method with the same
......@@ -27,10 +28,10 @@ public class SameObjectTermFactory implements TermFactory {
private static final SameObjectTermFactory INSTANCE = new SameObjectTermFactory();
private static final String FRESH_PREFIX = "Graal:EE";
private final Map<String, Constant> constants = new HashMap<>();
private final Map<Object, Literal<?>> literals = new HashMap<>();
private final Map<String, Variable> variables = new HashMap<>();
private final Map<String, Map<List<Term>, LogicalFunctionalTerm>> logicalFunctionalTerms = new HashMap<>();
private final Map<String, SoftReference<Constant>> constants = new WeakHashMap<>();
private final Map<Object, SoftReference<Literal<?>>> literals = new WeakHashMap<>();
private final Map<String, SoftReference<Variable>> variables = new WeakHashMap<>();
private final Map<String, Map<List<Term>, SoftReference<LogicalFunctionalTerm>>> logicalFunctionalTerms = new WeakHashMap<>();
private int fresh_counter = 0;
......@@ -51,35 +52,61 @@ public class SameObjectTermFactory implements TermFactory {
@Override
public synchronized Constant createOrGetConstant(String label) {
return this.constants.computeIfAbsent(label, IdentityConstantImpl::new);
SoftReference<Constant> ref = constants.get(label);
Constant constant = ref != null ? ref.get() : null;
if (constant == null) {
constant = new IdentityConstantImpl(label);
constants.put(label, new SoftReference<>(constant));
}
return constant;
}
@Override
public synchronized <T> Literal<T> createOrGetLiteral(T value) {
@SuppressWarnings("unchecked")
Literal<T> result = (Literal<T>) this.literals.get(value);
if (result == null) {
result = new IdentityLiteralImpl<>(value);
this.literals.put(value, result);
SoftReference<Literal<?>> ref = literals.get(value);
Literal<?> literal = ref != null ? ref.get() : null;
if (literal == null) {
Literal<T> newLiteral = new IdentityLiteralImpl<>(value);
literals.put(value, new SoftReference<>(newLiteral));
return newLiteral;
}
@SuppressWarnings("unchecked")
Literal<T> result = (Literal<T>) literal;
return result;
}
@Override
public synchronized Variable createOrGetVariable(String label) {
return this.variables.computeIfAbsent(label, IdentityVariableImpl::new);
SoftReference<Variable> ref = variables.get(label);
Variable variable = ref != null ? ref.get() : null;
if (variable == null) {
variable = new IdentityVariableImpl(label);
variables.put(label, new SoftReference<>(variable));
}
return variable;
}
@Override
public LogicalFunctionalTerm createOrGetLogicalFunctionalTerm(String name, List<Term> terms) {
return this.logicalFunctionalTerms
.computeIfAbsent(name, (n) -> new HashMap<>())
.computeIfAbsent(terms, (t) -> {
if (terms.stream().allMatch(Term::isGround)) {
return new GroundFunctionalTermImpl(name, terms) {};
}
return new SpecializableFunctionalTermImpl(name, terms) {};
});
public synchronized LogicalFunctionalTerm createOrGetLogicalFunctionalTerm(String name, List<Term> terms) {
Map<List<Term>, SoftReference<LogicalFunctionalTerm>> termMap =
logicalFunctionalTerms.computeIfAbsent(name, k -> new WeakHashMap<>());
SoftReference<LogicalFunctionalTerm> ref = termMap.get(terms);
LogicalFunctionalTerm lft = ref != null ? ref.get() : null;
if (lft == null) {
if (terms.stream().allMatch(Term::isGround)) {
lft = new GroundFunctionalTermImpl(name, terms) {};
} else {
lft = new SpecializableFunctionalTermImpl(name, terms) {};
}
termMap.put(terms, new SoftReference<>(lft));
}
return lft;
}
@Override
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment