Skip to content

Commit

Permalink
Improve the readability of code that uses PreferenceGraph.
Browse files Browse the repository at this point in the history
  • Loading branch information
enasca committed Feb 6, 2017
1 parent 7d954dc commit 252bac7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
34 changes: 15 additions & 19 deletions src/main/java/it/poliba/enasca/ontocpnets/OntologicalCPNet.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package it.poliba.enasca.ontocpnets;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableTable;
import com.google.common.collect.Sets;
Expand Down Expand Up @@ -72,12 +71,12 @@ private OntologicalCPNet(Builder builder) throws OWLOntologyCreationException {
closure = new Lazy<>(this::computeClosure);
// Build a mapping between domain values and their OWL representations.
OWLDataFactory dataFactory = builder.baseOntology.getOWLOntologyManager().getOWLDataFactory();
Map<String, OWLClass> owlDomainValues = builder.domainValues.stream()
Map<String, OWLClass> owlDomainValues = graph.domainValues()
.collect(Collectors.toMap(
Function.identity(),
domainValue -> dataFactory.getOWLClass(domainTable.getIRI(domainValue))));
// Build the new class definition axioms.
Stream<OWLEquivalentClassesAxiom> classDefinitions = builder.domainValues.stream()
Stream<OWLEquivalentClassesAxiom> classDefinitions = graph.domainValues()
.map(domainValue -> dataFactory.getOWLEquivalentClassesAxiom(
owlDomainValues.get(domainValue),
builder.definitions.get(domainValue)));
Expand Down Expand Up @@ -261,23 +260,20 @@ private Outcome interpretModel(DimacsLiterals model) {
.filter(dimacsLiteral -> dimacsLiteral > 0)
.mapToObj(domainTable::fromPositiveLiteral)
.collect(Collectors.toSet());
// Verify that the model contains exactly one domain value per preference variable.
if (graph.size() != domainValuesInModel.size()) {
throw new IllegalStateException(
String.format("incorrect model size: expected %d, got %d",
graph.size(),
domainValuesInModel.size()));
}
// Map each preference variable to the corresponding domain value in the model.
Map<String, String> assignments = graph.domainMap().entrySet().stream()
.collect(Collectors.toMap(
Map.Entry::getKey,
entry -> {
Set<String> matchingDomainValues = entry.getValue().stream()
.filter(domainValuesInModel::contains)
.collect(Collectors.toSet());
if (matchingDomainValues.isEmpty()) {
throw new IllegalStateException(
String.format("no assignment for variable '%s'", entry.getKey()));
}
if (matchingDomainValues.size() > 1) {
throw new IllegalStateException(
String.format("conflicting assignments for variable '%s'", entry.getKey()));
}
return matchingDomainValues.iterator().next();
}
entry -> entry.getValue().stream()
.filter(domainValuesInModel::contains)
.findAny().get()
));
try {
return new Outcome(assignments);
Expand Down Expand Up @@ -346,7 +342,7 @@ public OntologicalConstraints build() {
/**
* An equivalency table that stores different representations of user preferences.
*/
public static class Table implements ModelConverter {
public class Table implements ModelConverter {

/**
* Contains equivalent representations of user preferences.
Expand Down Expand Up @@ -390,7 +386,7 @@ private Table(Builder builder) {
.filter(iri -> !existingIRIs.contains(iri))
.findFirst().orElseThrow(() -> new IllegalStateException(
String.format("Unable to generate a unique IRI for domain value '%s'", str)));
ImmutableList<String> domainList = ImmutableSet.copyOf(builder.domainValues).asList();
List<String> domainList = graph.domainValues().collect(Collectors.toList());
ImmutableTable.Builder<String, Integer, IRI> tableBuilder = ImmutableTable.builder();
IntStream.range(0, domainList.size()).forEachOrdered(index -> {
String domainValue = domainList.get(index);
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/it/poliba/enasca/ontocpnets/PreferenceGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class PreferenceGraph {
Map<String, Node> nodeMap;

private PreferenceGraph(Map<String, Node> nodeMap) {
this.nodeMap = Objects.requireNonNull(nodeMap);
this.nodeMap = nodeMap;
}

/**
Expand Down Expand Up @@ -63,6 +63,14 @@ public Set<OptimalityConstraint> getOptimalityConstraints() {
.collect(Collectors.toSet());
}

/**
* Return the number of preference variables (nodes) in the graph.
* @return
*/
public int size() {
return nodeMap.size();
}

public static Builder builder() {
return new Builder();
}
Expand Down

0 comments on commit 252bac7

Please sign in to comment.