From 59a53fc819355fb2809b23544a5ca19ffff802fb Mon Sep 17 00:00:00 2001 From: OszkarSemerath Date: Mon, 16 Jul 2018 18:25:34 +0200 Subject: Scope support for attributes --- .../InstanceModel2PartialInterpretation.xtend | 81 ++++++++++++++++++-- .../PartialInterpretation2Logic.xtend | 22 +++++- .../PartialInterpretationInitialiser.xtend | 86 +++++++++++++++++++--- 3 files changed, 169 insertions(+), 20 deletions(-) (limited to 'Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme') diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretation2logic/InstanceModel2PartialInterpretation.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretation2logic/InstanceModel2PartialInterpretation.xtend index f4fc1fce..bd0f4ca4 100644 --- a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretation2logic/InstanceModel2PartialInterpretation.xtend +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretation2logic/InstanceModel2PartialInterpretation.xtend @@ -20,6 +20,10 @@ import org.eclipse.emf.ecore.EObject import org.eclipse.emf.ecore.resource.Resource import static extension hu.bme.mit.inf.dslreasoner.util.CollectionsUtil.* +import java.util.HashSet +import java.util.Set +import java.math.BigDecimal +import org.eclipse.emf.ecore.EAttribute class InstanceModel2PartialInterpretation { val extension LogiclanguageFactory factory = LogiclanguageFactory.eINSTANCE @@ -43,7 +47,12 @@ class InstanceModel2PartialInterpretation { { val problem = metamodelTranslationResult.output val ecore2LogicTrace = metamodelTranslationResult.trace - val tracedOutput = partialInterpretationInitialiser.initialisePartialInterpretation(problem, new TypeScopes) + + val referencesUsed = ecore2Logic.allReferencesInScope(ecore2LogicTrace).toSet + val attributesUsed = ecore2Logic.allAttributesInScope(ecore2LogicTrace).toSet + + val typeScope = createTypeScopesFromKnownAttributeValues(objects,attributesUsed) + val tracedOutput = partialInterpretationInitialiser.initialisePartialInterpretation(problem, typeScope) val partialInterpretation = tracedOutput.output val partialInterpretationTrace = tracedOutput.trace @@ -54,7 +63,7 @@ class InstanceModel2PartialInterpretation { val object = objects.get(objectIndex) val element = createDefinedElement => [ it.name = if(withID) - { '''o «objectIndex»''' } else + { '''o «objectIndex+1»''' } else { null } ] @@ -71,8 +80,6 @@ class InstanceModel2PartialInterpretation { object2DefinedElement.put(object, element) } - val referencesUsed = ecore2Logic.allReferencesInScope(ecore2LogicTrace).toSet - val attributesUsed = ecore2Logic.allAttributesInScope(ecore2LogicTrace).toSet for(source : objects) { // Translating the references for(reference : source.eClass.EAllReferences.filter[ @@ -126,6 +133,52 @@ class InstanceModel2PartialInterpretation { return partialInterpretation } + private def createTypeScopesFromKnownAttributeValues(List objects, Set attributesUsed) { + val Set integers = new HashSet + val Set reals = new HashSet + val Set strings = new HashSet + for(object: objects) { + for(attribute : object.eClass.EAllAttributes.filter[attributesUsed.contains(it)]) { + val value = object.eGet(attribute) + if(value !== null) { + if(value instanceof List) { + for(v : value) { + shortValue(v,integers,reals,strings) + } + } else { + shortValue(value,integers,reals,strings) + } + } + } + } + return new TypeScopes => [ + it.knownIntegers += integers + it.knownReals += reals + it.knownStrings += strings + ] + } + private def dispatch shortValue(Boolean value, Set integers, Set reals, Set strings) { + // Do nothing + } + private def dispatch shortValue(Integer value, Set integers, Set reals, Set strings) { + integers += value + } + private def dispatch shortValue(Float value, Set integers, Set reals, Set strings) { + reals += BigDecimal.valueOf(value) + } + private def dispatch shortValue(Double value, Set integers, Set reals, Set strings) { + reals += BigDecimal.valueOf(value) + } + private def dispatch shortValue(String value, Set integers, Set reals, Set strings) { + strings += value + } + private def dispatch shortValue(Void value, Set integers, Set reals, Set strings) { + // Do nothing + } + private def dispatch shortValue(Object value, Set integers, Set reals, Set strings) { + // Do nothing + } + protected def translateLink(PartialRelationInterpretation interpretation, DefinedElement source, DefinedElement target) { interpretation.relationlinks += createBinaryElementRelationLink => [it.param1 = source it.param2 = target] } @@ -138,7 +191,23 @@ class InstanceModel2PartialInterpretation { else throw new AssertionError('''term should be a defined element?''') } - dispatch protected def translateValue(Object value, Ecore2Logic_Trace ecore2LogicTrace, Problem2PartialInterpretationTrace partialInterpretationTrace) { - //throw new UnsupportedOperationException('''Mapping of «value.class.simpleName» in partial models is currently not supported!''') + dispatch protected def translateValue(Boolean value, Ecore2Logic_Trace ecore2LogicTrace, Problem2PartialInterpretationTrace partialInterpretationTrace) { + value.lookup(partialInterpretationTrace.primitiveValues.booleanMap) + } + + dispatch protected def translateValue(Integer value, Ecore2Logic_Trace ecore2LogicTrace, Problem2PartialInterpretationTrace partialInterpretationTrace) { + value.lookup(partialInterpretationTrace.primitiveValues.integerMap) + } + + dispatch protected def translateValue(Double value, Ecore2Logic_Trace ecore2LogicTrace, Problem2PartialInterpretationTrace partialInterpretationTrace) { + BigDecimal.valueOf(value).lookup(partialInterpretationTrace.primitiveValues.realMap) + } + + dispatch protected def translateValue(Float value, Ecore2Logic_Trace ecore2LogicTrace, Problem2PartialInterpretationTrace partialInterpretationTrace) { + BigDecimal.valueOf(value).lookup(partialInterpretationTrace.primitiveValues.realMap) + } + + dispatch protected def translateValue(String value, Ecore2Logic_Trace ecore2LogicTrace, Problem2PartialInterpretationTrace partialInterpretationTrace) { + value.lookup(partialInterpretationTrace.primitiveValues.stringMap) } } \ No newline at end of file diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretation2logic/PartialInterpretation2Logic.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretation2logic/PartialInterpretation2Logic.xtend index 5d8d9313..bba4ae92 100644 --- a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretation2logic/PartialInterpretation2Logic.xtend +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretation2logic/PartialInterpretation2Logic.xtend @@ -24,6 +24,9 @@ import org.eclipse.xtend.lib.annotations.Data import static extension hu.bme.mit.inf.dslreasoner.util.CollectionsUtil.* import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PrimitiveElement +import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.BooleanElement +import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.IntegerElement +import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.RealElement @Data class PartialInterpretation2Logic_Trace { Map new2Old = new HashMap @@ -148,7 +151,7 @@ class PartialInterpretation2Logic { val ^annotation= Partial2logicannotationsFactory.eINSTANCE.createPartialModelRelation2Assertion ^annotation.target = assertion ^annotation.targetRelation = relation - ^annotation.links += links.map[EcoreUtil.copy(it)] + //^annotation.links += links.map[EcoreUtil.copy(it)] //val error= assertion.eAllContents.toIterable.filter(SymbolicValue).filter[it.symbolicReference === null] //error.forEach[println("error")] p.add(assertion) @@ -162,12 +165,25 @@ class PartialInterpretation2Logic { if((link.param1 !== null) && (link.param2 !== null)) { return createSymbolicValue=>[ it.symbolicReference=relationDeclaration - it.parameterSubstitutions += createSymbolicValue => [it.symbolicReference = link.param1] - it.parameterSubstitutions += createSymbolicValue => [it.symbolicReference = link.param2] + it.parameterSubstitutions += createValueInLink(link.param1) + it.parameterSubstitutions += createValueInLink(link.param2) ] } else { throw new IllegalArgumentException } } else throw new UnsupportedOperationException } + + def private dispatch createValueInLink(BooleanElement element) { + return element.value.asTerm + } + def private dispatch createValueInLink(IntegerElement element) { + return element.value.asTerm + } + def private dispatch createValueInLink(RealElement element) { + return element.value.asTerm + } + def private dispatch createValueInLink(DefinedElement element) { + return createSymbolicValue => [it.symbolicReference = element] + } } \ No newline at end of file diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretationlanguage/PartialInterpretationInitialiser.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretationlanguage/PartialInterpretationInitialiser.xtend index 2a350d53..cc76ce3f 100644 --- a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretationlanguage/PartialInterpretationInitialiser.xtend +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretationlanguage/PartialInterpretationInitialiser.xtend @@ -2,6 +2,7 @@ package hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage import hu.bme.mit.inf.dslreasoner.logic.model.builder.TracedOutput import hu.bme.mit.inf.dslreasoner.logic.model.builder.TypeScopes +import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.DefinedElement import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.LogiclanguageFactory import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.RelationDeclaration import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Type @@ -10,8 +11,10 @@ import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.TypeDefinition import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicProblem import hu.bme.mit.inf.dslreasoner.logic.model.patterns.SupertypeStar import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partial2logicannotations.PartialModelRelation2Assertion +import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.BinaryElementRelationLink import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.BooleanElement import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.IntegerElement +import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.NaryRelationLink import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialBooleanInterpretation import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialComplexTypeInterpretation import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialIntegerInterpretation @@ -22,16 +25,22 @@ import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.par import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialinterpretationFactory import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.RealElement import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.StringElement +import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.UnaryElementRelationLink import java.math.BigDecimal import java.util.HashMap import java.util.Map import java.util.SortedSet -import org.eclipse.emf.ecore.util.EcoreUtil import org.eclipse.viatra.query.runtime.api.ViatraQueryEngine import org.eclipse.viatra.query.runtime.emf.EMFScope import org.eclipse.xtend.lib.annotations.Data import static extension hu.bme.mit.inf.dslreasoner.util.CollectionsUtil.* +import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.SymbolicValue +import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.And +import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.BoolLiteral +import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.IntLiteral +import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.RealLiteral +import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.StringLiteral @Data class Problem2PartialInterpretationTrace { Map type2Interpretation @@ -81,7 +90,7 @@ class PartialInterpretationInitialiser { typeScopes.maxNewElements) // Initialise relations - val relation2Interpretation = initRelations(res) + val relation2Interpretation = initRelations(res,primitiveTrace) val trace = new Problem2PartialInterpretationTrace(type2Interpretation,primitiveTrace,relation2Interpretation) return new TracedOutput(res,trace) @@ -95,13 +104,13 @@ class PartialInterpretationInitialiser { { return new PrimitiveValueTrace( booleanType, - booleanType.elements.filter(BooleanElement).toMap[value], + booleanType.elements.filter(BooleanElement).filter[it.isValueSet].toMap[value], integerType, - integerType.elements.filter(IntegerElement).toMap[value], + integerType.elements.filter(IntegerElement).filter[it.isValueSet].toMap[value], realType, - realType.elements.filter(RealElement).toMap[value], + realType.elements.filter(RealElement).filter[it.isValueSet].toMap[value], stringType, - stringType.elements.filter(StringElement).toMap[value] + stringType.elements.filter(StringElement).filter[it.isValueSet].toMap[value] ) } @@ -130,7 +139,9 @@ class PartialInterpretationInitialiser { partialInterpretation.newElements += integerElement } if(maxNewIntegers>0) { - throw new UnsupportedOperationException('''Unspecified Integers are currently not supported!''') + val newElements = createIntegerElement => [it.name = "New Integers" it.valueSet = false] + partialInterpretation.openWorldElements += newElements + integerInterpretation.elements += newElements } return integerInterpretation @@ -146,7 +157,9 @@ class PartialInterpretationInitialiser { partialInterpretation.newElements += realElement } if(maxNewReals>0) { - throw new UnsupportedOperationException('''Unspecified Real values are currently not supported!''') + val newElements = createRealElement => [it.name = "New Reals" it.valueSet = false] + partialInterpretation.openWorldElements += newElements + realInterpretation.elements += newElements } return realInterpretation } @@ -161,7 +174,9 @@ class PartialInterpretationInitialiser { partialInterpretation.newElements += stringElement } if(maxNewStrings>0) { - throw new UnsupportedOperationException('''Unspecified String values are currently not supported!''') + val newElements = createStringElement => [it.name = "New Strings" it.valueSet = false] + partialInterpretation.openWorldElements += newElements + stringInterpretation.elements += newElements } return stringInterpretation @@ -201,7 +216,7 @@ class PartialInterpretationInitialiser { } } - def private initRelations(PartialInterpretation interpretation) { + def private Map initRelations(PartialInterpretation interpretation, PrimitiveValueTrace trace) { val Map relation2Interpretation = new HashMap for(relation : interpretation.problem.relations.filter(RelationDeclaration)) { val partialInterpretation = relation.initialisePartialRelationInterpretation @@ -211,8 +226,24 @@ class PartialInterpretationInitialiser { for(pMR2A : interpretation.problem.annotations.filter(PartialModelRelation2Assertion)) { val relation = pMR2A.targetRelation val r = relation.lookup(relation2Interpretation) - r.relationlinks+=pMR2A.links.map[EcoreUtil.copy(it)] + val assertion = pMR2A.target.value + val links = if(assertion instanceof SymbolicValue) { + #[assertion] + } else if(assertion instanceof And){ + val res = assertion.operands.filter(SymbolicValue) + if(res.size != assertion.operands) { + throw new UnsupportedOperationException('''Assertion describing partial model of "«r.interpretationOf.name»" contains unsupported constructs''') + } else { + res + } + } else { + throw new UnsupportedOperationException('''Assertion describing partial model of "«r.interpretationOf.name»" contains unsupported constructs''') + } + for(link:links) { + r.relationlinks += createLink(link,trace) + } } + // interpretation.partialfunctioninterpretation += interpretation.problem.functions.filter(FunctionDeclaration) // .map[initialisePartialFunctionInterpretation(trace)] // interpretation.partialconstantinterpretation += interpretation.problem.constants.filter(ConstantDeclaration) @@ -220,6 +251,39 @@ class PartialInterpretationInitialiser { return relation2Interpretation } + def private createLink(SymbolicValue v, PrimitiveValueTrace trace) { + val translatedValues = v.parameterSubstitutions.map[getElement(trace)].toList + if(translatedValues.size == 1) { + return createUnaryElementRelationLink => [it.param1 = translatedValues.get(0)] + } else if(translatedValues.size == 2) { + return createBinaryElementRelationLink => [it.param1 = translatedValues.get(0) it.param2 = translatedValues.get(1)] + } else { + val res = createNaryRelationLink + for(i : 0.. [ + it.index = i + it.param = translatedValues.get(i) + ] + } + return res + } + } + + def private dispatch getElement(SymbolicValue element, PrimitiveValueTrace trace) { + return element.symbolicReference as DefinedElement + } + def private dispatch getElement(BoolLiteral element, PrimitiveValueTrace trace) { + element.value.lookup(trace.booleanMap) + } + def private dispatch getElement(IntLiteral element, PrimitiveValueTrace trace) { + element.value.lookup(trace.integerMap) + } + def private dispatch getElement(RealLiteral element, PrimitiveValueTrace trace) { + element.value.lookup(trace.realMap) + } + def private dispatch getElement(StringLiteral element, PrimitiveValueTrace trace) { + element.value.lookup(trace.stringMap) + } def private initialisePartialTypeInterpretation(TypeDeclaration t, ViatraQueryEngine engine) { val supertypeMatcher = SupertypeStar.Matcher.on(engine) -- cgit v1.2.3-54-g00ecf