Commit eef68b1a authored by Alexander Weigl's avatar Alexander Weigl

make MatchPath return PosInOccurrence

* but PositionTable is incompatible
parent 2bb44850
Pipeline #13112 failed with stage
in 2 minutes and 55 seconds
......@@ -2,13 +2,14 @@ package edu.kit.formal.psdb.gui.controls;
import de.uka.ilkd.key.java.Services;
import de.uka.ilkd.key.logic.NamespaceSet;
import de.uka.ilkd.key.logic.PosInOccurrence;
import de.uka.ilkd.key.logic.Sequent;
import de.uka.ilkd.key.pp.*;
import de.uka.ilkd.key.proof.Goal;
import de.uka.ilkd.key.proof.Node;
import de.uka.ilkd.key.settings.ProofIndependentSettings;
import edu.kit.formal.psdb.interpreter.KeYProofFacade;
import edu.kit.formal.psdb.termmatcher.MatchPath;
import edu.kit.formal.psdb.termmatcher.mp.MatchPath;
import edu.kit.formal.psdb.termmatcher.MatcherFacade;
import edu.kit.formal.psdb.termmatcher.Matchings;
import javafx.beans.Observable;
......@@ -21,6 +22,7 @@ import javafx.scene.paint.Color;
import org.fxmisc.richtext.CharacterHit;
import org.fxmisc.richtext.CodeArea;
import org.key_project.util.collection.ImmutableList;
import org.key_project.util.collection.ImmutableSLList;
import java.io.StringWriter;
import java.util.Collections;
......@@ -193,19 +195,22 @@ public class SequentView extends CodeArea {
if (node.get().sequent() != null) {
Matchings m = MatcherFacade.matches(pattern, node.get().sequent(), true);
if (m.size() == 0) return false;
for (Map<String, MatchPath> va : m) {
MatchPath<?> complete = va.get("?COMPLETE");
highlightTerm(complete.getPos());
Map<String, MatchPath> va = m.first();
System.out.println(va);//TODO remove
for (MatchPath mp : va.values()) {
System.out.println(mp.pio());
highlightTerm(mp.pio());
}
}
return true;
}
private void highlightTerm(ImmutableList<Integer> complete) {
private void highlightTerm(PosInOccurrence complete) {
if (backend == null) {
return;
}
Range r = backend.getInitialPositionTable().rangeForPath(complete);
ImmutableList<Integer> path = ImmutableSLList.singleton(1);
Range r = backend.getInitialPositionTable().rangeForPath(path);
setStyle(r.start(), r.end(), Collections.singleton("search-highlight"));
}
}
......@@ -122,10 +122,13 @@ public class Utils {
private Utils() {
}
public static String getRandomName(String suffx) {
return getRandomName() + suffx;
}
public static String getRandomName() {
Random r = new Random();
return (ADJECTIVES[r.nextInt(ADJECTIVES.length)] + "_" + ANIMALS[r.nextInt(ANIMALS.length)] + ".kps").toLowerCase();
return (ADJECTIVES[r.nextInt(ADJECTIVES.length)] + "_" + ANIMALS[r.nextInt(ANIMALS.length)]).toLowerCase();
}
public static void createWithFXML(Object node) {
......
package edu.kit.formal.psdb.termmatcher;
import de.uka.ilkd.key.logic.Semisequent;
import de.uka.ilkd.key.logic.Sequent;
import de.uka.ilkd.key.logic.Term;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.key_project.util.collection.ImmutableList;
import org.key_project.util.collection.ImmutableSLList;
/**
* @author Alexander Weigl
* @version 1 (24.08.17)
*/
@Data
@EqualsAndHashCode(of = {"term"})
public abstract class MatchPath<T> {
public static final int ROOT = -1;
public static final int POSITION_ANTECEDENT = -2;
public static final int POSITION_SUCCEDENT = -3;
private final MatchPath<?> parent;
private final T term;
private final int posInParent;
public MatchPath(MatchPath<?> parent, T unit, int pos) {
posInParent = pos;
term = unit;
this.parent = parent;
}
public static MatchPath<Term> createTermPath(MatchPath<Term> path, int i) {
return new MatchPathTerm(path, path.getTerm().sub(i), i);
}
public ImmutableList<Integer> getPos() {
if (parent == null) {
return ImmutableSLList.singleton(posInParent);
} else {
return parent.getPos().append(posInParent);
}
}
public static MatchPath<Term> createRoot(Term keyTerm) {
return new MatchPathTerm(null, keyTerm, -1);
}
public String toString() {
return term.toString();
}
public static MatchPathSemiSequent createSemiSequent(Sequent s, boolean antec) {
MatchPathSemiSequent mp = new MatchPathSemiSequent(
createSequent(s), antec ? s.antecedent() : s.succedent(), antec);
return mp;
}
private static MatchPathSequent createSequent(Sequent s) {
return new MatchPathSequent(s);
}
public static MatchPathSemiSequent createSuccedent(Sequent sequent) {
return createSemiSequent(sequent, false);
}
public static MatchPathSemiSequent createAntecedent(Sequent sequent) {
return createSemiSequent(sequent, true);
}
public abstract int depth();
public static class MatchPathTerm extends MatchPath<Term> {
public MatchPathTerm(MatchPath<?> parent, Term unit, int pos) {
super(parent, unit, pos);
}
@Override
public int depth() {
return getTerm().depth();
}
}
public static class MatchPathSequent extends MatchPath<Sequent> {
public MatchPathSequent(Sequent unit) {
super(null, unit, ROOT);
}
@Override
public int depth() {
return 0;
}
}
public static class MatchPathSemiSequent extends MatchPath<Semisequent> {
public MatchPathSemiSequent(MatchPathSequent parent, Semisequent unit, boolean antec) {
super(parent, unit, antec ? POSITION_ANTECEDENT : POSITION_SUCCEDENT);
}
@Override
public int depth() {
return 1;
}
}
}
......@@ -4,9 +4,12 @@ import de.uka.ilkd.key.logic.Semisequent;
import de.uka.ilkd.key.logic.Sequent;
import de.uka.ilkd.key.logic.Term;
import edu.kit.formal.psdb.termmatcher.MatchPatternParser.SemiSeqPatternContext;
import edu.kit.formal.psdb.termmatcher.mp.MatchPathFacade;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import static edu.kit.formal.psdb.termmatcher.MatchPatternParser.SequentPatternContext;
import static edu.kit.formal.psdb.termmatcher.MatchPatternParser.TermPatternContext;
......@@ -18,12 +21,14 @@ import static edu.kit.formal.psdb.termmatcher.MatchPatternParser.TermPatternCont
* @author S.Grebing
*/
public class MatcherFacade {
private static Logger logger = LogManager.getLogger(MatcherFacade.class);
public static Matchings matches(String pattern, Term keyTerm) {
MatcherImpl matcher = new MatcherImpl();
matcher.setCatchAll(false);
MatchPatternParser mpp = getParser(pattern);
TermPatternContext ctx = mpp.termPattern();
return matcher.accept(ctx, MatchPath.createRoot(keyTerm));
return matcher.accept(ctx, MatchPathFacade.createRoot(keyTerm));
}
......@@ -44,8 +49,8 @@ public class MatcherFacade {
/**
* Match a semisequent against a sequent
*
* @param pattern Semisequent pattern e.g. f(x), f(x)
* @param semiSeq Concrete KeY Semisequent
* @param pattern Semisequent pattern e.g. f(x), f(x)
* @param semiSeq Concrete KeY Semisequent
* @param catchAll
* @return Matchings
*/
......@@ -54,7 +59,7 @@ public class MatcherFacade {
SemiSeqPatternContext ctx = mpp.semiSeqPattern();
MatcherImpl matcher = new MatcherImpl();
matcher.setCatchAll(catchAll);
Matchings m = matcher.accept(ctx, new MatchPath.MatchPathSemiSequent(null, semiSeq, true));
Matchings m = matcher.accept(ctx, MatchPathFacade.createRoot(semiSeq));
return m;
}
......@@ -69,9 +74,14 @@ public class MatcherFacade {
public static Matchings matches(String pattern, Sequent sequent, boolean catchAll) {
MatchPatternParser mpp = getParser(pattern);
SequentPatternContext ctx = mpp.sequentPattern();
if (mpp.getNumberOfSyntaxErrors() != 0) {
logger.info("Invalid pattern syntax '{}' no matches returned.", pattern);
return new Matchings();
}
MatcherImpl matcher = new MatcherImpl();
matcher.setCatchAll(catchAll);
Matchings m = matcher.accept(ctx, new MatchPath.MatchPathSequent(sequent));
Matchings m = matcher.accept(ctx, MatchPathFacade.create(sequent));
return m;
}
......@@ -84,6 +94,6 @@ public class MatcherFacade {
* @return
*/
public static Matchings matches(String pattern, Sequent sequent) {
return matches(pattern, sequent,false);
return matches(pattern, sequent, false);
}
}
package edu.kit.formal.psdb.termmatcher;
import com.google.common.collect.Sets;
import edu.kit.formal.psdb.termmatcher.mp.MatchPath;
import java.util.*;
......
package edu.kit.formal.psdb.termmatcher.mp;
import de.uka.ilkd.key.logic.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* @author Alexander Weigl
* @version 1 (24.08.17)
*/
@Data
@EqualsAndHashCode(of = {"unit"})
public abstract class MatchPath<T, P> {
public static final int SEQUENT_FORMULA_ROOT = -1;
public static final int POSITION_ANTECEDENT = -2;
public static final int POSITION_SUCCEDENT = -3;
private final MatchPath<? extends P, ?> parent;
private final T unit;
private final int posInParent;
private MatchPath(MatchPath<? extends P, ?> parent, T unit, int pos) {
posInParent = pos;
this.unit = unit;
this.parent = parent;
}
public abstract PosInOccurrence pio();
public abstract Sequent getSequent();
public abstract SequentFormula getSequentFormula();
public abstract int depth();
public String toString() {
return unit.toString();
}
public static class MPTerm extends MatchPath<Term, Object> {
MPTerm(MatchPath<? extends Object, ?> parent, Term unit, int pos) {
super(parent, unit, pos);
}
@Override
public PosInOccurrence pio() {
if(getParent()==null) return null;
PosInOccurrence pio = getParent().pio();
if(getPosInParent()==SEQUENT_FORMULA_ROOT)
return pio;
return pio.down(getPosInParent());
}
@Override
public Sequent getSequent() {
return null;
}
@Override
public SequentFormula getSequentFormula() {
return null;
}
@Override
public int depth() {
return getUnit().depth();
}
}
public static class MPSequentFormula extends MatchPath<SequentFormula, Semisequent> {
MPSequentFormula(MatchPath<Semisequent, Sequent> parent, SequentFormula unit, int pos) {
super(parent, unit, pos);
}
@Override
public PosInOccurrence pio() {
return new PosInOccurrence(getUnit(), PosInTerm.getTopLevel(),
getParent().getPosInParent() == POSITION_ANTECEDENT
);
}
@Override
public Sequent getSequent() {
if (getParent() != null)
return getParent().getSequent();
return null;
}
@Override
public SequentFormula getSequentFormula() {
return getUnit();
}
@Override
public int depth() {
return getUnit().formula().depth();
}
}
public static class MPSequent extends MatchPath<Sequent, Void> {
MPSequent(Sequent unit) {
super(null, unit, SEQUENT_FORMULA_ROOT);
}
@Override
public int depth() {
return 0;
}
@Override
public PosInOccurrence pio() {
return null;
}
@Override
public Sequent getSequent() {
return getUnit();
}
@Override
public SequentFormula getSequentFormula() {
return null;
}
}
public static class MPSemiSequent extends MatchPath<Semisequent, Sequent> {
MPSemiSequent(MPSequent parent, Semisequent unit, boolean antec) {
super(parent, unit, antec ? POSITION_ANTECEDENT : POSITION_SUCCEDENT);
}
@Override
public int depth() {
return 1;
}
@Override
public PosInOccurrence pio() {
return null;
}
@Override
public Sequent getSequent() {
if (getParent() == null) return null;
return getParent().getSequent();
}
@Override
public SequentFormula getSequentFormula() {
return null;
}
}
}
package edu.kit.formal.psdb.termmatcher.mp;
import de.uka.ilkd.key.logic.Semisequent;
import de.uka.ilkd.key.logic.Sequent;
import de.uka.ilkd.key.logic.Term;
/**
* @author Alexander Weigl
* @version 1 (27.08.17)
*/
public class MatchPathFacade {
public static MatchPath.MPTerm create(MatchPath<Term, ?> path, int subTerm) {
return new MatchPath.MPTerm(path, path.getUnit().sub(subTerm), subTerm);
}
public static MatchPath.MPSequentFormula create(MatchPath.MPSemiSequent ss, int pos) {
return new MatchPath.MPSequentFormula(ss, ss.getUnit().get(pos), pos);
}
public static MatchPath.MPSemiSequent create(MatchPath.MPSequent s, boolean antec) {
MatchPath.MPSemiSequent mp = new MatchPath.MPSemiSequent(
s, antec
? s.getUnit().antecedent()
: s.getUnit().succedent(),
antec);
return mp;
}
public static MatchPath.MPSequent create(Sequent s) {
return new MatchPath.MPSequent(s);
}
public static MatchPath.MPTerm create(MatchPath.MPSequentFormula sf) {
return new MatchPath.MPTerm(sf, sf.getUnit().formula(), MatchPath.SEQUENT_FORMULA_ROOT);
}
public static MatchPath.MPSemiSequent createSuccedent(MatchPath.MPSequent sequent) {
return create(sequent, false);
}
public static MatchPath.MPSemiSequent createAntecedent(MatchPath.MPSequent sequent) {
return create(sequent, true);
}
/**
* only for testing
*
* @param keyTerm
* @return
*/
public static MatchPath createRoot(Term keyTerm) {
return new MatchPath.MPTerm(null, keyTerm, MatchPath.SEQUENT_FORMULA_ROOT);
}
/**
* only for testing
*
* @param semiSeq
* @return
*/
public static MatchPath createRoot(Semisequent semiSeq) {
return new MatchPath.MPSemiSequent(null, semiSeq, true);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment