Cleanup and optimize Rule.Tag, drop unused methods

This commit is contained in:
Fedor Isakov 2021-05-16 11:56:29 +02:00
parent 24ce9167bf
commit dc4f046510
6 changed files with 30 additions and 48 deletions

View File

@ -48,7 +48,7 @@ internal open class MatchJournalImpl(
val rulesWithOrigin = HashSet<Any>(4)
override fun toString() = "(id=$evidence, ${justifications()}, ${match.rule().uniqueTag().name()}, $entries)"
override fun toString() = "(id=$evidence, ${justifications()}, ${match.rule().uniqueTag().toString()}, $entries)"
}
private class OccChunkImpl(override val occ: Occurrence) : ChunkImpl(), Justified by occ, OccChunk {

View File

@ -17,8 +17,7 @@
package jetbrains.mps.logic.reactor.program;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
/**
* A constraint rule description.
@ -28,18 +27,7 @@ import java.util.List;
public abstract class Rule {
public abstract Rule.Kind kind();
/**
* A list of objects identifying the segment this rule belongs to. An empty segment path signifies the root segment.
* An occurrence produced from the root segment can be processed by any rule in the program.
* An occurrence produced from segment identified by a path P can be processed by a rule from any segment that
* has P as the path prefix.
*/
// TODO Make abstract
public List<Object> segmentPath() {
return Collections.emptyList();
}
public boolean isBasis() { return false; }
/**
@ -66,29 +54,26 @@ public abstract class Rule {
public static final class Tag {
/**
* The name of the template that created this rule.
* @deprecated
*/
public String groupName() { return group; }
public String name() { return name; }
/**
* Rule is "stable" if it has no unique part
* and its name coincides with unique tag.
* @return true if rule has no unique part
*
* @param groupName template that created this rule
* @param tagName
* @param uniquePart
*/
public boolean stable() { return utag.length() == name.length(); }
public Tag(String groupPrefix, String commonPart, Object uniquePart) {
if (!commonPart.startsWith(groupPrefix)) {
public Tag(String groupName, String tagName, Object uniquePart) {
if (!tagName.startsWith(groupName)) {
throw new IllegalArgumentException();
}
StringBuilder utagSb = new StringBuilder(commonPart);
if (uniquePart != null) {
utagSb.append('_').append(uniquePart);
}
this.group = groupPrefix;
this.name = commonPart;
this.utag = utagSb.toString();
this.hash = utag.hashCode();
this.group = groupName;
this.tag = tagName;
this.id = uniquePart;
this.hash = Objects.hash(tag, id);
}
@Deprecated
@ -98,20 +83,20 @@ public abstract class Rule {
public int hashCode() { return hash; }
@Override
public boolean equals(Object obj) {
if (obj == null) return false;
if (this == obj) return true;
// FIXME WTF? NB: allow equality to String
return this.toString().equals(obj.toString());
public boolean equals(Object that) {
if (that == null) return false;
if (this == that) return true;
if (!(that instanceof Tag)) return false;
return Objects.equals(this.id, ((Tag) that).id) && Objects.equals(this.tag, ((Tag) that).tag);
}
@Override
public String toString() { return utag; }
public String toString() { return tag; }
private final int hash;
private final String group;
private final String name;
private final String utag;
private final String tag;
private final Object id;
}
}

View File

@ -43,7 +43,7 @@ class HandlerBuilder(val name: String) {
fun toHandler(): RulesList = MockHandler(name, rules.values.toList())
}
class RuleBuilder(val tag: String, val segmentPath: List<Any>) {
class RuleBuilder(val tag: String) {
val kept = ArrayList<Constraint>()
val replaced = ArrayList<Constraint>()
val guard = ArrayList<Predicate>()
@ -62,7 +62,7 @@ class RuleBuilder(val tag: String, val segmentPath: List<Any>) {
if (alt || body.isEmpty()) body.add(ArrayList<AndItem>())
body.last().addAll(andItem)
}
fun toRule(): Rule = MockRule(Rule.Tag(tag), segmentPath, kept, replaced, guard, body)
fun toRule(): Rule = MockRule(Rule.Tag(tag), kept, replaced, guard, body)
}
class MockHandler(
@ -76,7 +76,6 @@ class MockHandler(
class MockRule(
val tag: Rule.Tag,
val segmentPath: List<Any>,
val kept: Collection<Constraint>,
val replaced: Collection<Constraint>,
val guard: Collection<Predicate>,
@ -84,8 +83,6 @@ class MockRule(
override fun kind(): Kind = TODO()
override fun segmentPath(): List<Any> = segmentPath
override fun uniqueTag() = tag
override fun headKept(): Iterable<Constraint> = kept

View File

@ -125,7 +125,7 @@ fun Builder.insertRulesWhen(at: (Rule) -> Boolean, vararg ruleBuilders: () -> Ru
updateBuilder(this, arrayOf(insertRulesInHandlerWhen(at, "test", rulesLists.first(), * ruleBuilders)))
fun rule(tag: String, vararg component: RuleBuilder.() -> Unit): () -> Rule = {
val rb = RuleBuilder(tag, emptyList())
val rb = RuleBuilder(tag)
for (cmp in component) {
rb.cmp()
}

View File

@ -1326,7 +1326,7 @@ class TestIncrementalProgram {
).launch("launch", progSpec) { result ->
result.storeView().constraintSymbols() shouldBe setOf(sym0("important"))
assertTrue( result.lastChunk().let { it is MatchJournal.MatchChunk && it.match.rule().uniqueTag().name() == "foo" } )
assertTrue( result.lastChunk().let { it is MatchJournal.MatchChunk && it.match.rule().uniqueTag().toString() == "foo" } )
}.also { (builder, evalRes) ->

View File

@ -550,7 +550,7 @@ class TestRuleMatcher {
expand(occurrence("bar", x, y)) }.apply {
matches().size shouldBe 1
matches().first().rule().uniqueTag().name() shouldBe "rule1"
matches().first().rule().uniqueTag().toString() shouldBe "rule1"
}
}