diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java b/src/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java index 7da91d42..7762af78 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java @@ -29,8 +29,13 @@ public class OrderingUnifyPair extends Ordering> { public int compare (UnifyPair left, UnifyPair right) { if (left == null || right == null) - System.out.println("Fehler"); - return fc.compare(left.getRhsType(), right.getRhsType()); + System.out.println("Fehler"); + if (left.getLhsType() instanceof PlaceholderType) { + return fc.compare(left.getRhsType(), right.getRhsType()); + } + else { + return fc.compare(left.getLhsType(), right.getLhsType()); + } } public Pair> compare (UnifyType left, UnifyType right) { @@ -54,9 +59,21 @@ public class OrderingUnifyPair extends Ordering> { } public int compare (Set left, Set right) { - if (!true) { - Stream lseq = left.stream().filter(x -> (x.getLhsType() instanceof PlaceholderType && x.getPairOp() == PairOperator.EQUALSDOT)); - Stream rseq = right.stream().filter(x -> (x.getLhsType() instanceof PlaceholderType && x.getPairOp() == PairOperator.EQUALSDOT)); + Set lefteq = left.stream() + .filter(x -> (x.getLhsType() instanceof PlaceholderType && x.getPairOp() == PairOperator.EQUALSDOT)) + .collect(Collectors.toCollection(HashSet::new)); + Set righteq = right.stream() + .filter(x -> (x.getLhsType() instanceof PlaceholderType && x.getPairOp() == PairOperator.EQUALSDOT)) + .collect(Collectors.toCollection(HashSet::new)); + Set leftle = left.stream() + .filter(x -> (x.getLhsType() instanceof PlaceholderType && x.getPairOp() == PairOperator.SMALLERDOTWC)) + .collect(Collectors.toCollection(HashSet::new)); + Set rightle = right.stream() + .filter(x -> (x.getLhsType() instanceof PlaceholderType && x.getPairOp() == PairOperator.SMALLERDOTWC)) + .collect(Collectors.toCollection(HashSet::new)); + if (lefteq.size() > 1) { + Stream lseq = lefteq.stream(); //left.filter(x -> (x.getLhsType() instanceof PlaceholderType && x.getPairOp() == PairOperator.EQUALSDOT)); + Stream rseq = righteq.stream(); //right.filter(x -> (x.getLhsType() instanceof PlaceholderType && x.getPairOp() == PairOperator.EQUALSDOT)); BinaryOperator> combiner = (x,y) -> { x.putAll(y); return x;}; HashMap hm = rseq.reduce(new HashMap(), (x, y)-> { x.put(y.getLhsType(),y); return x; }, combiner); lseq = lseq.filter(x -> !(hm.get(x.getLhsType()) == null)); @@ -64,34 +81,44 @@ public class OrderingUnifyPair extends Ordering> { if (!si.isPresent()) return 0; else return si.get(); } - else { + if (lefteq.size() == 1 && righteq.size() == 1) { //TODO 2018-03-02: hier muessen mehree =. eingefuegt werden und alle weiteren Faelle pruefen - Optional lseq = left.stream() - .filter(x -> (x.getLhsType() instanceof PlaceholderType && x.getPairOp() == PairOperator.EQUALSDOT)).findFirst(); - Optional rseq = right.stream() - .filter(x -> (x.getLhsType() instanceof PlaceholderType && x.getPairOp() == PairOperator.EQUALSDOT)).findFirst(); - Set lsle = left.stream() + UnifyPair lseq = lefteq.iterator().next(); + UnifyPair rseq = righteq.iterator().next(); + /*Set lsle = left.stream() .filter(x -> (x.getLhsType() instanceof PlaceholderType && x.getPairOp() == PairOperator.SMALLERDOTWC)) .collect(Collectors.toCollection(HashSet::new)); Set rsle = right.stream() .filter(x -> (x.getLhsType() instanceof PlaceholderType && x.getPairOp() == PairOperator.SMALLERDOTWC)) .collect(Collectors.toCollection(HashSet::new)); - Pair> int_Unifier = compare(lseq.get().getRhsType(), rseq.get().getRhsType()); + */ + Pair> int_Unifier = compare(lseq.getRhsType(), rseq.getRhsType()); Unifier uni = new Unifier(); int_Unifier.getValue().stream().forEach(x -> uni.add((PlaceholderType) x.getLhsType(), x.getRhsType())); - if (lsle.size() == 0) return int_Unifier.getKey(); + if (leftle.size() == 0) return int_Unifier.getKey(); else { - Stream lslestr = lsle.stream().map(uni::apply); - Stream rslestr = rsle.stream().map(uni::apply); + Stream lslestr = leftle.stream().map(uni::apply); + Stream rslestr = rightle.stream().map(uni::apply); BinaryOperator> combiner = (x,y) -> { x.putAll(y); return x;}; - HashMap hm = rslestr.reduce(new HashMap(), (x, y)-> { x.put(y.getLhsType(),y); return x; }, combiner); - lseq = lseq.filter(x -> !(hm.get(x.getLhsType()) == null)); - Optional si = lslestr.map(x -> compare(x, hm.get(x.getLhsType()))).reduce((x,y)-> { if (x == y) return x; else return 0; } ); + + HashMap hm; + Optional si; + if (leftle.iterator().next().getLhsType() instanceof PlaceholderType) { + hm = rslestr.reduce(new HashMap(), (x, y)-> { x.put(y.getLhsType(),y); return x; }, combiner); + lslestr = lslestr.filter(x -> !(hm.get(x.getLhsType()) == null)); + si = lslestr.map(x -> compare(x, hm.get(x.getLhsType()))).reduce((x,y)-> { if (x == y) return x; else return 0; } ); + } + else { + hm = rslestr.reduce(new HashMap(), (x, y)-> { x.put(y.getRhsType(),y); return x; }, combiner); + rslestr = rslestr.filter(x -> !(hm.get(x.getRhsType()) == null)); + si = rslestr.map(x -> compare(x, hm.get(x.getRhsType()))).reduce((x,y)-> { if (x == y) return x; else return 0; } ); + } if (!si.isPresent()) return 0; else return si.get(); } - } + } + return 0; } }