From 60f01f46ba232ed6416054f0a6115cb2a9b70b4e Mon Sep 17 00:00:00 2001 From: OszkarSemerath Date: Sat, 10 Jun 2017 19:05:05 +0200 Subject: Migrating Additional projects --- .../dslreasoner/ecore2logic/AttributeMapper.xtend | 119 ++++++++++++++++ .../ecore2logic/ContainmentMapper.xtend | 133 ++++++++++++++++++ .../inf/dslreasoner/ecore2logic/EClassMapper.xtend | 53 +++++++ .../inf/dslreasoner/ecore2logic/EEnumMapper.xtend | 60 ++++++++ .../dslreasoner/ecore2logic/EReferenceMapper.xtend | 154 +++++++++++++++++++++ .../inf/dslreasoner/ecore2logic/Ecore2Logic.xtend | 98 +++++++++++++ 6 files changed, 617 insertions(+) create mode 100644 Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/AttributeMapper.xtend create mode 100644 Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/ContainmentMapper.xtend create mode 100644 Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/EClassMapper.xtend create mode 100644 Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/EEnumMapper.xtend create mode 100644 Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/EReferenceMapper.xtend create mode 100644 Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/Ecore2Logic.xtend (limited to 'Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic') diff --git a/Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/AttributeMapper.xtend b/Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/AttributeMapper.xtend new file mode 100644 index 00000000..ebf49196 --- /dev/null +++ b/Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/AttributeMapper.xtend @@ -0,0 +1,119 @@ +package hu.bme.mit.inf.dslreasoner.ecore2logic + +import hu.bme.mit.inf.dslreasoner.ecore2logic.ecore2logicannotations.Ecore2logicannotationsFactory +import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicProblemBuilder +import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Assertion +import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.RelationDeclaration +import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Term +import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.TermDescription +import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.TypeDescriptor +import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicProblem +import java.util.HashMap +import java.util.Map +import org.eclipse.emf.ecore.EAttribute +import org.eclipse.emf.ecore.EEnum + +interface EAttributeMapper { + def void transformEAttributes(Ecore2Logic_Trace trace, LogicProblem logicProblem, Iterable attributes) + def Term IsAttributeValue(Ecore2Logic_Trace trace, TermDescription object, TermDescription value, EAttribute attribute) + def RelationDeclaration relationOfAttribute(Ecore2Logic_Trace trace, EAttribute attribute) + def TypeDescriptor TypeOfRange(Ecore2Logic_Trace trace, EAttribute attribute) +} + +class EAttributeMapper_RelationsOverTypes_Trace implements Trace{ + public var Map indicators = new HashMap + public var Map typeCompliance = new HashMap + public var Map lowerMultiplicity = new HashMap + public var Map upperMultiplicity = new HashMap +} + +class EAttributeMapper_RelationsOverTypes implements EAttributeMapper { + protected val extension LogicProblemBuilder builder = new LogicProblemBuilder + val extension Ecore2logicannotationsFactory builder2 = Ecore2logicannotationsFactory.eINSTANCE + protected val extension EClassMapper classMapper + protected val extension EEnumMapper enumMapper; + + public new(EClassMapper classMapper, EEnumMapper enumMapper) { + this.enumMapper = enumMapper + this.classMapper = classMapper + } + + public override transformEAttributes(Ecore2Logic_Trace trace, LogicProblem logicProblem, Iterable attributes) { + val attributeMapperTrace = new EAttributeMapper_RelationsOverTypes_Trace + trace.attributeMapperTrace = attributeMapperTrace + + for(attribute : attributes) { + val sourceType = trace.TypeofEClass(attribute.EContainingClass) + val rangeType = trace.TypeOfRange(attribute) + + // relations + val indicator = '''inAttribute «attribute.name» «attribute.EContainingClass.name»'''.RelationDeclaration(sourceType,rangeType) + + logicProblem.add(indicator) + attributeMapperTrace.indicators.put(attribute,indicator) + + // lower multiplicity + var Assertion lowerMultiplicity = null + if(attribute.lowerBound != 0) { + lowerMultiplicity = '''lowerMultiplicity «attribute.name» «attribute.EContainingClass.name»'''.Assertion( + Forall[ + val source = addVar('''src''', sourceType) + Exists[ context | + val targets = (1 .. attribute.lowerBound).map[context.addVar('''trg «it»''', rangeType)].toList + val attributeValue = And(targets.map[IsAttributeValue(trace, source, it, attribute)]) + if(targets.size > 1) return Distinct(targets) && attributeValue + else return attributeValue + ] + ] + ) + val annotation = createLowerMultiplicityAssertion => [ + it.lower = attribute.lowerBound + it.relation = indicator + ] + logicProblem.add(lowerMultiplicity) + logicProblem.annotations += annotation + lowerMultiplicity.annotations +=annotation + } + attributeMapperTrace.lowerMultiplicity.put(attribute,lowerMultiplicity) + + var Assertion upperMultiplicity = null + if(attribute.upperBound > 0) { + upperMultiplicity = '''upperMultiplicity «attribute.name» «attribute.EContainingClass.name»'''.Assertion( + Forall[ context | + val source = context.addVar('''src''', sourceType) + val targets = (1 .. attribute.upperBound+1).map[context.addVar('''trg «it»''', rangeType)].toList + And(targets.map[IsAttributeValue(trace, source, it, attribute)]) => ! Distinct(targets) + ]) + + val annotation = createUpperMultiplicityAssertion => [ + it.upper = attribute.upperBound + it.relation = indicator + ] + + logicProblem.add(upperMultiplicity) + logicProblem.annotations += annotation + upperMultiplicity.annotations += annotation + } + attributeMapperTrace.upperMultiplicity.put(attribute,upperMultiplicity) + } + } + + def asTrace(Trace trace) { return trace as EAttributeMapper_RelationsOverTypes_Trace} + + override IsAttributeValue(Ecore2Logic_Trace trace, TermDescription object, TermDescription value, EAttribute attribute) { + trace.attributeMapperTrace.asTrace.indicators.get(attribute).call(object,value) + } + + override TypeDescriptor TypeOfRange(Ecore2Logic_Trace trace, EAttribute attribute) { + if(attribute.EType instanceof EEnum) return enumMapper.TypeofEEnum(trace,attribute.EType as EEnum) + else if(attribute.EType.name.equals("EInt")) return LogicInt + else if(attribute.EType.name.equals("EBoolean")) return LogicBool + else if(attribute.EType.name.equals("EDouble") || + attribute.EType.name.equals("EFloat")) return LogicReal + else throw new UnsupportedOperationException('''Unsupported attribute type: «attribute.EType.name»''') + } + + override relationOfAttribute(Ecore2Logic_Trace trace, EAttribute attribute) { + trace.attributeMapperTrace.asTrace.indicators.get(attribute) + } +} \ No newline at end of file diff --git a/Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/ContainmentMapper.xtend b/Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/ContainmentMapper.xtend new file mode 100644 index 00000000..492be59f --- /dev/null +++ b/Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/ContainmentMapper.xtend @@ -0,0 +1,133 @@ +package hu.bme.mit.inf.dslreasoner.ecore2logic + +import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicProblemBuilder +import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Constant +import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Relation +import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicProblem +import org.eclipse.emf.ecore.EReference +import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Assertion + +interface ContainmentMapper { + def void transformContainment(Ecore2Logic_Trace trace, LogicProblem problem, Iterable references, boolean singleRoot); +} + +class ContainmentMapper_ReferenceConjuction_Trace implements Trace { + public var Constant root; + public var Relation contains + public var Assertion containsDefinition; + public var Assertion rootIsNotContained; + public var Assertion everithingElseContained; + public var Assertion notContainedByMore; +} + +class ContainmentMapper_ReferenceConjuction implements ContainmentMapper{ + val extension LogicProblemBuilder builder = new LogicProblemBuilder + val extension EClassMapper classMapper; + val extension EReferenceMapper referenceMapper + val int containmentAcyclicityApproximationLevel; + + public new(EClassMapper classMapper, EReferenceMapper referenceMapper, int containmentAcyclicityApproximationLevel) { + this.classMapper = classMapper + this.referenceMapper = referenceMapper + this.containmentAcyclicityApproximationLevel = containmentAcyclicityApproximationLevel + } + + override transformContainment(Ecore2Logic_Trace trace, LogicProblem problem, Iterable references, boolean singleRoot) { + val allClasses = classMapper.allClassesInScope(trace).map[classMapper.TypeofEClass(trace,it)] + val containmentRelation = references.filter[isContainment].map[referenceMapper.relationOfReference(trace,it)] + val containmentHierarchy = ContainmentHierarchy(allClasses,#[],containmentRelation,null) + problem.add(containmentHierarchy) + } +} + +/*val containmentMapperTrace = new ContainmentMapper_ReferenceConjuction_Trace + trace.containmentMapperTrace = containmentMapperTrace + + val containment = references.filter[containment] + + val contains = problem.add(RelationDeclaration('''contains''', getType(trace,references),getType(trace,references))) + containmentMapperTrace.contains = contains + containmentMapperTrace.containsDefinition = problem.add(Assertion('''containsDefinition''', + Forall[ + val parent = addVar("parent",getType(trace,references)) + val child = addVar("child",getType(trace,references)) + contains.call(parent,child) <=> Or(containment.map[IsInReference(trace,parent,child,it)]) + ] + )) + + + + containmentMapperTrace.notContainedByMore = problem.add(Assertion('''notContainedByMore''', + Forall[ + val child = addVar('''child''',getType(trace,references)) + val parent1 = addVar('''parent 1''',getType(trace,references)) + val parent2 = addVar('''parent 2''',getType(trace,references)) + (contains.call(parent1,child) && contains.call(parent2,child)) => (parent1 == parent2) + ] + )) + + (2..containmentAcyclicityApproximationLevel).forEach[problem.add(getAcyclicityConstraint(trace,references,it))] + + if (singleRoot) { + val root = problem.add(ConstantDeclaration('''root''', getType(trace, references))) + containmentMapperTrace.root = root + + containmentMapperTrace.rootIsNotContained = problem.add( + Assertion( + '''rootIsNotContained''', + Forall[ + val parent = addVar("parent", getType(trace, references)) + !contains.call(parent, root) + ] + )) + + containmentMapperTrace.everithingElseContained = problem.add( + Assertion( + '''nonrootIsContained''', + Forall[ + val child = addVar("child", getType(trace, references)) + (child != root) => Exists[ + val parent = addVar("parent", getType(trace, references)) + contains.call(parent, child) + ] + ] + )) + }*/ + + +// def asTrace(Trace trace) { trace as ContainmentMapper_ReferenceConjuction_Trace } +// +// def getAcyclicityConstraint(Ecore2Logic_Trace trace, Iterable references,int circleSize) { +// val newTrace = trace.containmentMapperTrace.asTrace +// //val newTrace2 = trace.containmentMapperTrace as ContainmentMapper_ReferenceConjuction_Trace // ??? +// +// Forall[context | +// val elements = (0..circleSize).map[context.addVar(getType(trace,references))].toList +// !(And((0..circleSize-1).map[newTrace.contains.call(elements.get(it),elements.get(it+1))]) && +// newTrace.contains.call(elements.last,elements.head)) +// ] +// } +// +// def private getType(Ecore2Logic_Trace trace, Iterable references) { +// trace.TypeofEClass(references.head.EContainingClass) +// } + + /*// This type is contained -> (From this type -> By those references) + val Map>> typeContainedBy = new HashMap + for(reference: references.filter[containment]) { + val sourceType = TypeofEClass(classMapperTrace, reference.EContainingClass) + val targetType = TypeofEClass(classMapperTrace, reference.EType as EClass) + if(typeContainedBy.containsKey(targetType)) { + val m = typeContainedBy.get(targetType) + if(m.containsKey(sourceType)) { + m.get(sourceType).add(reference) + } else { + m.put(sourceType,new HashSet => [add(reference)]) + } + } else { // initialise the collection + val Map> m = new HashMap + m.put(sourceType,new HashSet => [add(reference)]) + typeContainedBy.put(targetType,m) + } + }*/ + // Only one type \ No newline at end of file diff --git a/Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/EClassMapper.xtend b/Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/EClassMapper.xtend new file mode 100644 index 00000000..3949749b --- /dev/null +++ b/Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/EClassMapper.xtend @@ -0,0 +1,53 @@ +package hu.bme.mit.inf.dslreasoner.ecore2logic + +import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicProblemBuilder +import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Type +import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicProblem +import java.util.Map +import org.eclipse.emf.ecore.EClass +import java.util.HashMap + +interface EClassMapper { + def void transformEClasses(Ecore2Logic_Trace trace, LogicProblem problem, Iterable classes, int numberOfObjects, EcoreMetamodelDescriptor metamodelDescriptor) + def Iterable allClassesInScope(Ecore2Logic_Trace trace) + def Type TypeofEClass(Ecore2Logic_Trace trace, EClass type) +} + +class EClassMapper_AllElementAsObject_Trace implements Trace{ + public var Map typeMap = new HashMap; +} + +class EClassMapper_AllElementAsObject implements EClassMapper{ + val extension LogicProblemBuilder builder = new LogicProblemBuilder + + override transformEClasses(Ecore2Logic_Trace trace, LogicProblem problem, + Iterable classes, int numberOfObjects, EcoreMetamodelDescriptor metamodelDescriptor + ) { + val classMapperTrace = new EClassMapper_AllElementAsObject_Trace + trace.classMapperTrace = classMapperTrace + for(c:classes) { + val logicType = problem.add(TypeDeclaration('''class «c.name»''',c.isAbstract || c.isInterface)) + classMapperTrace.typeMap.put(c,logicType) + } + for(c:classes) { + for(s : c.ESuperTypes) { + Supertype(classMapperTrace.typeMap.get(c),classMapperTrace.typeMap.get(s)) + } + } + } + + def asTrace(Trace o) { o as EClassMapper_AllElementAsObject_Trace } + + override TypeofEClass(Ecore2Logic_Trace trace, EClass type) { + val typeMap = trace.classMapperTrace.asTrace.typeMap + if(typeMap.containsKey(type)) { + typeMap.get(type) + } else { + throw new IllegalArgumentException('''Class «type.name» is not translated to logic!''') + } + } + + override allClassesInScope(Ecore2Logic_Trace trace) { + trace.classMapperTrace.asTrace.typeMap.keySet + } +} diff --git a/Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/EEnumMapper.xtend b/Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/EEnumMapper.xtend new file mode 100644 index 00000000..eb7e9e44 --- /dev/null +++ b/Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/EEnumMapper.xtend @@ -0,0 +1,60 @@ +package hu.bme.mit.inf.dslreasoner.ecore2logic + +import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicProblemBuilder +import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.DefinedElement +import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.TermDescription +import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Type +import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicProblem +import java.util.ArrayList +import java.util.HashMap +import java.util.Map +import org.eclipse.emf.common.util.Enumerator +import org.eclipse.emf.ecore.EEnum + +import static extension hu.bme.mit.inf.dslreasoner.util.CollectionsUtil.* + +interface EEnumMapper { + def void transformEEnums(Ecore2Logic_Trace trace, LogicProblem problem, Iterable enums,Ecore2LogicConfiguration config) + def Type TypeofEEnum(Ecore2Logic_Trace trace, EEnum type) + def TermDescription Literal(Ecore2Logic_Trace trace, Enumerator literal) +} + +class EEnumMapper_PredefinedClasses_Trace implements Trace{ + public var Map enums + + public var Map literals +} + +class EEnumMapper_PredefinedClasses implements EEnumMapper { + val extension LogicProblemBuilder builder = new LogicProblemBuilder + + override transformEEnums(Ecore2Logic_Trace trace, LogicProblem problem, Iterable enums, Ecore2LogicConfiguration config) { + val enumTrace = new EEnumMapper_PredefinedClasses_Trace + trace.enumMapperTrace = enumTrace + enumTrace.enums = new HashMap + enumTrace.literals = new HashMap + + for(enum : enums) { + val l = new ArrayList(enum.ELiterals.size) + for(literal : enum.ELiterals.map[instance]) { + val element = Element('''«literal.name» «enum.name»''') + l+=element + enumTrace.literals.put(literal,element) + } + val type = TypeDefinition('''enum «enum.name»''',false,l) + problem.add(type) + enumTrace.enums.put(enum,type) + } + + } + + private def asTrace(Trace o) { o as EEnumMapper_PredefinedClasses_Trace } + + override TypeofEEnum(Ecore2Logic_Trace trace, EEnum type) { + trace.enumMapperTrace.asTrace.enums.get(type) + } + + override Literal(Ecore2Logic_Trace trace, Enumerator literal) { + literal.lookup(trace.enumMapperTrace.asTrace.literals) + } +} \ No newline at end of file diff --git a/Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/EReferenceMapper.xtend b/Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/EReferenceMapper.xtend new file mode 100644 index 00000000..e5de6584 --- /dev/null +++ b/Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/EReferenceMapper.xtend @@ -0,0 +1,154 @@ +package hu.bme.mit.inf.dslreasoner.ecore2logic + +import hu.bme.mit.inf.dslreasoner.ecore2logic.ecore2logicannotations.Ecore2logicannotationsFactory +import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicProblemBuilder +import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Assertion +import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.RelationDeclaration +import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Term +import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.TermDescription +import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicProblem +import java.util.HashMap +import java.util.Map +import org.eclipse.emf.ecore.EClass +import org.eclipse.emf.ecore.EReference + +interface EReferenceMapper{ + def void transformEReferences(Ecore2Logic_Trace trace, LogicProblem problem, Iterable classes); + def Term IsInReference(Ecore2Logic_Trace trace, TermDescription source, TermDescription target, EReference type) + def RelationDeclaration relationOfReference(Ecore2Logic_Trace trace, EReference reference) +} + +class EReferenceMapper_RelationsOverTypes_Trace implements Trace { + public var Map indicators; + public var Map typeCompliance + public var Map lowerMultiplicity + public var Map upperMultiplicity + public var Map inverseEdges +} + +class EReferenceMapper_RelationsOverTypes implements EReferenceMapper{ + val extension LogicProblemBuilder builder = new LogicProblemBuilder + val extension Ecore2logicannotationsFactory builder2 = Ecore2logicannotationsFactory.eINSTANCE + val extension EClassMapper classMapper; + + public new(EClassMapper classMapper) { + this.classMapper = classMapper + } + + def asTrace(Trace o) { o as EReferenceMapper_RelationsOverTypes_Trace } + + override transformEReferences(Ecore2Logic_Trace trace, LogicProblem problem, Iterable references) { + val newTrace = new EReferenceMapper_RelationsOverTypes_Trace + trace.referenceMapperTrace = newTrace + trace.createIndicatorDeclarations(problem,references) + trace.createMultiplicityConstraints(problem,references) + trace.createInverseReferenceConstraints(problem,references) + } + + def protected void createIndicatorDeclarations(Ecore2Logic_Trace trace, LogicProblem problem, Iterable references) { + trace.referenceMapperTrace.asTrace.indicators = new HashMap + for(reference : references) { + val relation = problem.add(RelationDeclaration( + '''inreference «reference.name» «reference.EContainingClass.name»''', + TypeofEClass(trace,reference.EContainingClass), + TypeofEClass(trace,reference.EType as EClass))) + trace.referenceMapperTrace.asTrace.indicators.put(reference,relation as RelationDeclaration) + } + } + + def void createMultiplicityConstraints(Ecore2Logic_Trace trace, LogicProblem problem, Iterable references) { + trace.referenceMapperTrace.asTrace.lowerMultiplicity = new HashMap + trace.referenceMapperTrace.asTrace.upperMultiplicity = new HashMap + + for(reference : references) { + val sourceType = reference.EContainingClass + val targetType = reference.EType as EClass + val lower = reference.lowerBound + val upper = reference.upperBound + + if (lower > 0) { + val assertion = Assertion('''lowerMultiplicity «reference.name» «sourceType.name»''', + Forall[ + val source = addVar('''src''', trace.TypeofEClass(sourceType)) + Exists[ context | + val targets = (1 .. lower).map[ + context.addVar('''trg «it»''', trace.TypeofEClass(targetType))].toList + val inReference = And(targets.map[IsInReference(trace, source, it, reference)]) + if(targets.size > 1) return Distinct(targets) && inReference + else return inReference + ] + ] + ) + val annotation = createLowerMultiplicityAssertion => [ + it.lower = lower + it.relation = trace.referenceMapperTrace.asTrace.indicators.get(reference) + ] + + problem.add(assertion) + problem.annotations += annotation + assertion.annotations +=annotation + trace.referenceMapperTrace.asTrace.lowerMultiplicity.put(reference,assertion) + } + else trace.referenceMapperTrace.asTrace.lowerMultiplicity.put(reference,null) + + if(upper != -1) { + val assertion = Assertion('''upperMultiplicity «reference.name» «sourceType.name»''', + Forall[ context | + val source = context.addVar('''src''', trace.TypeofEClass(sourceType)) + val targets = (1 .. upper+1).map[ + context.addVar('''trg «it»''', trace.TypeofEClass(targetType))].toList + And(targets.map[IsInReference(trace, source, it, reference)]) => ! Distinct(targets) + ]) + val annotation = createUpperMultiplicityAssertion => [ + it.upper = upper + it.relation = trace.referenceMapperTrace.asTrace.indicators.get(reference) + ] + + problem.add(assertion) + problem.annotations += annotation + assertion.annotations += annotation + trace.referenceMapperTrace.asTrace.lowerMultiplicity.put(reference,assertion) + } + else trace.referenceMapperTrace.asTrace.upperMultiplicity.put(reference,null) + } + } + + def createInverseReferenceConstraints(Ecore2Logic_Trace trace, LogicProblem problem, Iterable references) { + trace.referenceMapperTrace.asTrace.inverseEdges = new HashMap + for(reference : references) { + if(reference.EOpposite!=null) { + val opposite = reference.EOpposite + if(trace.referenceMapperTrace.asTrace.inverseEdges.containsKey(opposite)) { + trace.referenceMapperTrace.asTrace.inverseEdges.put(reference,trace.referenceMapperTrace.asTrace.inverseEdges.get(opposite)) + } else { + val sourceType = reference.EContainingClass + val targetType = reference.EType as EClass + val assertion = Assertion('''oppositeReference «reference.name» «sourceType.name»''', + Forall[ + val src = addVar('''src''', trace.TypeofEClass(sourceType)) + val trg = addVar('''trg''', trace.TypeofEClass(targetType)) + IsInReference(trace,src,trg,reference) <=> IsInReference(trace,trg,src,opposite) + ] + ) + problem.add(assertion) + trace.referenceMapperTrace.asTrace.inverseEdges.put(reference,assertion) + val annotation = createInverseRelationAssertion => [ + it.target = assertion + it.inverseA = trace.referenceMapperTrace.asTrace.indicators.get(reference) + it.inverseB = trace.referenceMapperTrace.asTrace.indicators.get(reference.EOpposite) + ] + problem.annotations += annotation + } + } + else trace.referenceMapperTrace.asTrace.inverseEdges.put(reference,null) + } + } + + override IsInReference(Ecore2Logic_Trace trace, TermDescription source, TermDescription target, EReference type) { + trace.referenceMapperTrace.asTrace.indicators.get(type).call(source,target) + } + + override relationOfReference(Ecore2Logic_Trace trace, EReference reference) { + trace.referenceMapperTrace.asTrace.indicators.get(reference) + } +} \ No newline at end of file diff --git a/Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/Ecore2Logic.xtend b/Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/Ecore2Logic.xtend new file mode 100644 index 00000000..cae489e3 --- /dev/null +++ b/Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/Ecore2Logic.xtend @@ -0,0 +1,98 @@ +package hu.bme.mit.inf.dslreasoner.ecore2logic + +import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicProblem +import java.util.List +import org.eclipse.emf.ecore.EAttribute +import org.eclipse.emf.ecore.EClass +import org.eclipse.emf.ecore.EEnum +import org.eclipse.emf.ecore.EEnumLiteral +import org.eclipse.emf.ecore.EReference +import org.eclipse.xtend.lib.annotations.Data +import org.eclipse.xtend.lib.annotations.Delegate +import hu.bme.mit.inf.dslreasoner.logic.model.builder.TracedOutput +import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicProblemBuilder +import java.util.Set +import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.ContainmentHierarchy + +@Data class EcoreMetamodelDescriptor { + val List classes; + val Set concretised; + val boolean isEObjectConcretised; + val List enums; + val List literals; + val List references; + val List attributes; + + def isConcretised(EClass clazz) {concretised.contains(clazz)} +} + +interface Trace {} + +class Ecore2Logic_Trace implements Trace { + public var ContainmentHierarchy containmentHierarchy; + public var Trace classMapperTrace + public var Trace enumMapperTrace + public var Trace referenceMapperTrace + public var Trace containmentMapperTrace + public var Trace attributeMapperTrace +} + +class Ecore2LogicConfiguration { + var public boolean singleRoot = true + var public int numberOfObjects = -1 + public static val Undefined = -1 +} + +class Ecore2LogicScope { + public static val int Unlimited = -1 + public var numberOfObjects = Unlimited +} + +class Ecore2Logic implements EClassMapper, EEnumMapper, EReferenceMapper, ContainmentMapper, EAttributeMapper { + @Delegate protected val EClassMapper classMapper + @Delegate protected val EEnumMapper enumMapper + @Delegate protected val EReferenceMapper referenceMapper + @Delegate protected val ContainmentMapper containmentMapper + @Delegate protected val EAttributeMapper attributeMapper + + public new() { + this.classMapper = new EClassMapper_AllElementAsObject + this.enumMapper = new EEnumMapper_PredefinedClasses + this.referenceMapper = new EReferenceMapper_RelationsOverTypes(this.classMapper) + this.containmentMapper = new ContainmentMapper_ReferenceConjuction(this.classMapper, this.referenceMapper,0) + this.attributeMapper = new EAttributeMapper_RelationsOverTypes(this.classMapper, this.enumMapper) + } + + public new( + EClassMapper classMapper, + EEnumMapper enumMapper, + EReferenceMapper referenceMapper, + ContainmentMapper containmentMapper, + EAttributeMapper attributeMapper) + { + if(classMapper === null || enumMapper === null || referenceMapper === null || containmentMapper === null || + attributeMapper === null) { + throw new IllegalArgumentException('''The mappers should not be null!''') + } + + this.classMapper = classMapper + this.enumMapper = enumMapper + this.referenceMapper = referenceMapper + this.containmentMapper = containmentMapper + this.attributeMapper = attributeMapper + } + + def public TracedOutput transformMetamodel(EcoreMetamodelDescriptor metamodel, Ecore2LogicConfiguration config) { + + + val Ecore2Logic_Trace trace = new Ecore2Logic_Trace + val LogicProblem problem = (new LogicProblemBuilder).createProblem + transformEClasses(trace,problem,metamodel.classes,config.numberOfObjects,metamodel) + transformEEnums(trace,problem,metamodel.enums,config) + transformEReferences(trace,problem,metamodel.references) + transformContainment(trace,problem,metamodel.references, config.singleRoot) + transformEAttributes(trace,problem,metamodel.attributes) + + return new TracedOutput(problem,trace) + } +} -- cgit v1.2.3-54-g00ecf