@Override public AttributedFeatureModel parseFile(String fileName) throws Exception { Injector injector = new VMStandaloneSetup().createInjectorAndDoEMFRegistration(); XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class); resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE); if (models != null) { for (String s : models) { resourceSet.getResource(URI.createURI(s), true); } } Resource resource = resourceSet.getResource(URI.createURI(fileName), true); Model model = (Model) resource.getContents().get(0); fm = new AttributedFeatureModel(); VmBlock relationships = null; VmBlock attsblock = null; VmBlock constratins = null; EList<VmBlock> bl = model.getBlocks(); for (VmBlock block : bl) { if (block instanceof Relationships) { relationships = block; } else if (block instanceof Attributes) { attsblock = block; } else if (block instanceof Constraints) { constratins = block; } else if (block instanceof PackageDeclaration) { for (VmBlock blockinpkg : ((PackageDeclaration) block).getBlocks()) { if (blockinpkg instanceof Relationships) { relationships = blockinpkg; } else if (blockinpkg instanceof Attributes) { attsblock = blockinpkg; } else if (blockinpkg instanceof Constraints) { constratins = blockinpkg; } } } } if (((Relationships) relationships) != null) { FeatureHierarchy fhs = ((Relationships) relationships).getRoot(); fr.familiar.attributedfm.Feature ffeat = new fr.familiar.attributedfm.Feature(fhs.getParent().getName()); visitFeatureHierarchy(ffeat, fhs); fm.setRoot(ffeat); } if ((Attributes) attsblock != null) { visitAttributes(((Attributes) attsblock).getAttrDefs(), fm); } if ((Constraints) constratins != null) { visitConstraints(((Constraints) constratins).getConstraints(), fm); } return fm; }
private void visitConstraints(EList<Constraint> con, AttributedFeatureModel fm) { for (Constraint co : con) { Tree<String> ast = new Tree<String>(); Node<String> root = traverseConstraint((ComplexExpression) co.getExpression()); ast.setRootElement(root); ComplexConstraint ccons = new ComplexConstraint(ast); fm.addConstraint(ccons); } }
private void visitAttributes(EList<AttrDef> att, AttributedFeatureModel fm) { for (AttrDef atdef : att) { GenericAttribute attribute = new GenericAttribute(""); attribute.nonTranstalable = atdef.isNotTranslatable(); attribute.runTime = atdef.isRunTime(); attribute.nonDesicion = atdef.isNotDecidable(); BasicAttrDef at = atdef.getBasicAttrDef(); if (at instanceof BooleanAttrDef) { String name = ((BooleanAttrDef) at).getName().getName(); Boolean val = Boolean.parseBoolean(((BooleanAttrDef) at).getValue()); if (((BooleanAttrDef) at).getDefault() != null) { Boolean defaultvalue = Boolean.parseBoolean(((BooleanAttrDef) at).getDefault().getValue()); attribute.setDefaultValue(defaultvalue); } if (val != null) { // tiene 2 valores attribute.setDomain(new Domain(new BooleanRange())); } // }else{ // Collection<Integer> values= new LinkedList<Integer>(); // if(val==true){values.add(1);}else{values.add(0);} // Domain domain = new Domain(new IntegerRange(values)); // // } attribute.setName(name); fr.familiar.attributedfm.Feature searchFeatureByName = fm.searchFeatureByName( ((BooleanAttrDef) at).getName().getHead().getOwnedFeature().getName()); searchFeatureByName.addAttribute(attribute); } else if (at instanceof StringAttrDef) { String name = ((StringAttrDef) at).getName().getName(); String val = ((StringAttrDef) at).getValue(); if (((StringAttrDef) at).getDefault() != null) { String defaultvalue = ((StringAttrDef) at).getDefault().getValue(); attribute.setDefaultValue(defaultvalue); } StringRange rang = new StringRange(); rang.add(val); Domain domain = new Domain(rang); attribute.setDomain(domain); attribute.setName(name); fr.familiar.attributedfm.Feature searchFeatureByName = fm.searchFeatureByName( ((StringAttrDef) at).getName().getHead().getOwnedFeature().getName()); searchFeatureByName.addAttribute(attribute); } else if (at instanceof IntegerAttrDefBounded) { // bounded can have deltas and multiples ranges, thus, will // always use a set integerDomain String name = ((IntegerAttrDefBounded) at).getName().getName(); if (((IntegerAttrDefBounded) at).getDefault() != null) { Integer defaultvalue = ((IntegerAttrDefBounded) at).getDefault().getValue(); attribute.setDefaultValue(defaultvalue); } // Set<Integer> vals= new HashSet<Integer>(); EList<IntegerAttrDefComplement> complements = ((IntegerAttrDefBounded) at).getComplements(); Range range = new IntegerRange(); for (IntegerAttrDefComplement complement : complements) { int delta = 1; if (complement.getDelta() != null) { delta = complement.getDelta().getValue(); } Integer max = Integer.parseInt(complement.getMax()); Integer min = Integer.parseInt(complement.getMin()); // Add the values to the set for (int i = min; i <= max; i += delta) { range.getItems().add(new Integer(i)); } } // Domain domain= new SetIntegerDomain(vals); Domain domain = new Domain(range); attribute.setDomain(domain); attribute.setName(name); fr.familiar.attributedfm.Feature searchFeatureByName = fm.searchFeatureByName( ((IntegerAttrDefBounded) at).getName().getHead().getOwnedFeature().getName()); searchFeatureByName.addAttribute(attribute); } else if (at instanceof IntegerAttrDefUnbounded) { String name = ((IntegerAttrDefUnbounded) at).getName().getName(); // Integer val =Integer.parseInt(((IntegerAttrDefUnbounded) // at).getValue()); if (((IntegerAttrDefUnbounded) at).getDefault() != null) { Integer defaultvalue = ((IntegerAttrDefUnbounded) at).getDefault().getValue(); attribute.setDefaultValue(defaultvalue); } Range range = new IntegerRange(); range.getItems().add(Integer.MIN_VALUE); range.getItems().add(Integer.MAX_VALUE); Domain domain = new Domain(range); attribute.setDomain(domain); // attribute.setValue(val); attribute.setName(name); fr.familiar.attributedfm.Feature searchFeatureByName = fm.searchFeatureByName( ((IntegerAttrDefUnbounded) at).getName().getHead().getOwnedFeature().getName()); searchFeatureByName.addAttribute(attribute); } else if (at instanceof RealAttrDefBounded) { String name = ((RealAttrDefBounded) at).getName().getName(); if (((RealAttrDefBounded) at).getDefault() != null) { Float defaultvalue = Float.parseFloat(((RealAttrDefBounded) at).getDefault().getValue()); attribute.setDefaultValue(defaultvalue); } // Set<Float> vals= new HashSet<Float>(); Collection<Range> ranges = new LinkedList<Range>(); EList<RealAttrDefComplement> complements = ((RealAttrDefBounded) at).getComplement(); if (!fakeRealSupport) { for (RealAttrDefComplement complement : complements) { Float max = Float.parseFloat(complement.getMax()); Float min = Float.parseFloat(complement.getMin()); // Range r= null; Collection<Float> floats = new LinkedList<Float>(); if (complement.getDelta() != null) { float delta = Float.parseFloat(complement.getDelta().getValue()); // a set of values // Add the values to the set for (float i = min; i <= max; i += delta) { floats.add(i); } } else { // max min floats.add(max); floats.add(min); } ranges.add(new RealRange(floats)); } // Domain domain= new SetRealDomain(vals); } else { // we create a huge single integer range ArrayList<Object> realToInteger = new ArrayList<Object>(); int intIndex = 0; for (RealAttrDefComplement complement : complements) { Float max = Float.parseFloat(complement.getMax()); Float min = Float.parseFloat(complement.getMin()); // Collection<Float> floats = new LinkedList<Float>(); if (complement.getDelta() != null) { float delta = Float.parseFloat(complement.getDelta().getValue()); // a set of values // Add the values to the set for (float i = min; i <= max; i += delta) { // floats.add(i); realToInteger.add(i); intIndex++; } } else { // max min // floats.add(max); realToInteger.add(max); // floats.add(min); realToInteger.add(min); intIndex++; intIndex++; } // ranges.add(new RealRange(floats)); } Collection<Integer> integerCol = new ArrayList<Integer>(); for (int i = 0; i < intIndex; i++) { integerCol.add(i); } ranges.add(new IntegerRange(integerCol)); fakingRealValues.put(name, realToInteger); } Domain domain = new Domain(); domain.setRanges(ranges); attribute.setDomain(domain); attribute.setName(name); fr.familiar.attributedfm.Feature searchFeatureByName = fm.searchFeatureByName( ((RealAttrDefBounded) at).getName().getHead().getOwnedFeature().getName()); searchFeatureByName.addAttribute(attribute); } else if (at instanceof RealAttrDefUnbounded) { String name = ((RealAttrDefUnbounded) at).getName().getName(); Float val = Float.parseFloat(((RealAttrDefUnbounded) at).getValue()); if (((RealAttrDefUnbounded) at).getDefault() != null) { Float defaultvalue = Float.parseFloat(((RealAttrDefUnbounded) at).getDefault().getValue()); attribute.setDefaultValue(defaultvalue); } Collection<Float> floats = new LinkedList<Float>(); floats.add(val); floats.add(Float.MAX_VALUE); floats.add(Float.MAX_VALUE); RealRange range = new RealRange(floats); Domain domain = new Domain(); domain.addRange(range); attribute.setDomain(domain); // attribute.setValue(val); attribute.setName(name); fr.familiar.attributedfm.Feature searchFeatureByName = fm.searchFeatureByName( ((RealAttrDefUnbounded) at).getName().getHead().getOwnedFeature().getName()); searchFeatureByName.addAttribute(attribute); } EnumAttrDef enumAttrDef = atdef.getEnumAttrDef(); if (enumAttrDef instanceof EnumStringDef) { // tratar el caso de emulacion de enteros String name = ((EnumStringDef) enumAttrDef).getName().getName(); if (((EnumStringDef) enumAttrDef).getDefault() != null) { String defaultvalue = ((EnumStringDef) enumAttrDef).getDefault().getValue(); attribute.setDefaultValue(defaultvalue); } ArrayList<Object> vals = new ArrayList<Object>(); EList<String> values = ((EnumStringDef) enumAttrDef).getValue(); for (String value : values) { vals.add(value); } Range range = null; if (fakeRealSupport) { Collection<Integer> integerCol = new ArrayList<Integer>(); for (int i = 0; i < vals.size(); i++) { integerCol.add(i); } range = new IntegerRange(integerCol); fakingRealValues.put(name, vals); } else { range = new StringRange(vals); } attribute.setDomain(new Domain(range)); attribute.setName(name); fr.familiar.attributedfm.Feature searchFeatureByName = fm.searchFeatureByName( ((EnumStringDef) enumAttrDef).getName().getHead().getOwnedFeature().getName()); searchFeatureByName.addAttribute(attribute); } else if (enumAttrDef instanceof EnumIntegerDef) { String name = ((EnumIntegerDef) enumAttrDef).getName().getName(); if (((EnumIntegerDef) enumAttrDef).getDefault() != null) { Integer defaultvalue = ((EnumIntegerDef) enumAttrDef).getDefault().getValue(); attribute.setDefaultValue(defaultvalue); } Set<Integer> vals = new HashSet<Integer>(); EList<String> values = ((EnumIntegerDef) enumAttrDef).getValue(); for (String value : values) { vals.add(Integer.parseInt(value)); } Range range = new IntegerRange(vals); attribute.setDomain(new Domain(range)); attribute.setName(name); fr.familiar.attributedfm.Feature searchFeatureByName = fm.searchFeatureByName( ((EnumIntegerDef) enumAttrDef).getName().getHead().getOwnedFeature().getName()); searchFeatureByName.addAttribute(attribute); } else if (enumAttrDef instanceof EnumRealDef) { String name = ((EnumRealDef) enumAttrDef).getName().getName(); if (((EnumRealDef) enumAttrDef).getDefault() != null) { Float defaultvalue = Float.parseFloat(((EnumRealDef) enumAttrDef).getDefault().getValue()); attribute.setDefaultValue(defaultvalue); } Set<Float> vals = new HashSet<Float>(); EList<String> values = ((EnumIntegerDef) enumAttrDef).getValue(); for (String value : values) { vals.add(Float.parseFloat(value)); } Range range = new RealRange(vals); attribute.setDomain(new Domain(range)); attribute.setName(name); fr.familiar.attributedfm.Feature searchFeatureByName = fm.searchFeatureByName( ((EnumRealDef) enumAttrDef).getName().getHead().getOwnedFeature().getName()); searchFeatureByName.addAttribute(attribute); } } }