From 746bb0ba666286b9586ff8c2724ba21e1490c906 Mon Sep 17 00:00:00 2001 From: Kristóf Marussy Date: Tue, 19 May 2020 23:55:46 +0200 Subject: Add new state coder --- .../neighbourhood/Descriptor.xtend | 196 +++++++++++++-------- 1 file changed, 120 insertions(+), 76 deletions(-) (limited to 'Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretationlanguage/neighbourhood/Descriptor.xtend') diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretationlanguage/neighbourhood/Descriptor.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretationlanguage/neighbourhood/Descriptor.xtend index 41482b28..e4bdb086 100644 --- a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretationlanguage/neighbourhood/Descriptor.xtend +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretationlanguage/neighbourhood/Descriptor.xtend @@ -4,10 +4,21 @@ import java.util.HashMap import java.util.Map import java.util.Set import org.eclipse.xtend.lib.annotations.Data +import org.eclipse.xtend2.lib.StringConcatenationClient @Data abstract class AbstractNodeDescriptor { long dataHash - + + protected def StringConcatenationClient prettyPrint() { + '''(«dataHash»)[«class.simpleName»]''' + } + + override toString() { + ''' + «prettyPrint» + ''' + } + // @Pure // @Override // override public boolean equals(Object obj) { @@ -24,34 +35,45 @@ import org.eclipse.xtend.lib.annotations.Data // } } -@Data class LocalNodeDescriptor extends AbstractNodeDescriptor{ +@Data class LocalNodeDescriptor extends AbstractNodeDescriptor { Set types String id; + new(String id, Set types) { - super(calcualteDataHash(id,types)) + super(calcualteDataHash(id, types)) this.types = types this.id = id } - + def private static calcualteDataHash(String id, Set types) { val int prime = 31; var result = 0 - if(id !== null) - result = id.hashCode(); - if(types !== null) { - result = prime * result + types.hashCode - } - return result + if (id !== null) + result = id.hashCode(); + if (types !== null) { + result = prime * result + types.hashCode + } + return result } - + override hashCode() { return this.dataHash.hashCode } - + + override equals(Object other) { + other.class == LocalNodeDescriptor && (other as AbstractNodeDescriptor).hashCode == hashCode + } + + override protected prettyPrint() { + '''(«dataHash»)[«IF id !== null»id = "«id»"«IF types === null || !types.empty», «ENDIF»«ENDIF»«IF types === null»TYPES = null«ELSE»«FOR type : types SEPARATOR ", "»«type»«ENDFOR»«ENDIF»]''' + } + override toString() { - return class.name + this.dataHash + ''' + «prettyPrint» + ''' } - + // @Pure // @Override // override public boolean equals(Object obj) { @@ -66,7 +88,6 @@ import org.eclipse.xtend.lib.annotations.Data // return false; // return true; // } - // @Pure // override public boolean equals(Object obj) { // if (this === obj) @@ -97,49 +118,74 @@ import org.eclipse.xtend.lib.annotations.Data String type } -@Data class FurtherNodeDescriptor extends AbstractNodeDescriptor{ - - NodeRep previousRepresentation - Map,Integer> incomingEdges - Map,Integer> outgoingEdges - - new( - NodeRep previousRepresentation, - Map,Integer> incomingEdges, - Map,Integer> outgoingEdges) - { - super(calculateDataHash(previousRepresentation,incomingEdges,outgoingEdges)) - this.previousRepresentation = previousRepresentation - this.incomingEdges = new HashMap(incomingEdges) - this.outgoingEdges = new HashMap(outgoingEdges) - } - - static def private int calculateDataHash( - NodeRep previousRepresentation, - Map,Integer> incomingEdges, - Map,Integer> outgoingEdges) - { - val int prime = 31; - var int result = previousRepresentation.hashCode; - if(incomingEdges !== null) - result = prime * result + incomingEdges.hashCode(); - if(outgoingEdges !== null) - result = prime * result + outgoingEdges.hashCode(); - return result; - } - - override hashCode() { - return this.dataHash.hashCode +@Data class FurtherNodeDescriptor extends AbstractNodeDescriptor { + + NodeRep previousRepresentation + Map, Integer> incomingEdges + Map, Integer> outgoingEdges + + new(NodeRep previousRepresentation, Map, Integer> incomingEdges, + Map, Integer> outgoingEdges) { + super(calculateDataHash(previousRepresentation, incomingEdges, outgoingEdges)) + this.previousRepresentation = previousRepresentation + this.incomingEdges = new HashMap(incomingEdges) + this.outgoingEdges = new HashMap(outgoingEdges) } - + + static def private int calculateDataHash(NodeRep previousRepresentation, + Map, Integer> incomingEdges, Map, Integer> outgoingEdges) { + val int prime = 31; + var int result = previousRepresentation.hashCode; + if (incomingEdges !== null) + result = prime * result + incomingEdges.hashCode(); + if (outgoingEdges !== null) + result = prime * result + outgoingEdges.hashCode(); + return result; + } + + override hashCode() { + return this.dataHash.hashCode + } + + override equals(Object other) { + other.class == FurtherNodeDescriptor && (other as AbstractNodeDescriptor).hashCode == hashCode + } + + override prettyPrint() { + ''' + («dataHash»)[ + PREV = «previousRepresentation?.prettyPrint» + «IF incomingEdges === null» + IN null + «ELSE» + «FOR edge : incomingEdges.entrySet» + IN «edge.value» «edge.key.type» = «edge.key.from.prettyPrint» + «ENDFOR» + «ENDIF» + «IF outgoingEdges === null» + OUT null + «ELSE» + «FOR edge : outgoingEdges.entrySet» + OUT «edge.value» «edge.key.type» = «edge.key.to.prettyPrint» + «ENDFOR» + «ENDIF» + ]''' + } + + private def StringConcatenationClient prettyPrint(NodeRep rep) { + if (rep instanceof AbstractNodeDescriptor) { + rep.prettyPrint + } else { + '''«rep»''' + } + } + override toString() { - return class.name + dataHash -// return '''[«previousRepresentation»,(«FOR -// in: incomingEdges.entrySet»(«in.key.type.name»=«in.key.from»,«in.value»)«ENDFOR»),(«FOR -// out: outgoingEdges.entrySet»(«out.key.type.name»=«out.key.to»,«out.value»)«ENDFOR»),«FOR -// att: attributeValues»(«att.type.name»=«att.value»)«ENDFOR»]''' + ''' + «prettyPrint» + ''' } - + // @Pure // @Override // override public boolean equals(Object obj) { @@ -154,7 +200,6 @@ import org.eclipse.xtend.lib.annotations.Data // return false; // return true; // } - // @Pure // override public boolean equals(Object obj) { // if (this === obj) @@ -191,24 +236,23 @@ import org.eclipse.xtend.lib.annotations.Data // return true; // } } - /* -@Data -class ModelDescriptor { - int dataHash - int unknownElements - Map knownElements - - public new(Map knownElements, int unknownElements) { - this.dataHash = calculateDataHash(knownElements,unknownElements) - this.unknownElements = unknownElements - this.knownElements = knownElements - } - - def private static calculateDataHash(Map knownElements, int unknownElements) - { - val int prime = 31; - return knownElements.hashCode * prime + unknownElements.hashCode - } -} -*/ \ No newline at end of file + * @Data + * class ModelDescriptor { + * int dataHash + * int unknownElements + * Map knownElements + * + * public new(Map knownElements, int unknownElements) { + * this.dataHash = calculateDataHash(knownElements,unknownElements) + * this.unknownElements = unknownElements + * this.knownElements = knownElements + * } + * + * def private static calculateDataHash(Map knownElements, int unknownElements) + * { + * val int prime = 31; + * return knownElements.hashCode * prime + unknownElements.hashCode + * } + * } + */ -- cgit v1.2.3-70-g09d2